1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\SettingsBundle\Tests\Unit\Service; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\PhpFileCache; |
15
|
|
|
use ONGR\CookiesBundle\Cookie\Model\GenericCookie; |
16
|
|
|
use ONGR\ElasticsearchBundle\Result\DocumentIterator; |
17
|
|
|
use ONGR\ElasticsearchBundle\Service\Manager; |
18
|
|
|
use ONGR\ElasticsearchBundle\Service\Repository; |
19
|
|
|
use ONGR\ElasticsearchDSL\Search; |
20
|
|
|
use ONGR\SettingsBundle\Document\Setting; |
21
|
|
|
use ONGR\SettingsBundle\Service\SettingsManager; |
22
|
|
|
|
23
|
|
|
class SettingsManagerTest extends \PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Manager|\PHPUnit_Framework_MockObject_MockObject |
28
|
|
|
*/ |
29
|
|
|
private $manager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Repository|\PHPUnit_Framework_MockObject_MockObject |
33
|
|
|
*/ |
34
|
|
|
private $repository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var PhpFileCache|\PHPUnit_Framework_MockObject_MockObject |
38
|
|
|
*/ |
39
|
|
|
private $cache; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var GenericCookie|\PHPUnit_Framework_MockObject_MockObject |
43
|
|
|
*/ |
44
|
|
|
private $cookie; |
45
|
|
|
|
46
|
|
|
public function setUp() |
47
|
|
|
{ |
48
|
|
|
$this->manager = $this->getMockBuilder('ONGR\ElasticsearchBundle\Service\Manager') |
49
|
|
|
->disableOriginalConstructor() |
50
|
|
|
->setMethods(['persist', 'commit']) |
51
|
|
|
->getMock(); |
52
|
|
|
$this->cache = $this->getMockBuilder('Doctrine\Common\Cache\PhpFileCache') |
53
|
|
|
->disableOriginalConstructor() |
54
|
|
|
->setMethods(['contains', 'fetch', 'save', 'delete']) |
55
|
|
|
->getMock(); |
56
|
|
|
$this->cookie = $this->getMockBuilder('ONGR\CookiesBundle\Cookie\Model\GenericCookie') |
57
|
|
|
->disableOriginalConstructor() |
58
|
|
|
->setMethods(['getValue']) |
59
|
|
|
->getMock(); |
60
|
|
|
$this->repository = $this->getMockBuilder('ONGR\ElasticsearchBundle\Service\Repository') |
61
|
|
|
->disableOriginalConstructor() |
62
|
|
|
->setMethods( |
63
|
|
|
[ |
64
|
|
|
'findOneBy', |
65
|
|
|
'remove', |
66
|
|
|
'createSearch', |
67
|
|
|
'findDocuments', |
68
|
|
|
'getClassName', |
69
|
|
|
'getManager', |
70
|
|
|
'getAggregation', |
71
|
|
|
] |
72
|
|
|
) |
73
|
|
|
->getMock(); |
74
|
|
|
|
75
|
|
|
$this->repository->expects($this->any())->method('getClassName')->willReturn(Setting::class); |
76
|
|
|
$this->repository->expects($this->any())->method('getManager')->willReturn($this->manager); |
77
|
|
|
$this->repository->expects($this->any())->method('createSearch')->willReturn(new Search()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Test get cache getter. |
82
|
|
|
*/ |
83
|
|
|
public function testGetCache() |
84
|
|
|
{ |
85
|
|
|
$manager = new SettingsManager( |
86
|
|
|
$this->repository, |
87
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
88
|
|
|
); |
89
|
|
|
$manager->setCache($this->cache); |
90
|
|
|
|
91
|
|
|
$this->assertInstanceOf('Doctrine\Common\Cache\PhpFileCache', $manager->getCache()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Test get cookie getter. |
96
|
|
|
*/ |
97
|
|
|
public function testGetActiveProfilesCookie() |
98
|
|
|
{ |
99
|
|
|
$manager = new SettingsManager( |
100
|
|
|
$this->repository, |
101
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
102
|
|
|
); |
103
|
|
|
$manager->setActiveProfilesCookie($this->cookie); |
104
|
|
|
|
105
|
|
|
$this->assertInstanceOf('ONGR\CookiesBundle\Cookie\Model\GenericCookie', $manager->getActiveProfilesCookie()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Test get cookie getter. |
110
|
|
|
*/ |
111
|
|
|
public function testGetActiveProfilesSettingName() |
112
|
|
|
{ |
113
|
|
|
$manager = new SettingsManager( |
114
|
|
|
$this->repository, |
115
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
116
|
|
|
); |
117
|
|
|
$manager->setActiveProfilesSettingName('ongr'); |
118
|
|
|
|
119
|
|
|
$this->assertEquals('ongr', $manager->getActiveProfilesSettingName()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Test setting create function. |
124
|
|
|
*/ |
125
|
|
|
public function testCreate() |
126
|
|
|
{ |
127
|
|
|
$data = [ |
128
|
|
|
'name' => 'acme', |
129
|
|
|
'type' => 'string', |
130
|
|
|
'value' => 'foo', |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
$this->manager->expects($this->once())->method('persist')->with($this->callback(function ($obj) { |
|
|
|
|
134
|
|
|
return $obj instanceof Setting; |
135
|
|
|
}))->willReturn(null); |
136
|
|
|
$this->manager->expects($this->once())->method('persist')->willReturn(null); |
|
|
|
|
137
|
|
|
|
138
|
|
|
$manager = new SettingsManager( |
139
|
|
|
$this->repository, |
140
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
141
|
|
|
); |
142
|
|
|
$document = $manager->create($data); |
143
|
|
|
|
144
|
|
|
foreach ($data as $key => $value) { |
145
|
|
|
$this->assertEquals($value, $document->{'get' . ucfirst($key)}()); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @expectedException \LogicException |
151
|
|
|
* @expectedExceptionMessage Missing one of the mandatory field! |
152
|
|
|
*/ |
153
|
|
|
public function testCreateMandatoryParameters() |
154
|
|
|
{ |
155
|
|
|
$data = ['bar' => 'foo']; |
156
|
|
|
|
157
|
|
|
$manager = new SettingsManager( |
158
|
|
|
$this->repository, |
159
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
160
|
|
|
); |
161
|
|
|
$manager->create($data); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @expectedException \LogicException |
166
|
|
|
* @expectedExceptionMessage Setting acme already exists. |
167
|
|
|
*/ |
168
|
|
|
public function testCreateWhenSettingExists() |
169
|
|
|
{ |
170
|
|
|
$data = [ |
171
|
|
|
'name' => 'acme', |
172
|
|
|
'type' => 'string', |
173
|
|
|
'value' => 'foo', |
174
|
|
|
]; |
175
|
|
|
|
176
|
|
|
$this->repository->expects($this->once())->method('findOneBy')->willReturn(new Setting()); |
|
|
|
|
177
|
|
|
|
178
|
|
|
$manager = new SettingsManager( |
179
|
|
|
$this->repository, |
180
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
181
|
|
|
); |
182
|
|
|
$manager->create($data); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Tests setting create without value set. Should be set to 0 by default. |
187
|
|
|
*/ |
188
|
|
|
public function testWithoutDataValueSet() |
189
|
|
|
{ |
190
|
|
|
$data = [ |
191
|
|
|
'name' => 'acme', |
192
|
|
|
'type' => 'string', |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
$manager = new SettingsManager( |
196
|
|
|
$this->repository, |
197
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
198
|
|
|
); |
199
|
|
|
$document = $manager->create($data); |
200
|
|
|
|
201
|
|
|
$this->assertEquals(0, $document->getValue()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Tests setting update. |
206
|
|
|
*/ |
207
|
|
|
public function testUpdate() |
208
|
|
|
{ |
209
|
|
|
$setting = new Setting(); |
210
|
|
|
$setting->setName('acme'); |
211
|
|
|
$setting->setValue('foo'); |
212
|
|
|
|
213
|
|
|
$this->repository->expects($this->once())->method('findOneBy')->willReturn($setting); |
|
|
|
|
214
|
|
|
$manager = new SettingsManager( |
215
|
|
|
$this->repository, |
216
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
217
|
|
|
); |
218
|
|
|
$manager->setCache($this->cache); |
219
|
|
|
|
220
|
|
|
$document = $manager->update('acme', ['value' => 'bar']); |
221
|
|
|
$this->assertEquals('acme', $document->getName()); |
222
|
|
|
$this->assertEquals('bar', $document->getValue()); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @expectedException \LogicException |
227
|
|
|
* @expectedExceptionMessage Setting acme not exist. |
228
|
|
|
*/ |
229
|
|
View Code Duplication |
public function testUpdateWhenSettingNotExists() |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
$this->repository->expects($this->once())->method('findOneBy')->willReturn(null); |
|
|
|
|
232
|
|
|
|
233
|
|
|
$manager = new SettingsManager( |
234
|
|
|
$this->repository, |
235
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
236
|
|
|
); |
237
|
|
|
$manager->setCache($this->cache); |
238
|
|
|
$manager->update('acme', ['value' => 'foo']); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Tests setting delete. |
243
|
|
|
*/ |
244
|
|
View Code Duplication |
public function testDelete() |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
$setting = new Setting(); |
247
|
|
|
$setting->setId('acme'); |
248
|
|
|
$setting->setName('acme'); |
249
|
|
|
|
250
|
|
|
$this->repository->expects($this->any()) |
|
|
|
|
251
|
|
|
->method('findOneBy')->with($this->equalTo(['name.name' => 'acme']))->willReturn($setting); |
252
|
|
|
$manager = new SettingsManager( |
253
|
|
|
$this->repository, |
254
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
255
|
|
|
); |
256
|
|
|
$manager->setCache($this->cache); |
257
|
|
|
|
258
|
|
|
$manager->delete('acme'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
View Code Duplication |
public function testHas() |
|
|
|
|
262
|
|
|
{ |
263
|
|
|
$setting = new Setting(); |
264
|
|
|
$setting->setName('acme'); |
265
|
|
|
$setting->setValue('foo'); |
266
|
|
|
|
267
|
|
|
$this->repository->expects($this->once()) |
|
|
|
|
268
|
|
|
->method('findOneBy')->with($this->equalTo(['name.name' => 'acme']))->willReturn($setting); |
269
|
|
|
$manager = new SettingsManager( |
270
|
|
|
$this->repository, |
271
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
272
|
|
|
); |
273
|
|
|
|
274
|
|
|
$result = $manager->has('acme'); |
275
|
|
|
|
276
|
|
|
$this->assertTrue($result); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Test has method when there is no setting. |
281
|
|
|
*/ |
282
|
|
View Code Duplication |
public function testHasWhenThereIsNoSetting() |
|
|
|
|
283
|
|
|
{ |
284
|
|
|
$this->repository->expects($this->once()) |
|
|
|
|
285
|
|
|
->method('findOneBy')->with($this->equalTo(['name.name' => 'acme']))->willReturn(null); |
286
|
|
|
$manager = new SettingsManager( |
287
|
|
|
$this->repository, |
288
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
289
|
|
|
); |
290
|
|
|
|
291
|
|
|
$result = $manager->has('acme'); |
292
|
|
|
|
293
|
|
|
$this->assertFalse($result); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Tests setting update. |
298
|
|
|
*/ |
299
|
|
View Code Duplication |
public function testGetValue() |
|
|
|
|
300
|
|
|
{ |
301
|
|
|
$setting = new Setting(); |
302
|
|
|
$setting->setName('acme'); |
303
|
|
|
$setting->setValue('foo'); |
304
|
|
|
|
305
|
|
|
$this->repository->expects($this->once())->method('findOneBy')->willReturn($setting); |
|
|
|
|
306
|
|
|
$manager = new SettingsManager( |
307
|
|
|
$this->repository, |
308
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
309
|
|
|
); |
310
|
|
|
|
311
|
|
|
$result = $manager->getValue('acme'); |
312
|
|
|
$this->assertEquals('foo', $result); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Tests setting update. |
317
|
|
|
*/ |
318
|
|
View Code Duplication |
public function testGetValueWhenThereIsNoSetting() |
|
|
|
|
319
|
|
|
{ |
320
|
|
|
$this->repository->expects($this->once())->method('findOneBy')->willReturn(null); |
|
|
|
|
321
|
|
|
$manager = new SettingsManager( |
322
|
|
|
$this->repository, |
323
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
324
|
|
|
); |
325
|
|
|
|
326
|
|
|
$result = $manager->getValue('acme', 'bar'); |
|
|
|
|
327
|
|
|
$this->assertEquals('bar', $result); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Returns document iterator with pre-loaded aggregations. |
332
|
|
|
* |
333
|
|
|
* @return DocumentIterator |
334
|
|
|
*/ |
335
|
|
|
private function getDocumentIterator() |
336
|
|
|
{ |
337
|
|
|
$rawData = [ |
338
|
|
|
'aggregations' => [ |
339
|
|
|
'filter' => [ |
340
|
|
|
'profiles' => [ |
341
|
|
|
'buckets' => [ |
342
|
|
|
[ |
343
|
|
|
'key' => 'default', |
344
|
|
|
'doc_count' => 2, |
345
|
|
|
'documents' => [ |
346
|
|
|
'hits' => [ |
347
|
|
|
'total' => 2, |
348
|
|
|
'max_score' => 1, |
349
|
|
|
'hits' => [ |
350
|
|
|
[ |
351
|
|
|
'_index' => 'settings', |
352
|
|
|
'_type' => 'setting', |
353
|
|
|
'_id' => 'kk', |
354
|
|
|
'_score' => 1, |
355
|
|
|
'_source' => [ |
356
|
|
|
'name' => 'foo', |
357
|
|
|
'profile' => [ |
358
|
|
|
'bar' |
359
|
|
|
], |
360
|
|
|
'type' => 'bool', |
361
|
|
|
'value' => 1 |
362
|
|
|
] |
363
|
|
|
], |
364
|
|
|
[ |
365
|
|
|
'_index' => 'settings', |
366
|
|
|
'_type' => 'setting', |
367
|
|
|
'_id' => 'xx', |
368
|
|
|
'_score' => 1, |
369
|
|
|
'_source' => [ |
370
|
|
|
'name' => 'kk', |
371
|
|
|
'profile' => [ |
372
|
|
|
'kk' |
373
|
|
|
], |
374
|
|
|
'type' => 'bool', |
375
|
|
|
'value' => 1 |
376
|
|
|
] |
377
|
|
|
] |
378
|
|
|
] |
379
|
|
|
] |
380
|
|
|
], |
381
|
|
|
'key' => 'foo', |
382
|
|
|
'doc_count' => 1, |
383
|
|
|
'documents' => [ |
384
|
|
|
'hits' => [ |
385
|
|
|
'total' => 1, |
386
|
|
|
'max_score' => 1, |
387
|
|
|
'hits' => [ |
388
|
|
|
[ |
389
|
|
|
'_index' => 'settings', |
390
|
|
|
'_type' => 'setting', |
391
|
|
|
'_id' => 'kk', |
392
|
|
|
'_score' => 1, |
393
|
|
|
'_source' => [ |
394
|
|
|
'name' => 'foo', |
395
|
|
|
'profile' => [ |
396
|
|
|
'bar' |
397
|
|
|
], |
398
|
|
|
'type' => 'bool', |
399
|
|
|
'value' => 1 |
400
|
|
|
] |
401
|
|
|
] |
402
|
|
|
] |
403
|
|
|
] |
404
|
|
|
], |
405
|
|
|
'key' => 'kk', |
406
|
|
|
'doc_count' => 1, |
407
|
|
|
'documents' => [ |
408
|
|
|
'hits' => [ |
409
|
|
|
'total' => 1, |
410
|
|
|
'max_score' => 1, |
411
|
|
|
'hits' => [ |
412
|
|
|
[ |
413
|
|
|
'_index' => 'settings', |
414
|
|
|
'_type' => 'setting', |
415
|
|
|
'_id' => 'kk', |
416
|
|
|
'_score' => 1, |
417
|
|
|
'_source' => [ |
418
|
|
|
'name' => 'foo', |
419
|
|
|
'profile' => [ |
420
|
|
|
'bar' |
421
|
|
|
], |
422
|
|
|
'type' => 'bool', |
423
|
|
|
'value' => 1 |
424
|
|
|
] |
425
|
|
|
] |
426
|
|
|
] |
427
|
|
|
] |
428
|
|
|
] |
429
|
|
|
] |
430
|
|
|
] |
431
|
|
|
] |
432
|
|
|
] |
433
|
|
|
] |
434
|
|
|
]; |
435
|
|
|
|
436
|
|
|
return new DocumentIterator($rawData, $this->manager); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Tests setting update. |
441
|
|
|
*/ |
442
|
|
|
public function testGetCachedValue() |
443
|
|
|
{ |
444
|
|
|
|
445
|
|
|
$activeProfilesSetting = 'active_profiles'; |
446
|
|
|
|
447
|
|
|
$this->repository->expects($this->any())->method('findOneBy')->willReturnCallback( |
|
|
|
|
448
|
|
|
function ($arg) use ($activeProfilesSetting) { |
449
|
|
|
$settingName = $arg['name.name']; |
450
|
|
|
$setting = new Setting(); |
451
|
|
|
switch ($settingName) { |
452
|
|
|
case 'active_profiles': |
453
|
|
|
$setting->setName($activeProfilesSetting); |
454
|
|
|
$setting->setValue(['bar', 'foo']); |
|
|
|
|
455
|
|
|
return $setting; |
456
|
|
|
break; |
|
|
|
|
457
|
|
|
case 'acme': |
458
|
|
|
$setting->setName('acme'); |
459
|
|
|
$setting->setValue('foo'); |
460
|
|
|
$setting->setProfile(['foo', 'default']); |
461
|
|
|
break; |
462
|
|
|
} |
463
|
|
|
return $setting; |
464
|
|
|
} |
465
|
|
|
); |
466
|
|
|
|
467
|
|
|
$this->repository->expects($this->any())->method('findDocuments')->willReturn($this->getDocumentIterator()); |
|
|
|
|
468
|
|
|
$this->cookie->expects($this->any())->method('getValue')->willReturn(['foo']); |
|
|
|
|
469
|
|
|
|
470
|
|
|
$manager = new SettingsManager( |
471
|
|
|
$this->repository, |
472
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
473
|
|
|
); |
474
|
|
|
$manager->setCache($this->cache); |
475
|
|
|
$manager->setActiveProfilesSettingName($activeProfilesSetting); |
476
|
|
|
$manager->setActiveProfilesCookie($this->cookie); |
477
|
|
|
$manager->setActiveProfilesList(['default']); |
478
|
|
|
|
479
|
|
|
$result = $manager->getCachedValue('acme'); |
480
|
|
|
$this->assertEquals('foo', $result); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Tests setting update. |
485
|
|
|
*/ |
486
|
|
|
public function testGetCachedValueFromCache() |
487
|
|
|
{ |
488
|
|
|
|
489
|
|
|
$activeProfilesSetting = 'active_profiles'; |
490
|
|
|
|
491
|
|
|
$this->repository->expects($this->any())->method('execute')->willReturn($this->getDocumentIterator()); |
|
|
|
|
492
|
|
|
$this->cookie->expects($this->any())->method('getValue')->willReturn(['foo']); |
|
|
|
|
493
|
|
|
$this->cache->expects($this->any())->method('contains')->willReturn(true); |
|
|
|
|
494
|
|
|
$this->cache->expects($this->any())->method('fetch')->willReturnCallback( |
495
|
|
|
function ($arg) use ($activeProfilesSetting) { |
496
|
|
|
if ($arg == $activeProfilesSetting) { |
497
|
|
|
return ['foo']; |
498
|
|
|
} |
499
|
|
|
return ['value' => 'foo', 'profiles' => ['foo']]; |
500
|
|
|
} |
501
|
|
|
); |
502
|
|
|
|
503
|
|
|
$manager = new SettingsManager( |
504
|
|
|
$this->repository, |
505
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
506
|
|
|
); |
507
|
|
|
$manager->setCache($this->cache); |
508
|
|
|
$manager->setActiveProfilesSettingName($activeProfilesSetting); |
509
|
|
|
$manager->setActiveProfilesCookie($this->cookie); |
510
|
|
|
|
511
|
|
|
$result = $manager->getCachedValue('acme'); |
512
|
|
|
$this->assertEquals('foo', $result); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Tests setting update. |
517
|
|
|
*/ |
518
|
|
|
public function testGetCachedValueWithoutActiveProfiles() |
519
|
|
|
{ |
520
|
|
|
$activeProfilesSetting = 'active_profiles'; |
521
|
|
|
$this->repository->expects($this->any())->method('execute')->willReturn($this->getDocumentIterator()); |
|
|
|
|
522
|
|
|
$this->cookie->expects($this->any())->method('getValue')->willReturn(['foo']); |
|
|
|
|
523
|
|
|
$this->cache->expects($this->any())->method('contains')->willReturn(true); |
|
|
|
|
524
|
|
|
$this->cache->expects($this->any())->method('fetch')->willReturn(['value' => 'foo', 'profiles' => ['foo']]); |
525
|
|
|
|
526
|
|
|
$manager = new SettingsManager( |
527
|
|
|
$this->repository, |
528
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
529
|
|
|
); |
530
|
|
|
$manager->setCache($this->cache); |
531
|
|
|
$manager->setActiveProfilesSettingName($activeProfilesSetting); |
532
|
|
|
$manager->setActiveProfilesCookie($this->cookie); |
533
|
|
|
|
534
|
|
|
$result = $manager->getCachedValue('acme', false); |
535
|
|
|
$this->assertEquals('foo', $result); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Tests if there is no setting. |
540
|
|
|
*/ |
541
|
|
|
public function testGetCachedValueWhenItsNotExist() |
542
|
|
|
{ |
543
|
|
|
$activeProfilesSetting = 'active_profiles'; |
544
|
|
|
$this->repository->expects($this->any())->method('execute')->willReturn($this->getDocumentIterator()); |
|
|
|
|
545
|
|
|
$manager = new SettingsManager( |
546
|
|
|
$this->repository, |
547
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
548
|
|
|
); |
549
|
|
|
$manager->setCache($this->cache); |
550
|
|
|
$manager->setActiveProfilesSettingName($activeProfilesSetting); |
551
|
|
|
|
552
|
|
|
$value = $manager->getCachedValue('acme'); |
553
|
|
|
$this->assertNull($value); |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
public function testGetAllActiveProfilesNameList() |
557
|
|
|
{ |
558
|
|
|
$document = new Setting(); |
559
|
|
|
$document->setName('active_profiles'); |
560
|
|
|
$document->setValue(['kk']); |
|
|
|
|
561
|
|
|
$this->repository->expects($this->any())->method('findOneBy')->willReturn($document); |
|
|
|
|
562
|
|
|
$this->repository->expects($this->any())->method('findDocuments')->willReturn($this->getDocumentIterator()); |
563
|
|
|
$manager = new SettingsManager( |
564
|
|
|
$this->repository, |
565
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
566
|
|
|
); |
567
|
|
|
$manager->setCache($this->cache); |
568
|
|
|
|
569
|
|
|
$value = $manager->getActiveProfiles(); |
570
|
|
|
$this->assertEquals(['kk'], $value); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
public function testGetActiveExperimentsFromRepository() |
574
|
|
|
{ |
575
|
|
|
$activeExperimentsSettingName = 'foo'; |
576
|
|
|
$experimentName = 'bar'; |
577
|
|
|
$cache = $this->cache; |
578
|
|
|
$cache->expects($this->any())->method('contains')->willReturn(false); |
|
|
|
|
579
|
|
|
$activeExperiments = new Setting; |
580
|
|
|
$activeExperiments->setName($activeExperimentsSettingName); |
581
|
|
|
$activeExperiments->setValue([$experimentName]); |
|
|
|
|
582
|
|
|
$repository = $this->repository; |
583
|
|
|
$repository->expects($this->any())->method('findOneBy') |
|
|
|
|
584
|
|
|
->with(['name.name' => $activeExperimentsSettingName])->willReturn($activeExperiments); |
585
|
|
|
$manager = new SettingsManager( |
586
|
|
|
$repository, |
|
|
|
|
587
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
588
|
|
|
); |
589
|
|
|
$manager->setActiveExperimentsSettingName($activeExperimentsSettingName); |
590
|
|
|
$manager->setCache($cache); |
|
|
|
|
591
|
|
|
|
592
|
|
|
$activeExperiments = $manager->getActiveExperiments(); |
593
|
|
|
|
594
|
|
|
$this->assertEquals([$experimentName], $activeExperiments); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
View Code Duplication |
public function testGetActiveExperimentsFromCache() |
|
|
|
|
598
|
|
|
{ |
599
|
|
|
$activeExperimentsSettingName = 'foo'; |
600
|
|
|
$experimentName = 'bar'; |
601
|
|
|
$cache = $this->cache; |
602
|
|
|
$cache->expects($this->any())->method('contains') |
|
|
|
|
603
|
|
|
->with($activeExperimentsSettingName)->willReturn(true); |
604
|
|
|
$cache->expects($this->any())->method('fetch') |
605
|
|
|
->with($activeExperimentsSettingName)->willReturn(['value' => [$experimentName]]); |
606
|
|
|
$manager = new SettingsManager( |
607
|
|
|
$this->repository, |
608
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
609
|
|
|
); |
610
|
|
|
$manager->setActiveExperimentsSettingName($activeExperimentsSettingName); |
611
|
|
|
$manager->setCache($cache); |
|
|
|
|
612
|
|
|
|
613
|
|
|
$activeExperiments = $manager->getActiveExperiments(); |
614
|
|
|
|
615
|
|
|
$this->assertEquals([$experimentName], $activeExperiments); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
View Code Duplication |
public function testGetActiveExperimentsCreateNew() |
|
|
|
|
619
|
|
|
{ |
620
|
|
|
$activeExperimentsSettingName = 'foo'; |
621
|
|
|
$cache = $this->cache; |
622
|
|
|
$cache->expects($this->any())->method('contains') |
|
|
|
|
623
|
|
|
->with($activeExperimentsSettingName)->willReturn(false); |
624
|
|
|
$repository = $this->repository; |
625
|
|
|
$repository->expects($this->any())->method('findOneBy') |
|
|
|
|
626
|
|
|
->with(['name.name' => $activeExperimentsSettingName])->willReturn(null); |
627
|
|
|
$manager = new SettingsManager( |
628
|
|
|
$repository, |
|
|
|
|
629
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
630
|
|
|
); |
631
|
|
|
$manager->setActiveExperimentsSettingName($activeExperimentsSettingName); |
632
|
|
|
$manager->setCache($cache); |
|
|
|
|
633
|
|
|
|
634
|
|
|
$activeExperiments = $manager->getActiveExperiments(); |
635
|
|
|
|
636
|
|
|
$this->assertEquals([], $activeExperiments); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
public function testToggleExperiment() |
640
|
|
|
{ |
641
|
|
|
$activeExperimentsSettingName = 'active_profiles'; |
642
|
|
|
$activeExperimentsSetting = new Setting(); |
643
|
|
|
$activeExperimentsSetting->setName($activeExperimentsSettingName); |
644
|
|
|
$repository = $this->repository; |
645
|
|
|
$repository->expects($this->any())->method('findOneBy') |
|
|
|
|
646
|
|
|
->with(['name.name' => $activeExperimentsSettingName])->willReturn($activeExperimentsSetting); |
647
|
|
|
$manager = new SettingsManager( |
648
|
|
|
$repository, |
|
|
|
|
649
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
650
|
|
|
); |
651
|
|
|
$manager->setCache($this->cache); |
652
|
|
|
$manager->setActiveExperimentsSettingName($activeExperimentsSettingName); |
653
|
|
|
$manager->toggleExperiment('foo'); |
654
|
|
|
$this->assertEquals(['foo'], $activeExperimentsSetting->getValue()); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* @expectedException \LogicException |
659
|
|
|
* @expectedExceptionMessage The setting `active_profiles` is not set |
660
|
|
|
*/ |
661
|
|
|
public function testGetCachedExperimentException() |
662
|
|
|
{ |
663
|
|
|
$activeExperimentsSettingName = 'active_profiles'; |
664
|
|
|
$repository = $this->repository; |
665
|
|
|
$repository->expects($this->any())->method('findOneBy') |
|
|
|
|
666
|
|
|
->with(['name.name' => $activeExperimentsSettingName])->willReturn(null); |
667
|
|
|
$manager = new SettingsManager( |
668
|
|
|
$repository, |
|
|
|
|
669
|
|
|
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
670
|
|
|
); |
671
|
|
|
$manager->setActiveExperimentsSettingName($activeExperimentsSettingName); |
672
|
|
|
$manager->toggleExperiment('foo'); |
673
|
|
|
} |
674
|
|
|
} |
675
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: