Symfony 5和API平台2.5功能测试不允许使用的方法

问题描述 投票:0回答:1

我有刚刚安装了flex的Symfony 5和API-Platform 2.5。我创建了用户实体作为API资源,必须禁止收集列表,因此我创建了功能测试来对其进行检查。

测试正确通过,但我也收到错误:

$ ./vendor/bin/simple-phpunit --filter=UsersTest::testGetCollection
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

Testing Project Test Suite
2020-05-03T21:50:50+00:00 [error] Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: "No route found for "GET /api/users": Method Not Allowed (Allow: POST)" at /srv/api/vendor/symfony/http-kernel/EventListener/RouterListener.php line 140
.                                                                   1 / 1 (100%)

Time: 691 ms, Memory: 30.00 MB

OK (1 test, 1 assertion)

我想隐藏错误消息。

以下是文件:

用户类别

/**
 * @ApiResource(collectionOperations={"post"}, itemOperations={"get"})
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User
{
   /* some code */
}

测试

class UsersTest extends ApiTestCase
{
    use RefreshDatabaseTrait;

    /* some code */

    public function testGetCollection(): void
    {
        try {
            self::createClient()->request(Request::METHOD_GET, '/api/users');
            self::assertResponseStatusCodeSame(Response::HTTP_METHOD_NOT_ALLOWED);
        } catch (\Exception $e) {
        }
    }

    /* some code */
}

phpunit.xml.dist

<php>
    <ini name="error_reporting" value="-1" />
    <server name="APP_ENV" value="test" force="true" />
    <server name="SHELL_VERBOSITY" value="-1" />
    <server name="SYMFONY_PHPUNIT_REMOVE" value="" />
    <server name="SYMFONY_PHPUNIT_VERSION" value="7.5" />
</php>

谢谢!

php symfony testing phpunit api-platform.com
1个回答
0
投票

您可以使用

$client = self::createClient();
$client->catchExceptions(false);

但是我建议使用

$client = self::createClient(); 
$client->request(Request::METHOD_GET, "/api/users");
$this->assertEquals(Response::HTTP_METHOD_NOT_ALLOWED, $client->getResponse()->getStatusCode());

而不是用try / catch包装代码

© www.soinside.com 2019 - 2024. All rights reserved.