|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://hyperf.wiki |
| 9 | + |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | +namespace Hyperf\Testing\Plugin; |
| 13 | + |
| 14 | +use Hyperf\Coordinator\Constants; |
| 15 | +use Hyperf\Coordinator\CoordinatorManager; |
| 16 | +use Pest\Contracts\Plugins\HandlesArguments; |
| 17 | +use Pest\Exceptions\InvalidOption; |
| 18 | +use Pest\Kernel; |
| 19 | +use Pest\Plugins\Concerns\HandleArguments; |
| 20 | +use Pest\Support\Container; |
| 21 | +use PHPUnit\TextUI\Application; |
| 22 | +use Swoole\Coroutine; |
| 23 | +use Swoole\Timer; |
| 24 | +use Symfony\Component\Console\Output\OutputInterface; |
| 25 | + |
| 26 | +/** |
| 27 | + * @property string $vendorDir |
| 28 | + */ |
| 29 | +class Pest implements HandlesArguments |
| 30 | +{ |
| 31 | + use HandleArguments; |
| 32 | + |
| 33 | + public function handleArguments(array $arguments): array |
| 34 | + { |
| 35 | + $arguments = $this->prepend($arguments); |
| 36 | + |
| 37 | + if (Coroutine::getCid() > 0) { |
| 38 | + return $arguments; |
| 39 | + } |
| 40 | + |
| 41 | + if (! $this->hasArgument('--coroutine', $arguments)) { |
| 42 | + return $arguments; |
| 43 | + } |
| 44 | + |
| 45 | + if ($this->hasArgument('--parallel', $arguments) || $this->hasArgument('-p', $arguments)) { |
| 46 | + throw new InvalidOption('The coroutine mode is not supported when running in parallel.'); |
| 47 | + } |
| 48 | + |
| 49 | + $arguments = $this->popArgument('--coroutine', $arguments); |
| 50 | + |
| 51 | + exit($this->runInCoroutine($arguments)); |
| 52 | + } |
| 53 | + |
| 54 | + private function runInCoroutine(array $arguments): int |
| 55 | + { |
| 56 | + $code = 0; |
| 57 | + $output = Container::getInstance()->get(OutputInterface::class); |
| 58 | + $kernel = new Kernel( |
| 59 | + new Application(), |
| 60 | + $output, |
| 61 | + ); |
| 62 | + |
| 63 | + Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL, 'exit_condition' => function () { |
| 64 | + return Coroutine::stats()['coroutine_num'] === 0; |
| 65 | + }]); |
| 66 | + |
| 67 | + /* @phpstan-ignore-next-line */ |
| 68 | + \Swoole\Coroutine\run(function () use (&$code, $kernel, $arguments) { |
| 69 | + $code = $kernel->handle($arguments); |
| 70 | + Timer::clearAll(); |
| 71 | + CoordinatorManager::until(Constants::WORKER_EXIT)->resume(); |
| 72 | + }); |
| 73 | + |
| 74 | + $kernel->shutdown(); |
| 75 | + |
| 76 | + return $code; |
| 77 | + } |
| 78 | + |
| 79 | + private function prepend(array $arguments): array |
| 80 | + { |
| 81 | + $prepend = null; |
| 82 | + foreach ($arguments as $key => $argument) { |
| 83 | + if (str_starts_with($argument, '--prepend=')) { |
| 84 | + $prepend = explode('=', $argument, 2)[1]; |
| 85 | + unset($arguments[$key]); |
| 86 | + break; |
| 87 | + } |
| 88 | + if (str_starts_with($argument, '--prepend')) { |
| 89 | + if (isset($arguments[$key + 1])) { |
| 90 | + $prepend = $arguments[$key + 1]; |
| 91 | + unset($arguments[$key + 1]); |
| 92 | + } |
| 93 | + unset($arguments[$key]); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if ($prepend && file_exists($prepend)) { |
| 98 | + require_once $prepend; |
| 99 | + } |
| 100 | + |
| 101 | + return $arguments; |
| 102 | + } |
| 103 | +} |
0 commit comments