HPi*_*rce 4 php phpunit symfony
我正在使用Symfony2(2.7.3)应用程序进行测试,并且页面控制器仅在从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
}
}
Run Code Online (Sandbox Code Playgroud)
运行时抛出此错误$ phpunit -c app/:
PHP致命错误:第28行的SymfonyRoot /src/AppBundle/Controller/ContactController.php中找不到类'AppBundle\Entity\ContactMessage'
通过浏览器调用时,页面按预期加载.使用时$ 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>
Run Code Online (Sandbox Code Playgroud)
如何在使用PHPUnit时加载这些类?
我尴尬地忽略了一个关键细节:我的应用程序运行在不同于我的PATH环境变量指向的PHP解释器上.
当我运行时$ phpunit -c app/,它使用我的操作系统的默认PHP(5.5.20) - 我的网站运行在PHP 5.6.10上,由MAMP提供.
通过在调用PHPUnit时提供自己的正确PHP解释器路径,我能够解决这个问题:
$ /path/to/correct/php phpunit -c app/
| 归档时间: |
|
| 查看次数: |
4984 次 |
| 最近记录: |