|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ApiPlatform\State\Tests; |
| 4 | + |
| 5 | +use ApiPlatform\Metadata\Exception\RuntimeException; |
| 6 | +use ApiPlatform\Metadata\Get; |
| 7 | +use ApiPlatform\State\CallableProcessor; |
| 8 | +use ApiPlatform\State\ProcessorInterface; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Psr\Container\ContainerInterface; |
| 11 | + |
| 12 | +class CallableProcessorTest extends TestCase |
| 13 | +{ |
| 14 | + public function testNoProcessor(): void |
| 15 | + { |
| 16 | + $operation = new Get(name: 'hello'); |
| 17 | + $data = new \stdClass(); |
| 18 | + $this->assertEquals($data, (new CallableProcessor())->process($data, $operation)); |
| 19 | + } |
| 20 | + |
| 21 | + public function testCallable(): void |
| 22 | + { |
| 23 | + $operation = new Get(name: 'hello', processor: fn () => ['ok']); |
| 24 | + $this->assertEquals((new CallableProcessor())->process(new \stdClass(), $operation), ['ok']); |
| 25 | + } |
| 26 | + |
| 27 | + public function testCallableServiceLocator(): void |
| 28 | + { |
| 29 | + $operation = new Get(name: 'hello', processor: 'processor'); |
| 30 | + $provider = $this->createMock(ProcessorInterface::class); |
| 31 | + $provider->method('process')->willReturn(['ok']); |
| 32 | + $container = $this->createMock(ContainerInterface::class); |
| 33 | + $container->method('has')->with('processor')->willReturn(true); |
| 34 | + $container->method('get')->with('processor')->willReturn($provider); |
| 35 | + $this->assertEquals((new CallableProcessor($container))->process(new \stdClass(), $operation), ['ok']); |
| 36 | + } |
| 37 | + |
| 38 | + public function testCallableServiceLocatorDoesNotExist(): void |
| 39 | + { |
| 40 | + $this->expectException(RuntimeException::class); |
| 41 | + $this->expectExceptionMessage('Processor "processor" not found on operation "hello"'); |
| 42 | + $operation = new Get(name: 'hello', processor: 'processor'); |
| 43 | + $container = $this->createMock(ContainerInterface::class); |
| 44 | + $container->method('has')->with('processor')->willReturn(false); |
| 45 | + (new CallableProcessor($container))->process(new \stdClass(), $operation); |
| 46 | + } |
| 47 | +} |
0 commit comments