|
| 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 | +namespaceSymfony\Component\HttpFoundation\Tests; |
| 13 | + |
| 14 | +usePHPUnit\Framework\TestCase; |
| 15 | +useSymfony\Component\HttpFoundation\StreamedJsonResponse; |
| 16 | + |
| 17 | +class StreamedJsonResponseTestextends TestCase |
| 18 | +{ |
| 19 | +publicfunctiontestResponseSimpleList() |
| 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 | +publicfunctiontestResponseObjectsList() |
| 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 | +publicfunctiontestResponseWithoutGenerator() |
| 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 | +publicfunctiontestResponseWithPlaceholder() |
| 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 | +publicfunctiontestResponseWithMixedKeyType() |
| 78 | + { |
| 79 | +$content =$this->createSendResponse( |
| 80 | + [ |
| 81 | +'_embedded' => [ |
| 82 | +'list' => (function ():\Generator { |
| 83 | +yield0 =>'test'; |
| 84 | +yield'key' =>'value'; |
| 85 | + })(), |
| 86 | +'map' => (function ():\Generator { |
| 87 | +yield'key' =>'value'; |
| 88 | +yield0 =>'test'; |
| 89 | + })(), |
| 90 | +'integer' => (function ():\Generator { |
| 91 | +yield1 =>'one'; |
| 92 | +yield3 =>'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 | +publicfunctiontestResponseOtherTraversable() |
| 102 | + { |
| 103 | +$arrayObject =new \ArrayObject(['__symfony_json__' =>'__symfony_json__']); |
| 104 | + |
| 105 | +$iteratorAggregate =newclass()implements \IteratorAggregate { |
| 106 | +publicfunctiongetIterator():\Traversable |
| 107 | + { |
| 108 | +returnnew \ArrayIterator(['__symfony_json__']); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | +$jsonSerializable =newclass()implements \IteratorAggregate, \JsonSerializable { |
| 113 | +publicfunctiongetIterator():\Traversable |
| 114 | + { |
| 115 | +returnnew \ArrayIterator(['This should be ignored']); |
| 116 | + } |
| 117 | + |
| 118 | +publicfunctionjsonSerialize():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 | +publicfunctiontestPlaceholderAsKeyAndValueInStructure() |
| 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 | +publicfunctiontestResponseStatusCode() |
| 152 | + { |
| 153 | +$response =newStreamedJsonResponse([],201); |
| 154 | + |
| 155 | +$this->assertSame(201,$response->getStatusCode()); |
| 156 | + } |
| 157 | + |
| 158 | +publicfunctiontestPlaceholderAsObjectStructure() |
| 159 | + { |
| 160 | +$object =newclass() { |
| 161 | +public$__symfony_json__ ='foo'; |
| 162 | +public$bar ='__symfony_json__'; |
| 163 | + }; |
| 164 | + |
| 165 | +$content =$this->createSendResponse( |
| 166 | + [ |
| 167 | +'object' =>$object, |
| 168 | +// add a Generator to make sure it still work in combination with other object holding placeholders |
| 169 | +'articles' =>$this->generatorArray('Article'), |
| 170 | + ], |
| 171 | + ); |
| 172 | + |
| 173 | +$this->assertSame('{"object":{"__symfony_json__":"foo","bar":"__symfony_json__"},"articles":[{"title":"Article 1"},{"title":"Article 2"},{"title":"Article 3"}]}',$content); |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | +publicfunctiontestResponseHeaders() |
| 178 | + { |
| 179 | +$response =newStreamedJsonResponse([],200, ['X-Test' =>'Test']); |
| 180 | + |
| 181 | +$this->assertSame('Test',$response->headers->get('X-Test')); |
| 182 | + } |
| 183 | + |
| 184 | +publicfunctiontestCustomContentType() |
| 185 | + { |
| 186 | +$response =newStreamedJsonResponse([],200, ['Content-Type' =>'application/json+stream']); |
| 187 | + |
| 188 | +$this->assertSame('application/json+stream',$response->headers->get('Content-Type')); |
| 189 | + } |
| 190 | + |
| 191 | +publicfunctiontestEncodingOptions() |
| 192 | + { |
| 193 | +$response =newStreamedJsonResponse([ |
| 194 | +'_embedded' => [ |
| 195 | +'count' =>'2',// options are applied to the initial json encode |
| 196 | +'values' => (function ():\Generator { |
| 197 | +yield'with/unescaped/slash' =>'With/a/slash';// options are applied to key and values |
| 198 | +yield'3' =>'3';// numeric check for value, but not for the key |
| 199 | + })(), |
| 200 | + ], |
| 201 | + ], encodingOptions: \JSON_UNESCAPED_SLASHES | \JSON_NUMERIC_CHECK); |
| 202 | + |
| 203 | +ob_start(); |
| 204 | +$response->send(); |
| 205 | +$content =ob_get_clean(); |
| 206 | + |
| 207 | +$this->assertSame('{"_embedded":{"count":2,"values":{"with/unescaped/slash":"With/a/slash","3":3}}}',$content); |
| 208 | + } |
| 209 | + |
| 210 | +/** |
| 211 | + * @param mixed[] $data |
| 212 | + */ |
| 213 | +privatefunctioncreateSendResponse(array$data):string |
| 214 | + { |
| 215 | +$response =newStreamedJsonResponse($data); |
| 216 | + |
| 217 | +ob_start(); |
| 218 | +$response->send(); |
| 219 | + |
| 220 | +returnob_get_clean(); |
| 221 | + } |
| 222 | + |
| 223 | +/** |
| 224 | + * @return \Generator<int, string> |
| 225 | + */ |
| 226 | +privatefunctiongeneratorSimple(string$test):\Generator |
| 227 | + { |
| 228 | +yield$test.' 1'; |
| 229 | +yield$test.' 2'; |
| 230 | +yield$test.' 3'; |
| 231 | + } |
| 232 | + |
| 233 | +/** |
| 234 | + * @return \Generator<int, array{title: string}> |
| 235 | + */ |
| 236 | +privatefunctiongeneratorArray(string$test):\Generator |
| 237 | + { |
| 238 | +yield ['title' =>$test.' 1']; |
| 239 | +yield ['title' =>$test.' 2']; |
| 240 | +yield ['title' =>$test.' 3']; |
| 241 | + } |
| 242 | +} |