初始化注册异常运行时

Testing Is Documentation

tests/Kernel/Bootstrap/RegisterExceptionRuntimeTest.php初始化注册异常运行时 - 图1

QueryPHP 在内核执行过程中会执行初始化,分为 4 个步骤,载入配置、载入语言包、注册异常运行时和遍历服务提供者注册服务。

内核初始化,包括 \Leevel\Kernel\IKernel::bootstrap\Leevel\Kernel\IKernelConsole::bootstrap 均会执行上述 4 个步骤。

Uses

  1. <?php
  2. use Error;
  3. use ErrorException;
  4. use Exception;
  5. use Leevel\Di\Container;
  6. use Leevel\Di\IContainer;
  7. use Leevel\Http\Request;
  8. use Leevel\Http\Response;
  9. use Leevel\Kernel\App as Apps;
  10. use Leevel\Kernel\Bootstrap\RegisterExceptionRuntime;
  11. use Leevel\Kernel\IApp;
  12. use Leevel\Kernel\IExceptionRuntime;
  13. use Symfony\Component\Console\Output\ConsoleOutput;

set_error_handler 设置错误处理函数

  1. public function testSetErrorHandle(): void
  2. {
  3. $this->expectException(\ErrorException::class);
  4. $this->expectExceptionMessage(
  5. 'foo.'
  6. );
  7. $bootstrap = new RegisterExceptionRuntime();
  8. $container = Container::singletons();
  9. $app = new App4($container, $appPath = __DIR__.'/app');
  10. $this->assertInstanceof(IContainer::class, $container);
  11. $this->assertInstanceof(Container::class, $container);
  12. $this->assertInstanceof(IApp::class, $app);
  13. $this->assertInstanceof(Apps::class, $app);
  14. $this->invokeTestMethod($bootstrap, 'setErrorHandle', [400, 'foo.']);
  15. }

set_exception_handler 设置异常处理函数

  1. public function testSetExceptionHandler(): void
  2. {
  3. $bootstrap = new RegisterExceptionRuntime();
  4. $container = Container::singletons();
  5. $app = new App4($container, $appPath = __DIR__.'/app');
  6. $request = $this->createMock(Request::class);
  7. $request->method('isConsole')->willReturn(true);
  8. $this->assertTrue($request->isConsole());
  9. $container->singleton('request', function () use ($request) {
  10. return $request;
  11. });
  12. $runtime = $this->createMock(IExceptionRuntime::class);
  13. $this->assertNull($runtime->renderForConsole(new ConsoleOutput(), new Exception()));
  14. $container->singleton(IExceptionRuntime::class, function () use ($runtime) {
  15. return $runtime;
  16. });
  17. $bootstrap->handle($app, true);
  18. $this->assertInstanceof(IContainer::class, $container);
  19. $this->assertInstanceof(Container::class, $container);
  20. $this->assertInstanceof(IApp::class, $app);
  21. $this->assertInstanceof(Apps::class, $app);
  22. $e = new Exception('foo.');
  23. $this->assertNull($this->invokeTestMethod($bootstrap, 'setExceptionHandler', [$e]));
  24. $error = new Error('hello world.');
  25. $this->assertNull($this->invokeTestMethod($bootstrap, 'setExceptionHandler', [$error]));
  26. }