Skip to content

Commit

Permalink
Merge pull request #29 from MacPaw/feat/check-entity-with-null-property
Browse files Browse the repository at this point in the history
feat: check entity with null value
  • Loading branch information
Yozhef authored Oct 28, 2024
2 parents f107203 + 014822f commit e021cfd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Context/ORMContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ private function seeInRepository(int $count, string $entityClass, ?array $params

if (null !== $params) {
foreach ($params as $columnName => $columnValue) {
$query->andWhere(sprintf('e.%s = :%s', $columnName, $columnName))
->setParameter($columnName, $columnValue);
if ($columnValue === null) {
$query->andWhere(sprintf('e.%s IS NULL', $columnName));
} else {
$query->andWhere(sprintf('e.%s = :%s', $columnName, $columnName))
->setParameter($columnName, $columnValue);
}
}
}

Expand Down
28 changes: 27 additions & 1 deletion tests/Unit/Context/DB/ORMContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ public function testThenISeeEntityInRepositoryWithProperties(): void
);
}

public function testThenISeeEntityInRepositoryWithPropertyNull(): void
{
$context = $this->createContext(
'App\Entity\SomeEntity',
1,
[
'id' => self::UUID,
'someProperty' => null,
],
);
$context->andISeeEntityInRepositoryWithProperties(
'App\Entity\SomeEntity',
new PyStringNode([
<<<'PSN'
{
"id": "e809639f-011a-4ae0-9ae3-8fcb460fe950",
"someProperty": null
}
PSN
], 1),
);
}

private function createContext(
string $entityName,
int $count = 1,
Expand Down Expand Up @@ -127,7 +150,10 @@ private function createContext(
$queryBuilderMock->expects(self::exactly(count($properties)))
->method('andWhere')
->willReturnSelf();
$queryBuilderMock->expects(self::exactly(count($properties)))
$setParametersCount = count(array_filter($properties, function ($value) {
return !is_null($value);
}));
$queryBuilderMock->expects(self::exactly($setParametersCount))
->method('setParameter')
->willReturnSelf();
}
Expand Down

0 comments on commit e021cfd

Please sign in to comment.