Skip to content

Commit 8c8076c

Browse files
authored
Merge pull request #12 from iter8-au/master
Add support for IS NOT NULL
2 parents 25f9275 + 9215be5 commit 8c8076c

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/Codeception/Lib/Driver/Db.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,12 @@ protected function generateWhereClause(array &$criteria)
215215
$params = [];
216216
foreach ($criteria as $k => $v) {
217217
if ($v === null) {
218-
$params[] = $this->getQuotedName($k) . " IS NULL ";
218+
if (strpos($k, ' !=') > 0) {
219+
$params[] = $this->getQuotedName(str_replace(" !=", '', $k)) . " IS NOT NULL ";
220+
} else {
221+
$params[] = $this->getQuotedName($k) . " IS NULL ";
222+
}
223+
219224
unset($criteria[$k]);
220225
continue;
221226
}

src/Codeception/Module/Db.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@
224224
* ```sql
225225
* SELECT COUNT(*) FROM `users` WHERE `name` = 'Davert' AND `email` LIKE 'davert%'
226226
* ```
227+
* Null comparisons are also available, as shown here:
228+
*
229+
* ```php
230+
* <?php
231+
* $I->seeInDatabase('users', ['name' => null, 'email !=' => null]);
232+
*
233+
* ```
234+
* Will generate:
235+
*
236+
* ```sql
237+
* SELECT COUNT(*) FROM `users` WHERE `name` IS NULL AND `email` IS NOT NULL
238+
* ```
227239
* ## Public Properties
228240
* * dbh - contains the PDO connection
229241
* * driver - contains the Connection Driver

tests/unit/Codeception/Lib/Driver/DbTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ public function testGenerateWhereClause($criteria, $expectedResult)
2323
public function getWhereCriteria()
2424
{
2525
return [
26-
'like' => [['email like' => 'mail.ua'], 'WHERE "email" LIKE ? '],
27-
'<=' => [['id <=' => '5'], 'WHERE "id" <= ? '],
28-
'<' => [['id <' => '5'], 'WHERE "id" < ? '],
29-
'>=' => [['id >=' => '5'], 'WHERE "id" >= ? '],
30-
'>' => [['id >' => '5'], 'WHERE "id" > ? '],
31-
'!=' => [['id !=' => '5'], 'WHERE "id" != ? '],
26+
'like' => [['email like' => 'mail.ua'], 'WHERE "email" LIKE ? '],
27+
'<=' => [['id <=' => '5'], 'WHERE "id" <= ? '],
28+
'<' => [['id <' => '5'], 'WHERE "id" < ? '],
29+
'>=' => [['id >=' => '5'], 'WHERE "id" >= ? '],
30+
'>' => [['id >' => '5'], 'WHERE "id" > ? '],
31+
'!=' => [['id !=' => '5'], 'WHERE "id" != ? '],
32+
'is null' => [['id' => null], 'WHERE "id" IS NULL '],
33+
'is not null' => [['id !=' => null], 'WHERE "id" IS NOT NULL '],
3234
];
3335
}
3436
}

0 commit comments

Comments
 (0)