Zend framework2 ZF2 PHPUnit-测试控制器

Zend framework2 ZF2 PHPUnit-测试控制器,zend-framework2,phpunit,Zend Framework2,Phpunit,所以我决定深入研究PHPUnit,我已经阅读了大量的文档并在各地找到了示例,但是我被一个错误卡住了,我根本无法通过。我希望有人能帮我指出我遗漏了什么 可以找到我正在使用的当前文档 以下是我的项目树的外观: LoginControllerTest.php <?php namespace LoginTest\Controller; use LoginTest\Bootstrap; use Zend\Test\PHPUnit\Controller\AbstractHttpControlle

所以我决定深入研究PHPUnit,我已经阅读了大量的文档并在各地找到了示例,但是我被一个错误卡住了,我根本无法通过。我希望有人能帮我指出我遗漏了什么

可以找到我正在使用的当前文档

以下是我的项目树的外观:

LoginControllerTest.php

<?php

namespace LoginTest\Controller;

use LoginTest\Bootstrap;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;

/**
 * Class TestControllerTest
 *
 * @package LoginTest\Controller
 */
class TestControllerTest extends AbstractHttpControllerTestCase
{
    /**
     * Setup
     */
    public function setUp()
    {
        $this->setTraceError(true);

        $this->setApplicationConfig(
            Bootstrap::getConfig()
        );
    }

    /**
     * Index Action
     */
    public function testIndexActionCanBeAccessed()
    {
        $authenticationService = $this->getMockBuilder('Zend\Authentication\AuthenticationService')
                                      ->disableOriginalConstructor()
                                      ->getMock();

        $entityManager = $this->getMockBuilder('Doctrine\ORM\EntityManager')
                              ->disableOriginalConstructor()
                              ->getMock();

        $functionDates = $this->getMockBuilder('OTFunction\Service\Dates')
                              ->disableOriginalConstructor()
                              ->getMock();

        $functionEncryption = $this->getMockBuilder('OTFunction\Service\Encryption')
                                   ->disableOriginalConstructor()
                                   ->getMock();

        $functionIpaddress = $this->getMockBuilder('OTFunction\Service\Ipaddress')
                                  ->disableOriginalConstructor()
                                  ->getMock();

        $serviceManager = $this->getApplicationServiceLocator();

        $serviceManager->setAllowOverride(true);

        $serviceManager->setService(
            'Zend\Authentication\AuthenticationService',
            $authenticationService
        );

        $serviceManager->setService(
            'Doctrine\ORM\EntityManager',
            $entityManager
        );

        $serviceManager->setService(
            'OTFunction\Service\Dates',
            $functionDates
        );

        $serviceManager->setService(
            'OTFunction\Service\Encryption',
            $functionEncryption
        );

        $serviceManager->setService(
            'OTFunction\Service\Ipaddress',
            $functionIpaddress
        );

        $this->dispatch('/');

        $this->assertResponseStatusCode(200);
        $this->assertModuleName('Login');
        $this->assertControllerName('Login\Controller\Login');
        $this->assertControllerClass('LoginController');
        $this->assertMatchedRouteName('login');
    }

    /**
     * Index Action (Copy)
     */
    public function testIndexActionCanBeAccessedCopy()
    {
        $authenticationService = $this->getMockBuilder('Zend\Authentication\AuthenticationService')
                                      ->disableOriginalConstructor()
                                      ->getMock();

        $entityManager = $this->getMockBuilder('Doctrine\ORM\EntityManager')
                              ->disableOriginalConstructor()
                              ->getMock();

        $functionDates = $this->getMockBuilder('OTFunction\Service\Dates')
                              ->disableOriginalConstructor()
                              ->getMock();

        $functionEncryption = $this->getMockBuilder('OTFunction\Service\Encryption')
                                   ->disableOriginalConstructor()
                                   ->getMock();

        $functionIpaddress = $this->getMockBuilder('OTFunction\Service\Ipaddress')
                                  ->disableOriginalConstructor()
                                  ->getMock();

        $serviceManager = $this->getApplicationServiceLocator();

        $serviceManager->setAllowOverride(true);

        $serviceManager->setService(
            'Zend\Authentication\AuthenticationService',
            $authenticationService
        );

        $serviceManager->setService(
            'Doctrine\ORM\EntityManager',
            $entityManager
        );

        $serviceManager->setService(
            'OTFunction\Service\Dates',
            $functionDates
        );

        $serviceManager->setService(
            'OTFunction\Service\Encryption',
            $functionEncryption
        );

        $serviceManager->setService(
            'OTFunction\Service\Ipaddress',
            $functionIpaddress
        );

        $this->dispatch('/');

        $this->assertResponseStatusCode(200);
        $this->assertModuleName('Login');
        $this->assertControllerName('Login\Controller\Login');
        $this->assertControllerClass('LoginController');
        $this->assertMatchedRouteName('login');
    }
}
第115行是:

$serviceManager = $this->getApplicationServiceLocator();
我目前正在运行Zend Framework 2.3.4和PHPUnit 4.4.5

如果我能提供任何其他信息,请让我知道。我在谷歌上搜索了一页又一页,现在我完全不知所措

有什么想法吗

编辑1

我决定使用composer重新完成整个项目,以安装ZF2和我的模块。我按照《ZF2单元测试指南》设置了我的项目行

编辑2

我不知道为什么,但如果我在引导程序中注释掉这一行

$serviceManager->get('ModuleManager')->loadModules();
然后测试就可以毫无问题地运行了。当我重新启用单行代码时,它会抱怨合并配置。正如我们从PHPUnit错误中看到的,第一个测试确实运行得很好,但是第二个没有


老实说,我不明白
loadModules()
函数与任何事情都有什么关系,但是如果禁用这一行就是我所要做的一切,那就好了。我只是觉得奇怪,它导致了这个问题。

我猜你在PHP中弄错了

您的代码:

/**
 * Setup
 */
public function setUp()
{
    $this->setApplicationConfig(
        Bootstrap::config()
    );

    parent::setUp();
}
不会在任何地方调用自动加载。 没有自动加载功能将自动触发的规则

选择1,更改设置:

/**
 * Setup
 */
public function setUp()
{
    Bootstrap::autoload()

    $this->setApplicationConfig(
        Bootstrap::config()
    );

    parent::setUp();
}
还有另一种选择,就是这样。

我想出来了

我的
PHPUnit
设置是正确的,但是在我的
config/module.config.php
文件中:

public function getConfig()
{
    return include_once __DIR__ . '/config/module.config.php';
}
应该是:

public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}
我假设这可以解释为什么第一个测试会运行,而不是剩下的测试


这也解决了问题,因为必须禁用
loadModules()
功能。它现在已重新启用,并且一切正常。

错误消息告诉您,配置方法返回了不可遍历的内容(例如:数组)。您正在加载的模块中是否有返回非数组的
Module::getConfig
方法?或者,如果在引导过程中从任何文件加载配置,这些文件是否不返回数组?我的所有模块当前都返回配置数组,但我不确定引导是否正确。我甚至没有想到这会带来问题,但我肯定会检查一下。我已经清理了我的问题,希望它能更好地解释我的情况@AdamLundrigan I还确保返回的所有内容都返回一个数组。/vhosts/admin application/vendor/otwebsoft/admin login/test/LoginTest/Controller/TestControllerTest.php的第160行是什么?实际上,自动加载是在引导程序内部调用的。不管怎样,我给了你一个选择,有很多随机错误。
public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}