Organizational Research By

Surprising Reserch Topic

phpunit and zf2 services using -'php,zend-framework2,phpunit'


phpunit and zf2 services  using -'php,zend-framework2,phpunit'

I have a little problem when creating tests with PHPUnit.

Here is my setup :

protected function setUp()
{
    $serviceManager = Bootstrap::getServiceManager();

    $this->mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
    $this->mockConnection = $this->getMock('Zend\Db\Adapter\Driver\ConnectionInterface');
    $this->mockDriver->expects($this->any())->method('checkEnvironment')->will($this->returnValue(true));
    $this->mockDriver->expects($this->any())->method('getConnection')->will($this->returnValue($this->mockConnection));
    $this->mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface');
    $this->mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
    $this->mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue($this->mockStatement));
    $this->adapter = new Adapter($this->mockDriver, $this->mockPlatform);
    $this->sql = new Sql($this->adapter);

    $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array(), array(), '', false);

    $maiIndiceTable = $this->getMockBuilder('Maintenance\Model\BDD\PMaiIndiceTable')
        ->setMethods(array())
        ->setConstructorArgs(array($mockTableGateway, $this->adapter, $this->sql))
        ->getMock();
    $maiIndiceService = $this->getMockBuilder('Maintenance\Service\Model\PMaiIndiceService')
        ->setMethods(array())
        ->setConstructorArgs(array($maiIndiceTable))
        ->getMock();
    $this->assertTrue($maiIndiceService instanceof PMaiIndiceService);

    $this->controller = new RevisionsController($maiIndiceService);

    $this->request    = new Request();
    $this->routeMatch = new RouteMatch(array('controller' => 'index'));
    $this->event      = new MvcEvent();
    $config = $serviceManager->get('Config');
    $routerConfig = isset($config['router']) ? $config['router'] : array();
    $router = HttpRouter::factory($routerConfig);
    $this->event->setRouter($router);
    $this->event->setRouteMatch($this->routeMatch);
    $this->controller->setEvent($this->event);
    $this->controller->setServiceLocator($serviceManager);
}


Here is the test for my function :

public function testEditFormuleActionCanBeAccessed()
{
    $this->routeMatch->setParam('action', 'loadformule');
    $this->routeMatch->setParam('idformule', '23');
    $result   = $this->controller->dispatch($this->request);
    $response = $this->controller->getResponse();
    $this->assertEquals(200, $response->getStatusCode());
}


And my Controler :

public function loadindiceAction()
{
    try {
        $iStatus = 0;
        $iMaiIndiceId = (int) $this->params('idindice');

        $oIndice = $this->maiIndiceService->selectByIdOrCreate($iMaiIndiceId);
        $maiIndicerevisionForm = new PMaiIndicerevisionForm($oIndice);

        if ($this->getRequest()->isPost()) {
            /* etc ... */
        }

        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        $viewModel->setVariables([
            'maiIndicerevisionForm' => $maiIndicerevisionForm,
            'iMaiIndiceId'          => $oIndice->getMaiIndiceId(),
            'iStatus'               => $iStatus
        ]);
        return $viewModel;
    } catch (\Exception $e) {
        throw new \Exception($e);
    }
}


But when I try to run my test, it shows an error, and I point that my test don't go into my service :


  1) MaintenanceTest\Controller\RevisionsControllerTest::testEditFormuleActionCanB
  eAccessed
  Exception: Argument 1 passed to Maintenance\Form\PMaiFormulerevisionForm::__cons
  truct() must be an instance of Maintenance\Model\PMaiFormulerevision, null given


I don't understand why my mock doesn't work ...

Thanks for your answers :)
    
asked Sep 7, 2015 by rajesh
0 votes
5 views



Related Hot Questions



Government Jobs Opening


...