2015-09-07 186 views
7

当使用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)...所以它仍然不起作用...

+0

请我真的封锁。我真的需要解决这个模拟问题,文档非常软! – Amelie

+0

未经测试,但它看起来像为maiFormulerevisionService创建了一个模拟,它为所有方法返回null。 所以在你的控制器,这将返回null '$ oFormule = $这个 - > maiFormulerevisionService-> selectByIdOrCreate($ iMaiFormuleRevisionId);' 这意味着,这将在构造函数中,这是你的错误信息 传递空 '$ maiFormulerevisionForm = new PMaiFormulerevisionForm($ oFormule);' – Ed209

+0

我测试过,但测试模式下的方法实际上并没有进入selectByIdOrCreate()函数,所以它不会返回null ...呃,实际上它没有' t进入服务或数据网关类... – Amelie

回答

2

你设置了一个模拟,但你也必须设置你的模拟返回调用方法selectByIdOrCreate。既然你做:

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

的模拟会,只要你不设置此方法的返回值返回nullselectByIdOrCreate方法。

尝试添加一个模拟的方法是这样的:

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

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