|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.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 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Hydra\Tests\State; |
| 15 | + |
| 16 | +use ApiPlatform\Hydra\State\JsonStreamerProvider; |
| 17 | +use ApiPlatform\Metadata\Get; |
| 18 | +use ApiPlatform\State\ProviderInterface; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Symfony\Component\HttpFoundation\Request; |
| 21 | +use Symfony\Component\JsonStreamer\StreamReaderInterface; |
| 22 | +use Symfony\Component\TypeInfo\Type; |
| 23 | + |
| 24 | +class JsonStreamerProviderTest extends TestCase |
| 25 | +{ |
| 26 | + public function testProvideReadsJsonLdContentIntoInputClass(): void |
| 27 | + { |
| 28 | + $data = new \stdClass(); |
| 29 | + $operation = new Get(class: \stdClass::class, jsonStream: true, deserialize: true); |
| 30 | + $request = new Request(content: '{}'); |
| 31 | + $request->attributes->set('input_format', 'jsonld'); |
| 32 | + |
| 33 | + $jsonStreamReader = $this->createMock(StreamReaderInterface::class); |
| 34 | + $jsonStreamReader->expects($this->once()) |
| 35 | + ->method('read') |
| 36 | + ->with($this->isType('resource'), $this->equalTo(Type::object(\stdClass::class))) |
| 37 | + ->willReturn($data); |
| 38 | + |
| 39 | + $decorated = $this->createMock(ProviderInterface::class); |
| 40 | + $decorated->method('provide')->willReturn(null); |
| 41 | + |
| 42 | + $provider = new JsonStreamerProvider($decorated, $jsonStreamReader); |
| 43 | + $result = $provider->provide($operation, [], ['request' => $request]); |
| 44 | + |
| 45 | + $this->assertSame($data, $result); |
| 46 | + $this->assertTrue($request->attributes->get('deserialized')); |
| 47 | + } |
| 48 | + |
| 49 | + public function testProvideBypassesWhenNotJsonLdFormat(): void |
| 50 | + { |
| 51 | + $data = new \stdClass(); |
| 52 | + $operation = new Get(class: \stdClass::class, jsonStream: true, deserialize: true); |
| 53 | + $request = new Request(); |
| 54 | + $request->attributes->set('input_format', 'json'); |
| 55 | + $request->attributes->set('data', $data); |
| 56 | + |
| 57 | + $jsonStreamReader = $this->createMock(StreamReaderInterface::class); |
| 58 | + $jsonStreamReader->expects($this->never())->method('read'); |
| 59 | + |
| 60 | + $decorated = $this->createMock(ProviderInterface::class); |
| 61 | + $decorated->method('provide')->willReturn($data); |
| 62 | + |
| 63 | + $provider = new JsonStreamerProvider($decorated, $jsonStreamReader); |
| 64 | + $result = $provider->provide($operation, [], ['request' => $request]); |
| 65 | + |
| 66 | + $this->assertSame($data, $result); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Regression test: input explicitly disabled (`input: false`) combined with an explicit |
| 71 | + * `deserialize: true` (a contradictory config) must not crash `Type::object(null)` — it should |
| 72 | + * bypass instead. |
| 73 | + */ |
| 74 | + public function testProvideBypassesWhenInputDisabledDespiteDeserializeTrue(): void |
| 75 | + { |
| 76 | + $data = new \stdClass(); |
| 77 | + $operation = new Get(class: \stdClass::class, jsonStream: true, deserialize: true, input: false); |
| 78 | + $request = new Request(content: '{}'); |
| 79 | + $request->attributes->set('input_format', 'jsonld'); |
| 80 | + |
| 81 | + $jsonStreamReader = $this->createMock(StreamReaderInterface::class); |
| 82 | + $jsonStreamReader->expects($this->never())->method('read'); |
| 83 | + |
| 84 | + $decorated = $this->createMock(ProviderInterface::class); |
| 85 | + $decorated->method('provide')->willReturn($data); |
| 86 | + |
| 87 | + $provider = new JsonStreamerProvider($decorated, $jsonStreamReader); |
| 88 | + $result = $provider->provide($operation, [], ['request' => $request]); |
| 89 | + |
| 90 | + $this->assertSame($data, $result); |
| 91 | + } |
| 92 | +} |
0 commit comments