我正在为我的 Symfony2 应用程序中的 Manager 类编写代码接收单元测试,我想知道如何模拟实体管理器。例如,假设我的 AcmeManager 服务类中有以下函数:
<?php
namespace Acme\AcmeBundle\Manager;
use Doctrine\Common\Persistence\ObjectManager;
class AcmeManager
{
private $em;
public function __construct (ObjectManager $em)
{
$this->em = $em;
}
public function findMatches($index)
{
// Find and display matches.
$matches = $this->em
->getRepository('AcmeBundle:AssignMatch')
->findBy(array('assignIndex' => $index));
return $matches;
}
}
我想编写以下测试函数:
<?php
use Codeception\Util\Stub;
class AutoManagerTest extends \Codeception\TestCase\Test
{
/**
* @var \CodeGuy
*/
protected $codeGuy;
protected function _before()
{
}
protected function _after()
{
}
/**
* Tests findMatches($index).
*/
public function testFindMatches()
{
//... $mockedEntityManager is our mocked em
$acmeManager = $this->getModule('Symfony2')->container->get('acme_manager');
$acmeManager->findMatches(0);
// $this->assert(isCalled($mockedEntityManager));
}
}
我如何模拟实体管理器,这样当我调用时$acmeManager->findMatches(0);
,我可以断言模拟的实体管理器被调用(即使$acmeManager
在其正常实现中使用常规的 Symfony2 实体管理器)?