|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | +namespace Rebing\GraphQL\Tests\Unit; |
| 5 | + |
| 6 | +use Illuminate\Routing\Route; |
| 7 | +use Illuminate\Support\Collection; |
| 8 | +use Rebing\GraphQL\Tests\Support\Objects\ExampleMiddleware; |
| 9 | +use Rebing\GraphQL\Tests\TestCase; |
| 10 | + |
| 11 | +class RouteWithSchemaTest extends TestCase |
| 12 | +{ |
| 13 | + protected function getEnvironmentSetUp($app): void |
| 14 | + { |
| 15 | + // Laravel registers routes for local filesystems by default. |
| 16 | + // However, for the purpose of this test, we don't want it to register any routes. |
| 17 | + $app['config']->set('filesystems.disks.local.serve', false); |
| 18 | + |
| 19 | + $app['config']->set('graphql', [ |
| 20 | + 'route' => [ |
| 21 | + 'prefix' => 'graphql_test', |
| 22 | + ], |
| 23 | + |
| 24 | + 'schemas' => [ |
| 25 | + 'default' => [ |
| 26 | + 'middleware' => [ExampleMiddleware::class], |
| 27 | + ], |
| 28 | + 'with_route_attributes' => [ |
| 29 | + 'middleware' => [ExampleMiddleware::class], |
| 30 | + 'route_attributes' => [ |
| 31 | + 'domain' => 'api.example.com', |
| 32 | + ], |
| 33 | + ], |
| 34 | + ], |
| 35 | + ]); |
| 36 | + } |
| 37 | + |
| 38 | + public function testRoutesWithSchemaAttributes(): void |
| 39 | + { |
| 40 | + $expected = [ |
| 41 | + 'graphql' => [ |
| 42 | + 'methods' => [ |
| 43 | + 'GET', |
| 44 | + 'POST', |
| 45 | + 'HEAD', |
| 46 | + ], |
| 47 | + 'uri' => 'graphql_test', |
| 48 | + 'middleware' => [ |
| 49 | + ExampleMiddleware::class, |
| 50 | + ], |
| 51 | + 'domain' => null, |
| 52 | + 'action_middleware' => [ExampleMiddleware::class], |
| 53 | + 'action_excluded_middleware' => [], |
| 54 | + ], |
| 55 | + 'graphql.default' => [ |
| 56 | + 'methods' => [ |
| 57 | + 'GET', |
| 58 | + 'POST', |
| 59 | + 'HEAD', |
| 60 | + ], |
| 61 | + 'uri' => 'graphql_test/default', |
| 62 | + 'middleware' => [ |
| 63 | + ExampleMiddleware::class, |
| 64 | + ], |
| 65 | + 'domain' => null, |
| 66 | + 'action_middleware' => [ExampleMiddleware::class], |
| 67 | + 'action_excluded_middleware' => [], |
| 68 | + ], |
| 69 | + 'graphql.with_route_attributes' => [ |
| 70 | + 'methods' => [ |
| 71 | + 'GET', |
| 72 | + 'POST', |
| 73 | + 'HEAD', |
| 74 | + ], |
| 75 | + 'uri' => 'graphql_test/with_route_attributes', |
| 76 | + 'middleware' => [ |
| 77 | + ExampleMiddleware::class, |
| 78 | + ], |
| 79 | + 'domain' => 'api.example.com', |
| 80 | + 'action_middleware' => [ExampleMiddleware::class], |
| 81 | + 'action_excluded_middleware' => [], |
| 82 | + ], |
| 83 | + ]; |
| 84 | + |
| 85 | + $actual = Collection::make( |
| 86 | + app('router')->getRoutes()->getRoutesByName() |
| 87 | + )->map(function (Route $route) { |
| 88 | + $action = $route->getAction(); |
| 89 | + |
| 90 | + return [ |
| 91 | + 'methods' => $route->methods(), |
| 92 | + 'uri' => $route->uri(), |
| 93 | + 'middleware' => $route->middleware(), |
| 94 | + 'domain' => $route->getDomain(), |
| 95 | + 'action_middleware' => $action['middleware'] ?? [], |
| 96 | + 'action_excluded_middleware' => $action['excluded_middleware'] ?? [], |
| 97 | + ]; |
| 98 | + })->all(); |
| 99 | + |
| 100 | + self::assertEquals($expected, $actual); |
| 101 | + } |
| 102 | +} |
0 commit comments