Repository navigation
Expand file tree
/
Copy pathContextTest.php
More file actions
225 lines (193 loc) · 6.85 KB
/
Copy pathContextTest.php
File metadata and controls
225 lines (193 loc) · 6.85 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?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\Context;
use Hyperf\Context\Context;
use Hyperf\Context\RequestContext;
use Hyperf\Context\ResponseContext;
use Hyperf\Coroutine\Coroutine;
use Hyperf\Coroutine\Waiter;
use Hyperf\Engine\Channel;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use stdClass;
use Swow\Psr7\Message\ResponsePlusInterface;
use Swow\Psr7\Message\ServerRequestPlusInterface;
use function Hyperf\Coroutine\go;
use function Hyperf\Coroutine\parallel;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class ContextTest extends TestCase
{
public function testOverride()
{
Context::set('override.id', 1);
$this->assertSame(2, Context::override('override.id', function ($id) {
return $id + 1;
}));
$this->assertSame(2, Context::get('override.id'));
}
public function testGetOrSet()
{
Context::set('test.store.id', null);
$this->assertSame(1, Context::getOrSet('test.store.id', function () {
return 1;
}));
$this->assertSame(1, Context::getOrSet('test.store.id', function () {
return 2;
}));
Context::set('test.store.id', null);
$this->assertSame(1, Context::getOrSet('test.store.id', 1));
}
public function testCopy()
{
Context::set('test.store.id', $uid = uniqid());
$id = Coroutine::id();
parallel([
function () use ($id, $uid) {
Context::copy($id, ['test.store.id']);
$this->assertSame($uid, Context::get('test.store.id'));
},
]);
}
public function testCopyAfterSet()
{
Context::set('test.store.id', $uid = uniqid());
$id = Coroutine::id();
parallel([
function () use ($id, $uid) {
Context::set('test.store.name', 'Hyperf');
Context::copy($id, ['test.store.id']);
$this->assertSame($uid, Context::get('test.store.id'));
// TODO: Context::copy will delete origin values.
$this->assertNull(Context::get('test.store.name'));
},
]);
}
public function testContextChangeAfterCopy()
{
$obj = new stdClass();
$obj->id = $uid = uniqid();
Context::set('test.store.id', $obj);
Context::set('test.store.useless.id', 1);
$id = Coroutine::id();
$tid = uniqid();
parallel([
function () use ($id, $uid, $tid) {
Context::copy($id, ['test.store.id']);
$obj = Context::get('test.store.id');
$this->assertSame($uid, $obj->id);
$obj->id = $tid;
$this->assertFalse(Context::has('test.store.useless.id'));
},
]);
$this->assertSame($tid, Context::get('test.store.id')->id);
}
public function testContextFromNull()
{
$res = Context::get('id', $default = 'Hello World!', -1);
$this->assertSame($default, $res);
$res = Context::get('id', null, -1);
$this->assertSame(null, $res);
$this->assertFalse(Context::has('id', -1));
Context::copy(-1);
parallel([
function () {
Context::set('id', $id = uniqid());
Context::copy(-1, ['id']);
$this->assertSame($id, Context::get('id'));
},
]);
}
public function testContextDestroy()
{
Context::set($id = uniqid(), $value = uniqid());
$this->assertSame($value, Context::get($id));
Context::destroy($id);
$this->assertNull(Context::get($id));
}
public function testRequestContext()
{
$request = Mockery::mock(ServerRequestPlusInterface::class);
RequestContext::set($request);
$this->assertSame($request, RequestContext::get());
Context::set(ServerRequestInterface::class, $req = Mockery::mock(ServerRequestPlusInterface::class));
$this->assertNotSame($request, RequestContext::get());
$this->assertSame($req, RequestContext::get());
$this->assertSame($req, Context::get(ServerRequestInterface::class));
}
public function testResponseContext()
{
$response = Mockery::mock(ResponsePlusInterface::class);
ResponseContext::set($response);
$this->assertSame($response, ResponseContext::get());
Context::set(ResponseInterface::class, $req = Mockery::mock(ResponsePlusInterface::class));
$this->assertNotSame($response, ResponseContext::get());
$this->assertSame($req, ResponseContext::get());
$this->assertSame($req, Context::get(ResponseInterface::class));
}
public function testResponseContextWithCoroutineId()
{
$response = Mockery::mock(ResponsePlusInterface::class);
$chan = new Channel(1);
$close = new Channel(1);
go(function () use ($chan, $response, $close) {
ResponseContext::set($response);
$this->assertSame($response, ResponseContext::get());
$chan->push(Coroutine::id());
$close->pop(1);
});
$id = $chan->pop(5);
$this->assertSame($response, ResponseContext::get($id));
$close->push(true);
}
public function testRequestContextWithCoroutineId()
{
$request = Mockery::mock(ServerRequestPlusInterface::class);
RequestContext::set($request);
$id = Coroutine::id();
(new Waiter())->wait(function () use ($id, $request) {
$this->assertSame($request, RequestContext::get($id));
});
}
public function testContextOverrideWithCoroutineId()
{
$id = Coroutine::id();
$value = uniqid();
Context::override('override.id.coroutine_id', fn () => $value);
(new Waiter())->wait(function () use ($id, $value) {
Context::override(
'override.id.coroutine_id',
function ($v) use ($value) {
$this->assertSame($v, $value);
return '123';
},
$id
);
});
$this->assertSame('123', Context::get('override.id.coroutine_id'));
}
public function testContextGetOrSetWithCoroutineId()
{
$id = Coroutine::id();
$value = uniqid();
Context::getOrSet('get_or_set.id.coroutine_id', fn () => $value);
(new Waiter())->wait(function () use ($id, $value) {
$res = Context::getOrSet('get_or_set.id.coroutine_id', fn () => '123', $id);
$this->assertSame($res, $value);
});
}
}