PHPUnit Doctrine Repository 魔术方法
PHPUnit Doctrine Repository magic methods
我需要一些帮助来全面测试我的 class 这取决于 Doctrine。
我的 class 中有一种方法使用 Doctrine magic 方法 findOneBy... EntityRepository,如下图:
但是当我 运行 我的测试时,我总是有这个警告:
我怎样才能模拟那个电话?下面我介绍 EntityManager 魔法 __call 方法应该如何工作:
我认为您可以将魔术方法 findOneByXXXX('identifier') 替换为真正的方法 findOneBy(array('XXXX' => 'identifier')).
也就是说:
$this->shopUrlRepository->findOneByIdShopUrl($this->shop->getId());
收件人:
$this->shopUrlRepository->findOneBy(array('IdShopUrl' => $this->shop->getId()));
这是一个真正的方法。 Doctrine documentation
希望对您有所帮助
您可以模拟魔术方法 __call
,它在调用无法访问的方法时被调用。
$mock
->expects($this->once())
->method('__call')
->with(
$this->equalTo('findOneByIdShopUrl'), //
$this->equalTo(['5'])
)
->willReturn(['shop' => 'shop info']); // return shop object
也检查http://php.net/manual/en/language.oop5.overloading.php#object.call
另一种选择是使用 setMethods()
$this->getMockBuilder('ShopRepository')->setMethods(['findOneByShopId'])->getMock();
然后是 will 等方法的其余逻辑
我需要一些帮助来全面测试我的 class 这取决于 Doctrine。
我的 class 中有一种方法使用 Doctrine magic 方法 findOneBy... EntityRepository,如下图:
但是当我 运行 我的测试时,我总是有这个警告:
我怎样才能模拟那个电话?下面我介绍 EntityManager 魔法 __call 方法应该如何工作:
我认为您可以将魔术方法 findOneByXXXX('identifier') 替换为真正的方法 findOneBy(array('XXXX' => 'identifier')).
也就是说:
$this->shopUrlRepository->findOneByIdShopUrl($this->shop->getId());
收件人:
$this->shopUrlRepository->findOneBy(array('IdShopUrl' => $this->shop->getId()));
这是一个真正的方法。 Doctrine documentation
希望对您有所帮助
您可以模拟魔术方法 __call
,它在调用无法访问的方法时被调用。
$mock
->expects($this->once())
->method('__call')
->with(
$this->equalTo('findOneByIdShopUrl'), //
$this->equalTo(['5'])
)
->willReturn(['shop' => 'shop info']); // return shop object
也检查http://php.net/manual/en/language.oop5.overloading.php#object.call
另一种选择是使用 setMethods()
$this->getMockBuilder('ShopRepository')->setMethods(['findOneByShopId'])->getMock();
然后是 will 等方法的其余逻辑