Symfony 5 和 API-不允许方法的平台 2.5 功能测试
Symfony 5 and API-Platform 2.5 functional test for Not allowed method
我有 Symfony 5 和 API-Platform 2.5,刚刚安装了 flex。
我创建了 User 实体作为 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)
我想隐藏错误信息。
文件如下:
用户Class
/**
* @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>
谢谢!
您可以使用
$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
包装代码
我检查了代码,看起来你没有在你的代码中使用 monolog 库。你是对的,如果你没有为你的项目定义任何 loggerIterface(日志驱动程序),RouterListener 将打印出错误消息。一旦安装 symfony/monolog-bundle
,您将不会再看到那些丑陋的消息。
我有 Symfony 5 和 API-Platform 2.5,刚刚安装了 flex。
我创建了 User 实体作为 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)
我想隐藏错误信息。
文件如下:
用户Class
/**
* @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>
谢谢!
您可以使用
$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
包装代码我检查了代码,看起来你没有在你的代码中使用 monolog 库。你是对的,如果你没有为你的项目定义任何 loggerIterface(日志驱动程序),RouterListener 将打印出错误消息。一旦安装 symfony/monolog-bundle
,您将不会再看到那些丑陋的消息。