Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions system/Test/CIUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 assertSqlEquals(string $expected, string $actual, string $message = ''): void
{
$this->assertSame($expected, str_replace("\n", ' ', $actual), $message);
}

// --------------------------------------------------------------------
// Utility
// --------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/system/Database/Builder/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testDelete(): void
],
];

$this->assertSame($expectedSQL, str_replace("\n", ' ', $answer));
$this->assertSqlEquals($expectedSQL, $answer);
$this->assertSame($expectedBinds, $builder->getBinds());
}

Expand Down
6 changes: 3 additions & 3 deletions tests/system/Database/Builder/PrefixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testPrefixesSetOnTableNamesWithWhereClause(): void

$builder->where($where);

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
$this->assertSqlEquals($expectedSQL, $builder->getCompiledSelect());
$this->assertSame($expectedBinds, $builder->getBinds());
}

Expand All @@ -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->assertSqlEquals($expectedSQL, $builder->getCompiledSelect());
$this->assertSame($expectedBinds, $builder->getBinds());
}

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

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

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
$this->assertSqlEquals($expectedSQL, $builder->getCompiledSelect());
$this->assertSame($expectedBinds, $builder->getBinds());
}

Expand Down
11 changes: 11 additions & 0 deletions tests/system/Test/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,15 @@ public function testCloseEnoughStringBadLength(): void
$result = $this->assertCloseEnoughString('apples & oranges', 'apples');
$this->assertFalse($result, 'Different string lengths should have returned false');
}

public function testAssertSqlEqualsIgnoresNewlinesInActualSql(): void
{
$expected = 'SELECT * FROM "jobs" WHERE "id" = 1';
$actual = <<<'SQL'
SELECT * FROM "jobs"
WHERE "id" = 1
SQL;

$this->assertSqlEquals($expected, $actual);
}
}
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.8.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ Commands
Testing
=======

- Added ``assertSqlEquals()`` to ``CIUnitTestCase`` to compare generated SQL while ignoring newlines in the actual SQL.

Database
========

Expand Down
7 changes: 7 additions & 0 deletions user_guide_src/source/testing/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

assertSqlEquals($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
--------------------------------------

Expand Down
6 changes: 6 additions & 0 deletions user_guide_src/source/testing/overview/023.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$expected = 'SELECT * FROM "jobs" WHERE "id" = 1';
$actual = $builder->where('id', 1)->getCompiledSelect();

$this->assertSqlEquals($expected, $actual);
Loading