1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Tests\ExpressionLanguage; |
4
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\Definition\GlobalVariables; |
6
|
|
|
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage; |
7
|
|
|
use Overblog\GraphQLBundle\Tests\DIContainerMockTrait; |
8
|
|
|
use PHPUnit\Framework\TestCase as BaseTestCase; |
9
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunction; |
10
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
11
|
|
|
|
12
|
|
|
abstract class TestCase extends BaseTestCase |
13
|
|
|
{ |
14
|
|
|
use DIContainerMockTrait; |
15
|
|
|
|
16
|
|
|
/** @var ExpressionLanguage */ |
17
|
|
|
protected $expressionLanguage; |
18
|
|
|
|
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->expressionLanguage = new ExpressionLanguage(); |
22
|
|
|
foreach ($this->getFunctions() as $function) { |
23
|
|
|
$this->expressionLanguage->addFunction($function); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return ExpressionFunction[] |
29
|
|
|
*/ |
30
|
|
|
abstract protected function getFunctions(); |
31
|
|
|
|
32
|
|
|
protected function assertExpressionCompile($expression, $with, array $vars = [], $expects = null, $return = true, $assertMethod = 'assertTrue') |
33
|
|
|
{ |
34
|
|
|
$code = $this->expressionLanguage->compile($expression, array_keys($vars)); |
35
|
|
|
$globalVariable = new GlobalVariables([ |
36
|
|
|
'container' => $this->getDIContainerMock( |
37
|
|
|
['security.authorization_checker' => $this->getAuthorizationCheckerIsGrantedWithExpectation($with, $expects, $return)] |
38
|
|
|
), |
39
|
|
|
]); |
40
|
|
|
$globalVariable->get('container'); |
41
|
|
|
extract($vars); |
42
|
|
|
|
43
|
|
|
$this->$assertMethod(eval('return '.$code.';')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function getAuthorizationCheckerIsGrantedWithExpectation($with, $expects = null, $return = true) |
47
|
|
|
{ |
48
|
|
|
if (null === $expects) { |
49
|
|
|
$expects = $this->once(); |
50
|
|
|
} |
51
|
|
|
$authChecker = $this->getAuthorizationCheckerMock(); |
52
|
|
|
|
53
|
|
|
if ($return instanceof \PHPUnit_Framework_MockObject_Stub_Return) { |
54
|
|
|
$returnValue = $return; |
55
|
|
|
} else { |
56
|
|
|
$returnValue = $this->returnValue($return); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$methodExpectation = $authChecker |
60
|
|
|
->expects($expects) |
61
|
|
|
->method('isGranted'); |
62
|
|
|
|
63
|
|
|
call_user_func_array([$methodExpectation, 'with'], is_array($with) ? $with : [$with]); |
64
|
|
|
|
65
|
|
|
$methodExpectation->will($returnValue); |
66
|
|
|
|
67
|
|
|
return $authChecker; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function getAuthorizationCheckerMock() |
71
|
|
|
{ |
72
|
|
|
$AuthorizationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class) |
73
|
|
|
->disableOriginalConstructor() |
74
|
|
|
->setMethods(['isGranted']) |
75
|
|
|
->getMock() |
76
|
|
|
; |
77
|
|
|
|
78
|
|
|
return $AuthorizationChecker; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|