【问题标题】:the PHPUnit test of a uri fails while the request succeeded in the browser请求在浏览器中成功时,URI 的 PHPUnit 测试失败
【发布时间】:2021-02-25 09:53:00
【问题描述】:

我的应用程序在 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 !!!!!!

我该如何解决这个问题?

感谢您的回答

【问题讨论】:

    标签: phpunit symfony4


    【解决方案1】:

    我的问题解决了!

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 2022-01-11
      • 2019-12-11
      相关资源
      最近更新 更多