-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathTestCase.php
136 lines (116 loc) · 4.36 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
declare(strict_types=1);
namespace Overblog\GraphQLBundle\Tests\ExpressionLanguage;
use Overblog\GraphQLBundle\Definition\GraphQLServices;
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage;
use Overblog\GraphQLBundle\Generator\TypeGenerator;
use Overblog\GraphQLBundle\Resolver\MutationResolver;
use Overblog\GraphQLBundle\Resolver\TypeResolver;
use Overblog\GraphQLBundle\Security\Security;
use Overblog\GraphQLBundle\Tests\DIContainerMockTrait;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Rule\AnyInvokedCount;
use PHPUnit\Framework\MockObject\Rule\InvokedCount;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase as BaseTestCase;
use Symfony\Bundle\SecurityBundle\Security as BundleSecurity;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Security\Core\Security as CoreSecurity;
use function array_keys;
use function call_user_func_array;
use function extract;
use function is_array;
abstract class TestCase extends BaseTestCase
{
use DIContainerMockTrait;
/** @var ExpressionLanguage */
protected $expressionLanguage;
public function setUp(): void
{
$this->expressionLanguage = new ExpressionLanguage();
foreach ($this->getFunctions() as $function) {
$this->expressionLanguage->addFunction($function);
}
}
/**
* @return ExpressionFunction[]
*/
abstract protected function getFunctions();
/**
* @param Expression|string $expression
* @param mixed $with
*/
protected function assertExpressionCompile(
$expression,
$with,
array $vars = [],
?InvokedCount $expects = null,
bool $return = true,
string $assertMethod = 'assertTrue'
): void {
$code = $this->expressionLanguage->compile($expression, array_keys($vars));
${TypeGenerator::GRAPHQL_SERVICES} = $this->createGraphQLServices([
'security' => $this->getSecurityIsGrantedWithExpectation($with, $expects, $return),
]);
${TypeGenerator::GRAPHQL_SERVICES}->get('security');
extract($vars);
$this->$assertMethod(eval('return '.$code.';'));
}
/**
* @param mixed $with
* @param InvokedCount|AnyInvokedCount|null $expects
* @param mixed $return
*/
protected function getSecurityIsGrantedWithExpectation($with, $expects = null, $return = true): Security
{
if (null === $expects) {
$expects = $this->once();
}
$security = $this->getCoreSecurityMock();
if ($return instanceof Stub) {
$returnValue = $return;
} else {
$returnValue = $this->returnValue($return);
}
// @phpstan-ignore-next-line
$methodExpectation = $security
->expects($expects)
->method('isGranted');
call_user_func_array([$methodExpectation, 'with'], is_array($with) ? $with : [$with]);
// @phpstan-ignore-next-line
$methodExpectation->will($returnValue);
// @phpstan-ignore-next-line
return new Security($security);
}
/**
* @return CoreSecurity|BundleSecurity|MockObject
*/
private function getCoreSecurityMock()
{
if (Kernel::VERSION_ID >= 60200) {
return $this->getMockBuilder(BundleSecurity::class)
->disableOriginalConstructor()
->setMethods(['isGranted'])
->getMock();
} else {
return $this->getMockBuilder(CoreSecurity::class)
->disableOriginalConstructor()
->setMethods(['isGranted'])
->getMock();
}
}
protected function createGraphQLServices(array $services = []): GraphQLServices
{
$locateableServices = [
'typeResolver' => fn () => $this->createMock(TypeResolver::class),
'queryResolver' => fn () => $this->createMock(TypeResolver::class),
'mutationResolver' => fn () => $$this->createMock(MutationResolver::class),
];
foreach ($services as $id => $service) {
$locateableServices[$id] = fn () => $service;
}
return new GraphQLServices($locateableServices);
}
}