Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PeeHaa committed Apr 1, 2019
1 parent b7cfc6a commit 4e19cfd
Show file tree
Hide file tree
Showing 19 changed files with 861 additions and 40 deletions.
11 changes: 0 additions & 11 deletions src/Exception/UnsupportedConditionOperator.php

This file was deleted.

12 changes: 5 additions & 7 deletions src/QueryBuilder/Factory/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace HarmonyIO\Dbal\QueryBuilder\Factory;

use HarmonyIO\Dbal\Exception\InvalidConditionDefinition;
use HarmonyIO\Dbal\Exception\UnsupportedConditionOperator;
use HarmonyIO\Dbal\QueryBuilder\Column\Column;
use HarmonyIO\Dbal\QueryBuilder\Column\Value;
use HarmonyIO\Dbal\QueryBuilder\Condition\Condition as ConditionObject;
Expand Down Expand Up @@ -33,19 +32,21 @@ public function __construct(QuoteStyle $quoteStyle, Field $fieldFactory)
*/
public function buildFromString(string $string, $parameter = null): ConditionObject
{
$pattern = '~(\s*(!=|=|<>|<|>|\s+IS(\s+NOT(\s+NULL)?)?|\s+IN)\s*)~i';
$pattern = '~(\s*(!=|=|<>|<|>|\s+IS(\s+NOT)?\s+NULL|(\s+NOT)?\s+IN)\s*)~i';

$fields = preg_split($pattern, $string);

$pattern = '~\s*(!=|=|<>|<|>|\s+IS(?:\s+NOT(?:\s+NULL)?)?|\s+IN)\s*~i';
$pattern = '~\s*(!=|=|<>|<|>|\s+IS(?:\s+NOT)?\s+NULL|(\s+NOT)?\s+IN)\s*~i';

if (preg_match($pattern, $string, $matches) !==1) {
throw new InvalidConditionDefinition($string);
}

$field1 = $this->fieldFactory->buildFromString($fields[0]);

$parameter = $this->buildParameter($fields[1], $parameter);
if ($fields[1]!== '') {
$parameter = $this->buildParameter($fields[1], $parameter);
}

$operator = strtoupper(trim($matches[1]));

Expand All @@ -68,9 +69,6 @@ public function buildFromString(string $string, $parameter = null): ConditionObj

case 'NOT IN':
return new NotInCondition($field1, $parameter);

default:
throw new UnsupportedConditionOperator($operator);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/QueryBuilder/Factory/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(QuoteStyle $quoteStyle)

public function buildFromString(string $string): Column
{
$pattern = '~^(?:(?:(?P<function>.+)\s*\(\s*(?:(?P<table1>.+)(?:\.))?(?P<column1>[^ ]+)\s*\))|(?:(?P<table2>.+)(?:\.))?(?P<column2>[^ ]+))(?:\s+as\s+(?P<alias>.+))?$~ix';
$pattern = '~^(?:(?:(?P<function>[^\s]+)\s*\(\s*(?:(?P<table1>[^\s\(\)]+)(?:\.))?(?P<column1>[^\s\(\)]+)\s*\))|(?:(?P<table2>[^\s\(\)]+)(?:\.))?(?P<column2>[^\s\(\)]+))(?:\s+as\s+(?P<alias>.+))?$~ix';

if (preg_match($pattern, $string, $fieldParts) !== 1) {
throw new InvalidFieldDefinition($string);
Expand Down
8 changes: 4 additions & 4 deletions src/QueryBuilder/Factory/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HarmonyIO\Dbal\QueryBuilder\Factory;

use HarmonyIO\Dbal\Exception\InvalidFieldDefinition;
use HarmonyIO\Dbal\Exception\InvalidTableDefinition;
use HarmonyIO\Dbal\QueryBuilder\Identifier\Table as TableObject;
use HarmonyIO\Dbal\QueryBuilder\QuoteStyle;

Expand All @@ -18,12 +18,12 @@ public function __construct(QuoteStyle $quoteStyle)

public function buildFromString(string $string): TableObject
{
$pattern = '~^(?P<table>.+)(?:\s+as\s+(?P<alias>.+))?$~i';
$pattern = '~^(?P<table>[^\s]+)(?:\s+as\s+(?P<alias>[^\s]+))?$~i';

if (preg_match($pattern, $string, $tableParts) !== 1) {
throw new InvalidFieldDefinition($string);
throw new InvalidTableDefinition($string);
}

return new TableObject($this->quoteStyle, $tableParts['table'], $fieldParts['alias'] ?? null);
return new TableObject($this->quoteStyle, $tableParts['table'], $tableParts['alias'] ?? null);
}
}
17 changes: 0 additions & 17 deletions tests/Unit/Exception/UnsupportedConditionOperatorTest.php

This file was deleted.

35 changes: 35 additions & 0 deletions tests/Unit/QueryBuilder/Condition/InConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace HarmonyIO\DbalTest\Unit\QueryBuilder\Condition;

use HarmonyIO\Dbal\QueryBuilder\Column\Field;
use HarmonyIO\Dbal\QueryBuilder\Column\Value;
use HarmonyIO\Dbal\QueryBuilder\Condition\InCondition;
use HarmonyIO\Dbal\QueryBuilder\QuoteStyle;
use HarmonyIO\PHPUnitExtension\TestCase;

class InConditionTest extends TestCase
{
/** @var QuoteStyle */
private $quoteStyle;

//phpcs:ignore SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
public function setUp()
{
$this->quoteStyle = new QuoteStyle(QuoteStyle::MYSQL);
}

public function testToSqlWithValueAsColumn(): void
{
$condition = new InCondition(new Value($this->quoteStyle, 3), [1, 2, 3]);

$this->assertSame('? IN (?, ?, ?)', $condition->toSql());
}

public function testToSqlWithFieldAsColumn(): void
{
$condition = new InCondition(new Field($this->quoteStyle, 'column', 'table'), [1, 2, 3]);

$this->assertSame('`table`.`column` IN (?, ?, ?)', $condition->toSql());
}
}
85 changes: 85 additions & 0 deletions tests/Unit/QueryBuilder/Condition/MatchingConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php declare(strict_types=1);

namespace HarmonyIO\DbalTest\Unit\QueryBuilder\Condition;

use HarmonyIO\Dbal\QueryBuilder\Column\Field;
use HarmonyIO\Dbal\QueryBuilder\Column\Value;
use HarmonyIO\Dbal\QueryBuilder\Condition\MatchingCondition;
use HarmonyIO\Dbal\QueryBuilder\QuoteStyle;
use HarmonyIO\PHPUnitExtension\TestCase;

class MatchingConditionTest extends TestCase
{
/** @var QuoteStyle */
private $quoteStyle;

//phpcs:ignore SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
public function setUp()
{
$this->quoteStyle = new QuoteStyle(QuoteStyle::MYSQL);
}

public function testToSqlWithValuesOnBothLeftAndRightSide(): void
{
$this->assertSame(
'? = ?',
(new MatchingCondition(new Value($this->quoteStyle, 1), '=', new Value($this->quoteStyle, 1)))->toSql()
);
}

public function testToSqlWithValueOnLeftSide(): void
{
$condition = (new MatchingCondition(
new Field($this->quoteStyle, 'column'),
'=',
new Value($this->quoteStyle, 1)
));

$this->assertSame(
'`column` = ?',
$condition->toSql()
);
}

public function testToSqlWithValueOnRightSide(): void
{
$condition = (new MatchingCondition(
new Value($this->quoteStyle, 1),
'=',
new Field($this->quoteStyle, 'column')
));

$this->assertSame(
'? = `column`',
$condition->toSql()
);
}

public function testToSqlWithLiteralValueOnRightSide(): void
{
$condition = (new MatchingCondition(
new Field($this->quoteStyle, 'column'),
'=',
1
));

$this->assertSame(
'`column` = ?',
$condition->toSql()
);
}

public function testToSqlWithFieldsOnBothLeftAndRightSide(): void
{
$condition = (new MatchingCondition(
new Field($this->quoteStyle, 'column1'),
'=',
new Field($this->quoteStyle, 'column2')
));

$this->assertSame(
'`column1` = `column2`',
$condition->toSql()
);
}
}
35 changes: 35 additions & 0 deletions tests/Unit/QueryBuilder/Condition/NotInConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace HarmonyIO\DbalTest\Unit\QueryBuilder\Condition;

use HarmonyIO\Dbal\QueryBuilder\Column\Field;
use HarmonyIO\Dbal\QueryBuilder\Column\Value;
use HarmonyIO\Dbal\QueryBuilder\Condition\NotInCondition;
use HarmonyIO\Dbal\QueryBuilder\QuoteStyle;
use HarmonyIO\PHPUnitExtension\TestCase;

class NotInConditionTest extends TestCase
{
/** @var QuoteStyle */
private $quoteStyle;

//phpcs:ignore SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
public function setUp()
{
$this->quoteStyle = new QuoteStyle(QuoteStyle::MYSQL);
}

public function testToSqlWithValueAsColumn(): void
{
$condition = new NotInCondition(new Value($this->quoteStyle, 3), [1, 2, 3]);

$this->assertSame('? NOT IN (?, ?, ?)', $condition->toSql());
}

public function testToSqlWithFieldAsColumn(): void
{
$condition = new NotInCondition(new Field($this->quoteStyle, 'column', 'table'), [1, 2, 3]);

$this->assertSame('`table`.`column` NOT IN (?, ?, ?)', $condition->toSql());
}
}
27 changes: 27 additions & 0 deletions tests/Unit/QueryBuilder/Condition/NotNullConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace HarmonyIO\DbalTest\Unit\QueryBuilder\Condition;

use HarmonyIO\Dbal\QueryBuilder\Column\Field;
use HarmonyIO\Dbal\QueryBuilder\Condition\NotNullCondition;
use HarmonyIO\Dbal\QueryBuilder\QuoteStyle;
use HarmonyIO\PHPUnitExtension\TestCase;

class NotNullConditionTest extends TestCase
{
/** @var QuoteStyle */
private $quoteStyle;

//phpcs:ignore SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
public function setUp()
{
$this->quoteStyle = new QuoteStyle(QuoteStyle::MYSQL);
}

public function testToSql(): void
{
$condition = new NotNullCondition(new Field($this->quoteStyle, 'column', 'table'));

$this->assertSame('`table`.`column` IS NOT NULL', $condition->toSql());
}
}
27 changes: 27 additions & 0 deletions tests/Unit/QueryBuilder/Condition/NullConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace HarmonyIO\DbalTest\Unit\QueryBuilder\Condition;

use HarmonyIO\Dbal\QueryBuilder\Column\Field;
use HarmonyIO\Dbal\QueryBuilder\Condition\NullCondition;
use HarmonyIO\Dbal\QueryBuilder\QuoteStyle;
use HarmonyIO\PHPUnitExtension\TestCase;

class NullConditionTest extends TestCase
{
/** @var QuoteStyle */
private $quoteStyle;

//phpcs:ignore SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
public function setUp()
{
$this->quoteStyle = new QuoteStyle(QuoteStyle::MYSQL);
}

public function testToSql(): void
{
$condition = new NullCondition(new Field($this->quoteStyle, 'column', 'table'));

$this->assertSame('`table`.`column` IS NULL', $condition->toSql());
}
}
Loading

0 comments on commit 4e19cfd

Please sign in to comment.