微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHPUnit Mocking View Helper ZF2

我创建了一个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');
    }
}

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐