PHPUnit和ZF2服务

7

我在使用PHPUnit创建测试时遇到了一些小问题。

以下是我的设置:

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

我的控制器:

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\Controller\RevisionsControllerTest::testEditFormuleActionCanBeAccessed Exception: Argument 1 passed to Maintenance\Form\PMaiFormulerevisionForm::__construct() must be an instance of Maintenance\Model\PMaiFormulerevision, null given
我不明白为什么我的模拟不起作用...
感谢您的回答 :)
编辑:
嗯...当我尝试这样做时:
$maiFormulerevisionService = new PMaiFormulerevisionService($maiFormuleRevisionTable);

改为以下内容:

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

它进入了服务,但没有进入构造函数中指定的TableGateway($maiFormuleRevisionTable) ... 所以它还是没有工作...


请帮帮我,我真的卡住了。我非常需要解决这个模拟问题,但文档真的很简略! - Amelie
尚未测试,但看起来您正在创建一个模拟的maiFormulerevisionService,该服务对所有方法返回null。因此,在您的控制器中,这将返回null:$oFormule = $this->maiFormulerevisionService->selectByIdOrCreate($iMaiFormuleRevisionId);这意味着在构造函数中传递了null,这就是您的错误消息:$maiFormulerevisionForm = new PMaiFormulerevisionForm($oFormule); - Ed209
我进行了测试,但是测试模式下的方法实际上并没有进入 selectByIdOrCreate() 函数,因此它不会返回 null... 实际上,它并没有进入服务或数据网关类... - Amelie
在这里展示您的控制器构造函数会很有用。 - Benjamin
1个回答

2
您设置了一个模拟,但是在调用方法selectByIdOrCreate时,您还需要设置模拟返回的内容。因此,您需要执行以下操作:
$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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接