Last active
January 15, 2016 00:31
-
-
Save yourwebmaker/a48eab64d85c50970b78 to your computer and use it in GitHub Desktop.
How to mock a Doctrine Custom Repository using Profecy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
namespace App; | |
use Doctrine\ORM\EntityRepository; | |
use App\Exception\EntityNotFoundException; | |
class DefaultRepository extends EntityRepository | |
{ | |
/** | |
* Searches for an object. If not found, throws an exception. | |
* This is only a shorthand for $repo->find($id) in order to avoid | |
* "if" statements scattered throughout code. | |
* | |
* @see http://ocramius.github.io/doctrine-best-practices/#/97 | |
* @param $id | |
* @return object | |
* @throws EntityNotFoundException | |
*/ | |
public function get($id) | |
{ | |
$object = $this->_em->find($this->getEntityName(), $id); | |
if ($object == null) { | |
throw new EntityNotFoundException(); | |
} | |
return $object; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
namespace App; | |
use PHPUnit_Framework_TestCase as TestCase; | |
use Doctrine\ORM\EntityManager; | |
use Doctrine\ORM\Mapping\ClassMetadata; | |
use Prophecy\Prophecy\ObjectProphecy; | |
use Doctrine\ORM\EntityRepository; | |
class DefaultRepositoryTest extends TestCase | |
{ | |
/** | |
* @var EntityManager|ObjectProphecy | |
*/ | |
protected $entityManager; | |
/** | |
* @var ClassMetadata|ObjectProphecy | |
*/ | |
protected $classMetadata; | |
public function setUp() | |
{ | |
$this->entityManager = $this->prophesize(EntityManager::class); | |
$this->classMetadata = $this->prophesize(ClassMetadata::class); | |
} | |
/** | |
* @expectedException \App\Exception\EntityNotFoundException | |
*/ | |
public function testThrowsExceptionWhenItemWasNotFound() | |
{ | |
$id = 999; | |
$this->entityManager->find(DefaultRepositoryTestDumb::class, $id)->willReturn(null); | |
$this->classMetadata->name = DefaultRepositoryTestDumb::class; | |
$repository = new DefaultRepository($this->entityManager->reveal(), $this->classMetadata->reveal()); | |
$repository->get($id); | |
} | |
public function testGetReturnsTheObjectWhenItWasFound() | |
{ | |
$id = 1; | |
$found = new DefaultRepositoryTest(); | |
$this->entityManager->find(DefaultRepositoryTestDumb::class, $id)->willReturn($found); | |
$this->classMetadata->name = DefaultRepositoryTestDumb::class; | |
$repository = new DefaultRepository($this->entityManager->reveal(), $this->classMetadata->reveal()); | |
$this->assertEquals($found, $repository->get($id)); | |
} | |
} | |
class DefaultRepositoryTestDumb | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment