Skip to content

Commit 4aff8dc

Browse files
committed
test: add SQL equality assertion helper
- Add assertSqlEquals() to CIUnitTestCase - Document the helper in the testing guide - Use the helper in representative Query Builder tests Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent f889329 commit 4aff8dc

7 files changed

Lines changed: 38 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 assertSqlEquals(string $expected, string $actual, string $message = ''): void
508+
{
509+
$this->assertSame($expected, str_replace("\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->assertSqlEquals($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->assertSqlEquals($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->assertSqlEquals($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->assertSqlEquals($expectedSQL, $builder->getCompiledSelect());
9090
$this->assertSame($expectedBinds, $builder->getBinds());
9191
}
9292

tests/system/Test/TestCaseTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,15 @@ 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 testAssertSqlEqualsIgnoresNewlinesInActualSql(): 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->assertSqlEquals($expected, $actual);
106+
}
96107
}

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ Commands
213213
Testing
214214
=======
215215

216+
- Added ``assertSqlEquals()`` to ``CIUnitTestCase`` to compare generated SQL while ignoring newlines in the actual SQL.
217+
216218
Database
217219
========
218220

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+
assertSqlEquals($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->assertSqlEquals($expected, $actual);

0 commit comments

Comments
 (0)