當使用PHPUnit創建測試時,我遇到了一些小問題。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 :: __構建體()必須維護\模型\ PMaiFormulerevision的一個實例,給定的零
我不明白爲什麼我的模擬不工作...
謝謝您的回答:)
編輯:
嗚嗚......當我試試這個:
$maiFormulerevisionService = new PMaiFormulerevisionService($maiFormuleRevisionTable);
取而代之的是:
$maiFormulerevisionService = $this->getMockBuilder('Maintenance\Service\Model\PMaiFormulerevisionService')
->setMethods(array())
->setConstructorArgs(array($maiFormuleRevisionTable))
->getMock();
它進入服務而不是爲在服務的構造函數中指定的TableGateway( $ maiFormuleRevisionTable)...所以它仍然不起作用...
請我真的封鎖。我真的需要解決這個模擬問題,文檔非常軟! – Amelie
未經測試,但它看起來像爲maiFormulerevisionService創建了一個模擬,它爲所有方法返回null。 所以在你的控制器,這將返回null '$ oFormule = $這個 - > maiFormulerevisionService-> selectByIdOrCreate($ iMaiFormuleRevisionId);' 這意味着,這將在構造函數中,這是你的錯誤信息 傳遞空 '$ maiFormulerevisionForm = new PMaiFormulerevisionForm($ oFormule);' – Ed209
我測試過,但測試模式下的方法實際上並沒有進入selectByIdOrCreate()函數,所以它不會返回null ...呃,實際上它沒有' t進入服務或數據網關類... – Amelie