diff --git a/src/Testing/Concerns/InteractsWithEntities.php b/src/Testing/Concerns/InteractsWithEntities.php index 070b8c07..1cdeb5ac 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,34 @@ 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..a13a6442 --- /dev/null +++ b/tests/Testing/Concerns/InteractsWithEntitiesTest.php @@ -0,0 +1,93 @@ +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']); + } +}