0

I'm trying to do mock for viewHelper and can't do this. When I am trying to run phpunit I get the assert error "Failed asserting that two strings are equal." because the analiticHelper hasn't been overwriten. I wrote setAllowOverride(true) for my ServiseManager, but this didn't help. Could you please tell me how can I do mock for viewHelper in Zend Framework 2? I will appreciate your time and help! Thanks!

# 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;
    }
}
Oleksii.B
  • 618
  • 8
  • 16
  • If I don't use **viewHelperManager** for do mocking, and I use **Helper** for similar situation it works fine. For examole: 1. **It doesn't work** `$viewHelperManager = $this->sm->get('ViewHelperManager');` `$analiticHelper = $viewHelperManager->get('analiticHelper');` 2. **It works fine** `$helper = $this->sm->get('UserHelper');` `$configFacebook = $helper->getFacebookConfig();` – Oleksii.B Mar 21 '17 at 07:58
  • What do you want to test? Why do you want set mock to service manager? – SzymonM Mar 21 '17 at 08:49
  • Depending on what lies in the session, I need to assign a tag to the user. I have a viewHelper that returns a value depending on what lies in the session (the value describes the additional logic). I want to implement the function of assigning a tag with Unit tests. For this to be possible I need the viewHelper that returns the value specified by me. – Oleksii.B Mar 28 '17 at 12:16

0 Answers0