Repository navigation
Expand file tree
/
Copy pathAsyncQueueAspectTest.php
More file actions
158 lines (132 loc) · 5.26 KB
/
Copy pathAsyncQueueAspectTest.php
File metadata and controls
158 lines (132 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\AsyncQueue;
use Hyperf\AsyncQueue\Annotation\AsyncQueueMessage;
use Hyperf\AsyncQueue\AnnotationJob;
use Hyperf\AsyncQueue\Aspect\AsyncQueueAspect;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\AsyncQueue\Driver\DriverInterface;
use Hyperf\AsyncQueue\Environment;
use Hyperf\Context\ApplicationContext;
use Hyperf\Context\Context;
use Hyperf\Coroutine\Waiter;
use Hyperf\Di\Annotation\AnnotationCollector;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\Ast;
use Hyperf\Di\ReflectionManager;
use HyperfTest\AsyncQueue\Stub\FooProxy;
use HyperfTest\AsyncQueue\Stub\FooService;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use function Hyperf\Coroutine\wait;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class AsyncQueueAspectTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
Context::set(FooProxy::class, null);
ReflectionManager::clear();
}
#[Group('NonCoroutine')]
public function testNotAsyncMessage()
{
$container = $this->getContainer();
$proxy = $container->get(FooProxy::class);
$proxy->dump($id = rand(10000, 99999), $uuid = uniqid(), $data = [
'id' => rand(0, 9999),
]);
$this->assertSame([$id, $uuid, $data], Context::get(FooProxy::class));
}
#[Group('NonCoroutine')]
public function testAsyncMessage()
{
$container = $this->getContainer();
$proxy = $container->get(FooProxy::class);
$proxy->async($data = [
'id' => rand(0, 9999),
]);
$this->assertSame($data, Context::get(FooProxy::class));
}
#[Group('NonCoroutine')]
public function testAsyncMessageVariadic()
{
$container = $this->getContainer();
$proxy = $container->get(FooProxy::class);
$proxy->variadic($id = rand(10000, 99999), $uuid = uniqid(), $data = [
'id' => rand(0, 9999),
]);
$this->assertSame([$id, $uuid, $data], Context::get(FooProxy::class));
}
public function testAnnotationJob()
{
$container = Mockery::mock(ContainerInterface::class);
ApplicationContext::setContainer($container);
$container->shouldReceive('get')->with(Waiter::class)->andReturn(new Waiter());
wait(function () use ($container) {
$container->shouldReceive('get')->with(FooService::class)->andReturn(new FooService());
$container->shouldReceive('get')->with(Environment::class)->andReturn(new Environment());
$job = new AnnotationJob(FooService::class, 'test', []);
$job->handle();
$this->assertSame(1, Context::get(FooService::class . '::test'));
$job = new AnnotationJob(FooService::class, 'foo', []);
$job->handle();
$this->assertSame(1, Context::get(FooService::class . '::foo'));
});
}
protected function getContainer()
{
$container = Mockery::mock(ContainerInterface::class);
ApplicationContext::setContainer($container);
$aspect = new Aspect();
$aspect->collectClass(AsyncQueueAspect::class);
AnnotationCollector::collectMethod(FooProxy::class, 'async', AsyncQueueMessage::class, new AsyncQueueMessage());
AnnotationCollector::collectMethod(FooProxy::class, 'variadic', AsyncQueueMessage::class, new AsyncQueueMessage());
$pid = pcntl_fork();
if (! $pid) {
$ast = new Ast();
$code = $ast->proxy(FooProxy::class);
if (! is_dir($dir = BASE_PATH . '/runtime/container/proxy/')) {
mkdir($dir, 0777, true);
}
file_put_contents($file = $dir . 'FooProxy.proxy.php', $code);
exit;
}
pcntl_wait($status);
require_once BASE_PATH . '/runtime/container/proxy/FooProxy.proxy.php';
$container->shouldReceive('get')->with(FooProxy::class)->andReturn(new FooProxy());
$container->shouldReceive('get')->with(AsyncQueueAspect::class)->andReturnUsing(function ($_) use ($container) {
return new AsyncQueueAspect($container);
});
$container->shouldReceive('get')->with(Environment::class)->andReturn($environment = new Environment());
$container->shouldReceive('get')->with(DriverFactory::class)->andReturnUsing(function ($_) use ($environment) {
$factory = Mockery::mock(DriverFactory::class);
$driver = Mockery::mock(DriverInterface::class);
$driver->shouldReceive('push')->andReturnUsing(function ($job) use ($environment) {
$this->assertInstanceOf(AnnotationJob::class, $job);
$environment->setAsyncQueue(true);
$origin = new FooProxy();
$origin->{$job->method}(...$job->params);
return true;
});
$factory->shouldReceive('get')->andReturn($driver);
return $factory;
});
return $container;
}
}