Skip to content

Commit 80ad3ba

Browse files
authored
Adds RunTestsInCoroutine trait (#5867)
1 parent 5813731 commit 80ad3ba

File tree

4 files changed

+61
-108
lines changed

4 files changed

+61
-108
lines changed

composer.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,9 @@
3232
"hyperf/utils": "~3.1.0",
3333
"symfony/http-foundation": "^5.4|^6.0"
3434
},
35-
"suggest": {
36-
"pestphp/pest": "For testing with Pest.(^2.8.0)"
37-
},
3835
"extra": {
3936
"branch-alias": {
4037
"dev-master": "3.0-dev"
41-
},
42-
"pest": {
43-
"plugins": [
44-
"Hyperf\\Testing\\Plugin\\Pest"
45-
]
4638
}
4739
},
4840
"bin": [
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Concerns;
13+
14+
use Throwable;
15+
16+
/**
17+
* @method string getName()
18+
*/
19+
trait RunTestsInCoroutine
20+
{
21+
protected bool $enableCoroutine = true;
22+
23+
protected string $realTestName = '';
24+
25+
final protected function runTestsInCoroutine(...$arguments)
26+
{
27+
parent::setName($this->realTestName);
28+
29+
$testResult = null;
30+
$exception = null;
31+
32+
/* @phpstan-ignore-next-line */
33+
\Swoole\Coroutine\run(function () use (&$testResult, &$exception, $arguments) {
34+
try {
35+
$testResult = $this->{$this->realTestName}(...$arguments);
36+
} catch (Throwable $e) {
37+
$exception = $e;
38+
} finally {
39+
\Swoole\Timer::clearAll();
40+
\Hyperf\Coordinator\CoordinatorManager::until(\Hyperf\Coordinator\Constants::WORKER_EXIT)->resume();
41+
}
42+
});
43+
44+
if ($exception) {
45+
throw $exception;
46+
}
47+
48+
return $testResult;
49+
}
50+
51+
final protected function runTest(): mixed
52+
{
53+
if (extension_loaded('swoole') && \Swoole\Coroutine::getCid() === -1 && $this->enableCoroutine) {
54+
$this->realTestName = $this->getName();
55+
parent::setName('runTestsInCoroutine');
56+
}
57+
58+
return parent::runTest();
59+
}
60+
}

src/Plugin/Pest.php

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

src/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
2222
{
2323
use Concerns\InteractsWithContainer;
2424
use Concerns\MakesHttpRequests;
25+
use Concerns\RunTestsInCoroutine;
2526

2627
protected function setUp(): void
2728
{

0 commit comments

Comments
 (0)