Repository navigation
Expand file tree
/
Copy pathServerTest.php
More file actions
127 lines (109 loc) · 4.49 KB
/
Copy pathServerTest.php
File metadata and controls
127 lines (109 loc) · 4.49 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
<?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\HttpServer;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ContainerInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Coordinator\Constants;
use Hyperf\Coordinator\CoordinatorManager;
use Hyperf\Dispatcher\HttpDispatcher;
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
use Hyperf\HttpMessage\Exception\BadRequestHttpException;
use Hyperf\HttpMessage\Exception\HttpException;
use Hyperf\HttpMessage\Server\Response as Psr7Response;
use Hyperf\HttpServer\ResponseEmitter;
use Hyperf\Support\SafeCaller;
use HyperfTest\HttpServer\Stub\ServerStub;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use RuntimeException;
use Swoole\Http\Request;
use Swoole\Http\Response;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class ServerTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
CoordinatorManager::clear(Constants::WORKER_START);
}
public function testThrowExceptionInCatchOnRequest()
{
CoordinatorManager::until(Constants::WORKER_START)->resume();
$container = $this->getContainer();
$dispatcher = Mockery::mock(ExceptionHandlerDispatcher::class);
$emitter = Mockery::mock(ResponseEmitter::class);
$server = Mockery::mock(ServerStub::class . '[initRequestAndResponse]', [
$container,
Mockery::mock(HttpDispatcher::class),
$dispatcher,
$emitter,
]);
$dispatcher->shouldReceive('dispatch')->andReturnUsing(function ($exception) {
throw new RuntimeException('Fatal Error');
});
$emitter->shouldReceive('emit')->once()->andReturnUsing(function ($response) {
$this->assertInstanceOf(Psr7Response::class, $response);
$this->assertSame(400, $response->getStatusCode());
});
$server->shouldReceive('initRequestAndResponse')->andReturnUsing(function () {
// Initialize PSR-7 Request and Response objects.
throw new BadRequestHttpException();
});
$server->onRequest($req = Mockery::mock(Request::class), $res = Mockery::mock(Response::class));
}
public function testOnRequest()
{
CoordinatorManager::until(Constants::WORKER_START)->resume();
$container = $this->getContainer();
$dispatcher = Mockery::mock(ExceptionHandlerDispatcher::class);
$emitter = Mockery::mock(ResponseEmitter::class);
$server = Mockery::mock(ServerStub::class . '[initRequestAndResponse]', [
$container,
Mockery::mock(HttpDispatcher::class),
$dispatcher,
$emitter,
]);
$dispatcher->shouldReceive('dispatch')->andReturnUsing(function ($exception) {
if ($exception instanceof HttpException) {
return (new Psr7Response())->withStatus($exception->getStatusCode());
}
return null;
});
$emitter->shouldReceive('emit')->once()->andReturnUsing(function ($response) {
$this->assertInstanceOf(Psr7Response::class, $response);
$this->assertSame(400, $response->getStatusCode());
});
$server->shouldReceive('initRequestAndResponse')->andReturnUsing(function () {
// Initialize PSR-7 Request and Response objects.
throw new BadRequestHttpException();
});
$server->onRequest($req = Mockery::mock(Request::class), $res = Mockery::mock(Response::class));
}
protected function getContainer()
{
$container = Mockery::mock(ContainerInterface::class);
$container->shouldReceive('has')->with(StdoutLoggerInterface::class)->andReturnFalse();
$container->shouldReceive('get')->with(SafeCaller::class)->andReturn(new SafeCaller($container));
$dispatcher = Mockery::mock(EventDispatcherInterface::class);
$dispatcher->shouldReceive('dispatch')->andReturn(true);
$container->shouldReceive('has')->with(EventDispatcherInterface::class)->andReturn(true);
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn($dispatcher);
ApplicationContext::setContainer($container);
return $container;
}
}