|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Tests\ExpressionLanguage; |
|
4
|
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage; |
|
6
|
|
|
use Overblog\GraphQLBundle\Tests\DIContainerMockTrait; |
|
7
|
|
|
use PHPUnit\Framework\TestCase as BaseTestCase; |
|
8
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunction; |
|
9
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
10
|
|
|
|
|
11
|
|
|
abstract class TestCase extends BaseTestCase |
|
12
|
|
|
{ |
|
13
|
|
|
use DIContainerMockTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** @var ExpressionLanguage */ |
|
16
|
|
|
protected $expressionLanguage; |
|
17
|
|
|
|
|
18
|
|
|
public function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->expressionLanguage = new ExpressionLanguage(); |
|
21
|
|
|
$container = $this->getDIContainerMock(); |
|
22
|
|
|
$this->expressionLanguage->setContainer($container); |
|
23
|
|
|
foreach ($this->getFunctions() as $function) { |
|
24
|
|
|
$this->expressionLanguage->addFunction($function); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return ExpressionFunction[] |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract protected function getFunctions(); |
|
32
|
|
|
|
|
33
|
|
|
protected function assertExpressionCompile($expression, $with, array $expressionValues = [], $expects = null, $return = true, $assertMethod = 'assertTrue') |
|
34
|
|
|
{ |
|
35
|
|
|
// container var is needed by eval |
|
36
|
|
|
$container = $this->getDIContainerMock(['security.authorization_checker' => $this->getAuthorizationCheckerIsGrantedWithExpectation($with, $expects, $return)]); |
|
|
|
|
|
|
37
|
|
|
extract($expressionValues); |
|
38
|
|
|
|
|
39
|
|
|
$code = $this->expressionLanguage->compile($expression, array_keys($expressionValues)); |
|
40
|
|
|
|
|
41
|
|
|
$this->$assertMethod(eval('return '.$code.';')); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function getAuthorizationCheckerIsGrantedWithExpectation($with, $expects = null, $return = true) |
|
45
|
|
|
{ |
|
46
|
|
|
if (null === $expects) { |
|
47
|
|
|
$expects = $this->once(); |
|
48
|
|
|
} |
|
49
|
|
|
$authChecker = $this->getAuthorizationCheckerMock(); |
|
50
|
|
|
|
|
51
|
|
|
if ($return instanceof \PHPUnit_Framework_MockObject_Stub_Return) { |
|
|
|
|
|
|
52
|
|
|
$returnValue = $return; |
|
53
|
|
|
} else { |
|
54
|
|
|
$returnValue = $this->returnValue($return); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$methodExpectation = $authChecker |
|
58
|
|
|
->expects($expects) |
|
59
|
|
|
->method('isGranted'); |
|
60
|
|
|
|
|
61
|
|
|
call_user_func_array([$methodExpectation, 'with'], is_array($with) ? $with : [$with]); |
|
62
|
|
|
|
|
63
|
|
|
$methodExpectation->will($returnValue); |
|
64
|
|
|
|
|
65
|
|
|
return $authChecker; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function getAuthorizationCheckerMock() |
|
69
|
|
|
{ |
|
70
|
|
|
$AuthorizationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class) |
|
71
|
|
|
->disableOriginalConstructor() |
|
72
|
|
|
->setMethods(['isGranted']) |
|
73
|
|
|
->getMock() |
|
74
|
|
|
; |
|
75
|
|
|
|
|
76
|
|
|
return $AuthorizationChecker; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.