如何解决用phpunit测试路由通配符?
我有以下控制器,其路由包含通配符
/**
* @Route("api/1.0/test/{$a}",name="test",methods={"GET"})
* @return JsonResponse
*/
public function testSimple(int $a)
{
return $this->json(['success' => ($a == 1) ? true : false]);
}
我正在尝试测试通配符$ a = 1是否返回true,但是我似乎无法找到正确的语法来测试路由:
A-作为完整路径,将通配符设置为要传递的值:
/**
* @test
*/
public function checkRouteWildcardReturnsExepctedResult()
{
$client = $this->createClient();
$request = $client->request('GET','api/1.0/test/1');
$response = $client->getResponse()->getContent();
$this->assertEquals(json_encode(["success" => true]),$response);
}
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'{"success":true}'
+'{"success":false,"errors":["No route found for GET \/api\/1.0\/test\/1"],"data":[]}'
B-精确指定路线,找到正确的路线,但未设置$ a
/**
* @test
*/
public function checkRouteWildcardReturnsExepctedResult()
{
$client = $this->createClient();
$request = $client->request('GET','api/1.0/test/{$a}',['a' => 1]);
$response = $client->getResponse()->getContent();
$this->assertEquals(json_encode(["success" => true]),$response);
}
--- Expected
+++ Actual
@@ @@
-'{"success":true}'
+'{"success":false,"errors":["Could not resolve argument $a of [__CLASS__]::testSimple(),maybe you forgot to register the controller as a service or missed tagging it with the controller.service_arguments?"],"data":[]}'
任何链接到某些文档的建议,我似乎都无法找到,不胜感激-欢呼。
解决方法
发现了问题,选项1是正确的解决方案,并且路由通配符不应包含$,应为@Route("api/1.0/test/{a}",name="test",methods={"GET"})
所以它确实按我最初预期的那样工作
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。