symfony - Php-unit - Failed asserting that 'myDomain\Entity\Likes' is an instance of class "myDomain\Entity\Likes" -
i'm making tests web application, removes likes record database, i'm getting ambiguous fail.
expectation failed method name equal when invoked 1 time(s). parameter 0 invocation mymelomanbundle\repository\likesrepository::remove('mydomain\entity\likes') not match expected value. failed asserting 'mydomain\entity\likes' instance of class "mydomain\entity\likes".
dislikepublicationusecasetest.php
<?php namespace mymelomanbundle\likes; class dislikepublicationusecasetest extends \phpunit_framework_testcase { const user = 2; const pub = 15; /** * @var \phpunit_framework_mockobject_mockobject */ private $publicationrepositorymock; /** * @var \phpunit_framework_mockobject_mockobject */ private $userrepositorymock; /** * @var \phpunit_framework_mockobject_mockobject */ private $likesrepositorymock; /** * @var \phpunit_framework_mockobject_mockobject */ private $entitymanagermock; /** * @var \phpunit_framework_mockobject_mockobject */ private $usermock; /** * @var \phpunit_framework_mockobject_mockobject */ private $publicationmock; private $likesmock; /** * @var dislikepublicationusecase */ private $dislikepublicationusecase; /** * @var likedto */ private $likedto; protected function setup() { $this->publicationrepositorymock = $this- >createmock(publicationrepository::class); $this->userrepositorymock = $this- >createmock(userrepository::class); $this->likesrepositorymock = $this- >createmock(likesrepository::class); $this->entitymanagermock = $this- >createmock(entitymanager::class); $this->usermock = $this->createmock(user::class); $this->publicationmock = $this->createmock(publication::class); $this->likesmock = $this->createmock(likes::class); $this->likedto = new likedto(self::user, self::pub); $this->dislikepublicationusecase = new dislikepublicationusecase( $this->publicationrepositorymock, $this->userrepositorymock, $this->likesrepositorymock, $this->entitymanagermock ); } protected function teardown() { $this->publicationrepositorymock = null; $this->userrepositorymock = null; $this->likesrepositorymock = null; $this->entitymanagermock = null; $this->usermock = null; $this->publicationmock = null; $this->likesmock = null; } /** @test */ public function dummytest() { $this->dislikepublicationusecase; } /** @test */ public function shouldremovealikeonetimeifitexist() { $this->givenalikerepositorythathasaspecificlike(); $this->andgivenauserrepositorythathaveaspecifiuser(); $this->andgivenapublicationrepositorythathaveaspecificpublication(); $this->thenthelikeshouldberemovedonce(); $this->whenthedislikepublicationusecaseisexecutedwithaspecificparameters(); } private function givenalikerepositorythathasaspecificlike() { $this->likesrepositorymock ->method('findoneby') ->willreturn(likes::class); } private function andgivenauserrepositorythathaveaspecifiuser() { $this->userrepositorymock ->method('find') ->willreturn($this->usermock); } private function andgivenapublicationrepositorythathaveaspecificpublication() { $this->publicationrepositorymock ->method('find') ->willreturn($this->publicationmock); } private function thenthelikeshouldberemovedonce() { $this->likesrepositorymock ->expects($this->once()) ->method('remove') ->with($this->isinstanceof(likes::class)); // here fails } private function whenthedislikepublicationusecaseisexecutedwithaspecificparameters() { $this->dislikepublicationusecase->execute($this->likedto); } }
dislikepublicationusecase.php
<?php namespace mydomain\usecases\like; class dislikepublicationusecase { private $publicationrepository; private $userrepository; private $likesrepository; private $entitymanager; public function __construct ( publicationrepositoryinterface $publicationrepository, userrepositoryinterface $userrepository, likesrepositoryinterface $likesrepository, entitymanagerinterface $entitymanager ) { $this->publicationrepository = $publicationrepository; $this->userrepository = $userrepository; $this->likesrepository = $likesrepository; $this->entitymanager = $entitymanager; } public function execute(likedto $likedto) { try { $user = $this->userrepository->find($likedto->getuserid()); $publication = $this->publicationrepository->find($likedto->getpublicationid()); $like = $this->likesrepository->findoneby( array( 'user' => $user, 'publication' => $publication ) ); $this->likesrepository->remove($like); return true; } catch (\exception $e) { return false; } } }
why fails if it's same entity? it's entity i'm expecting.
it seems want pass instance of likes
parameter likesrepository::remove()
. you're close, you're trying pass boolean
parameter instead.
so need pass likes
instance. have mock of it, pass it:
private function thenthelikeshouldberemovedonce() { $this->likesrepositorymock ->expects($this->once()) ->method('remove') ->with($this->likesmock); // here fails }
i don't know remove()
's return value is, can test too:
private function thenthelikeshouldberemovedonce() { $this->likesrepositorymock ->expects($this->once()) ->method('remove') ->with($this->likesmock) ->will($this->returnvalue(true)); // if remove() returns true on success }
Comments
Post a Comment