如何在测试时调试控制器,使用:Guzzle,Symfony,PhpUnit,PhpStorm,REST?



在 PhpStorm 中调试测试类(PHPUnit_Framework_TestCase 的子类)时,它会在此类中设置的断点处停止,但不会在请求指向的控制器 (Symfony) 中停止。

//test class - here debugger stops
class FooControllerTest extends PHPUnit_Framework_TestCase
{
public function testPOST()
{
$response = $this->client->post('/api/foo', [
'body' => json_encode($data)
]);

.

//controller - here debugger not stopping
/**
* @Route("/api/foo")
* @Method("POST")
*/
public function newAction(Request $request)
{
//...
return new Response($json, 201, array(
'Content-Type' => 'application/json'
));

请求肯定会输入到这个控制器,因为我可以在那里更改 http 代码,并且此更改在client->post(行后的测试类中是可读

的测试时如何调试控制器?

如果你正在使用Symfony和PHPUnit测试你的api,我可以给你一些建议,前提是我并不真正完全理解你所说的"调试控制器"是什么意思。

首先:当测试失败时,将侦听器附加到 PHPUnit,该侦听器将打印 PSR-7 HTTP 请求和响应正文。如果您使用的是Guzzle 6,则可以轻松实现。以下是一些可以帮助您的文档:

  • PHP 单元中的侦听器
  • Guzzzle6 Psr7 响应和请求(检查它显示异常的地方)

第二:在 config_test.yml 中启用探查器。这样,探查器将收集测试的信息,并使调试更容易。由于您正在打印 Psr7 请求和响应字符串,因此标头将包含指向该请求的探查器的链接。

使用SQLite数据库使我的设置用于测试,我发现它非常困难。现在,如果你真的想跳到下一个级别,你应该使用Behat。:)

在 PhpStrom 中,您需要准备同时调试会话。因此,在测试类中,将GET参数添加到URL:

class FooControllerTest extends PHPUnit_Framework_TestCase
{
public function testPOST()
{
//...
$response = $this->client->post('/api/foo'. '?' . $this->getDebugQuery(), [
'body' => json_encode($data)
]);
//...
}
private function getDebugQuery()
{
$debuggingQuerystring = '';
if (isset($_GET['XDEBUG_SESSION_START'])) { // xdebug
$debuggingQuerystring = 'XDEBUG_SESSION_START=' . $_GET['XDEBUG_SESSION_START'];
}
if (isset($_COOKIE['XDEBUG_SESSION'])) { // xdebug (cookie)
$debuggingQuerystring = 'XDEBUG_SESSION_START=PHPSTORM';
}
if (isset($_GET['start_debug'])) { // zend debugger
$debuggingQuerystring = 'start_debug=' . $_GET['start_debug'];
}
if (empty($debuggingQuerystring)) {
$debuggingQuerystring = 'XDEBUG_SESSION_START=PHPSTORM';
}
return $debuggingQuerystring;
}

并切换"侦听调试器连接"按钮。

最新更新