|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Tests\ExpressionLanguage\ExpressionFunction\Security; |
|
4
|
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\Definition\GlobalVariables; |
|
6
|
|
|
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security\GetUser; |
|
7
|
|
|
use Overblog\GraphQLBundle\Tests\ExpressionLanguage\TestCase; |
|
8
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
11
|
|
|
|
|
12
|
|
|
class GetUserTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
protected function getFunctions() |
|
15
|
|
|
{ |
|
16
|
|
|
return [new GetUser()]; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function testGetUserNoTokenStorage() |
|
20
|
|
|
{ |
|
21
|
|
|
$globalVariable = new GlobalVariables(['container' => $this->getDIContainerMock()]); |
|
22
|
|
|
$globalVariable->has('container'); |
|
|
|
|
|
|
23
|
|
|
$this->assertNull(eval($this->getCompileCode())); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testGetUserNoToken() |
|
27
|
|
|
{ |
|
28
|
|
|
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock(); |
|
29
|
|
|
$globalVariable = new GlobalVariables(['container' => $this->getDIContainerMock(['security.token_storage' => $tokenStorage])]); |
|
30
|
|
|
$globalVariable->get('container'); |
|
31
|
|
|
|
|
32
|
|
|
$this->getDIContainerMock(['security.token_storage' => $tokenStorage]); |
|
33
|
|
|
$this->assertNull(eval($this->getCompileCode())); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @dataProvider getUserProvider |
|
38
|
|
|
* |
|
39
|
|
|
* @param $user |
|
40
|
|
|
* @param $expectedUser |
|
41
|
|
|
*/ |
|
42
|
|
|
public function testGetUser($user, $expectedUser) |
|
43
|
|
|
{ |
|
44
|
|
|
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock(); |
|
45
|
|
|
$token = $this->getMockBuilder(TokenInterface::class)->getMock(); |
|
46
|
|
|
$globalVariable = new GlobalVariables(['container' => $this->getDIContainerMock(['security.token_storage' => $tokenStorage])]); |
|
47
|
|
|
$globalVariable->get('container'); |
|
48
|
|
|
|
|
49
|
|
|
$token |
|
50
|
|
|
->expects($this->once()) |
|
51
|
|
|
->method('getUser') |
|
52
|
|
|
->will($this->returnValue($user)); |
|
53
|
|
|
$tokenStorage |
|
54
|
|
|
->expects($this->once()) |
|
55
|
|
|
->method('getToken') |
|
56
|
|
|
->will($this->returnValue($token)); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertSame($expectedUser, eval($this->getCompileCode())); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getUserProvider() |
|
62
|
|
|
{ |
|
63
|
|
|
$user = $this->getMockBuilder(UserInterface::class)->getMock(); |
|
64
|
|
|
$std = new \stdClass(); |
|
65
|
|
|
$token = $this->getMockBuilder(TokenInterface::class)->getMock(); |
|
66
|
|
|
|
|
67
|
|
|
return [ |
|
68
|
|
|
[$user, $user], |
|
69
|
|
|
[$std, $std], |
|
70
|
|
|
[$token, $token], |
|
71
|
|
|
['Anon.', null], |
|
72
|
|
|
[null, null], |
|
73
|
|
|
[10, null], |
|
74
|
|
|
[true, null], |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function getCompileCode() |
|
79
|
|
|
{ |
|
80
|
|
|
return 'return '.$this->expressionLanguage->compile('getUser()').';'; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: