【问题标题】:PHPUnit cannot locate Zend_Test_PHPUnit_ControllerTestCase when outside of base "tests" folderPHPUnit 在基本“测试”文件夹之外时无法找到 Zend_Test_PHPUnit_ControllerTestCase
【发布时间】:2012-12-19 23:33:39
【问题描述】:

我正在使用 PHP 5.3.4、Zend 1.11.11 和 PHPUnit 3.7.1 运行 Wamp Server。我的项目结构是默认的 Zend 布局,其中测试文件夹组织如下:

tests/
   application/
   library/
   bootstrap.php
   phpunit.xml
   IndexControllerTest.php

application/library/ 都是空的。这个基本案例适用于我当前的设置,我可以从终端运行phpunit IndexControllerTest,它运行测试没有错误。一旦我将 IndexControllerTest.php 移动到不同的位置(例如,在application/ 目录中),问题就会出现。然后我从 phpunit 收到一个致命错误:

PHP Fatal error: Class 'Zend_Test_PHPUnit_ControllerTestCase' not found in 
X:\Program Files (x86)\Wamp\www\Local_site\Zend\login\tests\application\IndexControllerTest.php 
on line 3

现在除非我弄错了,否则自动加载器应该会处理这个require(),但是一旦我更改了测试文件的位置,就会出现问题。我已经尝试一遍又一遍地调整 phpunit.xml 文件,但我不确定这是我的问题所在,还是在我的 bootstrap.php 或我的代码中。任何见解将不胜感激。甚至向正确的方向轻推。以下是相关文件:

phpunit.xml

<phpunit bootstrap="./bootstrap.php">
<testsuites>
<testsuite name="My Project">
    <directory>./tests</directory>
</testsuite>

</testsuites>

<filter>
    <!-- If Zend Framework is inside your project's library, uncomment this filter -->

    <whitelist>
        <directory suffix=".php">/library/Zend</directory>
    </whitelist>

</filter>
</phpunit>

bootstrap.php

<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));


require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

IndexControllerTest.php

<?php
class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    protected $application;

    public function setUp() 
    {
        // Assign and instantiate in one step:
        $this->bootstrap = new Zend_Application(
            'testing',
            APPLICATION_PATH . '/configs/application.ini'
            );
        parent::setUp();
    }

    public function testTrue()
    {
         $this->assertTrue(true);
    }
}

根据黄金互联网的其他建议,我还将我的 php/php.ini include_path 更改为:

include_path=".;X:\Program Files (x86)\Wamp\bin\php\php5.3.4\pear;X:\Program Files (x86)\Wamp\bin\php\php5.3.4\pear\PHPUnit"

我是否需要明确包含Zend/Test/PHPUnit/ControllerTestCase.php?我已经尝试了数百种解决方案,但我一直在盲目飞行,所以我可能非常接近,甚至都不知道。

【问题讨论】:

  • 当您移动 *Test.php 文件时,您从哪里运行 phpunit?在这种情况下会加载配置文件吗?
  • 如果 IndexControllerTest.php 位于 tests/application/IndexControllerTest.php 中,我是否正在运行 phpunit application/IndexControllerTest?是的,我做了(同样的错误),它不会为未加载的配置文件产生任何错误。即使我将配置文件(我假设是 phpunit.xml)移动到测试/应用程序中,我仍然会收到相同的错误。

标签: php zend-framework phpunit


【解决方案1】:

设法找到解决这个问题的方法(就这么简单......)。无论出于何种原因,phpunit.xml 都没有引导我的 bootstrap.php 文件。在我在 IndexControllerTest.php 中声明我的类之前需要我的引导程序:

IndexControllerTest

require_once '/path/to/bootstrap.php';

IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
      public function testTrue() 
      {
              $this->assertTrue(true);
      }
}

在每个测试文件中都需要这个似乎很奇怪,我相信我很快就会找到一个更持久的解决方案。希望这对某人有所帮助。

编辑

找到稍微好一点的解决方案,我只是将我的 phpunit.xml 文件复制到与 IndexControllerTest 相同的目录,并在新的 phpunit.xml 文件中将 bootstrap="bootstrap.php" 更改为 bootstrap="../bootstrap.php"。现在该目录中的所有测试文件都不需要require_once。似乎仍然是一个粗略的修复,但总比没有好。

【讨论】:

    【解决方案2】:

    过去几个小时我一直在努力解决这个问题,并使用我设法想出以下答案的答案,并将其添加到我的 application/bootstrap.php

    require_once 'Zend/Loader/Autoloader/Resource.php';
    
    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
    'basePath'      => APPLICATION_PATH,
    'namespace'     => '',
    'resourceTypes' => array(
        'cplib' => array(
            'path'      => 'library/CP',
            'namespace' => 'CP_'
        ),
    ),
    ));
    

    一旦我有了这个,我就可以自动加载正确的 Zend 库文件,而无需手动指定它们。

    【讨论】:

      【解决方案3】:

      您不需要在测试用例中明确包含引导文件。

      您需要在包含路径上有库文件夹(其中包含 Zend 库)。 bootstrap.php 文件应该对此负责。

      这个错误信息有两个可能的原因:

      1. phpunit.xml 无法引用正确的 bootstrap.php。

      2. 引导文件没有引用正确的库路径(我认为这里就是这种情况)

      您可以在 bootstrap.php 中回显包含路径,以确保其设置正确。

      基本上,将以下行添加到 bootstrap.php 之前的 require_once

      $includePath  = get_include_path();
      echo "appPath = ".APPLICATION_PATH. "\n";
      echo "includePath = $includePath \n";
      

      includePath 应该有一个应用程序/库文件夹的条目 (请注意,有 2 个应用程序文件夹。一个保存类,另一个保存测试。您应该指的是测试目录之外的那个)

      here 是典型的文件夹结构。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-31
        • 2011-04-03
        • 1970-01-01
        • 2015-02-24
        • 2021-08-23
        • 1970-01-01
        • 1970-01-01
        • 2016-03-19
        相关资源
        最近更新 更多