PHPUnit Mocking View Helper ZF2

时间:2015-10-02 07:14:04

标签: php zend-framework2 phpunit

我创建了一个View Helper:

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');
    }
}

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

  

$这 - > getView() - >部分();

提前致谢:)

2 个答案:

答案 0 :(得分:3)

在测试中,您需要模拟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);

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

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

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');
    }
}

答案 1 :(得分:2)

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

explorer .