Skip to content
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
30 changes: 27 additions & 3 deletions src/Testing/Concerns/InteractsWithEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand All @@ -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
Expand Down
93 changes: 93 additions & 0 deletions tests/Testing/Concerns/InteractsWithEntitiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Illuminate\Contracts\Container\Container;
use LaravelDoctrine\ORM\Testing\Concerns\InteractsWithEntities;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use PHPUnit\Framework\ExpectationFailedException;

class InteractsWithEntitiesTest extends MockeryTestCase
{
use InteractsWithEntities;

protected $em;
protected $app;

public function setUp(): void
{
$this->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']);
}
}