如何在zf2中模拟viewHelper

时间:2017-03-20 21:02:11

标签: php unit-testing zend-framework2 mocking viewhelper

我正在尝试为viewHelper做模拟而不能这样做。 当我试图运行phpunit时,我得到断言错误“失败断言两个字符串相等。”因为 analiticHelper 没有被覆盖。我为我的ServiseManager写了 setAllowOverride(true),但这没有帮助。 你能否告诉我如何在Zend Framework 2中为viewHelper做模拟? 我将感激您的时间和帮助! 谢谢!

# module\Application\test\Controller\IndexControllerTest.php
<?php

namespace ApplicationTest\Controller;

use Application\Controller\IndexController;
use PHPUnit_Framework_TestCase as TestCase;
use ApplicationTest\Bootstrap;

class HttpTest extends TestCase {

    /**
     * @var IndexController
     */
//    private $indexController;
    protected $traceError = false;

    public function setUp() {

        $this->sm = Bootstrap::getServiceManager();
        $this->em = $this->sm->get('Doctrine\ORM\EntityManager');
    }
        
    public function testAction() {
        
        $mockAnaliticHelper = $this->getMockBuilder(\Application\View\Helper\AnaliticHelper::class)
                     ->disableOriginalConstructor()
                     ->disableOriginalClone()
                     ->disableArgumentCloning()
                     ->disallowMockingUnknownTypes()
                     ->getMock();
        $mockAnaliticHelper->method('getKey')
             ->willReturn('some-value');
        
        $this->assertEquals('some-value', $mockAnaliticHelper->getKey()); // it's ok
        
        $this->sm->setAllowOverride(true);
        $this->sm->setService('analiticHelper', $mockAnaliticHelper); // before 
        
        $viewHelperManager = $this->sm->get('ViewHelperManager');
        $analiticHelper = $viewHelperManager->get('analiticHelper');
        
        $this->sm->setService('analiticHelper', $mockAnaliticHelper); // after
        
       $key = $analiticHelper->getKey(); // return 'web' 
        
        $this->assertEquals('some-value', $analiticHelper->getKey()); // return error

    }
}

# Module.php

<?php
....
public function getViewHelperConfig()
    {
        return array(
            'factories' => array(
                ...,
                'analiticHelper' => function($sm){
                    $locator = $sm->getServiceLocator();
                    $config = $locator->get('config');
                    $em = $locator->get('Doctrine\ORM\EntityManager');
                    $viewHelper = new \Application\View\Helper\AnaliticHelper($em, $locator, $config);   
                    return $viewHelper;
                },
                ),
        );

    }

# module\Application\src\Application\View\Helper\AnaliticHelper.php
<?php

namespace Application\View\Helper;

use Zend\View\Helper\HelperInterface;
use Zend\View\Renderer\RendererInterface as Renderer;
use Doctrine\ORM\EntityManager;
use ZfcUser\Entity\UserInterface;
use Zend\Session\Container;
 
class AnaliticHelper  implements \Zend\View\Helper\HelperInterface{
    
    protected $_em;
    protected $_serviceLocator;
    protected $_config;
    const SESSION_CONTANIER = 'analitic';
    const SESSION_KEY = 'analiticKey';
    const DEFAULT_KEY = 'web';    

    public function __construct(EntityManager $em, $serviceLocator, $config) {
        $this->_em = $em;
        $this->_serviceLocator = $serviceLocator;              
        $this->_config = $config; 
        $router = $this->_serviceLocator->get('router');
        $request = $this->_serviceLocator->get('request');
        $this->routeMatch = $router->match($request);
    }

    public function __invoke(){ 
        return $this;
    }
    
    public function getKey(){   
         // some actions       
        return self::DEFAULT_KEY;
    } 
    
    /**
     * Set the View object
     *
     * @param  Renderer $view
     * @return HelperInterface
     */
    public function setView(Renderer $view)
    {
        $this->view = $view;
        return $this;
    }

    /**
     * Get the View object
     *
     * @return Renderer
     */
    public function getView()
    {
        return $this->view;
    }
}

0 个答案:

没有答案