From 014822f9e044a8af10db83782c7b1cb7e461144a Mon Sep 17 00:00:00 2001 From: tamila Date: Mon, 7 Oct 2024 12:13:04 +0300 Subject: [PATCH] feat: check entity with null property --- src/Context/ORMContext.php | 8 +++++-- tests/Unit/Context/DB/ORMContextTest.php | 28 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/Context/ORMContext.php b/src/Context/ORMContext.php index 61f9b78..a0d0fef 100644 --- a/src/Context/ORMContext.php +++ b/src/Context/ORMContext.php @@ -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); + } } } diff --git a/tests/Unit/Context/DB/ORMContextTest.php b/tests/Unit/Context/DB/ORMContextTest.php index 001750e..9c85778 100644 --- a/tests/Unit/Context/DB/ORMContextTest.php +++ b/tests/Unit/Context/DB/ORMContextTest.php @@ -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, @@ -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(); }