uri的PHPUnit测试失败,而请求在浏览器中成功



我的应用程序在Symfony 4.4、PHP 7.4上。

我的composer.json:

{
"name": "saro0h/to-do-list",
"type": "project",
"license": "proprietary",
"require": {
"php": ">=7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11",
"doctrine/annotations": "^1.11",
"doctrine/doctrine-bundle": "^2.1",
"doctrine/doctrine-migrations-bundle": "^3.0",
"doctrine/orm": "^2.7",
"sensio/framework-extra-bundle": "^5.6",
"symfony/apache-pack": "^1.0",
"symfony/asset": "4.4.*",
"symfony/console": "4.4.*",
"symfony/dotenv": "4.4.*",
"symfony/flex": "^1.3.1",
"symfony/form": "4.4.*",
"symfony/framework-bundle": "4.4.*",
"symfony/mailer": "4.4.*",
"symfony/monolog-bundle": "^3.6",
"symfony/security-bundle": "4.4.*",
"symfony/security-csrf": "4.4.*",
"symfony/twig-bundle": "4.4.*",
"symfony/validator": "4.4.*",
"symfony/yaml": "4.4.*"
},
"require-dev": {
"phpstan/phpstan": "^0.12.54",
"symfony/browser-kit": "4.4.*",
"symfony/css-selector": "4.4.*",
"symfony/maker-bundle": "^1.23",
"symfony/phpunit-bridge": "^5.1",
"symfony/stopwatch": "^4.4",
"symfony/web-profiler-bundle": "^4.4"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\Tests\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.4.*"
}
}
}

有一个方法控制器我想测试:

<?php
namespace AppController;
use AppEntityUser;
use AppFormUserType;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;
class UserController extends AbstractController
{
/**
* list all users.
*
* @Route("/users", name="user_list")
*
* @return Response
*/
public function list(): Response
{
return $this->render('user/list.html.twig', ['users' => $this->getDoctrine()->getRepository(User::class)->findAll()]);
}
}

然后用PHPUnit 进行测试

<?php
namespace AppTestsController;
use SymfonyBundleFrameworkBundleTestWebTestCase;
/**
* @internal
* @coversNothing
*/
class UserControllerTest extends WebTestCase
{
/**
* testIndex.
*/
public function testUserList(): void
{
$client = static::createClient();
$client->request('GET', '/users');
$this->assertTrue($client->getResponse()->isSuccessful());
}
}

在浏览器中,请求成功(页面显示无错误,200 HTTP代码状态,探查器中无错误(

但是,当我运行PHPUnit时,测试失败,$client->getResponse((->getStatusCode((=500!!!!

我该怎么解决?

感谢您的回答

我解决了我的问题!

当a想到用测试环境显示页面时,我得到了错误:

驱动程序中发生异常:SQLSTATE[HY000][2002]Aucune connection'a puêtreétablelie car l'ordinateur cible l'a expressément rejectée。

所以我解决了在定义的.env.test.中为测试环境创建数据库的问题

最新更新