Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check entity with null value #29

Merged
merged 1 commit into from
Oct 28, 2024
Merged
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: 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
Loading