Skip to content

Commit 819fbd3

Browse files
committed
Check for Symfony version for security extends
1 parent 302bbee commit 819fbd3

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

src/Security/Security.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,42 @@
55
namespace Overblog\GraphQLBundle\Security;
66

77
use LogicException;
8+
use Symfony\Component\HttpKernel\Kernel;
89
use Symfony\Component\Security\Core\Security as CoreSecurity;
910
use Symfony\Component\Security\Core\User\UserInterface;
11+
use Symfony\Bundle\SecurityBundle\Security as BundleSecurity;
1012

1113
use function array_reduce;
1214

13-
final class Security
15+
if (Kernel::VERSION_ID >= 60200) {
16+
final class Security extends BaseSecurity
17+
{
18+
public function __construct(?BundleSecurity $security)
19+
{
20+
parent::__construct($security);
21+
}
22+
}
23+
} else {
24+
final class Security extends BaseSecurity
25+
{
26+
public function __construct(?CoreSecurity $security)
27+
{
28+
parent::__construct($security);
29+
}
30+
}
31+
}
32+
33+
abstract class BaseSecurity
1434
{
1535
/**
16-
* @var CoreSecurity
36+
* @var CoreSecurity|BundleSecurity
1737
*/
1838
private $coreSecurity;
1939

20-
public function __construct(?CoreSecurity $security)
40+
public function __construct($security)
2141
{
2242
// @phpstan-ignore-next-line
23-
$this->coreSecurity = $security ?? new class() {
43+
$this->coreSecurity = $security ?? new class () {
2444
public function isGranted(): bool
2545
{
2646
throw new LogicException('The "symfony/security-core" component is required.');

tests/ExpressionLanguage/ExpressionFunction/Security/GetUserTest.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1212
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1313
use Symfony\Component\Security\Core\Security as CoreSecurity;
14+
use Symfony\Bundle\SecurityBundle\Security as BundleSecurity;
15+
use Symfony\Component\HttpKernel\Kernel;
1416
use Symfony\Component\Security\Core\User\InMemoryUser;
1517
use Symfony\Component\Security\Core\User\User;
1618
use Symfony\Component\Security\Core\User\UserInterface;
@@ -22,14 +24,32 @@ protected function getFunctions()
2224
return [new GetUser()];
2325
}
2426

27+
protected function getMockedSecurity()
28+
{
29+
if (Kernel::VERSION_ID >= 60200) {
30+
return $this->createMock(BundleSecurity::class);
31+
} else {
32+
return $this->createMock(CoreSecurity::class);
33+
}
34+
}
35+
36+
protected function getSecurityWithStorage(TokenStorageInterface $storage)
37+
{
38+
if (Kernel::VERSION_ID >= 60200) {
39+
return new BundleSecurity($this->getDIContainerMock(['security.token_storage' => $storage]));
40+
} else {
41+
return new CoreSecurity($this->getDIContainerMock(['security.token_storage' => $storage]));
42+
}
43+
}
44+
2545
public function testEvaluator(): void
2646
{
2747
if (class_exists(InMemoryUser::class)) {
2848
$testUser = new InMemoryUser('testUser', 'testPassword');
2949
} else {
3050
$testUser = new User('testUser', 'testPassword');
3151
}
32-
$coreSecurity = $this->createMock(CoreSecurity::class);
52+
$coreSecurity = $this->getMockedSecurity();
3353
$coreSecurity->method('getUser')->willReturn($testUser);
3454
$services = $this->createGraphQLServices(['security' => new Security($coreSecurity)]);
3555

@@ -40,7 +60,7 @@ public function testEvaluator(): void
4060
public function testGetUserNoTokenStorage(): void
4161
{
4262
${TypeGenerator::GRAPHQL_SERVICES} = $this->createGraphQLServices(
43-
['security' => new Security($this->createMock(CoreSecurity::class))]
63+
['security' => new Security($this->getMockedSecurity())]
4464
);
4565
${TypeGenerator::GRAPHQL_SERVICES}->get('security');
4666
$this->assertNull(eval($this->getCompileCode()));
@@ -51,11 +71,7 @@ public function testGetUserNoToken(): void
5171
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
5272
${TypeGenerator::GRAPHQL_SERVICES} = $this->createGraphQLServices(
5373
[
54-
'security' => new Security(
55-
new CoreSecurity(
56-
$this->getDIContainerMock(['security.token_storage' => $tokenStorage])
57-
)
58-
),
74+
'security' => new Security($this->getSecurityWithStorage($tokenStorage)),
5975
]
6076
);
6177
${TypeGenerator::GRAPHQL_SERVICES}->get('security');
@@ -77,11 +93,7 @@ public function testGetUser($user, $expectedUser): void
7793

7894
${TypeGenerator::GRAPHQL_SERVICES} = $this->createGraphQLServices(
7995
[
80-
'security' => new Security(
81-
new CoreSecurity(
82-
$this->getDIContainerMock(['security.token_storage' => $tokenStorage])
83-
)
84-
),
96+
'security' => new Security($this->getSecurityWithStorage($tokenStorage)),
8597
]
8698
);
8799
${TypeGenerator::GRAPHQL_SERVICES}->get('security');

tests/ExpressionLanguage/TestCase.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Symfony\Component\ExpressionLanguage\Expression;
2020
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
2121
use Symfony\Component\Security\Core\Security as CoreSecurity;
22+
use Symfony\Bundle\SecurityBundle\Security as BundleSecurity;
23+
use Symfony\Component\HttpKernel\Kernel;
2224

2325
use function array_keys;
2426
use function call_user_func_array;
@@ -102,10 +104,17 @@ protected function getSecurityIsGrantedWithExpectation($with, $expects = null, $
102104
*/
103105
private function getCoreSecurityMock()
104106
{
105-
return $this->getMockBuilder(CoreSecurity::class)
106-
->disableOriginalConstructor()
107-
->setMethods(['isGranted'])
108-
->getMock();
107+
if (Kernel::VERSION_ID >= 60200) {
108+
return $this->getMockBuilder(BundleSecurity::class)
109+
->disableOriginalConstructor()
110+
->setMethods(['isGranted'])
111+
->getMock();
112+
} else {
113+
return $this->getMockBuilder(CoreSecurity::class)
114+
->disableOriginalConstructor()
115+
->setMethods(['isGranted'])
116+
->getMock();
117+
}
109118
}
110119

111120
protected function createGraphQLServices(array $services = []): GraphQLServices

0 commit comments

Comments
 (0)