1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skowronline\UserProcessorBundle\Tests\Processor; |
4
|
|
|
|
5
|
|
|
use Skowronline\UserProcessorBundle\Processor\UserProcessor; |
6
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
8
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Krzysztof Skaradziński <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class UserProcessorTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
public function testProcessorWithoutToken() |
16
|
|
|
{ |
17
|
|
|
$processor = new UserProcessor($this->mockTokenStorage()); |
|
|
|
|
18
|
|
|
$this->assertSame([], $processor([])); |
19
|
|
|
|
20
|
|
|
$record = ['data' => '2015-06-01 12:00:00']; |
21
|
|
|
$this->assertSame($record, $processor($record)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testProcessorWithoutUser() |
25
|
|
|
{ |
26
|
|
|
$token = $this->mockTokenInterface(); |
27
|
|
|
$storage = $this->mockTokenStorage($token); |
|
|
|
|
28
|
|
|
|
29
|
|
|
$processor = new UserProcessor($storage); |
|
|
|
|
30
|
|
|
$this->assertSame([], $processor([])); |
31
|
|
|
|
32
|
|
|
$record = ['date' => '2015-06-01 12:00:00']; |
33
|
|
|
$this->assertSame($record, $processor($record)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testUserAsString() |
37
|
|
|
{ |
38
|
|
|
$token = $this->mockTokenInterface('skowron-line'); |
39
|
|
|
$storage = $this->mockTokenStorage($token); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$processor = new UserProcessor($storage); |
|
|
|
|
42
|
|
|
$this->assertSame( |
43
|
|
|
[ |
44
|
|
|
'extra' => ['user' => 'skowron-line'] |
45
|
|
|
], |
46
|
|
|
$processor([]) |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$record = [ |
50
|
|
|
'date' => '2015-06-01 12:00:00', |
51
|
|
|
'extra' => [ |
52
|
|
|
'branch' => 'master' |
53
|
|
|
] |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$this->assertSame( |
57
|
|
|
[ |
58
|
|
|
'date' => '2015-06-01 12:00:00', |
59
|
|
|
'extra' => [ |
60
|
|
|
'branch' => 'master', |
61
|
|
|
'user' => 'skowron-line' |
62
|
|
|
] |
63
|
|
|
], |
64
|
|
|
$processor($record) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testUserAsObject() |
69
|
|
|
{ |
70
|
|
|
/** @var UserInterface|\PHPUnit_Framework_MockObject_MockObject $user */ |
71
|
|
|
$user = $this |
72
|
|
|
->getMockBuilder(UserInterface::class) |
73
|
|
|
->disableOriginalConstructor() |
74
|
|
|
->getMock(); |
75
|
|
|
|
76
|
|
|
$user |
77
|
|
|
->method('getUserName') |
78
|
|
|
->willReturn('skowron-line'); |
79
|
|
|
|
80
|
|
|
$token = $this->mockTokenInterface($user); |
81
|
|
|
$storage = $this->mockTokenStorage($token); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$processor = new UserProcessor($storage); |
|
|
|
|
84
|
|
|
$this->assertSame( |
85
|
|
|
[ |
86
|
|
|
'extra' => ['user' => 'skowron-line'] |
87
|
|
|
], |
88
|
|
|
$processor([]) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$record = [ |
92
|
|
|
'date' => '2015-06-01 12:00:00', |
93
|
|
|
'extra' => [ |
94
|
|
|
'branch' => 'master' |
95
|
|
|
] |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$this->assertSame( |
99
|
|
|
[ |
100
|
|
|
'date' => '2015-06-01 12:00:00', |
101
|
|
|
'extra' => [ |
102
|
|
|
'branch' => 'master', |
103
|
|
|
'user' => 'skowron-line' |
104
|
|
|
] |
105
|
|
|
], |
106
|
|
|
$processor($record) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/*** |
111
|
|
|
* @param null $returnValue |
112
|
|
|
* |
113
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|TokenStorageInterface |
114
|
|
|
*/ |
115
|
|
|
private function mockTokenStorage($returnValue = null) |
116
|
|
|
{ |
117
|
|
|
/** @var TokenStorageInterface|\PHPUnit_Framework_MockObject_MockObject $tokenStorage */ |
118
|
|
|
$tokenStorage = $this |
119
|
|
|
->getMockBuilder(TokenStorageInterface::class) |
120
|
|
|
->disableOriginalConstructor() |
121
|
|
|
->getMock(); |
122
|
|
|
|
123
|
|
|
$tokenStorage |
124
|
|
|
->method('getToken') |
125
|
|
|
->willReturn($returnValue); |
126
|
|
|
|
127
|
|
|
return $tokenStorage; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param null $returnValue |
132
|
|
|
* |
133
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|TokenInterface |
134
|
|
|
*/ |
135
|
|
|
private function mockTokenInterface($returnValue = null) |
136
|
|
|
{ |
137
|
|
|
/** @var TokenInterface|\PHPUnit_Framework_MockObject_MockObject $token */ |
138
|
|
|
$token = $this |
139
|
|
|
->getMockBuilder(TokenInterface::class) |
140
|
|
|
->disableOriginalConstructor() |
141
|
|
|
->getMock(); |
142
|
|
|
|
143
|
|
|
$token |
144
|
|
|
->method('getUser') |
145
|
|
|
->willReturn($returnValue); |
146
|
|
|
|
147
|
|
|
return $token; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.