Repository navigation
Expand file tree
/
Copy pathClientTest.php
More file actions
200 lines (165 loc) · 6.96 KB
/
Copy pathClientTest.php
File metadata and controls
200 lines (165 loc) · 6.96 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
<?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\Testing;
use Hyperf\Codec\Json;
use Hyperf\Config\Config;
use Hyperf\Context\ApplicationContext;
use Hyperf\Context\Context;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\NormalizerInterface;
use Hyperf\Coroutine\Coroutine;
use Hyperf\Coroutine\Waiter;
use Hyperf\Di\ClosureDefinitionCollectorInterface;
use Hyperf\Di\Container;
use Hyperf\Di\MethodDefinitionCollector;
use Hyperf\Di\MethodDefinitionCollectorInterface;
use Hyperf\Dispatcher\HttpDispatcher;
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
use Hyperf\HttpServer\CoreMiddleware;
use Hyperf\HttpServer\ResponseEmitter;
use Hyperf\HttpServer\Router\DispatcherFactory;
use Hyperf\HttpServer\Router\Router;
use Hyperf\Serializer\SimpleNormalizer;
use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Hyperf\Server\ServerFactory;
use Hyperf\Support\Filesystem\Filesystem;
use Hyperf\Testing\Client;
use HyperfTest\Testing\Stub\Exception\Handler\FooExceptionHandler;
use HyperfTest\Testing\Stub\FooController;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class ClientTest extends TestCase
{
public function testClientRequest()
{
$container = $this->getContainer();
$client = new Client($container);
$data = $client->get('/');
$this->assertSame(0, $data['code']);
$this->assertSame('Hello Hyperf!', $data['data']);
}
public function testSendCookies()
{
$container = $this->getContainer();
$client = new Client($container);
$response = $client->sendRequest($client->initRequest('POST', '/request')->withCookieParams([
'X-CODE' => $id = uniqid(),
]));
$data = Json::decode((string) $response->getBody());
$this->assertSame($id, $data['cookies']['X-CODE']);
}
public function testClientReturnCoroutineId()
{
$container = $this->getContainer();
$client = new Client($container);
$id = Coroutine::id();
$data = $client->get('/id');
$this->assertSame(0, $data['code']);
$this->assertNotEquals($id, $data['data']);
}
public function testClientException()
{
$container = $this->getContainer();
$client = new Client($container);
$data = $client->get('/exception');
$this->assertSame(500, $data['code']);
$this->assertSame('Server Error', $data['message']);
}
public function testClientGetUri()
{
$container = $this->getContainer();
$client = new Client($container);
$data = $client->get('/request', [
'id' => $id = uniqid(),
]);
$this->assertSame($data['uri'], [
'scheme' => 'http',
'host' => '127.0.0.1',
'port' => 9501,
'path' => '/request',
'query' => 'id=' . $id,
]);
$this->assertSame($id, $data['params']['id']);
}
public function testClientCallable()
{
$container = $this->getContainer();
$client = new Client($container);
$id = uniqid();
$res = $client->request('GET', '/context', callable: fn () => Context::set('request_id', $id));
$this->assertSame(['request_id' => $id], Json::decode((string) $res->getBody()));
}
public function getContainer()
{
$container = Mockery::mock(Container::class);
$container->shouldReceive('get')->with(HttpDispatcher::class)->andReturn(new HttpDispatcher($container));
$container->shouldReceive('get')->with(ExceptionHandlerDispatcher::class)->andReturn(new ExceptionHandlerDispatcher($container));
$container->shouldReceive('get')->with(ResponseEmitter::class)->andReturn(new ResponseEmitter(null));
$container->shouldReceive('get')->with(DispatcherFactory::class)->andReturn($factory = new DispatcherFactory());
$container->shouldReceive('get')->with(NormalizerInterface::class)->andReturn(new SimpleNormalizer());
$container->shouldReceive('get')->with(MethodDefinitionCollectorInterface::class)->andReturn(new MethodDefinitionCollector());
$container->shouldReceive('has')->with(ClosureDefinitionCollectorInterface::class)->andReturn(false);
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([
'exceptions' => [
'handler' => [
'http' => [
FooExceptionHandler::class,
],
],
],
'server' => [
'servers' => [
[
'name' => 'http',
'type' => Server::SERVER_HTTP,
'host' => '0.0.0.0',
'port' => 9501,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
Event::ON_REQUEST => [Server::class, 'onRequest'],
],
],
],
],
]));
$container->shouldReceive('get')->with(Filesystem::class)->andReturn(new Filesystem());
$container->shouldReceive('get')->with(FooController::class)->andReturn(new FooController());
$container->shouldReceive('has')->andReturn(true);
$container->shouldReceive('get')->with(FooExceptionHandler::class)->andReturn(new FooExceptionHandler());
$container->shouldReceive('make')->with(CoreMiddleware::class, Mockery::any())->andReturnUsing(function ($class, $args) {
return new CoreMiddleware(...array_values($args));
});
$container->shouldReceive('get')->with(Waiter::class)->andReturn(new Waiter());
$dispatcher = Mockery::mock(EventDispatcherInterface::class);
$dispatcher->shouldReceive('dispatch')->shouldReceive('dispatch')->andReturn(true);
$container->shouldReceive('has')->with(EventDispatcherInterface::class)->andReturn(true);
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn($dispatcher);
$container->shouldReceive('get')->with(ServerFactory::class)->andReturn(
Mockery::mock(ServerFactory::class)->shouldReceive('getConfig')->andReturn(null)->getMock()
);
ApplicationContext::setContainer($container);
Router::init($factory);
Router::get('/', [FooController::class, 'index']);
Router::get('/exception', [FooController::class, 'exception']);
Router::get('/id', [FooController::class, 'id']);
Router::get('/context', [FooController::class, 'context']);
Router::addRoute(['GET', 'POST'], '/request', [FooController::class, 'request']);
return $container;
}
}