|
4 | 4 |
|
5 | 5 | namespace Yiisoft\DataResponse\Tests\Formatter;
|
6 | 6 |
|
| 7 | +use ArrayObject; |
7 | 8 | use Nyholm\Psr7\Factory\Psr17Factory;
|
8 | 9 | use PHPUnit\Framework\TestCase;
|
9 |
| -use RuntimeException; |
10 | 10 | use stdClass;
|
11 | 11 | use Yiisoft\DataResponse\DataResponse;
|
12 | 12 | use Yiisoft\DataResponse\DataResponseFactory;
|
@@ -163,7 +163,154 @@ public function testArrayValues(): void
|
163 | 163 | );
|
164 | 164 | }
|
165 | 165 |
|
| 166 | + public function testTraversableValue(): void |
| 167 | + { |
| 168 | + $dataResponse = $this->createResponse(['array-value' => new ArrayObject(['test', null])]); |
| 169 | + $result = (new XmlDataResponseFormatter())->format($dataResponse); |
| 170 | + $result->getBody()->rewind(); |
| 171 | + |
| 172 | + $this->assertSame( |
| 173 | + $this->xml( |
| 174 | + <<<EOF |
| 175 | + <response> |
| 176 | + <array-value> |
| 177 | + <item>test</item> |
| 178 | + <item/> |
| 179 | + </array-value> |
| 180 | + </response> |
| 181 | + EOF |
| 182 | + ), |
| 183 | + $result->getBody()->getContents() |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + public function testTraversableValues(): void |
| 188 | + { |
| 189 | + $dataResponse = $this->createResponse([ |
| 190 | + new ArrayObject(['foo' => 'bar', null]), |
| 191 | + new ArrayObject([true, false]), |
| 192 | + new ArrayObject(['array' => [ |
| 193 | + new ArrayObject(['test', null]), |
| 194 | + new ArrayObject([true, false]), |
| 195 | + ]]), |
| 196 | + ]); |
| 197 | + $result = (new XmlDataResponseFormatter())->format($dataResponse); |
| 198 | + $result->getBody()->rewind(); |
| 199 | + |
| 200 | + $this->assertSame( |
| 201 | + $this->xml( |
| 202 | + <<<EOF |
| 203 | + <response> |
| 204 | + <item> |
| 205 | + <foo>bar</foo> |
| 206 | + <item/> |
| 207 | + </item> |
| 208 | + <item> |
| 209 | + <item>true</item> |
| 210 | + <item>false</item> |
| 211 | + </item> |
| 212 | + <item> |
| 213 | + <array> |
| 214 | + <item> |
| 215 | + <item>test</item> |
| 216 | + <item/> |
| 217 | + </item> |
| 218 | + <item> |
| 219 | + <item>true</item> |
| 220 | + <item>false</item> |
| 221 | + </item> |
| 222 | + </array> |
| 223 | + </item> |
| 224 | + </response> |
| 225 | + EOF |
| 226 | + ), |
| 227 | + $result->getBody()->getContents() |
| 228 | + ); |
| 229 | + } |
| 230 | + |
| 231 | + public function testPriorityXmlDataInterfaceOverTraversable(): void |
| 232 | + { |
| 233 | + $dataResponse = $this->createResponse(new class(['foo']) extends ArrayObject implements XmlDataInterface { |
| 234 | + public function xmlTagName(): string |
| 235 | + { |
| 236 | + return 'xml-data'; |
| 237 | + } |
| 238 | + |
| 239 | + public function xmlTagAttributes(): array |
| 240 | + { |
| 241 | + return ['attribute' => 'test']; |
| 242 | + } |
| 243 | + |
| 244 | + public function xmlData(): array |
| 245 | + { |
| 246 | + return ['foo' => 'bar']; |
| 247 | + } |
| 248 | + }); |
| 249 | + $result = (new XmlDataResponseFormatter())->format($dataResponse); |
| 250 | + $result->getBody()->rewind(); |
| 251 | + |
| 252 | + $this->assertSame( |
| 253 | + $this->xml( |
| 254 | + <<<EOF |
| 255 | + <response> |
| 256 | + <xml-data attribute="test"> |
| 257 | + <foo>bar</foo> |
| 258 | + </xml-data> |
| 259 | + </response> |
| 260 | + EOF |
| 261 | + ), |
| 262 | + $result->getBody()->getContents() |
| 263 | + ); |
| 264 | + } |
| 265 | + |
| 266 | + public function testPriorityXmlDataInterfaceOverTraversableInArray(): void |
| 267 | + { |
| 268 | + $dataResponse = $this->createResponse([new class(['foo']) extends ArrayObject implements XmlDataInterface { |
| 269 | + public function xmlTagName(): string |
| 270 | + { |
| 271 | + return 'xml-data'; |
| 272 | + } |
| 273 | + |
| 274 | + public function xmlTagAttributes(): array |
| 275 | + { |
| 276 | + return ['attribute' => 'test']; |
| 277 | + } |
| 278 | + |
| 279 | + public function xmlData(): array |
| 280 | + { |
| 281 | + return ['foo' => 'bar']; |
| 282 | + } |
| 283 | + }]); |
| 284 | + $result = (new XmlDataResponseFormatter())->format($dataResponse); |
| 285 | + $result->getBody()->rewind(); |
| 286 | + |
| 287 | + $this->assertSame( |
| 288 | + $this->xml( |
| 289 | + <<<EOF |
| 290 | + <response> |
| 291 | + <xml-data attribute="test"> |
| 292 | + <foo>bar</foo> |
| 293 | + </xml-data> |
| 294 | + </response> |
| 295 | + EOF |
| 296 | + ), |
| 297 | + $result->getBody()->getContents() |
| 298 | + ); |
| 299 | + } |
| 300 | + |
166 | 301 | public function testEmptyObjectValues(): void
|
| 302 | + { |
| 303 | + $dataResponse = $this->createResponse(['object' => new stdClass()]); |
| 304 | + $result = (new XmlDataResponseFormatter())->format($dataResponse); |
| 305 | + $result->getBody()->rewind(); |
| 306 | + |
| 307 | + $this->assertSame( |
| 308 | + $this->xml('<response><object/></response>'), |
| 309 | + $result->getBody()->getContents() |
| 310 | + ); |
| 311 | + } |
| 312 | + |
| 313 | + public function testEmptyObjectValuesImplementXmlDataInterface(): void |
167 | 314 | {
|
168 | 315 | $dataResponse = $this->createResponse(['object' => new class() implements XmlDataInterface {
|
169 | 316 | public function xmlTagName(): string
|
@@ -217,15 +364,6 @@ public function testObjectValues(): void
|
217 | 364 | );
|
218 | 365 | }
|
219 | 366 |
|
220 |
| - public function testObjectNotImplementXmlFormatDataInterface(): void |
221 |
| - { |
222 |
| - $dataResponse = $this->createResponse(new stdClass()); |
223 |
| - |
224 |
| - $this->expectException(RuntimeException::class); |
225 |
| - |
226 |
| - (new XmlDataResponseFormatter())->format($dataResponse); |
227 |
| - } |
228 |
| - |
229 | 367 | public function testArrayObjectValues(): void
|
230 | 368 | {
|
231 | 369 | $objects = [
|
|
0 commit comments