diff --git a/.travis.yml b/.travis.yml index f0761d6..f78c285 100644 --- a/.travis.yml +++ b/.travis.yml @@ -54,6 +54,9 @@ script: #- ./vendor/bin/simple-phpunit $PHPUNIT_FLAGS - if [[ $PHPSTAN == true ]]; then composer phpstan; fi - ./vendor/bin/phpunit + # Let's test without the security bundle + - composer remove --dev symfony/security-bundle + - phpunit Tests/NoSecurityBundleTest.php after_script: - ./vendor/bin/php-coveralls -v diff --git a/DependencyInjection/GraphqliteCompilerPass.php b/DependencyInjection/GraphqliteCompilerPass.php index 3dad39b..9a377c4 100644 --- a/DependencyInjection/GraphqliteCompilerPass.php +++ b/DependencyInjection/GraphqliteCompilerPass.php @@ -11,6 +11,7 @@ use Symfony\Component\Cache\Adapter\ApcuAdapter; use Symfony\Component\Cache\Adapter\PhpFilesAdapter; use Symfony\Component\Cache\Psr16Cache; +use TheCodingMachine\GraphQLite\Mappers\StaticClassListTypeMapper; use function class_exists; use Doctrine\Common\Annotations\AnnotationException; use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader; @@ -43,6 +44,7 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; +use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserProviderInterface; use TheCodingMachine\CacheUtils\ClassBoundCache; use TheCodingMachine\CacheUtils\ClassBoundCacheContract; @@ -76,6 +78,7 @@ use TheCodingMachine\GraphQLite\Types\MutableObjectType; use TheCodingMachine\GraphQLite\Types\ResolvableInputObjectType; use function var_dump; +use TheCodingMachine\Graphqlite\Bundle\Types\SymfonyUserInterfaceType; /** * Detects controllers and types automatically and tag them. @@ -192,6 +195,13 @@ public function process(ContainerBuilder $container): void $container->removeDefinition(AggregateControllerQueryProviderFactory::class); } + // Let's register the mapping with UserInterface if UserInterface is available. + if (class_exists(UserInterface::class)) { + $staticTypes = $container->getDefinition(StaticClassListTypeMapper::class)->getArgument(0); + $staticTypes[] = SymfonyUserInterfaceType::class; + $container->getDefinition(StaticClassListTypeMapper::class)->setArgument(0, $staticTypes); + } + foreach ($container->getDefinitions() as $id => $definition) { if ($definition->isAbstract() || $definition->getClass() === null) { continue; diff --git a/Resources/config/container/graphqlite.xml b/Resources/config/container/graphqlite.xml index b0afd18..2f4e40f 100644 --- a/Resources/config/container/graphqlite.xml +++ b/Resources/config/container/graphqlite.xml @@ -107,7 +107,6 @@ - TheCodingMachine\Graphqlite\Bundle\Types\SymfonyUserInterfaceType diff --git a/Tests/Fixtures/config/services.yaml b/Tests/Fixtures/config/services.yaml index 030ad34..7f050b7 100644 --- a/Tests/Fixtures/config/services.yaml +++ b/Tests/Fixtures/config/services.yaml @@ -17,6 +17,9 @@ services: resource: '../*' exclude: '../{Entities}' + TheCodingMachine\Graphqlite\Bundle\Tests\NoSecurityBundleFixtures\: + resource: '../../NoSecurityBundleFixtures/*' + someService: class: stdClass diff --git a/Tests/GraphqliteTestingKernel.php b/Tests/GraphqliteTestingKernel.php index 97b7ff1..dae16ea 100644 --- a/Tests/GraphqliteTestingKernel.php +++ b/Tests/GraphqliteTestingKernel.php @@ -16,6 +16,7 @@ use Symfony\Component\Routing\RouteCollectionBuilder; use TheCodingMachine\Graphqlite\Bundle\GraphqliteBundle; use Symfony\Component\Security\Core\User\User; +use function class_exists; class GraphqliteTestingKernel extends Kernel { @@ -50,8 +51,28 @@ class GraphqliteTestingKernel extends Kernel * @var int|null */ private $maximumQueryDepth; + /** + * @var array|string[] + */ + private $controllersNamespace; + /** + * @var array|string[] + */ + private $typesNamespace; - public function __construct(bool $enableSession = true, ?string $enableLogin = null, bool $enableSecurity = true, ?string $enableMe = null, bool $introspection = true, ?int $maximumQueryComplexity = null, ?int $maximumQueryDepth = null) + /** + * @param string[] $controllersNamespace + * @param string[] $typesNamespace + */ + public function __construct(bool $enableSession = true, + ?string $enableLogin = null, + bool $enableSecurity = true, + ?string $enableMe = null, + bool $introspection = true, + ?int $maximumQueryComplexity = null, + ?int $maximumQueryDepth = null, + array $controllersNamespace = ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Controller\\'], + array $typesNamespace = ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Types\\', 'TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Entities\\']) { parent::__construct('test', true); $this->enableSession = $enableSession; @@ -61,15 +82,18 @@ public function __construct(bool $enableSession = true, ?string $enableLogin = n $this->introspection = $introspection; $this->maximumQueryComplexity = $maximumQueryComplexity; $this->maximumQueryDepth = $maximumQueryDepth; + $this->controllersNamespace = $controllersNamespace; + $this->typesNamespace = $typesNamespace; } public function registerBundles() { - return [ - new FrameworkBundle(), - new SecurityBundle(), - new GraphqliteBundle(), - ]; + $bundles = [ new FrameworkBundle() ]; + if (class_exists(SecurityBundle::class)) { + $bundles[] = new SecurityBundle(); + } + $bundles[] = new GraphqliteBundle(); + return $bundles; } public function configureContainer(ContainerBuilder $c, LoaderInterface $loader) @@ -133,8 +157,8 @@ public function configureContainer(ContainerBuilder $c, LoaderInterface $loader) $graphqliteConf = array( 'namespace' => [ - 'controllers' => ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Controller\\'], - 'types' => ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Types\\', 'TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Entities\\'] + 'controllers' => $this->controllersNamespace, + 'types' => $this->typesNamespace ], ); diff --git a/Tests/NoSecurityBundleFixtures/Controller/EchoController.php b/Tests/NoSecurityBundleFixtures/Controller/EchoController.php new file mode 100644 index 0000000..a8e5d3d --- /dev/null +++ b/Tests/NoSecurityBundleFixtures/Controller/EchoController.php @@ -0,0 +1,17 @@ +boot(); + $container = $kernel->getContainer(); + + $schema = $container->get(Schema::class); + $this->assertInstanceOf(Schema::class, $schema); + $schema->assertValid(); + + $request = Request::create('/graphql', 'GET', ['query' => ' + { + echoMsg(message: "Hello world") + }']); + + $response = $kernel->handle($request); + + $result = json_decode($response->getContent(), true); + + $this->assertSame([ + 'data' => [ + 'echoMsg' => 'Hello world' + ] + ], $result); + } +}