|
| 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\GraphQl\Tests\Action; |
| 15 | + |
| 16 | +use ApiPlatform\GraphQl\Action\GraphQlPlaygroundAction; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Prophecy\Argument; |
| 19 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 20 | +use Symfony\Component\HttpFoundation\Request; |
| 21 | +use Symfony\Component\HttpFoundation\Response; |
| 22 | +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
| 23 | +use Symfony\Component\Routing\RouterInterface; |
| 24 | +use Twig\Environment as TwigEnvironment; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Alan Poulain <[email protected]> |
| 28 | + */ |
| 29 | +class GraphQlPlaygroundActionTest extends TestCase |
| 30 | +{ |
| 31 | + use ProphecyTrait; |
| 32 | + |
| 33 | + public function testEnabledAction(): void |
| 34 | + { |
| 35 | + $request = new Request(); |
| 36 | + $mockedAction = $this->getGraphQlPlaygroundAction(true); |
| 37 | + |
| 38 | + $this->assertInstanceOf(Response::class, $mockedAction($request)); |
| 39 | + } |
| 40 | + |
| 41 | + public function testDisabledAction(): void |
| 42 | + { |
| 43 | + $request = new Request(); |
| 44 | + $mockedAction = $this->getGraphQlPlaygroundAction(false); |
| 45 | + |
| 46 | + $this->expectExceptionObject(new BadRequestHttpException('GraphQL Playground is not enabled.')); |
| 47 | + |
| 48 | + $mockedAction($request); |
| 49 | + } |
| 50 | + |
| 51 | + private function getGraphQlPlaygroundAction(bool $enabled): GraphQlPlaygroundAction |
| 52 | + { |
| 53 | + $twigProphecy = $this->prophesize(TwigEnvironment::class); |
| 54 | + $twigProphecy->render(Argument::cetera())->willReturn(''); |
| 55 | + $routerProphecy = $this->prophesize(RouterInterface::class); |
| 56 | + $routerProphecy->generate('api_graphql_entrypoint')->willReturn('/graphql'); |
| 57 | + |
| 58 | + return new GraphQlPlaygroundAction($twigProphecy->reveal(), $routerProphecy->reveal(), $enabled, ''); |
| 59 | + } |
| 60 | +} |
0 commit comments