From 6324b935999a2d89895fdeaaf8c30f6f5a71ca1d Mon Sep 17 00:00:00 2001 From: k0ka Date: Sat, 18 Dec 2021 10:46:11 +0100 Subject: [PATCH 1/3] 1. sanitize criteria in InteractsWithEntities to remove circular dependencies. 2. simple tests for InteractsWithEntities --- .../Concerns/InteractsWithEntities.php | 29 +++++- .../Concerns/InteractsWithEntitiesTest.php | 95 +++++++++++++++++++ 2 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 tests/Testing/Concerns/InteractsWithEntitiesTest.php diff --git a/src/Testing/Concerns/InteractsWithEntities.php b/src/Testing/Concerns/InteractsWithEntities.php index 070b8c07..e1d5a105 100644 --- a/src/Testing/Concerns/InteractsWithEntities.php +++ b/src/Testing/Concerns/InteractsWithEntities.php @@ -50,14 +50,14 @@ public function entitiesMatch($class, array $criteria, $count = null) { $entities = $this->entityManager()->getRepository($class)->findBy($criteria); - Assert::assertNotEmpty($entities, "No [$class] entities were found with the given criteria: " . print_r($criteria, true)); + Assert::assertNotEmpty($entities, "No [$class] entities were found with the given criteria: " . $this->outputCriteria($criteria)); if ($count !== null) { Assert::assertCount( $count, $entities, "Expected to find $count [$class] entities, but found " . count($entities) . - ' with the given criteria: ' . print_r($criteria, true) + ' with the given criteria: ' . $this->outputCriteria($criteria) ); } @@ -75,10 +75,33 @@ public function noEntitiesMatch($class, array $criteria) { Assert::assertEmpty( $this->entityManager()->getRepository($class)->findBy($criteria), - "Some [$class] entities were found with the given criteria: " . print_r($criteria, true) + "Some [$class] entities were found with the given criteria: " . $this->outputCriteria($criteria) ); } + /** + * Replaces entities with their ids in the criteria array and print_r them + * + * @param array $criteria + * @return string + */ + private function outputCriteria(array $criteria) + { + $criteria = collect($criteria)->map(function ($value) { + if (!is_object($value)) + return $value; + + $unityOfWork = $this->entityManager()->getUnitOfWork(); + if ($unityOfWork->isInIdentityMap($value)) { + return $unityOfWork->getEntityIdentifier($value); + } + + return $value; + })->all(); + + return print_r($criteria, true); + } + /** * @throws \PHPUnit_Framework_SkippedTestError * @return EntityManager diff --git a/tests/Testing/Concerns/InteractsWithEntitiesTest.php b/tests/Testing/Concerns/InteractsWithEntitiesTest.php new file mode 100644 index 00000000..47763cd4 --- /dev/null +++ b/tests/Testing/Concerns/InteractsWithEntitiesTest.php @@ -0,0 +1,95 @@ +em = Mockery::mock(EntityManagerInterface::class); + + $this->app = Mockery::mock(Container::class); + $this->app + ->allows('make') + ->with('em') + ->andReturn($this->em); + } + + public function testEntitiesMatchWithMatch() + { + $repository = Mockery::mock(EntityRepository::class); + $repository->expects('findBy') + ->with(['someField' => 'someValue']) + ->once() + ->andReturn(['entity']); + + $this->em->expects('getRepository') + ->with('SomeClass') + ->once() + ->andReturn($repository); + + $this->entitiesMatch('SomeClass', ['someField' => 'someValue']); + } + + public function testEntitiesMatchWithoutMatch() + { + $repository = Mockery::mock(EntityRepository::class); + $repository->expects('findBy') + ->with(['someField' => 'someValue']) + ->once() + ->andReturn([]); + + $this->em->expects('getRepository') + ->with('SomeClass') + ->once() + ->andReturn($repository); + + $this->expectException(ExpectationFailedException::class); + $this->entitiesMatch('SomeClass', ['someField' => 'someValue']); + } + + public function testNoEntitiesMatchWithMatch() + { + $repository = Mockery::mock(EntityRepository::class); + $repository->expects('findBy') + ->with(['someField' => 'someValue']) + ->once() + ->andReturn(['entity']); + + $this->em->expects('getRepository') + ->with('SomeClass') + ->once() + ->andReturn($repository); + + $this->expectException(ExpectationFailedException::class); + $this->noEntitiesMatch('SomeClass', ['someField' => 'someValue']); + } + + public function testNoEntitiesMatchWithoutMatch() + { + $repository = Mockery::mock(EntityRepository::class); + $repository->expects('findBy') + ->with(['someField' => 'someValue']) + ->once() + ->andReturn([]); + + $this->em->expects('getRepository') + ->with('SomeClass') + ->once() + ->andReturn($repository); + + $this->noEntitiesMatch('SomeClass', ['someField' => 'someValue']); + } + +} \ No newline at end of file From 76bbd95e053b7200adef7dbf9215d5c7ad288a09 Mon Sep 17 00:00:00 2001 From: k0ka Date: Sat, 18 Dec 2021 11:04:34 +0100 Subject: [PATCH 2/3] fix styleCi --- src/Testing/Concerns/InteractsWithEntities.php | 5 +++-- tests/Testing/Concerns/InteractsWithEntitiesTest.php | 2 -- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Testing/Concerns/InteractsWithEntities.php b/src/Testing/Concerns/InteractsWithEntities.php index e1d5a105..1cdeb5ac 100644 --- a/src/Testing/Concerns/InteractsWithEntities.php +++ b/src/Testing/Concerns/InteractsWithEntities.php @@ -82,14 +82,15 @@ public function noEntitiesMatch($class, array $criteria) /** * Replaces entities with their ids in the criteria array and print_r them * - * @param array $criteria + * @param array $criteria * @return string */ private function outputCriteria(array $criteria) { $criteria = collect($criteria)->map(function ($value) { - if (!is_object($value)) + if (!is_object($value)) { return $value; + } $unityOfWork = $this->entityManager()->getUnitOfWork(); if ($unityOfWork->isInIdentityMap($value)) { diff --git a/tests/Testing/Concerns/InteractsWithEntitiesTest.php b/tests/Testing/Concerns/InteractsWithEntitiesTest.php index 47763cd4..c556321d 100644 --- a/tests/Testing/Concerns/InteractsWithEntitiesTest.php +++ b/tests/Testing/Concerns/InteractsWithEntitiesTest.php @@ -1,6 +1,5 @@ noEntitiesMatch('SomeClass', ['someField' => 'someValue']); } - } \ No newline at end of file From 8631f254507a7575ef6daafb811b01dd1240c79e Mon Sep 17 00:00:00 2001 From: k0ka Date: Sat, 18 Dec 2021 11:05:32 +0100 Subject: [PATCH 3/3] fix styleCi --- tests/Testing/Concerns/InteractsWithEntitiesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Testing/Concerns/InteractsWithEntitiesTest.php b/tests/Testing/Concerns/InteractsWithEntitiesTest.php index c556321d..a13a6442 100644 --- a/tests/Testing/Concerns/InteractsWithEntitiesTest.php +++ b/tests/Testing/Concerns/InteractsWithEntitiesTest.php @@ -90,4 +90,4 @@ public function testNoEntitiesMatchWithoutMatch() $this->noEntitiesMatch('SomeClass', ['someField' => 'someValue']); } -} \ No newline at end of file +}