Skip to content

Commit 6ca3195

Browse files
authored
test: add SQL equality assertion helper (#10299)
1 parent 5ba38c3 commit 6ca3195

7 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎system/Test/CIUnitTestCase.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,14 @@ public function assertCloseEnoughString($expected, $actual, string $message = ''
501501
return null;
502502
}
503503

504+
/**
505+
* Asserts that two SQL strings are the same, ignoring newlines in the actual SQL.
506+
*/
507+
public function assertSameSql(string $expected, string $actual, string $message = ''): void
508+
{
509+
$this->assertSame($expected, str_replace(["\r\n", "\r", "\n"], ' ', $actual), $message);
510+
}
511+
504512
// --------------------------------------------------------------------
505513
// Utility
506514
// --------------------------------------------------------------------

‎tests/system/Database/Builder/DeleteTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testDelete(): void
4646
],
4747
];
4848

49-
$this->assertSame($expectedSQL, str_replace("\n", ' ', $answer));
49+
$this->assertSameSql($expectedSQL, $answer);
5050
$this->assertSame($expectedBinds, $builder->getBinds());
5151
}
5252

‎tests/system/Database/Builder/PrefixTest.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testPrefixesSetOnTableNamesWithWhereClause(): void
5151

5252
$builder->where($where);
5353

54-
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
54+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
5555
$this->assertSame($expectedBinds, $builder->getBinds());
5656
}
5757

@@ -64,7 +64,7 @@ public function testPrefixesSetOnTableNamesWithWhereColumnClause(): void
6464

6565
$builder->whereColumn('users.created_at <', 'users.updated_at');
6666

67-
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
67+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
6868
$this->assertSame($expectedBinds, $builder->getBinds());
6969
}
7070

@@ -86,7 +86,7 @@ public function testPrefixesSetOnTableNamesWithWhereBetweenClause(): void
8686

8787
$builder->whereBetween('users.created_at', [1, 10]);
8888

89-
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
89+
$this->assertSameSql($expectedSQL, $builder->getCompiledSelect());
9090
$this->assertSame($expectedBinds, $builder->getBinds());
9191
}
9292

‎tests/system/Test/TestCaseTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,23 @@ public function testCloseEnoughStringBadLength(): void
9393
$result = $this->assertCloseEnoughString('apples & oranges', 'apples');
9494
$this->assertFalse($result, 'Different string lengths should have returned false');
9595
}
96+
97+
public function testAssertSameSqlIgnoresNewlinesInActualSql(): void
98+
{
99+
$expected = 'SELECT * FROM "jobs" WHERE "id" = 1';
100+
$actual = <<<'SQL'
101+
SELECT * FROM "jobs"
102+
WHERE "id" = 1
103+
SQL;
104+
105+
$this->assertSameSql($expected, $actual);
106+
}
107+
108+
public function testAssertSameSqlIgnoresCrLfInActualSql(): void
109+
{
110+
$expected = 'SELECT * FROM "jobs" WHERE "id" = 1';
111+
$actual = "SELECT * FROM \"jobs\"\r\nWHERE \"id\" = 1";
112+
113+
$this->assertSameSql($expected, $actual);
114+
}
96115
}

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ Commands
223223
Testing
224224
=======
225225

226+
- Added ``assertSameSql()`` to ``CIUnitTestCase`` to compare generated SQL while ignoring newlines in the actual SQL.
227+
226228
Database
227229
========
228230

‎user_guide_src/source/testing/overview.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ between expected and actual time, formatted as strings, is within the prescribed
202202

203203
The above test will allow the actual time to be either 660 or 661 seconds.
204204

205+
assertSameSql($expected, $actual, $message = '')
206+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
207+
208+
Asserts that two SQL strings are the same, ignoring newlines in the actual SQL:
209+
210+
.. literalinclude:: overview/023.php
211+
205212
Accessing Protected/Private Properties
206213
--------------------------------------
207214

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
$expected = 'SELECT * FROM "jobs" WHERE "id" = 1';
4+
$actual = $builder->where('id', 1)->getCompiledSelect();
5+
6+
$this->assertSameSql($expected, $actual);

0 commit comments

Comments
 (0)