PHPUnit 模拟视图助手 ZF2

PHPUnit Mocking View Helper ZF2

我创建了一个视图助手:

class SousMenuContrat extends AbstractHelper
{
    private $maiContratService;

    public function __construct(
        FMaiContratService $maiContratService,
    ) {
        $this->maiContratService                = $maiContratService;
    }

    public function __invoke($iMaiContratId, $sActive)
    {
        $oContrat = $this->maiContratService->selectById($iMaiContratId);

        return $this->getView()->partial('maintenance/sousmenucontrat', array(
            'oContrat'         => $oContrat
        ));
    }
}

所以现在我需要用 PHPUnit 来测试它:

class SousMenuContratTest extends TestCase
{
    private $myService;

    public function setUp()
    {
        $maiContratService = $this->getMockBuilder('Maintenance\Service\Model\FMaiContratService')
            ->disableOriginalConstructor()
            ->getMock();

        $oContrat = new FMaiContrat();
        $stub = $this->returnValue($oContrat);
        $maiContratService->expects($this->any())->method('selectById')->will($stub);
        $this->myService = new SousMenuContrat(
            $maiContratService
        );
    }

    public function testInvoque()
    {
        $this->myService->__invoke(2, 'contrat');
    }
}

但是测试发送错误,因为测试不知道:

$this->getView()->partial();

提前致谢:)

在您的测试中,您需要模拟 getView():

返回的渲染器
    /** @var PhpRenderer|\PHPUnit_Framework_MockObject_MockObject $rendererMock */
    $rendererMock = $this->getMockBuilder('Zend\View\Renderer\PhpRenderer')
        ->disableOriginalConstructor()
        ->getMock();
    $rendererMock->expects($this->once())
        ->method("partial")
        ->with(array(
            'maintenance/sousmenucontrat',
            array('oContrat' => new FMaiContrat()),
        ));
    $this->myService->setView($rendererMock);

最佳解决方案是在 with() 中使用您在 setUp() 中实例化的相同 FMaiContrat 对象,但在这种情况下,这同样有效。

编辑: 完整的测试代码如下所示:

class SousMenuContratTest extends TestCase
{
    private $myService;

    public function setUp()
    {
        $maiContratService = $this->getMockBuilder('Maintenance\Service\Model\FMaiContratService')
            ->disableOriginalConstructor()
            ->getMock();

        $oContrat = new FMaiContrat();
        $stub = $this->returnValue($oContrat);
        $maiContratService->expects($this->any())->method('selectById')->will($stub);
        $this->myService = new SousMenuContrat(
            $maiContratService
        );
    }

    public function testInvoque()
    {
        /** @var PhpRenderer|\PHPUnit_Framework_MockObject_MockObject $rendererMock */
        $rendererMock = $this->getMockBuilder('Zend\View\Renderer\PhpRenderer')
            ->disableOriginalConstructor()
            ->getMock();
        $rendererMock->expects($this->once())
            ->method("partial")
            ->with(array(
                'maintenance/sousmenucontrat',
                array('oContrat' => new FMaiContrat()),
            ));
        $this->myService->setView($rendererMock);
        $this->myService->__invoke(2, 'contrat');
    }
}

如果你只想使用 ZF2,你可以使用以下设置 类 然后在 SousMenuContrat 构造函数中模拟你的依赖关系

protected function setUp()
{
   $maiContratService = $this->getMockBuilder('Maintenance\Service\Model\FMaiContratService')
        ->disableOriginalConstructor()
        ->getMock();

    $oContrat = new FMaiContrat($maiContratService);
    $stub = $this->returnValue($oContrat);
    $maiContratService->expects($this->any())->method('selectById')->will($stub);

    Doctype::unsetDoctypeRegistry();

    $this->helper = new SousMenuContrat();
    $this->renderer = new PhpRenderer;
    $this->viewHelperManager = $this->renderer->getHelperPluginManager();
    $config  = new HelperConfig();
    $config->configureServiceManager($this->viewHelperManager);
    $this->helper->setView($this->renderer);

    parent::setUp();
}