Skip to content

Commit b95d54a

Browse files
authored
refactor(testing): add NonCoroutine attribute for flexible test execution control (#7605)
1 parent 008a887 commit b95d54a

4 files changed

Lines changed: 117 additions & 3 deletions

File tree

src/Attributes/NonCoroutine.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace Hyperf\Testing\Attributes;
14+
15+
use Attribute;
16+
17+
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
18+
class NonCoroutine
19+
{
20+
}

src/Concerns/RunTestsInCoroutine.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414

1515
use Hyperf\Coordinator\Constants;
1616
use Hyperf\Coordinator\CoordinatorManager;
17+
use Hyperf\Testing\Attributes\NonCoroutine;
18+
use ReflectionClass;
1719
use Swoole\Coroutine;
1820
use Swoole\Timer;
1921
use Throwable;
2022

2123
trait RunTestsInCoroutine
2224
{
23-
protected bool $enableCoroutine = true;
24-
2525
public function runBare(): void
2626
{
27-
if ($this->enableCoroutine && extension_loaded('swoole') && Coroutine::getCid() === -1) {
27+
if ($this->isCoroutineEnabled()) {
2828
$exception = null;
2929

3030
/* @phpstan-ignore-next-line */
@@ -48,4 +48,23 @@ public function runBare(): void
4848

4949
parent::runBare();
5050
}
51+
52+
private function isCoroutineEnabled(): bool
53+
{
54+
if (! extension_loaded('swoole') || Coroutine::getCid() !== -1) {
55+
return false;
56+
}
57+
58+
$refClass = new ReflectionClass(static::class);
59+
foreach ($refClass->getAttributes(NonCoroutine::class) as $attribute) {
60+
return false;
61+
}
62+
63+
$refMethod = $refClass->getMethod($this->name());
64+
foreach ($refMethod->getAttributes(NonCoroutine::class) as $attribute) {
65+
return false;
66+
}
67+
68+
return true;
69+
}
5170
}

tests/AttributeOnClassTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace HyperfTest\Testing;
14+
15+
use Hyperf\Coroutine\Coroutine;
16+
use Hyperf\Testing\Attributes\NonCoroutine;
17+
use Hyperf\Testing\Concerns\RunTestsInCoroutine;
18+
use PHPUnit\Framework\Attributes\Group;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* @internal
23+
* @coversNothing
24+
*/
25+
#[Group('NonCoroutine')]
26+
#[NonCoroutine]
27+
class AttributeOnClassTest extends TestCase
28+
{
29+
use RunTestsInCoroutine;
30+
31+
public function testGetCoroutineStatus()
32+
{
33+
$this->assertFalse(Coroutine::inCoroutine());
34+
}
35+
}

tests/AttributeOnMethodTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace HyperfTest\Testing;
14+
15+
use Hyperf\Coroutine\Coroutine;
16+
use Hyperf\Testing\Attributes\NonCoroutine;
17+
use Hyperf\Testing\Concerns\RunTestsInCoroutine;
18+
use PHPUnit\Framework\Attributes\Group;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* @internal
23+
* @coversNothing
24+
*/
25+
#[Group('NonCoroutine')]
26+
class AttributeOnMethodTest extends TestCase
27+
{
28+
use RunTestsInCoroutine;
29+
30+
#[NonCoroutine]
31+
public function testWithNonCoroutineAttribute()
32+
{
33+
$this->assertFalse(Coroutine::inCoroutine());
34+
}
35+
36+
public function testWithoutNonCoroutineAttribute()
37+
{
38+
$this->assertTrue(Coroutine::inCoroutine());
39+
}
40+
}

0 commit comments

Comments
 (0)