2015-09-07 21 views
7

Ho un piccolo problema durante la creazione di test con PHPUnit.Servizi PHPUnit e ZF2

Qui è la mia messa a punto:

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); 

    $maiFormuleRevisionTable = $this->getMockBuilder('Maintenance\Model\BDD\PMaiFormulerevisionTable') 
     ->setMethods(array()) 
     ->setConstructorArgs(array($mockTableGateway, $this->adapter, $this->sql)) 
     ->getMock(); 
    $maiFormulerevisionService = $this->getMockBuilder('Maintenance\Service\Model\PMaiFormulerevisionService') 
     ->setMethods(array()) 
     ->setConstructorArgs(array($maiFormuleRevisionTable)) 
     ->getMock(); 
    $this->assertTrue($maiFormulerevisionService instanceof PMaiFormulerevisionService); 

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

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

Ecco il test per la mia funzione:

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()); 
} 

E il mio Controler:

public function loadformuleAction() 
{ 
    try { 
     $iStatus = 0; 
     $iMaiFormuleRevisionId = (int) $this->params('idformule'); 

     $oFormule = $this->maiFormulerevisionService->selectByIdOrCreate($iMaiFormuleRevisionId); 
     $maiFormulerevisionForm = new PMaiFormulerevisionForm($oFormule); 

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

     $viewModel = new ViewModel(); 
     $viewModel->setTerminal(true); 
     $viewModel->setVariables([ 
      'maiFormulerevisionForm' => $maiFormulerevisionForm, 
      'iMaiFormuleRevisionId' => $oFormule->getMaiFormuleRevisionId(), 
      'iStatus'    => $iStatus 
     ]); 
     return $viewModel; 
    } catch (\Exception $e) { 
     throw new \Exception($e); 
    } 
} 

Ma quando si tenta di eseguire il mio test , mostra un errore, e indico che il mio test non entra nel mio servizio, quando lo chiamo ($ this-> maiFormulerevisionService):

1) MaintenanceTest \ Controller \ RevisionsControllerTest :: testEditFormuleActionCanBeAccessed Eccezione: Argomento 1 passato alla Manutenzione \ Form \ PMaiFormulerevisionForm :: __ construct() deve essere un'istanza di manutenzione \ modello \ PMaiFormulerevision, null data

non capisco il motivo per cui il mio finto non funziona ...

Grazie per le vostre risposte :)

Edit:

Hum ... quando provo questo:

$maiFormulerevisionService = new PMaiFormulerevisionService($maiFormuleRevisionTable); 

Invece di questo:

$maiFormulerevisionService = $this->getMockBuilder('Maintenance\Service\Model\PMaiFormulerevisionService') 
      ->setMethods(array()) 
      ->setConstructorArgs(array($maiFormuleRevisionTable)) 
      ->getMock(); 

Si va al servizio, ma non nella TableGateway specificato nel costruttore del servizio ($ maiFormuleRevisionTable) ... quindi continua a non funzionare ...

+0

prega Sono davvero bloccato. Ho davvero bisogno di risolvere questo finto problema e la documentazione è davvero morbida! – Amelie

+0

Non testato, ma sembra che si stia creando una simulazione per maiFormulerevisionService che restituisce null per tutti i metodi. Così nel controller, questo ritorna null '$ oFormule = $ this-> maiFormulerevisionService-> selectByIdOrCreate ($ iMaiFormuleRevisionId);' che significa che questo verrà passato nulla nel costruttore, che è il vostro messaggio di errore '$ maiFormulerevisionForm = new PMaiFormulerevisionForm ($ oFormule);' – Ed209

+0

Ho eseguito il test ma il metodo in modalità test non entra in funzione selectByIdOrCreate() in effetti, quindi non restituisce null ... beh, in effetti non lo fa t andare in servizi, o classi datagateway ... – Amelie

risposta

2

Imposta una simulazione, ma devi anche impostare ciò che il tuo finto torna quando chiami il metodo selectByIdOrCreate. Dal momento che si fa:

$oFormule = $this->maiFormulerevisionService->selectByIdOrCreate($iMaiFormuleRevisionId); 
$maiFormulerevisionForm = new PMaiFormulerevisionForm($oFormule); 

Il mock tornerà null per il metodo selectByIdOrCreate finché non si imposta un valore di ritorno per questo metodo.

tenta di aggiungere un metodo di finto come questo:

$mock = $maiFormulerevisionService; 
$methodName = 'selectByIdOrCreate'; 
$stub = $this->returnValue($maiFormuleRevisionTable); 

$mock->expects($this->any())->method($methodName)->will($stub);