Inncorect 请求 URL PHPUnit 功能测试 Symfony 4.2
Inncorect request URL PHPUnit functional test Symfony 4.2
我不能运行功能测试我仍然得到信息:
/blog public URL 加载正确。
未能断言 500 与 200 相同。
正如我检查调试 $client-> request('GET', $url) 创建地址 http://localhost/blog is incorrect page address. The correct page address is http: //project.local/blog or http://localhost/project/public/blog
class PagesControllerTest extends WebTestCase
{
public function testShowPost()
{
$client = static::createClient();
$client->request('GET', '/blog');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
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" />
<server name="KERNEL_CLASS" value="App\Kernel" />
<server name="HOSTNAME" value="http://project.local" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="/foobar/" />
</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" />
<listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitListener" />
</listeners>
</phpunit>
您可以创建。env.test此文件将在 .env 文件之后加载,并为您的测试环境覆盖环境变量。
DATABASE_URL=sqlite:///%kernel.project_dir%/data/database_test.sqlite
首先通过
打印出错误
$client = static::createClient();
$client->catchExceptions(false);
然后,在您的情况下,您的测试数据库似乎不存在,如下所示
An exception occurred in the driver: SQLSTATE[HY000] [1049] Unknown database 'database_test'
你有两个选择:
- 为您的测试创建新数据库
- 在您的数据库测试中删除 dbname_suffix,它负责为新数据库测试提供后缀名 -at
config/packages/test/doctrine.yaml -
我不能运行功能测试我仍然得到信息:
/blog public URL 加载正确。 未能断言 500 与 200 相同。
正如我检查调试 $client-> request('GET', $url) 创建地址 http://localhost/blog is incorrect page address. The correct page address is http: //project.local/blog or http://localhost/project/public/blog
class PagesControllerTest extends WebTestCase
{
public function testShowPost()
{
$client = static::createClient();
$client->request('GET', '/blog');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
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" />
<server name="KERNEL_CLASS" value="App\Kernel" />
<server name="HOSTNAME" value="http://project.local" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="/foobar/" />
</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" />
<listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitListener" />
</listeners>
</phpunit>
您可以创建。env.test此文件将在 .env 文件之后加载,并为您的测试环境覆盖环境变量。
DATABASE_URL=sqlite:///%kernel.project_dir%/data/database_test.sqlite
首先通过
打印出错误$client = static::createClient();
$client->catchExceptions(false);
然后,在您的情况下,您的测试数据库似乎不存在,如下所示
An exception occurred in the driver: SQLSTATE[HY000] [1049] Unknown database 'database_test'
你有两个选择:
- 为您的测试创建新数据库
- 在您的数据库测试中删除 dbname_suffix,它负责为新数据库测试提供后缀名 -at config/packages/test/doctrine.yaml -