|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 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\Core\Tests\Bridge\Symfony\Bundle\DependencyInjection\Compiler; |
| 15 | + |
| 16 | +use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlExceptionFormatterPass; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Prophecy\Argument; |
| 19 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 20 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 21 | +use Symfony\Component\DependencyInjection\Definition; |
| 22 | +use Symfony\Component\DependencyInjection\Reference; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Mahmood Bazdar <[email protected]> |
| 26 | + */ |
| 27 | +class GraphQlExceptionFormatterPassTest extends TestCase |
| 28 | +{ |
| 29 | + public function testProcess() |
| 30 | + { |
| 31 | + $filterPass = new GraphQlExceptionFormatterPass(); |
| 32 | + |
| 33 | + $this->assertInstanceOf(CompilerPassInterface::class, $filterPass); |
| 34 | + |
| 35 | + $exceptionFormatterLocatorDefinitionProphecy = $this->prophesize(Definition::class); |
| 36 | + $exceptionFormatterLocatorDefinitionProphecy->addArgument(Argument::that(function (array $arg) { |
| 37 | + return !isset($arg['foo']) && isset($arg['my_id']) && $arg['my_id'] instanceof Reference; |
| 38 | + }))->shouldBeCalled(); |
| 39 | + |
| 40 | + $exceptionFormatterFactoryDefinitionProphecy = $this->prophesize(Definition::class); |
| 41 | + $exceptionFormatterFactoryDefinitionProphecy->addArgument(['my_id'])->shouldBeCalled(); |
| 42 | + |
| 43 | + $containerBuilderProphecy = $this->prophesize(ContainerBuilder::class); |
| 44 | + $containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(true)->shouldBeCalled(); |
| 45 | + $containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.exception_formatter', true)->willReturn(['foo' => [], 'bar' => [['id' => 'my_id']]])->shouldBeCalled(); |
| 46 | + $containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_locator')->willReturn($exceptionFormatterLocatorDefinitionProphecy->reveal())->shouldBeCalled(); |
| 47 | + $containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_factory')->willReturn($exceptionFormatterFactoryDefinitionProphecy->reveal())->shouldBeCalled(); |
| 48 | + |
| 49 | + $filterPass->process($containerBuilderProphecy->reveal()); |
| 50 | + } |
| 51 | + |
| 52 | + public function testIdNotExist() |
| 53 | + { |
| 54 | + $filterPass = new GraphQlExceptionFormatterPass(); |
| 55 | + |
| 56 | + $this->assertInstanceOf(CompilerPassInterface::class, $filterPass); |
| 57 | + |
| 58 | + $exceptionFormatterLocatorDefinitionProphecy = $this->prophesize(Definition::class); |
| 59 | + $exceptionFormatterLocatorDefinitionProphecy->addArgument(Argument::that(function (array $arg) { |
| 60 | + return !isset($arg['foo']) && isset($arg['bar']) && $arg['bar'] instanceof Reference; |
| 61 | + }))->shouldBeCalled(); |
| 62 | + |
| 63 | + $exceptionFormatterFactoryDefinitionProphecy = $this->prophesize(Definition::class); |
| 64 | + $exceptionFormatterFactoryDefinitionProphecy->addArgument(['bar'])->shouldBeCalled(); |
| 65 | + |
| 66 | + $containerBuilderProphecy = $this->prophesize(ContainerBuilder::class); |
| 67 | + $containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(true)->shouldBeCalled(); |
| 68 | + $containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.exception_formatter', true)->willReturn(['foo' => [], 'bar' => [['hi' => 'hello']]])->shouldBeCalled(); |
| 69 | + $containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_locator')->willReturn($exceptionFormatterLocatorDefinitionProphecy->reveal())->shouldBeCalled(); |
| 70 | + $containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_factory')->willReturn($exceptionFormatterFactoryDefinitionProphecy->reveal())->shouldBeCalled(); |
| 71 | + |
| 72 | + $filterPass->process($containerBuilderProphecy->reveal()); |
| 73 | + } |
| 74 | + |
| 75 | + public function testDisabled() |
| 76 | + { |
| 77 | + $filterPass = new GraphQlExceptionFormatterPass(); |
| 78 | + |
| 79 | + $this->assertInstanceOf(CompilerPassInterface::class, $filterPass); |
| 80 | + |
| 81 | + $exceptionFormatterLocatorDefinitionProphecy = $this->prophesize(Definition::class); |
| 82 | + $exceptionFormatterLocatorDefinitionProphecy->addArgument(Argument::any())->shouldNotBeCalled(); |
| 83 | + |
| 84 | + $exceptionFormatterFactoryDefinitionProphecy = $this->prophesize(Definition::class); |
| 85 | + $exceptionFormatterFactoryDefinitionProphecy->addArgument(['my_id'])->shouldNotBeCalled(); |
| 86 | + |
| 87 | + $containerBuilderProphecy = $this->prophesize(ContainerBuilder::class); |
| 88 | + $containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(false)->shouldBeCalled(); |
| 89 | + $containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.exception_formatter', true)->willReturn(['foo' => [], 'bar' => [['id' => 'my_id']]])->shouldNotBeCalled(); |
| 90 | + $containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_locator')->willReturn($exceptionFormatterLocatorDefinitionProphecy->reveal())->shouldNotBeCalled(); |
| 91 | + $containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_factory')->willReturn($exceptionFormatterFactoryDefinitionProphecy->reveal())->shouldNotBeCalled(); |
| 92 | + |
| 93 | + $filterPass->process($containerBuilderProphecy->reveal()); |
| 94 | + } |
| 95 | +} |
0 commit comments