diff --git a/system/Test/CIUnitTestCase.php b/system/Test/CIUnitTestCase.php index edfa98df5c06..468f216079d4 100644 --- a/system/Test/CIUnitTestCase.php +++ b/system/Test/CIUnitTestCase.php @@ -501,6 +501,14 @@ public function assertCloseEnoughString($expected, $actual, string $message = '' return null; } + /** + * Asserts that two SQL strings are the same, ignoring newlines in the actual SQL. + */ + public function assertSameSql(string $expected, string $actual, string $message = ''): void + { + $this->assertSame($expected, str_replace(["\r\n", "\r", "\n"], ' ', $actual), $message); + } + // -------------------------------------------------------------------- // Utility // -------------------------------------------------------------------- diff --git a/tests/system/Database/Builder/DeleteTest.php b/tests/system/Database/Builder/DeleteTest.php index 53b66eed805b..59c776b90804 100644 --- a/tests/system/Database/Builder/DeleteTest.php +++ b/tests/system/Database/Builder/DeleteTest.php @@ -46,7 +46,7 @@ public function testDelete(): void ], ]; - $this->assertSame($expectedSQL, str_replace("\n", ' ', $answer)); + $this->assertSameSql($expectedSQL, $answer); $this->assertSame($expectedBinds, $builder->getBinds()); } diff --git a/tests/system/Database/Builder/PrefixTest.php b/tests/system/Database/Builder/PrefixTest.php index 44d405ce7643..dcd58a46e035 100644 --- a/tests/system/Database/Builder/PrefixTest.php +++ b/tests/system/Database/Builder/PrefixTest.php @@ -51,7 +51,7 @@ public function testPrefixesSetOnTableNamesWithWhereClause(): void $builder->where($where); - $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + $this->assertSameSql($expectedSQL, $builder->getCompiledSelect()); $this->assertSame($expectedBinds, $builder->getBinds()); } @@ -64,7 +64,7 @@ public function testPrefixesSetOnTableNamesWithWhereColumnClause(): void $builder->whereColumn('users.created_at <', 'users.updated_at'); - $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + $this->assertSameSql($expectedSQL, $builder->getCompiledSelect()); $this->assertSame($expectedBinds, $builder->getBinds()); } @@ -86,7 +86,7 @@ public function testPrefixesSetOnTableNamesWithWhereBetweenClause(): void $builder->whereBetween('users.created_at', [1, 10]); - $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + $this->assertSameSql($expectedSQL, $builder->getCompiledSelect()); $this->assertSame($expectedBinds, $builder->getBinds()); } diff --git a/tests/system/Test/TestCaseTest.php b/tests/system/Test/TestCaseTest.php index 6905d825533a..893caaa9119d 100644 --- a/tests/system/Test/TestCaseTest.php +++ b/tests/system/Test/TestCaseTest.php @@ -93,4 +93,23 @@ public function testCloseEnoughStringBadLength(): void $result = $this->assertCloseEnoughString('apples & oranges', 'apples'); $this->assertFalse($result, 'Different string lengths should have returned false'); } + + public function testAssertSameSqlIgnoresNewlinesInActualSql(): void + { + $expected = 'SELECT * FROM "jobs" WHERE "id" = 1'; + $actual = <<<'SQL' + SELECT * FROM "jobs" + WHERE "id" = 1 + SQL; + + $this->assertSameSql($expected, $actual); + } + + public function testAssertSameSqlIgnoresCrLfInActualSql(): void + { + $expected = 'SELECT * FROM "jobs" WHERE "id" = 1'; + $actual = "SELECT * FROM \"jobs\"\r\nWHERE \"id\" = 1"; + + $this->assertSameSql($expected, $actual); + } } diff --git a/user_guide_src/source/changelogs/v4.8.0.rst b/user_guide_src/source/changelogs/v4.8.0.rst index 54083bdbb925..614562d2991b 100644 --- a/user_guide_src/source/changelogs/v4.8.0.rst +++ b/user_guide_src/source/changelogs/v4.8.0.rst @@ -213,6 +213,8 @@ Commands Testing ======= +- Added ``assertSameSql()`` to ``CIUnitTestCase`` to compare generated SQL while ignoring newlines in the actual SQL. + Database ======== diff --git a/user_guide_src/source/testing/overview.rst b/user_guide_src/source/testing/overview.rst index 4d49c5569355..b00cbb8539d7 100644 --- a/user_guide_src/source/testing/overview.rst +++ b/user_guide_src/source/testing/overview.rst @@ -202,6 +202,13 @@ between expected and actual time, formatted as strings, is within the prescribed The above test will allow the actual time to be either 660 or 661 seconds. +assertSameSql($expected, $actual, $message = '') +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Asserts that two SQL strings are the same, ignoring newlines in the actual SQL: + +.. literalinclude:: overview/023.php + Accessing Protected/Private Properties -------------------------------------- diff --git a/user_guide_src/source/testing/overview/023.php b/user_guide_src/source/testing/overview/023.php new file mode 100644 index 000000000000..32c126ca780b --- /dev/null +++ b/user_guide_src/source/testing/overview/023.php @@ -0,0 +1,6 @@ +where('id', 1)->getCompiledSelect(); + +$this->assertSameSql($expected, $actual);