2015-09-07 11 views
7

Tôi có một vấn đề nhỏ khi tạo thử nghiệm với PHPUnit.Các dịch vụ PHPUnit và ZF2

Đây là thiết lập của tôi:

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

Đây là thử nghiệm cho chức năng của tôi:

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

Và Controler của tôi:

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

Nhưng khi tôi cố gắng chạy thử nghiệm của tôi , nó cho thấy một lỗi, và tôi chỉ ra rằng thử nghiệm của tôi không đi vào dịch vụ của tôi, khi tôi gọi nó ($ this-> maiFormulerevisionService):

1) MaintenanceTest \ khiển \ RevisionsControllerTest :: testEditFormuleActionCanBeAccessed Ngoại lệ: Đối số 1 truyền cho Maintenance \ Mẫu \ PMaiFormulerevisionForm :: __ construct() phải là một thể hiện của Maintenance \ mẫu \ PMaiFormulerevision, null cho

tôi không hiểu tại sao mô hình của tôi không hoạt động ...

Cám ơn câu trả lời của bạn :)

chỉnh sửa:

Hum ... khi tôi cố gắng này:

$maiFormulerevisionService = new PMaiFormulerevisionService($maiFormuleRevisionTable); 

Thay vì điều này:

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

Nó đi vào dịch vụ nhưng không vào TableGateway quy định tại các nhà xây dựng của dịch vụ ($ maiFormuleRevisionTable) ... do đó, nó vẫn không hoạt động ...

+0

Xin vui lòng tôi đang thực sự bị chặn. Tôi thực sự cần giải quyết vấn đề này và tài liệu thực sự rất mềm! – Amelie

+0

Không được thử nghiệm nhưng có vẻ như bạn đang tạo một mô hình cho maiFormulerevisionService trả về null cho tất cả các phương thức. Vì vậy, trong điều khiển của bạn, điều này sẽ trả về null '$ oFormule = $ this-> maiFormulerevisionService-> selectByIdOrCreate ($ iMaiFormuleRevisionId);' Có nghĩa này sẽ được thông qua null trong các nhà xây dựng, đó là thông báo lỗi của bạn '$ maiFormulerevisionForm = new PMaiFormulerevisionForm ($ oFormule);' – Ed209

+0

Tôi đã thử nghiệm nhưng phương thức trên chế độ thử nghiệm không đi vào hàm selectByIdOrCreate() trên thực tế, vì vậy nó không trả về null ... tốt, trên thực tế nó không ' đi vào các dịch vụ, hoặc các lớp datagateway ... – Amelie

Trả lời

2

Bạn thiết lập một mô hình, nhưng bạn cũng phải thiết lập những gì mô hình của bạn trở lại khi gọi phương thức selectByIdOrCreate. Vì bạn làm:

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

Các giả sẽ trở lại null cho phương pháp selectByIdOrCreate miễn là bạn không đặt một giá trị trả về cho phương pháp này.

Cố gắng thêm một phương pháp mô hình như thế này:

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

$mock->expects($this->any())->method($methodName)->will($stub); 
Các vấn đề liên quan