-
-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathStreamedJsonResponseTest.php
271 lines (227 loc) · 9.1 KB
/
StreamedJsonResponseTest.php
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\StreamedJsonResponse;
class StreamedJsonResponseTest extends TestCase
{
public function testResponseSimpleList()
{
$content = $this->createSendResponse(
[
'_embedded' => [
'articles' => $this->generatorSimple('Article'),
'news' => $this->generatorSimple('News'),
],
],
);
$this->assertSame('{"_embedded":{"articles":["Article 1","Article 2","Article 3"],"news":["News 1","News 2","News 3"]}}', $content);
}
public function testResponseSimpleGenerator()
{
$content = $this->createSendResponse($this->generatorSimple('Article'));
$this->assertSame('["Article 1","Article 2","Article 3"]', $content);
}
public function testResponseNestedGenerator()
{
$content = $this->createSendResponse((function (): iterable {
yield 'articles' => $this->generatorSimple('Article');
yield 'news' => $this->generatorSimple('News');
})());
$this->assertSame('{"articles":["Article 1","Article 2","Article 3"],"news":["News 1","News 2","News 3"]}', $content);
}
public function testResponseEmptyList()
{
$content = $this->createSendResponse(
[
'_embedded' => [
'articles' => $this->generatorSimple('Article', 0),
],
],
);
$this->assertSame('{"_embedded":{"articles":[]}}', $content);
}
public function testResponseObjectsList()
{
$content = $this->createSendResponse(
[
'_embedded' => [
'articles' => $this->generatorArray('Article'),
],
],
);
$this->assertSame('{"_embedded":{"articles":[{"title":"Article 1"},{"title":"Article 2"},{"title":"Article 3"}]}}', $content);
}
public function testResponseWithoutGenerator()
{
// while it is not the intended usage, all kind of iterables should be supported for good DX
$content = $this->createSendResponse(
[
'_embedded' => [
'articles' => ['Article 1', 'Article 2', 'Article 3'],
],
],
);
$this->assertSame('{"_embedded":{"articles":["Article 1","Article 2","Article 3"]}}', $content);
}
public function testResponseWithPlaceholder()
{
// the placeholder must not conflict with generator injection
$content = $this->createSendResponse(
[
'_embedded' => [
'articles' => $this->generatorArray('Article'),
'placeholder' => '__symfony_json__',
'news' => $this->generatorSimple('News'),
],
'placeholder' => '__symfony_json__',
],
);
$this->assertSame('{"_embedded":{"articles":[{"title":"Article 1"},{"title":"Article 2"},{"title":"Article 3"}],"placeholder":"__symfony_json__","news":["News 1","News 2","News 3"]},"placeholder":"__symfony_json__"}', $content);
}
public function testResponseWithMixedKeyType()
{
$content = $this->createSendResponse(
[
'_embedded' => [
'list' => (function (): \Generator {
yield 0 => 'test';
yield 'key' => 'value';
})(),
'map' => (function (): \Generator {
yield 'key' => 'value';
yield 0 => 'test';
})(),
'integer' => (function (): \Generator {
yield 1 => 'one';
yield 3 => 'three';
})(),
],
]
);
$this->assertSame('{"_embedded":{"list":["test","value"],"map":{"key":"value","0":"test"},"integer":{"1":"one","3":"three"}}}', $content);
}
public function testResponseOtherTraversable()
{
$arrayObject = new \ArrayObject(['__symfony_json__' => '__symfony_json__']);
$iteratorAggregate = new class implements \IteratorAggregate {
public function getIterator(): \Traversable
{
return new \ArrayIterator(['__symfony_json__']);
}
};
$jsonSerializable = new class implements \IteratorAggregate, \JsonSerializable {
public function getIterator(): \Traversable
{
return new \ArrayIterator(['This should be ignored']);
}
public function jsonSerialize(): mixed
{
return ['__symfony_json__' => '__symfony_json__'];
}
};
// while Generators should be used for performance reasons, the object should also work with any Traversable
// to make things easier for a developer
$content = $this->createSendResponse(
[
'arrayObject' => $arrayObject,
'iteratorAggregate' => $iteratorAggregate,
'jsonSerializable' => $jsonSerializable,
// add a Generator to make sure it still work in combination with other Traversable objects
'articles' => $this->generatorArray('Article'),
],
);
$this->assertSame('{"arrayObject":{"__symfony_json__":"__symfony_json__"},"iteratorAggregate":["__symfony_json__"],"jsonSerializable":{"__symfony_json__":"__symfony_json__"},"articles":[{"title":"Article 1"},{"title":"Article 2"},{"title":"Article 3"}]}', $content);
}
public function testPlaceholderAsKeyAndValueInStructure()
{
$content = $this->createSendResponse(
[
'__symfony_json__' => '__symfony_json__',
'articles' => $this->generatorArray('Article'),
],
);
$this->assertSame('{"__symfony_json__":"__symfony_json__","articles":[{"title":"Article 1"},{"title":"Article 2"},{"title":"Article 3"}]}', $content);
}
public function testResponseStatusCode()
{
$response = new StreamedJsonResponse([], 201);
$this->assertSame(201, $response->getStatusCode());
}
public function testPlaceholderAsObjectStructure()
{
$object = new class {
public $__symfony_json__ = 'foo';
public $bar = '__symfony_json__';
};
$content = $this->createSendResponse(
[
'object' => $object,
// add a Generator to make sure it still work in combination with other object holding placeholders
'articles' => $this->generatorArray('Article'),
],
);
$this->assertSame('{"object":{"__symfony_json__":"foo","bar":"__symfony_json__"},"articles":[{"title":"Article 1"},{"title":"Article 2"},{"title":"Article 3"}]}', $content);
}
public function testResponseHeaders()
{
$response = new StreamedJsonResponse([], 200, ['X-Test' => 'Test']);
$this->assertSame('Test', $response->headers->get('X-Test'));
}
public function testCustomContentType()
{
$response = new StreamedJsonResponse([], 200, ['Content-Type' => 'application/json+stream']);
$this->assertSame('application/json+stream', $response->headers->get('Content-Type'));
}
public function testEncodingOptions()
{
$response = new StreamedJsonResponse([
'_embedded' => [
'count' => '2', // options are applied to the initial json encode
'values' => (function (): \Generator {
yield 'with/unescaped/slash' => 'With/a/slash'; // options are applied to key and values
yield '3' => '3'; // numeric check for value, but not for the key
})(),
],
], encodingOptions: \JSON_UNESCAPED_SLASHES | \JSON_NUMERIC_CHECK);
ob_start();
$response->send();
$content = ob_get_clean();
$this->assertSame('{"_embedded":{"count":2,"values":{"with/unescaped/slash":"With/a/slash","3":3}}}', $content);
}
/**
* @param iterable<mixed> $data
*/
private function createSendResponse(iterable $data): string
{
$response = new StreamedJsonResponse($data);
ob_start();
$response->send();
return ob_get_clean();
}
/**
* @return \Generator<int, string>
*/
private function generatorSimple(string $test, int $length = 3): \Generator
{
for ($i = 1; $i <= $length; ++$i) {
yield $test.' '.$i;
}
}
/**
* @return \Generator<int, array{title: string}>
*/
private function generatorArray(string $test, int $length = 3): \Generator
{
for ($i = 1; $i <= $length; ++$i) {
yield ['title' => $test.' '.$i];
}
}
}