forked from php-runtime/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymfonyHttpBridgeTest.php
More file actions
174 lines (145 loc) · 6.37 KB
/
SymfonyHttpBridgeTest.php
File metadata and controls
174 lines (145 loc) · 6.37 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
<?php
namespace Runtime\Swoole\Tests\Unit;
use PHPUnit\Framework\TestCase;
use Runtime\Swoole\SymfonyHttpBridge;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* @author Piotr Kugla <piku235@gmail.com>
*/
class SymfonyHttpBridgeTest extends TestCase
{
public function testThatSwooleRequestIsConverted(): void
{
$request = $this->createMock(Request::class);
$request->server = ['request_method' => 'post'];
$request->header = ['content-type' => 'application/json'];
$request->cookie = ['foo' => 'cookie'];
$request->get = ['foo' => 'get'];
$request->post = ['foo' => 'post'];
$request->files = [
'foo' => [
'name' => 'file',
'type' => 'image/png',
'tmp_name' => '/tmp/file',
'error' => UPLOAD_ERR_CANT_WRITE,
'size' => 0,
],
];
$request->expects(self::once())->method('rawContent')->willReturn('{"foo": "body"}');
$sfRequest = SymfonyHttpBridge::convertSwooleRequest($request);
$this->assertSame(['REQUEST_METHOD' => 'post'], $sfRequest->server->all());
$this->assertSame(['content-type' => ['application/json']], $sfRequest->headers->all());
$this->assertSame(['foo' => 'cookie'], $sfRequest->cookies->all());
$this->assertSame(['foo' => 'get'], $sfRequest->query->all());
$this->assertSame(['foo' => 'post'], $sfRequest->request->all());
$this->assertEquals('{"foo": "body"}', $sfRequest->getContent());
$this->assertCount(1, $sfRequest->files);
/** @var UploadedFile $file */
$file = $sfRequest->files->get('foo');
$this->assertNotNull($file);
$this->assertEquals('file', $file->getClientOriginalName());
$this->assertEquals('image/png', $file->getClientMimeType());
$this->assertEquals('/tmp/file', $file->getPathname());
$this->assertEquals(UPLOAD_ERR_CANT_WRITE, $file->getError());
}
public function testThatSymfonyResponseIsReflected(): void
{
$fooCookie = (string) new Cookie('foo', '123');
$barCookie = (string) new Cookie('bar', '234');
$sfResponse = $this->createMock(SymfonyResponse::class);
$sfResponse->headers = new HeaderBag([
'X-Test' => 'Swoole-Runtime',
'Set-Cookie' => [$fooCookie, $barCookie],
]);
$sfResponse->expects(self::once())->method('getStatusCode')->willReturn(201);
$sfResponse->expects(self::once())->method('getContent')->willReturn('Test');
$response = $this->createMock(Response::class);
$expectedHeaders = [
['x-test', 'Swoole-Runtime'],
['set-cookie', $fooCookie],
['set-cookie', $barCookie],
];
$callCount = 0;
$response->expects(self::exactly(3))->method('header')
->willReturnCallback(function ($key, $value) use ($expectedHeaders, &$callCount) {
$this->assertArrayHasKey($callCount, $expectedHeaders);
$this->assertEquals($expectedHeaders[$callCount][0], $key);
$this->assertEquals($expectedHeaders[$callCount][1], $value);
++$callCount;
return true;
});
$response->expects(self::once())->method('status')->with(201);
$response->expects(self::once())->method('end')->with('Test');
SymfonyHttpBridge::reflectSymfonyResponse($sfResponse, $response);
}
public function testThatSymfonyStreamedResponseIsReflected(): void
{
$sfResponse = new StreamedResponse(function () {
echo "Foo\n";
ob_flush();
echo "Bar\n";
ob_flush();
});
$response = $this->createMock(Response::class);
$expectedWrites = [
"Foo\n",
"Bar\n",
'',
];
$callCount = 0;
$response->expects(self::exactly(3))->method('write')
->willReturnCallback(function ($string) use ($expectedWrites, &$callCount) {
$this->assertEquals($expectedWrites[$callCount], $string);
++$callCount;
return true;
});
$response->expects(self::once())->method('end');
SymfonyHttpBridge::reflectSymfonyResponse($sfResponse, $response);
}
public function testThatSymfonyBinaryFileResponseIsReflected(): void
{
$file = tempnam(sys_get_temp_dir(), uniqid());
file_put_contents($file, 'Foo');
$sfResponse = new BinaryFileResponse($file);
$response = $this->createMock(Response::class);
$response->expects(self::once())->method('sendfile')->with($file, null, null);
SymfonyHttpBridge::reflectSymfonyResponse($sfResponse, $response);
}
public function testThatSymfonyBinaryFileResponseWithRangeIsReflected(): void
{
$file = tempnam(sys_get_temp_dir(), uniqid());
file_put_contents($file, 'FooBar');
$request = new SymfonyRequest();
$request->headers->set('Range', 'bytes=2-4');
$sfResponse = new BinaryFileResponse($file);
$sfResponse->prepare($request);
$response = $this->createMock(Response::class);
$response->expects(self::once())->method('write')->with('oBa');
$response->expects(self::once())->method('end');
SymfonyHttpBridge::reflectSymfonyResponse($sfResponse, $response);
}
public function testStreamedResponseWillRespondWithOneChunkAtATime(): void
{
$sfResponse = new StreamedResponse(static function () {
echo str_repeat('a', 4096);
echo str_repeat('b', 4095);
});
$response = $this->createMock(Response::class);
$response->expects(self::exactly(2))
->method('write')
->with(self::logicalOr(
str_repeat('a', 4096),
str_repeat('b', 4095)
));
$response->expects(self::once())->method('end');
SymfonyHttpBridge::reflectSymfonyResponse($sfResponse, $response);
}
}