Skip to content

Commit

Permalink
test: move SQL statement into abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed May 30, 2024
1 parent 0760e46 commit b423a16
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ static function (Country $c) {
);
}

protected function assertExpectedSql(Select $select): void
{
$actual = $select->buildQuery()->sqlStatement();
$expected = <<<SQL
SELECT `country`.`id` AS `c0`, `country`.`name` AS `c1`, `country`.`code` AS `c2`, `country`.`is_friendly` AS `c3`
FROM `country` AS `country`
LEFT JOIN `translation` AS `trans`
ON `trans`.`country_id` = `country`.`id`
LEFT JOIN `translation` AS `transEn`
ON `transEn`.`country_id` = `country`.`id` AND `transEn`.`locale_id` = 1
WHERE `country`.`is_friendly` = TRUE AND (`country`.`code` LIKE '%eng%' OR `country`.`name` LIKE '%eng%' OR `trans`.`title` LIKE '%eng%' )
ORDER BY `transEn`.`title` ASC
SQL;
$this->assertEquals(
\array_map('trim', \explode($actual, PHP_EOL)),
\array_map('trim', \explode($expected, PHP_EOL))
);
}

private function makeTables(): void
{
// Make tables
Expand Down Expand Up @@ -150,6 +169,4 @@ private function fillData(): void
],
);
}

abstract protected function assertExpectedSql(Select $select): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,4 @@
class CaseTest extends AbstractTestCase
{
public const DRIVER = 'mysql';

protected function assertExpectedSql(Select $select): void
{
$actual = (string)$select->buildQuery();
$expected = <<<SQL
SELECT `country`.`id` AS `c0`, `country`.`name` AS `c1`, `country`.`code` AS `c2`, `country`.`is_friendly` AS `c3`
FROM `country` AS `country`
LEFT JOIN `translation` AS `trans`
ON `trans`.`country_id` = `country`.`id`
LEFT JOIN `translation` AS `transEn`
ON `transEn`.`country_id` = `country`.`id` AND `transEn`.`locale_id` = 1
WHERE `country`.`is_friendly` = TRUE AND (`country`.`code` LIKE '%eng%' OR `country`.`name` LIKE '%eng%' OR `trans`.`title` LIKE '%eng%' )
ORDER BY `transEn`.`title` ASC
SQL;
$this->assertEquals(
\array_map('trim', \explode($actual, PHP_EOL)),
\array_map('trim', \explode($expected, PHP_EOL))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,4 @@
class CaseTest extends AbstractTestCase
{
public const DRIVER = 'postgres';

protected function assertExpectedSql(Select $select): void
{
$actual = (string)$select->buildQuery();
$expected = <<<SQL
SELECT `country`.`id` AS `c0`, `country`.`name` AS `c1`, `country`.`code` AS `c2`, `country`.`is_friendly` AS `c3`
FROM `country` AS `country`
LEFT JOIN `translation` AS `trans`
ON `trans`.`country_id` = `country`.`id`
LEFT JOIN `translation` AS `transEn`
ON `transEn`.`country_id` = `country`.`id` AND `transEn`.`locale_id` = 1
WHERE `country`.`is_friendly` = TRUE AND (`country`.`code` LIKE '%eng%' OR `country`.`name` LIKE '%eng%' OR `trans`.`title` LIKE '%eng%' )
ORDER BY `transEn`.`title` ASC
SQL;
$this->assertEquals(
\array_map('trim', \explode($actual, PHP_EOL)),
\array_map('trim', \explode($expected, PHP_EOL))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@
class CaseTest extends AbstractTestCase
{
public const DRIVER = 'sqlserver';

protected function assertExpectedSql(Select $select): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@
class CaseTest extends AbstractTestCase
{
public const DRIVER = 'sqlite';

protected function assertExpectedSql(Select $select): void
{
}
}

0 comments on commit b423a16

Please sign in to comment.