首页 > 解决方案 > 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 App\Controller;

use App\Entity\User;
use App\Form\UserType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

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 App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
 * @internal
 * @coversNothing
 */
class UserControllerTest extends WebTestCase
{
    /**
     * testIndex.
     */
    public function testUserList(): void
    {
        $client = static::createClient();

        $client->request('GET', '/users');

        $this->assertTrue($client->getResponse()->isSuccessful());
    }
}

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

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

我该如何解决?

谢谢您的回答

标签: phpunitsymfony4

解决方案


我解决了我的问题!

当 a 有了在测试环境中显示页面的想法时,我得到了错误:

驱动程序发生异常:SQLSTATE[HY000] [2002] Aucune connexion n'a pu être établie car l'ordinateur cible l'a expressément refusée。

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


推荐阅读