Skip to content

Commit 24addda

Browse files
committed
Removing hidden dependency on the security-bundle
The Graphqlite bundle would fail to load of the security-bundle was not in the project (because of a hidden dependency on Symfony's UserInterface) This fixes the issue. Closes #72
1 parent b5fc672 commit 24addda

File tree

7 files changed

+117
-9
lines changed

7 files changed

+117
-9
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ script:
5454
#- ./vendor/bin/simple-phpunit $PHPUNIT_FLAGS
5555
- if [[ $PHPSTAN == true ]]; then composer phpstan; fi
5656
- ./vendor/bin/phpunit
57+
# Let's test without the security bundle
58+
- composer remove --dev symfony/security-bundle
59+
- phpunit Tests/NoSecurityBundleTest.php
5760

5861
after_script:
5962
- ./vendor/bin/php-coveralls -v

DependencyInjection/GraphqliteCompilerPass.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Symfony\Component\Cache\Adapter\ApcuAdapter;
1212
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
1313
use Symfony\Component\Cache\Psr16Cache;
14+
use TheCodingMachine\GraphQLite\Mappers\StaticClassListTypeMapper;
1415
use function class_exists;
1516
use Doctrine\Common\Annotations\AnnotationException;
1617
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
@@ -43,6 +44,7 @@
4344
use Symfony\Component\HttpFoundation\Session\SessionInterface;
4445
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
4546
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
47+
use Symfony\Component\Security\Core\User\UserInterface;
4648
use Symfony\Component\Security\Core\User\UserProviderInterface;
4749
use TheCodingMachine\CacheUtils\ClassBoundCache;
4850
use TheCodingMachine\CacheUtils\ClassBoundCacheContract;
@@ -76,6 +78,7 @@
7678
use TheCodingMachine\GraphQLite\Types\MutableObjectType;
7779
use TheCodingMachine\GraphQLite\Types\ResolvableInputObjectType;
7880
use function var_dump;
81+
use TheCodingMachine\Graphqlite\Bundle\Types\SymfonyUserInterfaceType;
7982

8083
/**
8184
* Detects controllers and types automatically and tag them.
@@ -192,6 +195,13 @@ public function process(ContainerBuilder $container): void
192195
$container->removeDefinition(AggregateControllerQueryProviderFactory::class);
193196
}
194197

198+
// Let's register the mapping with UserInterface if UserInterface is available.
199+
if (class_exists(UserInterface::class)) {
200+
$staticTypes = $container->getDefinition(StaticClassListTypeMapper::class)->getArgument(0);
201+
$staticTypes[] = SymfonyUserInterfaceType::class;
202+
$container->getDefinition(StaticClassListTypeMapper::class)->setArgument(0, $staticTypes);
203+
}
204+
195205
foreach ($container->getDefinitions() as $id => $definition) {
196206
if ($definition->isAbstract() || $definition->getClass() === null) {
197207
continue;

Resources/config/container/graphqlite.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@
107107

108108
<service id="TheCodingMachine\GraphQLite\Mappers\StaticClassListTypeMapperFactory">
109109
<argument type="collection">
110-
<argument>TheCodingMachine\Graphqlite\Bundle\Types\SymfonyUserInterfaceType</argument>
111110
</argument>
112111
<tag name="graphql.type_mapper_factory"/>
113112
</service>

Tests/Fixtures/config/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ services:
1717
resource: '../*'
1818
exclude: '../{Entities}'
1919

20+
TheCodingMachine\Graphqlite\Bundle\Tests\NoSecurityBundleFixtures\:
21+
resource: '../../NoSecurityBundleFixtures/*'
22+
2023
someService:
2124
class: stdClass
2225

Tests/GraphqliteTestingKernel.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Routing\RouteCollectionBuilder;
1717
use TheCodingMachine\Graphqlite\Bundle\GraphqliteBundle;
1818
use Symfony\Component\Security\Core\User\User;
19+
use function class_exists;
1920

2021
class GraphqliteTestingKernel extends Kernel
2122
{
@@ -50,8 +51,28 @@ class GraphqliteTestingKernel extends Kernel
5051
* @var int|null
5152
*/
5253
private $maximumQueryDepth;
54+
/**
55+
* @var array|string[]
56+
*/
57+
private $controllersNamespace;
58+
/**
59+
* @var array|string[]
60+
*/
61+
private $typesNamespace;
5362

54-
public function __construct(bool $enableSession = true, ?string $enableLogin = null, bool $enableSecurity = true, ?string $enableMe = null, bool $introspection = true, ?int $maximumQueryComplexity = null, ?int $maximumQueryDepth = null)
63+
/**
64+
* @param string[] $controllersNamespace
65+
* @param string[] $typesNamespace
66+
*/
67+
public function __construct(bool $enableSession = true,
68+
?string $enableLogin = null,
69+
bool $enableSecurity = true,
70+
?string $enableMe = null,
71+
bool $introspection = true,
72+
?int $maximumQueryComplexity = null,
73+
?int $maximumQueryDepth = null,
74+
array $controllersNamespace = ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Controller\\'],
75+
array $typesNamespace = ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Types\\', 'TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Entities\\'])
5576
{
5677
parent::__construct('test', true);
5778
$this->enableSession = $enableSession;
@@ -61,15 +82,18 @@ public function __construct(bool $enableSession = true, ?string $enableLogin = n
6182
$this->introspection = $introspection;
6283
$this->maximumQueryComplexity = $maximumQueryComplexity;
6384
$this->maximumQueryDepth = $maximumQueryDepth;
85+
$this->controllersNamespace = $controllersNamespace;
86+
$this->typesNamespace = $typesNamespace;
6487
}
6588

6689
public function registerBundles()
6790
{
68-
return [
69-
new FrameworkBundle(),
70-
new SecurityBundle(),
71-
new GraphqliteBundle(),
72-
];
91+
$bundles = [ new FrameworkBundle() ];
92+
if (class_exists(SecurityBundle::class)) {
93+
$bundles[] = new SecurityBundle();
94+
}
95+
$bundles[] = new GraphqliteBundle();
96+
return $bundles;
7397
}
7498

7599
public function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
@@ -133,8 +157,8 @@ public function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
133157

134158
$graphqliteConf = array(
135159
'namespace' => [
136-
'controllers' => ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Controller\\'],
137-
'types' => ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Types\\', 'TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Entities\\']
160+
'controllers' => $this->controllersNamespace,
161+
'types' => $this->typesNamespace
138162
],
139163
);
140164

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\Graphqlite\Bundle\Tests\NoSecurityBundleFixtures\Controller;
5+
6+
7+
use TheCodingMachine\GraphQLite\Annotations\Query;
8+
9+
class EchoController
10+
{
11+
/**
12+
* @Query()
13+
*/
14+
public function echoMsg(string $message): string {
15+
return $message;
16+
}
17+
}

Tests/NoSecurityBundleTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace TheCodingMachine\Graphqlite\Bundle\Tests;
4+
5+
use function json_decode;
6+
use PHPUnit\Framework\TestCase;
7+
use Psr\Container\ContainerInterface;
8+
use function spl_object_hash;
9+
use Symfony\Component\HttpFoundation\Cookie;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\Session\Session;
12+
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
13+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
14+
use Symfony\Component\Security\Core\User\User;
15+
use TheCodingMachine\Graphqlite\Bundle\Controller\GraphqliteController;
16+
use TheCodingMachine\Graphqlite\Bundle\Security\AuthenticationService;
17+
use TheCodingMachine\GraphQLite\GraphQLRuntimeException as GraphQLException;
18+
use TheCodingMachine\GraphQLite\Schema;
19+
use function var_dump;
20+
21+
/**
22+
* This test class is supposed to work even if the security bundle is not installed in the project.
23+
* It is here to check we don't have hidden dependencies on this bundle and that it remains optional.
24+
*/
25+
class NoSecurityBundleTest extends TestCase
26+
{
27+
public function testServiceWiring(): void
28+
{
29+
$kernel = new GraphqliteTestingKernel(true, null, false, null, true, null, null, ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\NoSecurityBundleFixtures\\Controller\\']);
30+
$kernel->boot();
31+
$container = $kernel->getContainer();
32+
33+
$schema = $container->get(Schema::class);
34+
$this->assertInstanceOf(Schema::class, $schema);
35+
$schema->assertValid();
36+
37+
$request = Request::create('/graphql', 'GET', ['query' => '
38+
{
39+
echoMsg(message: "Hello world")
40+
}']);
41+
42+
$response = $kernel->handle($request);
43+
44+
$result = json_decode($response->getContent(), true);
45+
46+
$this->assertSame([
47+
'data' => [
48+
'echoMsg' => 'Hello world'
49+
]
50+
], $result);
51+
}
52+
}

0 commit comments

Comments
 (0)