【问题标题】:PHPUnit testing ZF2 fail on modelPHPUnit 测试 ZF2 在模型上失败
【发布时间】:2016-11-07 17:17:33
【问题描述】:

我正在尝试对此代码进行简单的单元测试:

  class IndexController extends CommonController
{  
    public function indexAction()
    {
    $this->layout('layout/layouthome');
    $language = $this->getLanguage($this->params('language'));

    $sm = $this->getServiceLocator();
    $tests = $sm->get('Tests');
    $users = $sm->get('Users');

    $this->layout()->language = substr($language, 0, 2);
    $translations = $this->getTranslations($sm, $language);
    $this->layout()->getAllTranslationByLocale = $translations;

    $userSession = $this->loginService->getSessionUser();

    if (!empty($userSession)) {
        $this->redirectUser($userSession, $language);
    }

    return new ViewModel(array(
        'translations' => $translations,
        'language' => $language,
        'getAllJobsByTopjobs' => $tests->getAllTestsByTopTests(),
        'countAllUsers' => $users->countAllUsers(),
    ));
    }
}

而我的简单测试是这样的:

<?php namespace ApplicationTest\Controller;

use Zend\Stdlib\ArrayUtils;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;

class LogoutTest extends AbstractHttpControllerTestCase
{
    protected $traceError = true;

    public function setUp()
    {
        parent::setUp();
        $configOverrides = [];

        $this->setApplicationConfig(ArrayUtils::merge(
        // Grabbing the full application configuration:
            include __DIR__ . '/../../../../../config/application.config.php',
            $configOverrides
        ));

    }

    public function testIndexActionCanBeAccessed()
    {
        $this->dispatch('/');
        $this->assertResponseStatusCode(200);
    }
}

我遇到的问题是我无法实例化测试和用户模型。我是 ZF2 和单元测试的新手,因此非常感谢任何帮助。谢谢

稍后编辑

在 setup() 测试中添加

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

所以现在所有配置都正确加载了。感谢安德鲁的所有帮助:)

【问题讨论】:

  • 当你在浏览器中触发控制器时,代码真的有效吗?
  • 是的,这就是它的优点。我无法以某种方式在控制台中获取测试模型,但在浏览器中一切正常。
  • 你能发布更多你的测试控制器吗,你在扩展一个zend测试控制器吗? - 有时您需要设置配置,以便它可以找到您的服务。
  • 我添加了完整的测试文件,以便您有更好的视野

标签: php unit-testing zend-framework zend-framework2


【解决方案1】:

我需要查看更多测试代码,但如果您使用的是基于 zend 的 phpunit 类,则需要设置配置,示例如下:

namespace Something;

use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;

class SomethingTest extends AbstractHttpControllerTestCase
{ 
    public function setUp()
    {
        // now the tests know how to load your config and
        // services / service manager etc.
        // this may be your main config or a test specific config
        $this->setApplicationConfig(
           include __DIR__ . '/../../config/application.config.test.php'
        );
    }

    public function testIndexActionCanBeAccessed()
    {
        // access via application object..
        $bla = $this->getApplication()->getServiceManager()->get('Tests');

        $this->dispatch('/');
        $this->assertResponseStatusCode(200);
    }

}

【讨论】:

  • 感谢您的回答,这正是我此刻所拥有的代码。不同之处在于我使用的是 application.config.php 文件而不是专用的测试文件。所以它不适用于此代码,因为它在控制器中的模型调用失败($tests = $sm->get('Tests');)。错误消息是: Zend\ServiceManager\ServiceManager::get 无法获取或创建 Zend\Db\Adapter\Adapter 的实例。同时代码在浏览器上也能完美运行。
  • 你试过这个:$this->getApplication()->getServiceManager()->get('Tests');这将使用您提供的配置引导应用程序并允许 sm 查找内容
  • 安德鲁你明白了! :)请升级答案,我会接受:)。 PS还有一个问题:所有模型都打开了,测试正在解析孔方法索引,但我有一个错误消息: Zend\ServiceManager\ServiceManager::get is unable to fetch or create an instance for Zend\Db\Adapter \适配器 。这超出了问题范围,所以如果您也想回答这个问题,那将非常酷:) 谢谢
  • 还有一个请求:你能解释一下你的答案吗?为什么我需要这样做?我很想更好地了解那里正在发生的事情,以便将来对它有一个清晰的认识。
  • 如果您遵循控制器方法(只需查看它扩展的控制器),您将看到。它需要引导整个应用程序才能使用 SM
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-28
  • 2015-03-24
  • 1970-01-01
  • 2015-10-31
  • 2015-04-22
  • 2017-06-24
  • 2013-08-26
相关资源
最近更新 更多