From ef419769aadb13dc22f8e471c9255fa2444fca85 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 28 Nov 2022 16:31:58 +0100 Subject: [PATCH 1/3] Introduce trait `SqlAssertions` --- tests/Lib/SqlAssertions.php | 47 +++++++++++++++++++++++++++++++++++++ tests/Lib/TestAdapter.php | 12 ++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/Lib/SqlAssertions.php create mode 100644 tests/Lib/TestAdapter.php diff --git a/tests/Lib/SqlAssertions.php b/tests/Lib/SqlAssertions.php new file mode 100644 index 0000000..398ee77 --- /dev/null +++ b/tests/Lib/SqlAssertions.php @@ -0,0 +1,47 @@ +queryBuilder = new QueryBuilder(new $this->adapterClass()); + } + + /** + * Assert that the given statement equals the given SQL once assembled + * + * @param string $sql + * @param Delete|Insert|Select|Update $statement + * @param ?array $values + * @param string $message + * + * @return void + */ + public function assertSql(string $sql, $statement, array $values = null, string $message = ''): void + { + // Reduce whitespaces to just one space + $sql = preg_replace('/\s+/', ' ', trim($sql)); + + list($stmt, $bind) = $this->queryBuilder->assemble($statement); + + $this->assertSame($sql, $stmt, $message); + + if ($values !== null) { + $this->assertSame($values, $bind, $message); + } + } +} diff --git a/tests/Lib/TestAdapter.php b/tests/Lib/TestAdapter.php new file mode 100644 index 0000000..5173fd7 --- /dev/null +++ b/tests/Lib/TestAdapter.php @@ -0,0 +1,12 @@ + Date: Mon, 28 Nov 2022 16:32:20 +0100 Subject: [PATCH 2/3] Introduce class `TestCase` --- tests/TestCase.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/TestCase.php diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..351c323 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,43 @@ +query = new $this->queryClass(); + $this->sqlAssertionsSetUp(); + } + + /** @deprecated Unused. */ + protected function setupTest() + { + } + + /** + * @deprecated Use {@see self::assertSql} instead. + * + * @param string $statement + * @param array $values + * + * @return void + */ + protected function assertCorrectStatementAndValues($statement, $values = null) + { + $this->assertSql($statement, $this->query, $values); + } +} From 5c4f9a33a269845fed21b0601a89e56566c6b7f6 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 28 Nov 2022 16:32:41 +0100 Subject: [PATCH 3/3] Use class `TestCase` where required --- tests/DeleteTest.php | 24 +----------------------- tests/HavingTest.php | 32 +------------------------------- tests/InsertTest.php | 32 ++++---------------------------- tests/Mssql/SelectTest.php | 33 +++------------------------------ tests/SelectTest.php | 31 +------------------------------ tests/UpdateTest.php | 32 ++++---------------------------- tests/WhereTest.php | 31 +------------------------------ 7 files changed, 15 insertions(+), 200 deletions(-) diff --git a/tests/DeleteTest.php b/tests/DeleteTest.php index 51774ab..d0d37d0 100644 --- a/tests/DeleteTest.php +++ b/tests/DeleteTest.php @@ -5,22 +5,8 @@ use ipl\Sql\Delete; use ipl\Sql\QueryBuilder; -class DeleteTest extends \PHPUnit\Framework\TestCase +class DeleteTest extends TestCase { - /** - * The DELETE query to test - * - * @var Delete - */ - protected $query; - - /** - * The SQL query builder - * - * @var QueryBuilder - */ - protected $queryBuilder; - public function setupTestTest() { $this->query = new Delete(); @@ -53,12 +39,4 @@ public function testFromWithArray() $this->assertSame(['t1' => 'table'], $this->query->getFrom()); $this->assertCorrectStatementAndValues('DELETE FROM table t1', []); } - - protected function assertCorrectStatementAndValues($statement, $values) - { - list($actualStatement, $actualValues) = $this->queryBuilder->assembleDelete($this->query); - - $this->assertSame($statement, $actualStatement); - $this->assertSame($values, $actualValues); - } } diff --git a/tests/HavingTest.php b/tests/HavingTest.php index c035d05..84d65b9 100644 --- a/tests/HavingTest.php +++ b/tests/HavingTest.php @@ -3,32 +3,10 @@ namespace ipl\Tests\Sql; use ipl\Sql\Expression; -use ipl\Sql\QueryBuilder; use ipl\Sql\Select; -use ipl\Sql\Sql; -class HavingTest extends \PHPUnit\Framework\TestCase +class HavingTest extends TestCase { - /** - * The SELECT query to test - * - * @var Select - */ - protected $query; - - /** - * The SQL query builder - * - * @var QueryBuilder - */ - protected $queryBuilder; - - public function setupTest() - { - $this->query = new Select(); - $this->queryBuilder = new QueryBuilder(new TestAdapter()); - } - public function testHavingStringFormat() { $this->setupTest(); @@ -95,12 +73,4 @@ public function testResetHaving() $this->query->getHaving() ); } - - protected function assertCorrectStatementAndValues($statement, $values) - { - list($actualStatement, $actualValues) = $this->queryBuilder->assembleSelect($this->query); - - $this->assertSame($statement, $actualStatement); - $this->assertSame($values, $actualValues); - } } diff --git a/tests/InsertTest.php b/tests/InsertTest.php index dd1a200..1f3959d 100644 --- a/tests/InsertTest.php +++ b/tests/InsertTest.php @@ -4,30 +4,14 @@ use ipl\Sql\Expression; use ipl\Sql\Insert; -use ipl\Sql\QueryBuilder; use ipl\Sql\Select; -class InsertTest extends \PHPUnit\Framework\TestCase +class InsertTest extends TestCase { - /** - * The INSERT query to test - * - * @var Insert - */ - protected $query; - - /** - * The SQL query builder - * - * @var QueryBuilder - */ - protected $queryBuilder; + protected $queryClass = Insert::class; - public function setupTest() - { - $this->query = new Insert(); - $this->queryBuilder = new QueryBuilder(new TestAdapter()); - } + /** @var Insert */ + protected $query; public function testEmptyInsertInto() { @@ -157,12 +141,4 @@ public function testInsertIntoStatementWithColumnsAndValues() $this->assertCorrectStatementAndValues('INSERT INTO table (c1,c2) VALUES(?,?)', ['v1', 'v2']); } - - protected function assertCorrectStatementAndValues($statement, $values) - { - list($actualStatement, $actualValues) = $this->queryBuilder->assembleInsert($this->query); - - $this->assertSame($statement, $actualStatement); - $this->assertSame($values, $actualValues); - } } diff --git a/tests/Mssql/SelectTest.php b/tests/Mssql/SelectTest.php index 2f055f4..65652de 100644 --- a/tests/Mssql/SelectTest.php +++ b/tests/Mssql/SelectTest.php @@ -3,30 +3,11 @@ namespace ipl\Tests\Sql\Mssql; use ipl\Sql\Adapter\Mssql; -use ipl\Sql\QueryBuilder; -use ipl\Sql\Select; +use ipl\Tests\Sql\TestCase; -class SelectTest extends \PHPUnit\Framework\TestCase +class SelectTest extends TestCase { - /** - * The SELECT query to test - * - * @var Select - */ - protected $query; - - /** - * The SQL query builder - * - * @var QueryBuilder - */ - protected $queryBuilder; - - public function setupTest() - { - $this->query = new Select(); - $this->queryBuilder = new QueryBuilder(new Mssql()); - } + protected $adapterClass = Mssql::class; public function testLimitOffset() { @@ -63,12 +44,4 @@ public function testAutomaticallyFixesLimitWithoutOrder() $this->assertCorrectStatementAndValues('SELECT a FROM b ORDER BY 1 OFFSET 30 ROWS FETCH NEXT 10 ROWS ONLY'); } - - protected function assertCorrectStatementAndValues($statement, array $values = []) - { - list($actualStatement, $actualValues) = $this->queryBuilder->assembleSelect($this->query); - - $this->assertSame($statement, $actualStatement); - $this->assertSame($values, $actualValues); - } } diff --git a/tests/SelectTest.php b/tests/SelectTest.php index 16155cc..03b1246 100644 --- a/tests/SelectTest.php +++ b/tests/SelectTest.php @@ -3,33 +3,12 @@ namespace ipl\Tests\Sql; use ipl\Sql\Expression; -use ipl\Sql\QueryBuilder; use ipl\Sql\Select; use ipl\Sql\Sql; use UnexpectedValueException; -class SelectTest extends \PHPUnit\Framework\TestCase +class SelectTest extends TestCase { - /** - * The SELECT query to test - * - * @var Select - */ - protected $query; - - /** - * The SQL query builder - * - * @var QueryBuilder - */ - protected $queryBuilder; - - public function setupTest() - { - $this->query = new Select(); - $this->queryBuilder = new QueryBuilder(new TestAdapter()); - } - public function testDistinct() { $this->setupTest(); @@ -821,12 +800,4 @@ public function testInvalidOderByDirectionsThrowAnError() $this->queryBuilder->assembleSelect($this->query); } - - protected function assertCorrectStatementAndValues($statement, $values) - { - list($actualStatement, $actualValues) = $this->queryBuilder->assembleSelect($this->query); - - $this->assertSame($statement, $actualStatement); - $this->assertSame($values, $actualValues); - } } diff --git a/tests/UpdateTest.php b/tests/UpdateTest.php index 203bfa8..bb7a1b6 100644 --- a/tests/UpdateTest.php +++ b/tests/UpdateTest.php @@ -3,31 +3,15 @@ namespace ipl\Tests\Sql; use ipl\Sql\Expression; -use ipl\Sql\QueryBuilder; use ipl\Sql\Select; use ipl\Sql\Update; -class UpdateTest extends \PHPUnit\Framework\TestCase +class UpdateTest extends TestCase { - /** - * The UPDATE query to test - * - * @var Update - */ - protected $query; - - /** - * The SQL query builder - * - * @var QueryBuilder - */ - protected $queryBuilder; + protected $queryClass = Update::class; - public function setupTest() - { - $this->query = new Update(); - $this->queryBuilder = new QueryBuilder(new TestAdapter()); - } + /** @var Update */ + protected $query; public function testEmptyUpdateTable() { @@ -100,12 +84,4 @@ public function testUpdateStatementWithSet() $this->assertCorrectStatementAndValues('UPDATE table SET c1 = ?, c2 = ?', ['v1', 'v2']); } - - protected function assertCorrectStatementAndValues($statement, $values) - { - list($actualStatement, $actualValues) = $this->queryBuilder->assembleUpdate($this->query); - - $this->assertSame($statement, $actualStatement); - $this->assertSame($values, $actualValues); - } } diff --git a/tests/WhereTest.php b/tests/WhereTest.php index 67a9950..00ae5c6 100644 --- a/tests/WhereTest.php +++ b/tests/WhereTest.php @@ -3,32 +3,11 @@ namespace ipl\Tests\Sql; use ipl\Sql\Expression; -use ipl\Sql\QueryBuilder; use ipl\Sql\Select; use ipl\Sql\Sql; -class WhereTest extends \PHPUnit\Framework\TestCase +class WhereTest extends TestCase { - /** - * The SELECT query to test - * - * @var Select - */ - protected $query; - - /** - * The SQL query builder - * - * @var QueryBuilder - */ - protected $queryBuilder; - - public function setupTest() - { - $this->query = new Select(); - $this->queryBuilder = new QueryBuilder(new TestAdapter()); - } - public function testWhereStringFormat() { $this->setupTest(); @@ -287,12 +266,4 @@ public function testResetWhere() $this->query->getWhere() ); } - - protected function assertCorrectStatementAndValues($statement, $values) - { - list($actualStatement, $actualValues) = $this->queryBuilder->assembleSelect($this->query); - - $this->assertSame($statement, $actualStatement); - $this->assertSame($values, $actualValues); - } }