|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpFoundation\Tests; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpFoundation\StreamedJsonResponse; |
| 16 | + |
| 17 | +class StreamedJsonResponseTest extends TestCase |
| 18 | +{ |
| 19 | + public function testResponseSimpleList() |
| 20 | + { |
| 21 | + $content = $this->createSendResponse( |
| 22 | + [ |
| 23 | + '_embedded' => [ |
| 24 | + 'articles' => $this->generatorSimple('Article'), |
| 25 | + 'news' => $this->generatorSimple('News'), |
| 26 | + ], |
| 27 | + ], |
| 28 | + ); |
| 29 | + |
| 30 | + $this->assertSame('{"_embedded":{"articles":["Article 1","Article 2","Article 3"],"news":["News 1","News 2","News 3"]}}', $content); |
| 31 | + } |
| 32 | + |
| 33 | + public function testResponseObjectsList() |
| 34 | + { |
| 35 | + $content = $this->createSendResponse( |
| 36 | + [ |
| 37 | + '_embedded' => [ |
| 38 | + 'articles' => $this->generatorArray('Article'), |
| 39 | + ], |
| 40 | + ], |
| 41 | + ); |
| 42 | + |
| 43 | + $this->assertSame('{"_embedded":{"articles":[{"title":"Article 1"},{"title":"Article 2"},{"title":"Article 3"}]}}', $content); |
| 44 | + } |
| 45 | + |
| 46 | + public function testResponseWithoutGenerator() |
| 47 | + { |
| 48 | + // while it is not the intended usage, all kind of iterables should be supported for good DX |
| 49 | + $content = $this->createSendResponse( |
| 50 | + [ |
| 51 | + '_embedded' => [ |
| 52 | + 'articles' => ['Article 1', 'Article 2', 'Article 3'], |
| 53 | + ], |
| 54 | + ], |
| 55 | + ); |
| 56 | + |
| 57 | + $this->assertSame('{"_embedded":{"articles":["Article 1","Article 2","Article 3"]}}', $content); |
| 58 | + } |
| 59 | + |
| 60 | + public function testResponseWithPlaceholder() |
| 61 | + { |
| 62 | + // the placeholder must not conflict with generator injection |
| 63 | + $content = $this->createSendResponse( |
| 64 | + [ |
| 65 | + '_embedded' => [ |
| 66 | + 'articles' => $this->generatorArray('Article'), |
| 67 | + 'placeholder' => '__symfony_json__', |
| 68 | + 'news' => $this->generatorSimple('News'), |
| 69 | + ], |
| 70 | + 'placeholder' => '__symfony_json__', |
| 71 | + ], |
| 72 | + ); |
| 73 | + |
| 74 | + $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); |
| 75 | + } |
| 76 | + |
| 77 | + public function testResponseWithMixedKeyType() |
| 78 | + { |
| 79 | + $content = $this->createSendResponse( |
| 80 | + [ |
| 81 | + '_embedded' => [ |
| 82 | + 'list' => (function (): \Generator { |
| 83 | + yield 0 => 'test'; |
| 84 | + yield 'key' => 'value'; |
| 85 | + })(), |
| 86 | + 'map' => (function (): \Generator { |
| 87 | + yield 'key' => 'value'; |
| 88 | + yield 0 => 'test'; |
| 89 | + })(), |
| 90 | + 'integer' => (function (): \Generator { |
| 91 | + yield 1 => 'one'; |
| 92 | + yield 3 => 'three'; |
| 93 | + })(), |
| 94 | + ], |
| 95 | + ] |
| 96 | + ); |
| 97 | + |
| 98 | + $this->assertSame('{"_embedded":{"list":["test","value"],"map":{"key":"value","0":"test"},"integer":{"1":"one","3":"three"}}}', $content); |
| 99 | + } |
| 100 | + |
| 101 | + public function testResponseOtherTraversable() |
| 102 | + { |
| 103 | + $arrayObject = new \ArrayObject(['__symfony_json__' => '__symfony_json__']); |
| 104 | + |
| 105 | + $iteratorAggregate = new class() implements \IteratorAggregate { |
| 106 | + public function getIterator(): \Traversable |
| 107 | + { |
| 108 | + return new \ArrayIterator(['__symfony_json__']); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + $jsonSerializable = new class() implements \IteratorAggregate, \JsonSerializable { |
| 113 | + public function getIterator(): \Traversable |
| 114 | + { |
| 115 | + return new \ArrayIterator(['This should be ignored']); |
| 116 | + } |
| 117 | + |
| 118 | + public function jsonSerialize(): mixed |
| 119 | + { |
| 120 | + return ['__symfony_json__' => '__symfony_json__']; |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + // while Generators should be used for performance reasons, the object should also work with any Traversable |
| 125 | + // to make things easier for a developer |
| 126 | + $content = $this->createSendResponse( |
| 127 | + [ |
| 128 | + 'arrayObject' => $arrayObject, |
| 129 | + 'iteratorAggregate' => $iteratorAggregate, |
| 130 | + 'jsonSerializable' => $jsonSerializable, |
| 131 | + // add a Generator to make sure it still work in combination with other Traversable objects |
| 132 | + 'articles' => $this->generatorArray('Article'), |
| 133 | + ], |
| 134 | + ); |
| 135 | + |
| 136 | + $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); |
| 137 | + } |
| 138 | + |
| 139 | + public function testPlaceholderAsKeyAndValueInStructure() |
| 140 | + { |
| 141 | + $content = $this->createSendResponse( |
| 142 | + [ |
| 143 | + '__symfony_json__' => '__symfony_json__', |
| 144 | + 'articles' => $this->generatorArray('Article'), |
| 145 | + ], |
| 146 | + ); |
| 147 | + |
| 148 | + $this->assertSame('{"__symfony_json__":"__symfony_json__","articles":[{"title":"Article 1"},{"title":"Article 2"},{"title":"Article 3"}]}', $content); |
| 149 | + } |
| 150 | + |
| 151 | + public function testResponseStatusCode() |
| 152 | + { |
| 153 | + $response = new StreamedJsonResponse([], 201); |
| 154 | + |
| 155 | + $this->assertSame(201, $response->getStatusCode()); |
| 156 | + } |
| 157 | + |
| 158 | + public function testResponseHeaders() |
| 159 | + { |
| 160 | + $response = new StreamedJsonResponse([], 200, ['X-Test' => 'Test']); |
| 161 | + |
| 162 | + $this->assertSame('Test', $response->headers->get('X-Test')); |
| 163 | + } |
| 164 | + |
| 165 | + public function testCustomContentType() |
| 166 | + { |
| 167 | + $response = new StreamedJsonResponse([], 200, ['Content-Type' => 'application/json+stream']); |
| 168 | + |
| 169 | + $this->assertSame('application/json+stream', $response->headers->get('Content-Type')); |
| 170 | + } |
| 171 | + |
| 172 | + public function testEncodingOptions() |
| 173 | + { |
| 174 | + $response = new StreamedJsonResponse([ |
| 175 | + '_embedded' => [ |
| 176 | + 'count' => '2', // options are applied to the initial json encode |
| 177 | + 'values' => (function (): \Generator { |
| 178 | + yield 'with/unescaped/slash' => 'With/a/slash'; // options are applied to key and values |
| 179 | + yield '3' => '3'; // numeric check for value, but not for the key |
| 180 | + })(), |
| 181 | + ], |
| 182 | + ], encodingOptions: \JSON_UNESCAPED_SLASHES | \JSON_NUMERIC_CHECK); |
| 183 | + |
| 184 | + ob_start(); |
| 185 | + $response->send(); |
| 186 | + $content = ob_get_clean(); |
| 187 | + |
| 188 | + $this->assertSame('{"_embedded":{"count":2,"values":{"with/unescaped/slash":"With/a/slash","3":3}}}', $content); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @param mixed[] $data |
| 193 | + */ |
| 194 | + private function createSendResponse(array $data): string |
| 195 | + { |
| 196 | + $response = new StreamedJsonResponse($data); |
| 197 | + |
| 198 | + ob_start(); |
| 199 | + $response->send(); |
| 200 | + |
| 201 | + return ob_get_clean(); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * @return \Generator<int, string> |
| 206 | + */ |
| 207 | + private function generatorSimple(string $test): \Generator |
| 208 | + { |
| 209 | + yield $test.' 1'; |
| 210 | + yield $test.' 2'; |
| 211 | + yield $test.' 3'; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * @return \Generator<int, array{title: string}> |
| 216 | + */ |
| 217 | + private function generatorArray(string $test): \Generator |
| 218 | + { |
| 219 | + yield ['title' => $test.' 1']; |
| 220 | + yield ['title' => $test.' 2']; |
| 221 | + yield ['title' => $test.' 3']; |
| 222 | + } |
| 223 | +} |
0 commit comments