您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

69 行
1.4KB

  1. <?php
  2. namespace Framework\Testing;
  3. use PHPUnit\Runner\BeforeFirstTestHook;
  4. use PHPUnit\Runner\AfterLastTestHook;
  5. use Symfony\Component\Process\Process;
  6. final class ServerExtension implements BeforeFirstTestHook, AfterLastTestHook
  7. {
  8. private Process $process;
  9. private bool $startedServer = false;
  10. private function startServer()
  11. {
  12. if ($this->serverIsRunning()) {
  13. $this->startedServer = false;
  14. return;
  15. }
  16. $this->startedServer = true;
  17. $base = app('paths.base');
  18. $separator = DIRECTORY_SEPARATOR;
  19. $this->process = new Process([
  20. PHP_BINARY,
  21. "{$base}{$separator}command.php",
  22. "serve"
  23. ], $base);
  24. $this->process->start(function($type, $buffer) {
  25. print $buffer;
  26. });
  27. }
  28. private function serverIsRunning()
  29. {
  30. $connection = @fsockopen(
  31. env('APP_HOST', '127.0.0.1'),
  32. env('APP_PORT', '8000'),
  33. );
  34. if (is_resource($connection)) {
  35. fclose($connection);
  36. return true;
  37. }
  38. return false;
  39. }
  40. private function stopServer()
  41. {
  42. if ($this->startedServer) {
  43. $this->process->signal(SIGTERM);
  44. }
  45. }
  46. public function executeBeforeFirstTest(): void
  47. {
  48. $this->startServer();
  49. }
  50. public function executeAfterLastTest(): void
  51. {
  52. $this->stopServer();
  53. }
  54. }

Powered by TurnKey Linux.