致命错误:在X行的/path/tests/Foo/BarTest.php中找不到类'Foo \ Bar'

时间:2015-05-18 16:04:06

标签: php phpunit autoloader

我有一个项目,我试图运行单元测试但没有成功。它最有可能与自动加载相关。

/ path / to / project $ phpunit tests

  

致命错误:在/path/tests/Foo/BarTest.php中找不到类'Foo \ Bar'   在第X行

这一行是执行新Bar();

的行

我在tests目录中有一个像这个

的phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../tests/bootstrap.php"
         backupGlobals="false"
         backupStaticAttributes="false"
         verbose="true">

    <testsuites>
        <testsuite name="App">
            <directory suffix="Test.php">tests/Foo</directory>
        </testsuite>
    </testsuites>

    <logging>
        <log type="coverage-html" target="build/coverage"/>
        <log type="coverage-clover" target="build/logs/clover.xml"/>
        <log type="coverage-crap4j" target="build/logs/crap4j.xml"/>
        <log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
    </logging>

    <filter>
        <whitelist addUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
            <exclude>
                <file>src/bootstrap.php</file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

我的结构是 SRC /富/ Bar.php 测试/富/ BarTest.php

我的测试/ bootstrap.php

if ( ! file_exists($file = __DIR__.'/../vendor/autoload.php')) {
    throw new RuntimeException('Install dependencies to run this script.');
}

$loader = require_once $file;
$loader->add('Foo', __DIR__ . '../src/');

所以我将Foo名称空间映射到src目录,但PHPUnit似乎找不到它。

此自动加载是Composer生成的。

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。由于路径问题,PHPUnit没有找到引导程序,因此没有加载Composer自动加载。

因为它没有显示出影响真实问题的任何警告。

因此,假设您要从tests文件夹中运行测试,您应该指向您的phpunit.xml

测试/ phpunit.xml

<phpunit bootstrap="bootstrap.php" ...

所以bootstrap.php也应该在tests文件夹中。

相关问题