|
| 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\State\Tests\Processor; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Get; |
| 17 | +use ApiPlatform\State\Processor\SerializeProcessor; |
| 18 | +use ApiPlatform\State\ProcessorInterface; |
| 19 | +use ApiPlatform\State\SerializerContextBuilderInterface; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Symfony\Component\HttpFoundation\Request; |
| 22 | +use Symfony\Component\Serializer\SerializerInterface; |
| 23 | + |
| 24 | +class SerializeProcessorTest extends TestCase |
| 25 | +{ |
| 26 | + public function testHeadRequestSkipsSerializationAndForwardsNull(): void |
| 27 | + { |
| 28 | + $request = Request::create('/foos', 'HEAD'); |
| 29 | + |
| 30 | + $serializer = $this->createMock(SerializerInterface::class); |
| 31 | + $serializer->expects($this->never())->method('serialize'); |
| 32 | + |
| 33 | + $inner = $this->createMock(ProcessorInterface::class); |
| 34 | + $inner->expects($this->once()) |
| 35 | + ->method('process') |
| 36 | + ->with($this->isNull()) |
| 37 | + ->willReturn(null); |
| 38 | + |
| 39 | + $processor = new SerializeProcessor($inner, $serializer, $this->createStub(SerializerContextBuilderInterface::class)); |
| 40 | + $operation = (new Get())->withSerialize(true); |
| 41 | + |
| 42 | + $this->assertNull($processor->process(new \stdClass(), $operation, [], ['request' => $request])); |
| 43 | + } |
| 44 | + |
| 45 | + public function testHeadRequestSerializesWhenOptimizationDisabled(): void |
| 46 | + { |
| 47 | + $request = Request::create('/foos', 'HEAD'); |
| 48 | + |
| 49 | + $serializer = $this->createMock(SerializerInterface::class); |
| 50 | + $serializer->expects($this->once())->method('serialize')->willReturn(''); |
| 51 | + |
| 52 | + $inner = $this->createMock(ProcessorInterface::class); |
| 53 | + $inner->method('process')->willReturn('forwarded'); |
| 54 | + |
| 55 | + $contextBuilder = $this->createStub(SerializerContextBuilderInterface::class); |
| 56 | + $contextBuilder->method('createFromRequest')->willReturn([]); |
| 57 | + |
| 58 | + $processor = new SerializeProcessor($inner, $serializer, $contextBuilder, false); |
| 59 | + $operation = (new Get())->withSerialize(true); |
| 60 | + |
| 61 | + $this->assertSame('forwarded', $processor->process(new \stdClass(), $operation, [], ['request' => $request])); |
| 62 | + } |
| 63 | +} |
0 commit comments