• Debug
    • JSON 关联数组调试
    • JSON 非关联数组调试
    • 关闭调试
    • 启用调试
    • 启用调试但是未初始化
    • 调试消息等级
    • 调试 Session
    • 调试 Log
    • 调试时间
    • 调试带有标签的时间

    Debug

    添加一个组件调试。

    引入相关类

    • use Error;
    • use Exception;
    • use Leevel\Debug\Debug;
    • use Leevel\Event\Dispatch;
    • use Leevel\Event\IDispatch;
    • use Leevel\Http\JsonResponse;
    • use Leevel\Http\Request;
    • use Leevel\Http\Response;
    • use Leevel\Leevel\App as Apps;
    • use Leevel\Log\File as LogFile;
    • use Leevel\Log\ILog;
    • use Leevel\Log\Log;
    • use Leevel\Option\IOption;
    • use Leevel\Option\Option;
    • use Leevel\Session\File as SessionFile;
    • use Leevel\Session\ISession;
    • use Leevel\Session\Session;

      JSON 关联数组调试

    关联数组结构会在尾部追加一个选项 :trace 用于调试。

    返回结构

    1. $response = ["foo" => "bar", ":trace" => []];

    关联数组在尾部追加一个选项作为调试信息,这与非关联数组有所不同。

    1. public function testJson()
    2. {
    3. $debug = $this->createDebug();
    4. $this->assertFalse($debug->isBootstrap());
    5. $debug->bootstrap();
    6. $this->assertTrue($debug->isBootstrap());
    7. $request = new Request();
    8. $response = new JsonResponse(['foo' => 'bar']);
    9. $debug->handle($request, $response);
    10. $content = $response->getContent();
    11. $this->assertContains('{"foo":"bar",":trace":', $content);
    12. $this->assertContains('"php":{"version":', $content);
    13. $this->assertContains('Starts from this moment with QueryPHP.', $content);
    14. }

    JSON 非关联数组调试

    非关联数组结构会在尾部追加一个 :trace 用于调试。

    返回结构

    1. $response = ["foo", "bar", [":trace" => []]];

    非关联数组在尾部追加一个调试信息,将不会破坏返回接口的 JSON 结构。

    1. public function testJsonForNotAssociativeArray()
    2. {
    3. $debug = $this->createDebug();
    4. $this->assertFalse($debug->isBootstrap());
    5. $debug->bootstrap();
    6. $this->assertTrue($debug->isBootstrap());
    7. $request = new Request();
    8. $response = new JsonResponse(['foo', 'bar']);
    9. $debug->handle($request, $response);
    10. $content = $response->getContent();
    11. $this->assertContains('"foo","bar",{":trace":{', $content);
    12. $this->assertContains('"php":{"version":', $content);
    13. $this->assertContains('Starts from this moment with QueryPHP.', $content);
    14. }

    关闭调试

    1. public function testDisable()
    2. {
    3. $debug = $this->createDebug();
    4. $this->assertFalse($debug->isBootstrap());
    5. $debug->bootstrap();
    6. $this->assertTrue($debug->isBootstrap());
    7. $request = new Request();
    8. $response = new JsonResponse(['foo' => 'bar']);
    9. $debug->handle($request, $response);
    10. $content = $response->getContent();
    11. $this->assertContains('{"foo":"bar",":trace":', $content);
    12. $this->assertContains('"php":{"version":', $content);
    13. $this->assertContains('Starts from this moment with QueryPHP.', $content);
    14. $debug->disable();
    15. $response2 = new JsonResponse(['foo' => 'bar']);
    16. $debug->handle($request, $response2);
    17. $content = $response2->getContent();
    18. $this->assertNotContains('{"foo":"bar",":trace":', $content);
    19. $this->assertNotContains('"php":{"version":', $content);
    20. $this->assertNotContains('Starts from this moment with QueryPHP.', $content);
    21. }

    启用调试

    1. public function testEnable()
    2. {
    3. $debug = $this->createDebug();
    4. $this->assertFalse($debug->isBootstrap());
    5. $debug->bootstrap();
    6. $this->assertTrue($debug->isBootstrap());
    7. $request = new Request();
    8. $response = new JsonResponse(['foo' => 'bar']);
    9. $debug->disable();
    10. $debug->handle($request, $response);
    11. $content = $response->getContent();
    12. $this->assertNotContains('{"foo":"bar",":trace":', $content);
    13. $this->assertNotContains('"php":{"version":', $content);
    14. $this->assertNotContains('Starts from this moment with QueryPHP.', $content);
    15. $this->assertTrue($debug->isBootstrap());
    16. $debug->enable();
    17. $this->assertTrue($debug->isBootstrap());
    18. $response2 = new JsonResponse(['foo' => 'bar']);
    19. $debug->handle($request, $response2);
    20. $content = $response2->getContent();
    21. $this->assertContains('{"foo":"bar",":trace":', $content);
    22. $this->assertContains('"php":{"version":', $content);
    23. $this->assertContains('Starts from this moment with QueryPHP.', $content);
    24. }

    启用调试但是未初始化

    1. public function testEnableWithoutBootstrap()
    2. {
    3. $debug = $this->createDebug();
    4. $request = new Request();
    5. $response = new JsonResponse(['foo' => 'bar']);
    6. $debug->disable();
    7. $debug->handle($request, $response);
    8. $content = $response->getContent();
    9. $this->assertNotContains('{"foo":"bar",":trace":', $content);
    10. $this->assertNotContains('"php":{"version":', $content);
    11. $this->assertNotContains('Starts from this moment with QueryPHP.', $content);
    12. $this->assertFalse($debug->isBootstrap());
    13. $debug->enable();
    14. $this->assertTrue($debug->isBootstrap());
    15. $response2 = new JsonResponse(['foo' => 'bar']);
    16. $debug->handle($request, $response2);
    17. $content = $response2->getContent();
    18. $this->assertContains('{"foo":"bar",":trace":', $content);
    19. $this->assertContains('"php":{"version":', $content);
    20. $this->assertContains('Starts from this moment with QueryPHP.', $content);
    21. }

    调试消息等级

    支持的消息类型

    1. public function getMessageLevelsData()
    2. {
    3. return [
    4. ['emergency'], ['alert'], ['critical'],
    5. ['error'], ['warning'], ['notice'],
    6. ['info'], ['debug'], ['log'],
    7. ];
    8. }

    系统支持多种消息类型,可以参考这个进行调试。

    1. public function testMessageLevelsData(string $level)
    2. {
    3. $debug = $this->createDebug();
    4. $this->assertFalse($debug->isBootstrap());
    5. $debug->bootstrap();
    6. $this->assertTrue($debug->isBootstrap());
    7. $request = new Request();
    8. $response = new JsonResponse(['foo' => 'bar']);
    9. $debug->{$level}('hello', 'world');
    10. $debug->handle($request, $response);
    11. $content = $response->getContent();
    12. $this->assertContains('{"foo":"bar",":trace":', $content);
    13. $this->assertContains('"php":{"version":', $content);
    14. $this->assertContains('Starts from this moment with QueryPHP.', $content);
    15. $this->assertContains('{"message":"hello","message_html":null,"is_string":true,"label":"'.$level.'",', $content);
    16. $this->assertContains('{"message":"world","message_html":null,"is_string":true,"label":"'.$level.'",', $content);
    17. }

    调试 Session

    1. public function testWithSession()
    2. {
    3. $debug = $this->createDebug();
    4. $this->assertFalse($debug->isBootstrap());
    5. $debug->bootstrap();
    6. $this->assertTrue($debug->isBootstrap());
    7. $request = new Request();
    8. $response = new JsonResponse(['foo' => 'bar']);
    9. $session = $debug->getApp()->make('session');
    10. $session->set('test_session', 'test_value');
    11. $debug->handle($request, $response);
    12. $content = $response->getContent();
    13. $this->assertContains('"session":{"test_session":"test_value"},', $content);
    14. }

    调试 Log

    1. public function testWithLog()
    2. {
    3. $debug = $this->createDebugWithLog();
    4. $app = $debug->getApp();
    5. $this->assertFalse($debug->isBootstrap());
    6. $debug->bootstrap();
    7. $this->assertTrue($debug->isBootstrap());
    8. $request = new Request();
    9. $response = new JsonResponse(['foo' => 'bar']);
    10. $log = $app->make('log');
    11. $log->info('test_log', ['exends' => 'bar']);
    12. $log->debug('test_log_debug');
    13. $debug->handle($request, $response);
    14. $content = $response->getContent();
    15. $this->assertContains('"logs":{"count":2,', $content);
    16. $this->assertContains('test_log info: {\"exends\":\"bar\"}', $content);
    17. $this->assertContains('test_log_debug debug: []', $content);
    18. }

    调试时间

    1. public function testTime()
    2. {
    3. $debug = $this->createDebug();
    4. $this->assertFalse($debug->isBootstrap());
    5. $debug->bootstrap();
    6. $this->assertTrue($debug->isBootstrap());
    7. $request = new Request();
    8. $response = new JsonResponse(['foo' => 'bar']);
    9. $debug->time('time_test');
    10. $debug->end('time_test');
    11. $debug->handle($request, $response);
    12. $content = $response->getContent();
    13. $this->assertContains('"time":{"start"', $content);
    14. $this->assertContains('"measures":[{"label":"time_test","start":', $content);
    15. }

    调试带有标签的时间

    1. public function testTimeWithLabel()
    2. {
    3. $debug = $this->createDebug();
    4. $this->assertFalse($debug->isBootstrap());
    5. $debug->bootstrap();
    6. $this->assertTrue($debug->isBootstrap());
    7. $request = new Request();
    8. $response = new JsonResponse(['foo' => 'bar']);
    9. $debug->time('time_test', 'time_label');
    10. $debug->end('time_test');
    11. $debug->handle($request, $response);
    12. $content = $response->getContent();
    13. $this->assertContains('"time":{"start"', $content);
    14. $this->assertContains('"measures":[{"label":"time_label","start":', $content);
    15. }