|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Jim Martens <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace TwoMartens\Bundle\CoreBundle\Tests\Group; |
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
13
|
|
|
use Symfony\Component\Finder\Finder; |
|
14
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
15
|
|
|
use Symfony\Component\Yaml\Dumper; |
|
16
|
|
|
use Symfony\Component\Yaml\Parser; |
|
17
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
18
|
|
|
use TwoMartens\Bundle\CoreBundle\Event\GroupOptionTypeEvent; |
|
19
|
|
|
use TwoMartens\Bundle\CoreBundle\Group\GroupService; |
|
20
|
|
|
use TwoMartens\Bundle\CoreBundle\Group\GroupServiceInterface; |
|
21
|
|
|
use TwoMartens\Bundle\CoreBundle\Group\Option\BooleanOptionType; |
|
22
|
|
|
use TwoMartens\Bundle\CoreBundle\Model\Option; |
|
23
|
|
|
use TwoMartens\Bundle\CoreBundle\Model\User; |
|
24
|
|
|
use TwoMartens\Bundle\CoreBundle\Util\ConfigUtil; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Tests the GroupService. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Jim Martens <[email protected]> |
|
30
|
|
|
* @copyright 2013-2015 Jim Martens |
|
31
|
|
|
*/ |
|
32
|
|
|
class GroupServiceTest extends \PHPUnit_Framework_TestCase |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var GroupServiceInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $groupService; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* the dummy parser |
|
41
|
|
|
* @var Parser |
|
42
|
|
|
*/ |
|
43
|
|
|
private $dummyParser; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* the dummy dumper |
|
47
|
|
|
* @var Dumper |
|
48
|
|
|
*/ |
|
49
|
|
|
private $dummyDumper; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* the dummy filesystem |
|
53
|
|
|
* @var Filesystem |
|
54
|
|
|
*/ |
|
55
|
|
|
private $dummyFilesystem; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* the yaml parsed data |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
private $yamlData; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* the option data |
|
65
|
|
|
* @var array |
|
66
|
|
|
*/ |
|
67
|
|
|
private $optionData; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Prepares test environment. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setUp() |
|
73
|
|
|
{ |
|
74
|
|
|
$finder = new Finder(); |
|
75
|
|
|
$finder->files()->in(__DIR__.'/config/'); |
|
76
|
|
|
$finder->name('*.yml'); |
|
77
|
|
|
$this->dummyParser = $this->getMock('Symfony\Component\Yaml\Parser'); |
|
78
|
|
|
$this->dummyDumper = $this->getMock('Symfony\Component\Yaml\Dumper'); |
|
79
|
|
|
$this->dummyFilesystem = $this->getMock('Symfony\Component\Filesystem\Filesystem'); |
|
80
|
|
|
|
|
81
|
|
|
$dummyDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); |
|
82
|
|
|
$dummyDispatcher->expects($this->once()) |
|
83
|
|
|
->method('dispatch') |
|
84
|
|
|
->willReturnCallback(function ($name, $event) { |
|
85
|
|
|
/** @var GroupOptionTypeEvent $event */ |
|
86
|
|
|
$optionTypes = [ |
|
87
|
|
|
'acp' => [ |
|
88
|
|
|
'twomartens.core' => [ |
|
89
|
|
|
'access' => new BooleanOptionType() |
|
90
|
|
|
] |
|
91
|
|
|
] |
|
92
|
|
|
]; |
|
93
|
|
|
$event->addOptions($optionTypes); |
|
94
|
|
|
}); |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
$this->yamlData = []; |
|
98
|
|
|
$this->optionData = []; |
|
99
|
|
|
foreach ($finder as $file) { |
|
100
|
|
|
/** @var SplFileInfo $file */ |
|
101
|
|
|
$basename = $file->getBasename('.yml'); |
|
102
|
|
|
$this->yamlData[$basename] = Yaml::parse($file->getContents()); |
|
103
|
|
|
$this->optionData[$basename] = ConfigUtil::convertToOptions($this->yamlData[$basename]); |
|
104
|
|
|
} |
|
105
|
|
|
$this->dummyParser->expects($this->any()) |
|
106
|
|
|
->method('parse') |
|
107
|
|
|
->willReturnCallback(function ($content) { |
|
108
|
|
|
return Yaml::parse($content); |
|
109
|
|
|
}); |
|
110
|
|
|
|
|
111
|
|
|
/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dummyDispatcher */ |
|
112
|
|
|
$this->groupService = new GroupService( |
|
113
|
|
|
$finder, |
|
114
|
|
|
$this->dummyParser, |
|
115
|
|
|
$this->dummyDumper, |
|
116
|
|
|
$this->dummyFilesystem, |
|
117
|
|
|
$dummyDispatcher |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Tests the ability to set and then get options. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function testGetOptions() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->groupService->setOptionsFor('ADMIN', $this->optionData['admin']); |
|
127
|
|
|
$this->groupService->setOptionsFor('USER', $this->optionData['user']); |
|
128
|
|
|
$options = $this->groupService->getOptionsFor('ADMIN'); |
|
129
|
|
|
$this->assertEquals($this->optionData['admin'], $options); |
|
130
|
|
|
|
|
131
|
|
|
// setting; category: acp, option: access |
|
132
|
|
|
$this->assertTrue( |
|
133
|
|
|
$this->groupService->get( |
|
134
|
|
|
'ADMIN', |
|
135
|
|
|
'acp', |
|
136
|
|
|
'twomartens.core', |
|
137
|
|
|
'access' |
|
138
|
|
|
)->getValue() |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
// the various option types are tested separately |
|
142
|
|
|
// here we should only test the core functionality of getEffective |
|
143
|
|
|
$user = $this->getMock('\TwoMartens\Bundle\CoreBundle\Model\User'); |
|
144
|
|
|
$user->expects($this->any()) |
|
145
|
|
|
->method('getGroupNames') |
|
146
|
|
|
->willReturn(['ADMIN', 'USER']); |
|
147
|
|
|
/** @var User $user */ |
|
148
|
|
|
$option = $this->groupService->getEffective( |
|
149
|
|
|
$user, |
|
150
|
|
|
'acp', |
|
151
|
|
|
'twomartens.core', |
|
152
|
|
|
'access' |
|
153
|
|
|
); |
|
154
|
|
|
$this->assertTrue($option->getValue()); |
|
155
|
|
|
|
|
156
|
|
|
$option->setValue(false); |
|
157
|
|
|
$this->groupService->set('ADMIN', 'acp', 'twomartens.core', $option); |
|
|
|
|
|
|
158
|
|
|
$this->assertFalse($this->groupService->getEffective( |
|
159
|
|
|
$user, |
|
160
|
|
|
'acp', |
|
161
|
|
|
'twomartens.core', |
|
162
|
|
|
'access' |
|
163
|
|
|
)->getValue()); |
|
164
|
|
|
|
|
165
|
|
|
$option = $this->groupService->getEffective( |
|
166
|
|
|
$user, |
|
167
|
|
|
'acp', |
|
168
|
|
|
'twomartens.core', |
|
169
|
|
|
'carpot' |
|
170
|
|
|
); |
|
171
|
|
|
$this->assertNull($option); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Tests that the two set methods leave a consistent state. |
|
176
|
|
|
*/ |
|
177
|
|
|
public function testConsistency() |
|
178
|
|
|
{ |
|
179
|
|
|
$this->groupService->setOptionsFor('ADMIN', $this->optionData['admin']); |
|
180
|
|
|
$this->groupService->setOptionsFor('USER', $this->optionData['user']); |
|
181
|
|
|
|
|
182
|
|
|
// check single get method |
|
183
|
|
|
$option = $this->groupService->get('ADMIN', 'acp', 'twomartens.core', 'access'); |
|
184
|
|
|
$this->assertTrue($option->getValue()); |
|
185
|
|
|
// change via single change |
|
186
|
|
|
$newOption = new Option(0, 'access', 'boolean', false); |
|
187
|
|
|
$this->groupService->set('ADMIN', 'acp', 'twomartens.core', $newOption); |
|
188
|
|
|
// check complete get |
|
189
|
|
|
$options = $this->groupService->getOptionsFor('ADMIN'); |
|
190
|
|
|
$categories = $options->getCategories(); |
|
191
|
|
|
foreach ($categories as $category) { |
|
192
|
|
|
if ($category->getName() != 'acp') { |
|
193
|
|
|
continue; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
$_options = $category->getOptions(); |
|
197
|
|
|
foreach ($_options as $option) { |
|
198
|
|
|
if ($option->getName() != 'access') { |
|
199
|
|
|
continue; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$this->assertFalse($option->getValue()); |
|
203
|
|
|
break 2; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Tests that options can be removed. |
|
210
|
|
|
*/ |
|
211
|
|
|
public function testRemoval() |
|
212
|
|
|
{ |
|
213
|
|
|
$this->groupService->setOptionsFor('ADMIN', $this->optionData['admin']); |
|
214
|
|
|
$options = $this->groupService->getOptionsFor('ADMIN'); |
|
215
|
|
|
$this->assertEquals($this->optionData['admin'], $options); |
|
216
|
|
|
|
|
217
|
|
|
$this->groupService->removeOptionsFor('ADMIN'); |
|
218
|
|
|
try { |
|
219
|
|
|
$this->groupService->getOptionsFor('ADMIN'); |
|
220
|
|
|
$this->fail(); |
|
221
|
|
|
} catch (\Exception $e) { |
|
222
|
|
|
$this->assertTrue(true); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: