Skip to content

Commit 5cb9011

Browse files
authored
Remove co-pest, use pest-plugin-hyperf instead. (#5834)
1 parent 1536bbd commit 5cb9011

File tree

3 files changed

+110
-113
lines changed

3 files changed

+110
-113
lines changed

bin/co-pest

Lines changed: 0 additions & 110 deletions
This file was deleted.

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@
3333
"symfony/http-foundation": "^5.4|^6.0"
3434
},
3535
"suggest": {
36-
"pestphp/pest": "For testing with Pest"
36+
"pestphp/pest": "For testing with Pest.(^2.0)"
3737
},
3838
"extra": {
3939
"branch-alias": {
4040
"dev-master": "3.0-dev"
41+
},
42+
"pest": {
43+
"plugins": [
44+
"Hyperf\\Testing\\Plugin\\Pest"
45+
]
4146
}
4247
},
4348
"bin": [
44-
"co-phpunit",
45-
"bin/co-pest"
49+
"co-phpunit"
4650
]
4751
}

src/Plugin/Pest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
* @contact [email protected]
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

Comments
 (0)