Silex和phpunit JSON响应

Lee*_*lan 5 phpunit json silex

我正在尝试使用phpunit为Silex编写一些测试.

我有一个Symfony\Component\BrowserKit\Client类生成一个Crawler对象.

这个对象期望客户端的结果是xhtml但是我试图测试的api返回JSON并且爬虫不允许这样做.

在Silex或phpunit中是否有内置类可以使用JSON,还是我必须自己滚动?

干杯

igo*_*orw 22

处理json没有什么特别之处,但您可以在不使用爬虫的情况下使用客户端.只需调用getResponse()客户端即可获得响应,如下所示:

$client = $this->createClient();
$client->request('GET', '/');
$response = $client->getResponse();

$data = json_decode($response->getContent(), true);
$this->assertSame(array('id' => 1, 'name' => 'igorw'), $data['users'][0]);
Run Code Online (Sandbox Code Playgroud)

我建议你将这个逻辑转换为测试用例的辅助方法并使用它.