我正在使用Symfony2(2.7.3)应用程序进行测试,只有当PHPUnit (4.8.6)发送请求时,页面控制器才无法加载类。
测试如下:
//AppBundle/Tests/Controller/PagesAvailableTest.php
<?php
namespace AppBundleTestsController;
use SymfonyBundleFrameworkBundleTestWebTestCase;
class PagesAvailableTest extends WebTestCase
{
public function testpages()
{
$client = static::createClient();
$client->request('GET', '/contact'); // Throws the error
}
}
并在使用$ phpunit -c app/
:
当通过浏览器调用PHP致命错误:类'AppBundleEntityContactMessage'未在SymfonyRoot/src/AppBundle/Controller/ContactController.php第28行
时,页面按预期加载。当使用$ curl -I http://localhost/contact
调用时,该页的状态为HTTP/1.1 200 OK
我看过其他类似的问题,讨论自动加载的问题-但是Symfony文档建议类应该自动加载没有问题:
就像在你的实际应用程序中一样,自动加载是通过bootstrap.php.cache文件自动启用的(默认配置在app/phpunit.xml中)。dist文件)。
<!-- 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时加载这些类?
我尴尬地忽略了一个关键细节:我的应用程序运行在与PATH
环境变量所指向的不同的PHP解释器上。
当我运行$ phpunit -c app/
时,它使用了我的操作系统默认的PHP(5.5.20) -我的网站运行在PHP 5.6.10上,由MAMP提供
$ /path/to/correct/php phpunit -c app/