This is my first PHPUnit test with mocks, for a Symfony 3 project. Can anyone give me some advice about this code?
class CourierManager extends BaseManager
{
/**
* @param Courier $courier
*/
public function create (Courier $courier) : void
{
$this->em->persist($courier);
$this->em->flush();
}
/**
* @param Courier $courier
*/
public function update (Courier $courier) : void
{
$this->em->flush();
}
/**
* @param $phone
* @return null|Courier
*/
public function findByPhone (string $phone) : ?Courier
{
return $this->repository->findOneByPhone($phone);
}
}
BaseManager __construct
method:
public function __construct (EntityManager $entityManager, $repositoryClass, Container $container)
{
$this->em = $entityManager;
$this->repository = $entityManager->getRepository($repositoryClass);
$this->container = $container;
$this->entity = Order::class;
}
And CourierManagerTest
:
class CourierManagerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var EntityManager
*/
private $em;
/**
* @var Container
*/
private $container;
/**
* @var EntityRepository
*/
private $repository;
/**
* @var CourierManager
*/
private $manager;
private static $courierClass = Courier::class;
public function setUp () : void
{
$this->em = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
$this->em->expects($this->any())->method('persist')
->with($this->isInstanceOf(static::$courierClass))
;
$this->repository = $this->getMockBuilder(EntityRepository::class)->disableOriginalConstructor()->getMock();
$this->em->expects($this->any())->method('flush');
$this->em->expects($this->any())->method('getRepository')->with($this->equalTo('AppBundle:Courier'))->will($this->returnValue($this->repository));
$this->container = $this->getMockBuilder(Container::class)->getMock();
$this->manager = new CourierManager($this->em, 'AppBundle:Courier', $this->container);
}
public function testCreate () : void
{
$this->manager->create($this->getCourier());
}
public function testFindByPhone () : void
{
$this->manager->findByPhone('1234567890');
}
private function getCourier () : Courier
{
return new static::$courierClass();
}
}