File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1414
1515use Hyperf \Coordinator \Constants ;
1616use Hyperf \Coordinator \CoordinatorManager ;
17+ use Hyperf \Testing \Attributes \NonCoroutine ;
18+ use ReflectionClass ;
1719use Swoole \Coroutine ;
1820use Swoole \Timer ;
1921use Throwable ;
2022
2123trait 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments