Skip to content

Commit 014822f

Browse files
committed
feat: check entity with null property
1 parent f107203 commit 014822f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/Context/ORMContext.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ private function seeInRepository(int $count, string $entityClass, ?array $params
7575

7676
if (null !== $params) {
7777
foreach ($params as $columnName => $columnValue) {
78-
$query->andWhere(sprintf('e.%s = :%s', $columnName, $columnName))
79-
->setParameter($columnName, $columnValue);
78+
if ($columnValue === null) {
79+
$query->andWhere(sprintf('e.%s IS NULL', $columnName));
80+
} else {
81+
$query->andWhere(sprintf('e.%s = :%s', $columnName, $columnName))
82+
->setParameter($columnName, $columnValue);
83+
}
8084
}
8185
}
8286

tests/Unit/Context/DB/ORMContextTest.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@ public function testThenISeeEntityInRepositoryWithProperties(): void
9494
);
9595
}
9696

97+
public function testThenISeeEntityInRepositoryWithPropertyNull(): void
98+
{
99+
$context = $this->createContext(
100+
'App\Entity\SomeEntity',
101+
1,
102+
[
103+
'id' => self::UUID,
104+
'someProperty' => null,
105+
],
106+
);
107+
$context->andISeeEntityInRepositoryWithProperties(
108+
'App\Entity\SomeEntity',
109+
new PyStringNode([
110+
<<<'PSN'
111+
{
112+
"id": "e809639f-011a-4ae0-9ae3-8fcb460fe950",
113+
"someProperty": null
114+
}
115+
PSN
116+
], 1),
117+
);
118+
}
119+
97120
private function createContext(
98121
string $entityName,
99122
int $count = 1,
@@ -127,7 +150,10 @@ private function createContext(
127150
$queryBuilderMock->expects(self::exactly(count($properties)))
128151
->method('andWhere')
129152
->willReturnSelf();
130-
$queryBuilderMock->expects(self::exactly(count($properties)))
153+
$setParametersCount = count(array_filter($properties, function ($value) {
154+
return !is_null($value);
155+
}));
156+
$queryBuilderMock->expects(self::exactly($setParametersCount))
131157
->method('setParameter')
132158
->willReturnSelf();
133159
}

0 commit comments

Comments
 (0)