Repository navigation
Expand file tree
/
Copy pathAsyncQueueAspectTest.php
More file actions
115 lines (99 loc) · 3.79 KB
/
Copy pathAsyncQueueAspectTest.php
File metadata and controls
115 lines (99 loc) · 3.79 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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @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\Di\Annotation\AnnotationCollector;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\Ast;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Context;
use HyperfTest\AsyncQueue\Stub\FooProxy;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
/**
* @internal
* @coversNothing
*/
class AsyncQueueAspectTest extends TestCase
{
protected function tearDown()
{
Mockery::close();
Context::set(FooProxy::class, null);
}
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));
}
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));
}
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));
}
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());
$ast = new Ast();
$code = $ast->proxy(FooProxy::class, $proxy = FooProxy::class . 'Proxy');
if (! is_dir($dir = BASE_PATH . '/runtime/container/proxy/')) {
mkdir($dir, 0777, true);
}
file_put_contents($file = $dir . 'FooProxy.proxy.php', $code);
require_once $file;
$container->shouldReceive('get')->with(FooProxy::class)->andReturn(new $proxy());
$container->shouldReceive('get')->with(AsyncQueueAspect::class)->andReturnUsing(function ($_) use ($container) {
return new AsyncQueueAspect($container);
});
$container->shouldReceive('get')->with(Environment::class)->andReturn(new Environment());
$container->shouldReceive('get')->with(DriverFactory::class)->andReturnUsing(function ($_) {
$factory = Mockery::mock(DriverFactory::class);
$driver = Mockery::mock(DriverInterface::class);
$driver->shouldReceive('push')->andReturnUsing(function ($job) {
$this->assertInstanceOf(AnnotationJob::class, $job);
$origin = new FooProxy();
$origin->{$job->method}(...$job->params);
return true;
});
$factory->shouldReceive('get')->andReturn($driver);
return $factory;
});
return $container;
}
}