Skip to content

Commit a50e1a8

Browse files
authored
Merge pull request #73 from moufmouf/without_security_bundle
Removing hidden dependency on the security-bundle
2 parents b5fc672 + 00a6d94 commit a50e1a8

File tree

9 files changed

+132
-16
lines changed

9 files changed

+132
-16
lines changed

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ matrix:
1717
- php: 7.2
1818
env: PHPSTAN=true
1919
- php: 7.4
20-
env: COVERAGE=true PHPUNIT_FLAGS="-v --coverage-text"
20+
env: COVERAGE=true PHPUNIT_FLAGS="-v --coverage-text" TESTNOSECURITYBUNDLE=true
2121

2222
# Test LTS versions.
2323
- php: 7.4
@@ -53,7 +53,9 @@ script:
5353
# it helps with testing legacy code and deprecations (composer require symfony/phpunit-bridge)
5454
#- ./vendor/bin/simple-phpunit $PHPUNIT_FLAGS
5555
- if [[ $PHPSTAN == true ]]; then composer phpstan; fi
56-
- ./vendor/bin/phpunit
56+
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
57+
# Let's test without the security bundle
58+
- if [[ $TESTNOSECURITYBUNDLE == true ]]; then composer remove --dev symfony/security-bundle && phpunit Tests/NoSecurityBundleTest.php; fi
5759

5860
after_script:
5961
- ./vendor/bin/php-coveralls -v

DependencyInjection/GraphqliteCompilerPass.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
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;
15+
use TheCodingMachine\GraphQLite\Mappers\StaticClassListTypeMapperFactory;
1416
use function class_exists;
1517
use Doctrine\Common\Annotations\AnnotationException;
1618
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
@@ -43,6 +45,7 @@
4345
use Symfony\Component\HttpFoundation\Session\SessionInterface;
4446
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
4547
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
48+
use Symfony\Component\Security\Core\User\UserInterface;
4649
use Symfony\Component\Security\Core\User\UserProviderInterface;
4750
use TheCodingMachine\CacheUtils\ClassBoundCache;
4851
use TheCodingMachine\CacheUtils\ClassBoundCacheContract;
@@ -76,6 +79,7 @@
7679
use TheCodingMachine\GraphQLite\Types\MutableObjectType;
7780
use TheCodingMachine\GraphQLite\Types\ResolvableInputObjectType;
7881
use function var_dump;
82+
use TheCodingMachine\Graphqlite\Bundle\Types\SymfonyUserInterfaceType;
7983

8084
/**
8185
* Detects controllers and types automatically and tag them.
@@ -192,6 +196,13 @@ public function process(ContainerBuilder $container): void
192196
$container->removeDefinition(AggregateControllerQueryProviderFactory::class);
193197
}
194198

199+
// Let's register the mapping with UserInterface if UserInterface is available.
200+
if (interface_exists(UserInterface::class)) {
201+
$staticTypes = $container->getDefinition(StaticClassListTypeMapperFactory::class)->getArgument(0);
202+
$staticTypes[] = SymfonyUserInterfaceType::class;
203+
$container->getDefinition(StaticClassListTypeMapperFactory::class)->setArgument(0, $staticTypes);
204+
}
205+
195206
foreach ($container->getDefinitions() as $id => $definition) {
196207
if ($definition->isAbstract() || $definition->getClass() === null) {
197208
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: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
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;
20+
use function serialize;
1921

2022
class GraphqliteTestingKernel extends Kernel
2123
{
@@ -50,8 +52,28 @@ class GraphqliteTestingKernel extends Kernel
5052
* @var int|null
5153
*/
5254
private $maximumQueryDepth;
55+
/**
56+
* @var array|string[]
57+
*/
58+
private $controllersNamespace;
59+
/**
60+
* @var array|string[]
61+
*/
62+
private $typesNamespace;
5363

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)
64+
/**
65+
* @param string[] $controllersNamespace
66+
* @param string[] $typesNamespace
67+
*/
68+
public function __construct(bool $enableSession = true,
69+
?string $enableLogin = null,
70+
bool $enableSecurity = true,
71+
?string $enableMe = null,
72+
bool $introspection = true,
73+
?int $maximumQueryComplexity = null,
74+
?int $maximumQueryDepth = null,
75+
array $controllersNamespace = ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Controller\\'],
76+
array $typesNamespace = ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Types\\', 'TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Entities\\'])
5577
{
5678
parent::__construct('test', true);
5779
$this->enableSession = $enableSession;
@@ -61,15 +83,18 @@ public function __construct(bool $enableSession = true, ?string $enableLogin = n
6183
$this->introspection = $introspection;
6284
$this->maximumQueryComplexity = $maximumQueryComplexity;
6385
$this->maximumQueryDepth = $maximumQueryDepth;
86+
$this->controllersNamespace = $controllersNamespace;
87+
$this->typesNamespace = $typesNamespace;
6488
}
6589

6690
public function registerBundles()
6791
{
68-
return [
69-
new FrameworkBundle(),
70-
new SecurityBundle(),
71-
new GraphqliteBundle(),
72-
];
92+
$bundles = [ new FrameworkBundle() ];
93+
if (class_exists(SecurityBundle::class)) {
94+
$bundles[] = new SecurityBundle();
95+
}
96+
$bundles[] = new GraphqliteBundle();
97+
return $bundles;
7398
}
7499

75100
public function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
@@ -133,8 +158,8 @@ public function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
133158

134159
$graphqliteConf = array(
135160
'namespace' => [
136-
'controllers' => ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Controller\\'],
137-
'types' => ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Types\\', 'TheCodingMachine\\Graphqlite\\Bundle\\Tests\\Fixtures\\Entities\\']
161+
'controllers' => $this->controllersNamespace,
162+
'types' => $this->typesNamespace
138163
],
139164
);
140165

@@ -176,6 +201,6 @@ protected function configureRoutes(/*RoutingConfigurator*/ $routes)
176201

177202
public function getCacheDir()
178203
{
179-
return __DIR__.'/../cache/'.($this->enableSession?'withSession':'withoutSession').$this->enableLogin.($this->enableSecurity?'withSecurity':'withoutSecurity').$this->enableMe.'_'.($this->introspection?'withIntrospection':'withoutIntrospection').'_'.$this->maximumQueryComplexity.'_'.$this->maximumQueryDepth;
204+
return __DIR__.'/../cache/'.($this->enableSession?'withSession':'withoutSession').$this->enableLogin.($this->enableSecurity?'withSecurity':'withoutSecurity').$this->enableMe.'_'.($this->introspection?'withIntrospection':'withoutIntrospection').'_'.$this->maximumQueryComplexity.'_'.$this->maximumQueryDepth.'_'.md5(serialize($this->controllersNamespace).'_'.md5(serialize($this->typesNamespace)));
180205
}
181206
}
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+
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
"require-dev": {
3434
"symfony/security-bundle": "^4.1.9 | ^5",
3535
"symfony/yaml": "^4.1.9 | ^5",
36-
"phpunit/phpunit": "^7.5.1",
36+
"phpunit/phpunit": "^8.5.5",
3737
"phpstan/phpstan": "^0.12.25",
3838
"beberlei/porpaginas": "^1.2",
3939
"php-coveralls/php-coveralls": "^2.1.0",
40-
"symfony/phpunit-bridge": "^5.1"
40+
"symfony/phpunit-bridge": "^4.1.9 | ^5.1"
4141
},
4242
"scripts": {
4343
"phpstan": "phpstan analyse GraphqliteBundle.php DependencyInjection/ Controller/ Resources/ Security/ -c phpstan.neon --level=7 --no-progress"

phpunit.xml.dist

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,17 @@
3131
</filter>
3232
<logging>
3333
<log type="coverage-html" target="build/coverage"/>
34-
<log type="coverage-clover" target="build/logs/clover.xml"/>
3534
</logging>
3635

3736
<listeners>
38-
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
37+
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
38+
<arguments>
39+
<array>
40+
<!-- set this option to 0 to disable the DebugClassLoader integration -->
41+
<!-- See https://github.com/sebastianbergmann/phpunit/issues/4002 -->
42+
<element key="debug-class-loader"><integer>0</integer></element>
43+
</array>
44+
</arguments>
45+
</listener>
3946
</listeners>
4047
</phpunit>

0 commit comments

Comments
 (0)