2015-09-07 7 views
7

私は少し問題を抱えています。ここでのPHPUnitとZF2サービス

は私の設定です:ここでは

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

は私の機能のためのテストです:

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

そして、私の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); 
    } 
} 

しかし、私は私のテストを実行しよう、それがエラーを示しており、私はそれ(の$ this - > maiFormulerevisionService)を呼び出したときに、私のテストは、私のサービスに行かないことを指す:

1)MaintenanceTest \コントローラ\ RevisionsControllerTest :: 例外をtestEditFormuleActionCanBeAccessed:メンテナンス\フォーム\ PMaiFormulerevisionFormに渡される引数1を:: __構築物()はnullを与え、メンテナンス\モデル\ PMaiFormulerevisionのインスタンスでなければなりません

私のモックが動作しない理由を私は...あなたの答えのための

感謝を理解していない:)

編集:

ハム...私はこのしようとすると:この代わりの

$maiFormulerevisionService = new PMaiFormulerevisionService($maiFormuleRevisionTable); 

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

をそれは(サービスにではなく、サービスのコンストラクタで指定TableGatewayに入りますメソッドを呼び出すときに$ maiFormuleRevisionTable)...ので、それはまだ動作しません...

+0

私は本当にブロックされています:

は、このようなモックメソッドを追加してください。私は本当にこのモックの問題を解決する必要があり、ドキュメントは本当に柔らかいです! – Amelie

+0

テストしたが、あなたはすべてのメソッドでnullを返すmaiFormulerevisionServiceのモックを作成しているように見えません。 は、だからあなたのコントローラでは、これが返されますヌル '$ oFormule =の$ this - > maiFormulerevisionService-> selectByIdOrCreate($ iMaiFormuleRevisionId);'これはあなたのエラーメッセージ でコンストラクタにnullを渡されることを意味します '$ maiFormulerevisionForm =新しいPMaiFormulerevisionForm($ oFormule);' – Ed209

+0

私がテストしたが、テストモードでの方法は、実際にはselectByIdOrCreate()関数に行っていないので、それはnullを返しません...まあ、実際にはそれはdoesnのサービス、またはdatagatewayのクラスに行く... – Amelie

答えて

2

あなたがモックを設定していますが、何を、あなたのモックリターンを設定する必要があります。あなたが行うので:

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

モックは、限り、あなたは、このメソッドの戻り値を設定しないようselectByIdOrCreate方法のためnullを返します。

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

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