<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

/**
 * Generated by PHPUnit_SkeletonGenerator on 2017-04-11 at 09:23:51.
 */
class parseSQLTest extends \PHPUnit\Framework\TestCase
{
  /**
   * @var parseSQL
   */
  protected $object;

  /**
   * Sets up the fixture, for example, opens a network connection.
   * This method is called before a test is executed.
   */
  protected function setUp(): void
  {
    $this->object = new parseSQL();
  }

  /**
   * @covers parseSQL::escape
   * @test
   */
  public function escape_MssqlNormaleZeichen_unveraendert()
  {
    $this->object->servertyp = 'MSSQL';
    $this->assertEquals('1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', $this->object->escape('1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'));
    $this->assertEquals('äöüÄÖÜß!§$&@€<>´`', $this->object->escape('äöüÄÖÜß!§$&@€<>´`'));
    $this->assertEquals('\\/', $this->object->escape('\\/'));
    $this->assertEquals('%_', $this->object->escape('%_')); // Diese Zeichen könnten einen Like-Vergleich verwirren
  }

  /**
   * @covers parseSQL::escape
   * @test
   */
  public function escape_MssqlHochkommas_Maskieren()
  {
    $this->object->servertyp = 'MSSQL';
    $this->assertEquals("''", $this->object->escape("'"));
    $this->assertEquals("Don''t can''t", $this->object->escape("Don't can't"));
  }

  /**
   * @covers parseSQL::escape
   * @test
   */
  public function escape_MssqlSteuerzeichen_Entfernen()
  {
    $this->object->servertyp = 'MSSQL';
    $this->assertEquals('', $this->object->escape("\x00"));
    $this->assertEquals('', $this->object->escape("\x00\x01\x02"));
    $this->assertEquals('', $this->object->escape("\x0b"));
  }

  /**
   * @covers parseSQL::length
   * @test
   */
  public function length_mssql()
  {
    $this->object->servertyp = 'MSSQL';
    $this->assertEquals(' LEN(aa) ', $this->object->length('aa'));
  }

  /**
   * @covers parseSQL::length
   * @test
   */
  public function length_mysql()
  {
    $this->object->servertyp = 'MYSQL';
    $this->assertEquals(' LENGTH(aa) ', $this->object->length('aa'));
  }

  /**
   * @covers parseSQL::length
   * @test
   */
  public function length_oracle()
  {
    $this->object->servertyp = 'ORACLE';
    $this->assertEquals(' LENGTH(aa) ', $this->object->length('aa'));
  }

  /**
   * @covers parseSQL::caseInsensitiveGroupConcatLike
   * @test
   */
  public function caseInsensitiveGroupConcatLike_normalInputMssql_returnsLike()
  {
    $this->object->servertyp = 'MSSQL';
    $prefix = 'p.';
    $fieldname = 'field';
    $match = 'abc';
    $expected = "p.field LIKE 'abc'";
    $this->assertEquals($expected, $this->object->caseInsensitiveGroupConcatLike($prefix, $fieldname, $match));
  }

  /**
   * @covers parseSQL::caseInsensitiveGroupConcatLike
   * @test
   */
  public function caseInsensitiveGroupConcatLike_normalInputOracle_returnsLike()
  {
    $this->object->servertyp = 'ORACLE';
    $prefix = 'p.';
    $fieldname = 'field';
    $match = 'abc';
    $expected = "LOWER(p.field) LIKE LOWER('abc')";
    $this->assertEquals($expected, $this->object->caseInsensitiveGroupConcatLike($prefix, $fieldname, $match));
  }

  /**
   * @covers parseSQL::caseInsensitiveGroupConcatLike
   * @test
   */
  public function caseInsensitiveGroupConcatLike_normalInputMysql_returnsGroupConcat()
  {
    $this->object->servertyp = 'MYSQL';
    $prefix = 'p.';
    $fieldname = 'field';
    $match = 'abc';
    $expected = "GROUP_CONCAT(p.field) LIKE 'abc'";
    $this->assertEquals($expected, $this->object->caseInsensitiveGroupConcatLike($prefix, $fieldname, $match));
  }

  /**
   * @covers parseSQL::caseInsensitiveGroupConcatLike
   * @test
   */
  public function caseInsensitiveGroupConcatLike_extendedInputMysql_returnsGroupConcat()
  {
    $this->object->servertyp = 'MYSQL';
    $prefix = 'p.';
    $fieldname = 'field';
    $match = 'abc';
    $orderBy = 'orderfield';
    $separator = ' ';
    $expected = "GROUP_CONCAT(p.field order by orderfield separator ' ') LIKE 'abc'";
    $this->assertEquals($expected, $this->object->caseInsensitiveGroupConcatLike($prefix, $fieldname, $match, $orderBy, $separator));
  }

  /**
   * @covers parseSQL::groupConcat
   * @test
   */
  public function groupConcat_normalInputMssql_returnsNothing()
  {
    $this->object->servertyp = 'MSSQL';
    $prefix = 'p.';
    $fieldname = 'field';
    $match = 'abc';
    $expected = '';
    $this->assertEquals($expected, $this->object->groupConcat($prefix, $fieldname, $match));
  }

  /**
   * @covers parseSQL::groupConcat
   * @test
   */
  public function groupConcat_normalInputOracle_returnsNothing()
  {
    $this->object->servertyp = 'MSSQL';
    $prefix = 'p.';
    $fieldname = 'field';
    $match = 'abc';
    $expected = '';
    $this->assertEquals($expected, $this->object->groupConcat($prefix, $fieldname, $match));
  }

  /**
   * @covers parseSQL::groupConcat
   * @test
   */
  public function groupConcat_fullInputMySQL_returnsGroupConcat()
  {
    $this->object->servertyp = 'MYSQL';
    $prefix = 'p.';
    $fieldname = 'field';
    $orderBy = 'orderfield';
    $separator = ' ';
    $expected = "GROUP_CONCAT(p.field order by orderfield separator ' ')";
    $this->assertEquals($expected, $this->object->groupConcat($prefix, $fieldname, $orderBy, $separator));
  }
}
