Class 仅在使用 PHPUnit 时找不到
Class not found only when using PHPUnit
我正在使用 Symfony2 (2.7.3) 应用程序进行测试,并且页面控制器无法加载 class 仅当从 PHPUnit (4.8. 6)。
测试看起来像这样:
//AppBundle/Tests/Controller/PagesAvailableTest.php
<?php
namespace AppBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PagesAvailableTest extends WebTestCase
{
public function testpages()
{
$client = static::createClient();
$client->request('GET', '/contact'); // Throws the error
}
}
并在 运行 和 $ phpunit -c app/
时抛出此错误:
PHP Fatal error: Class 'AppBundle\Entity\ContactMessage' not found in SymfonyRoot/src/AppBundle/Controller/ContactController.php on line 28
通过浏览器调用时,页面按预期加载。使用 $ curl -I http://localhost/contact
调用时,页面状态为 HTTP/1.1 200 OK
我看过其他 similar questions that discuss problems with autoloading - but the Symfony docs 建议 classes 应该可以毫无问题地自动加载:
Just like in your real application - autoloading is automatically enabled via the bootstrap.php.cache file (as configured by default in the app/phpunit.xml.dist file).
<!-- app/phpunit.xml.dist -->
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="bootstrap.php.cache"
>
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
<directory>../src/*Bundle/Tests</directory>
</testsuite>
</testsuites>
<!--<php>-->
<!--In my experimentation, I uncommented this and tinkered with
a few values. After having no luck, I commented it out again. -->
<!--<server name="KERNEL_DIR" value="." />-->
<!--</php>-->
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>../src/*Bundle/Resources</directory>
<directory>../src/*Bundle/Tests</directory>
<directory>../src/*/*Bundle/Resources</directory>
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Resources</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
如何在使用 PHPUnit 时加载这些 classes?
我尴尬地忽略了一个关键细节:我的应用程序 运行 在与我的 PATH
环境变量指向的不同的 PHP 解释器上运行。
当我 运行 $ phpunit -c app/
时,它使用了我 OS 的默认 PHP (5.5.20) - 我的网站是 运行 PHP 5.6.10 由 MAMP 提供。
我可以通过在调用 PHPUnit:
时提供我自己的正确 PHP 解释器路径来解决这个问题
$ /path/to/correct/php phpunit -c app/
我正在使用 Symfony2 (2.7.3) 应用程序进行测试,并且页面控制器无法加载 class 仅当从 PHPUnit (4.8. 6)。
测试看起来像这样:
//AppBundle/Tests/Controller/PagesAvailableTest.php
<?php
namespace AppBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PagesAvailableTest extends WebTestCase
{
public function testpages()
{
$client = static::createClient();
$client->request('GET', '/contact'); // Throws the error
}
}
并在 运行 和 $ phpunit -c app/
时抛出此错误:
PHP Fatal error: Class 'AppBundle\Entity\ContactMessage' not found in SymfonyRoot/src/AppBundle/Controller/ContactController.php on line 28
通过浏览器调用时,页面按预期加载。使用 $ curl -I http://localhost/contact
调用时,页面状态为 HTTP/1.1 200 OK
我看过其他 similar questions that discuss problems with autoloading - but the Symfony docs 建议 classes 应该可以毫无问题地自动加载:
Just like in your real application - autoloading is automatically enabled via the bootstrap.php.cache file (as configured by default in the app/phpunit.xml.dist file).
<!-- app/phpunit.xml.dist -->
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="bootstrap.php.cache"
>
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
<directory>../src/*Bundle/Tests</directory>
</testsuite>
</testsuites>
<!--<php>-->
<!--In my experimentation, I uncommented this and tinkered with
a few values. After having no luck, I commented it out again. -->
<!--<server name="KERNEL_DIR" value="." />-->
<!--</php>-->
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>../src/*Bundle/Resources</directory>
<directory>../src/*Bundle/Tests</directory>
<directory>../src/*/*Bundle/Resources</directory>
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Resources</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
如何在使用 PHPUnit 时加载这些 classes?
我尴尬地忽略了一个关键细节:我的应用程序 运行 在与我的 PATH
环境变量指向的不同的 PHP 解释器上运行。
当我 运行 $ phpunit -c app/
时,它使用了我 OS 的默认 PHP (5.5.20) - 我的网站是 运行 PHP 5.6.10 由 MAMP 提供。
我可以通过在调用 PHPUnit:
时提供我自己的正确 PHP 解释器路径来解决这个问题$ /path/to/correct/php phpunit -c app/