How do I mock an invokable in a ZF2 unit test?

I'm trying to run a unit test on a controller that has a view that uses a view helper. My invokable is set up in my helper config as so:

<?php
return [
    'invokables' => [
        'viewHelper' => 'Theme\View\Helper\ViewHelper',
    ],
];

In my unit test, I have a number of services mocked in the setUp().

<?php

namespace Blargle\Controller;

class ActivityControllerTest extends AbstractZendHttpControllerTestCase
{
    /**
     * Someone set up us the bomb.
     */
    public function setUp()
    {
        parent::setUp();

        $mockActivityRepository = $this->getMock('Domain\Repository\Blargle\ActivityRepositoryInterface');
        $this->serviceManager->setService('Blargle\ActivityRepository', $mockActivityRepository);
    }

    /**
     * Test Add Action
     */
    public function testAddAction()
    {
        $this->createIdentity(); //Sets up the logged in user.

        $this->dispatch('/blargle/activity/add/1');
        $this->assertResponseStatusCode(200);

        $this->assertModuleName('blargle');
        $this->assertControllerName('Blargle\Controller\Activity');
        $this->assertControllerClass('ActivityController');
        $this->assertMatchedRouteName('blargle/activity');

        $mvcEvent = $this->getApplication()->getMvcEvent();
        $viewModel = $mvcEvent->getResult();

        $this->assertInstanceOf('Zend\View\Model\ViewModel', $viewModel);
        $this->assertEquals($viewModel->getTemplate(), 'blargle/activity/add');
    }
}

So, I now need to mock the invokable that gets loaded by the view, but the service manager that the view relies on is tied to the view. I've tried:

$mockHelper = $this->getMock('Theme\View\Helper\ViewHelper');
$this->serviceManager->setService('viewHelper', $mockHelper);

This is fruitless since this now lives in the controller service manager, while a peer one seems to be spawned with the view. The view unfortunately creates a new instance of the service before it goes out and looks at its peers for an instance.

So, with that, any ideas on how to effectively mock this?

Answers


Just be sure to mock the helper before dispatching your request :)

public function testMethod()
{
    /**
      * mock of your invokable class
      */
    $mock = ...;

    $sm = $this->getApplicationServiceLocator();
    $sm->setAllowOverride(true);
    $sm->setService('viewHelper', $mock);
    $sm->setAllowOverride(false);

    // your invokable is now mocked
}

Need Your Help

Is it possible to install SSL on Google app engine for iPhone application?

python google-app-engine ssl

I am using python language for google app engine based iphone application .I want to install/access ssl on python. I am unable to find a way to install/enable it in python file. please guide me how...