我创建了新的Symfony 4.1.x项目,但是当我尝试创建并执行功能测试时却出现错误:
PHP致命错误:类 在以下位置找不到'Symfony \ Bundle \ FrameworkBundle \ Test \ WebTestCase /home/tomasz/my_project/tests/ExampleControllerTest.php在第7行
复制步骤:
创建项目
composer create-project symfony/skeleton my_project
安装phpunit,功能测试组件和maker,以生成功能测试的框架
composer req --dev symfony/phpunit-bridge symfony/css-selector symfony/browser-kit symfony/maker-bundle
创建示例功能测试
bin/console make:functional-test ExampleControllerTest
已创建文件的内容
<?php
namespace App\Tests;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ExampleControllerTest extends WebTestCase
{
public function testSomething()
{
$client = static::createClient();
$crawler = $client->request('GET', '/');
$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertContains('Hello World', $crawler->filter('h1')->text());
}
}
运行测试
bin/phpunit
输出(安装phpunit依赖项后):
PHP致命错误:类 在以下位置找不到'Symfony \ Bundle \ FrameworkBundle \ Test \ WebTestCase /home/tomasz/my_project/tests/ExampleControllerTest.php在第7行
我检查过,Symfony\Bundle\FrameworkBundle\Test\WebTestCase
中确实存在类vendor/
。有任何线索吗?
phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</phpunit>