I am trying to unit test my package and I want to get some of the work from the EventManager Mock. Basically, I want to get the last saved object. I know in a regular application, I can do the same with EventSubscriber.
What I want to achieve basically checks the status of the previous saved record, if its flag is pending, and then in the next save, I want to update it so as not to wait.
Example:
This is how I get Event Manager:
private function getEntityManager(Entity\Friend $friendEntity = null)
{
$repository = $this
->getMockBuilder('\Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->setMethods(['findBy'])
->getMock();
$repository
->expects($this->any())
->method('findBy')
->will($this->returnValue(null));
$entityManager = $this
->getMockBuilder('\Doctrine\ORM\EntityManager')
->setMethods(['getRepository', 'getUnitOfWork', 'persist'])
->disableOriginalConstructor()
->getMock();
$entityManager
->expects($this->any())
->method('getRepository')
->will($this->returnValue($repository));
$entityManager
->expects($this->any())
->method('getUnitOfWork')
->will($this->returnValue($repository));
$entityManager
->expects($this->any())
->method('persist')
->with($friendEntity)
->will($this->returnValue(null));
return $entityManager;
}
And in my test:
public function testAddFriendPending()
{
$friendEntity = new Entity\Friend($this->friend, $this->user);
$entityManager = $this->getEntityManager($friendEntity);
$pendingRequest = new FriendService($entityManager);
$lastInsert = $pendingRequest->addFriend($this->friend, $this->user);
$lastInsert->setId(1);
$friendAddRequest = new FriendService($entityManager);
$friendAddRequest->addFriend($this->user, $this->friend);
$response = $friendAddRequest->getStatus();
$this->assertEquals(Entity\Friend::FRIEND_ADD_STATUS_COMPLETED, $response);
}
EDIT
Errors still being accepted:
vendor/phpunit/phpunit/phpunit src/NalabTnahsarp/FriendFollowerBundle/Tests/Service/
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.
EE 2 / 2 (100%)
Time: 102 ms, Memory: 4.00MB
There were 2 errors:
1) NalabTnahsarp\FriendFollowerBundle\Tests\Service\FriendTest::testAddFriendNotPending
Error: Call to a member function getUnitOfWork() on null
/Users/Sites/app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:194
/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Service/Friend.php:140
/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Service/Friend.php:63
/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Tests/Service/FriendTest.php:43
2) NalabTnahsarp\FriendFollowerBundle\Tests\Service\FriendTest::testAddFriendPending
Error: Call to a member function getUnitOfWork() on null
/Users/Sites/app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:194
/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Service/Friend.php:140
/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Service/Friend.php:63
/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Tests/Service/FriendTest.php:57
source
share