/
ConfigManifestTest.php
542 lines (452 loc) · 15.3 KB
/
ConfigManifestTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
class ConfigManifestTest_ConfigManifestAccess extends SS_ConfigManifest {
public function relativeOrder($a, $b) {
return parent::relativeOrder($a, $b);
}
}
class ConfigManifestTest extends SapphireTest {
/**
* This is a helper method for getting a new manifest
* @param $name
* @return any
*/
protected function getConfigFixtureValue($name) {
$manifest = new SS_ConfigManifest(dirname(__FILE__).'/fixtures/configmanifest', true, true);
return $manifest->get('ConfigManifestTest', $name);
}
/**
* This is a helper method for displaying a relevant message about a parsing failure
*/
protected function getParsedAsMessage($path) {
return sprintf('Reference path "%s" failed to parse correctly', $path);
}
/**
* A helper method to return a mock of the cache in order to test expectations and reduce dependency
* @return Zend_Cache_Core
*/
protected function getCacheMock() {
return $this->getMock(
'Zend_Cache_Core',
array('load', 'save'),
array(),
'',
false
);
}
/**
* A helper method to return a mock of the manifest in order to test expectations and reduce dependency
* @param $methods
* @return SS_ConfigManifest
*/
protected function getManifestMock($methods) {
return $this->getMock(
'SS_ConfigManifest',
$methods,
array(), // no constructor arguments
'', // default
false // don't call the constructor
);
}
/**
* Test the caching functionality when we are forcing regeneration
*
* 1. Test that regenerate is called in the default case and that cache->load isn't
* 2. Test that save is called correctly after the regeneration
*/
public function testCachingForceRegeneration() {
// Test that regenerate is called correctly.
$manifest = $this->getManifestMock(array('getCache', 'regenerate', 'buildYamlConfigVariant'));
$manifest->expects($this->once()) // regenerate should be called once
->method('regenerate')
->with($this->equalTo(true)); // includeTests = true
// Set up a cache where we expect load to never be called
$cache = $this->getCacheMock();
$cache->expects($this->never())
->method('load');
$manifest->expects($this->any())
->method('getCache')
->will($this->returnValue($cache));
$manifest->__construct(dirname(__FILE__).'/fixtures/configmanifest', true, true);
// Test that save is called correctly
$manifest = $this->getManifestMock(array('getCache'));
$cache = $this->getCacheMock();
$cache->expects($this->atLeastOnce())
->method('save');
$manifest->expects($this->any())
->method('getCache')
->will($this->returnValue($cache));
$manifest->__construct(dirname(__FILE__).'/fixtures/configmanifest', true, true);
}
/**
* Test the caching functionality when we are not forcing regeneration
*
* 1. Test that load is called
* 2. Test the regenerate is called when the cache is unprimed
* 3. Test that when there is a value in the cache regenerate isn't called
*/
public function testCachingNotForceRegeneration() {
// Test that load is called
$manifest = $this->getManifestMock(array('getCache', 'regenerate', 'buildYamlConfigVariant'));
// Load should be called twice
$cache = $this->getCacheMock();
$cache->expects($this->exactly(2))
->method('load');
$manifest->expects($this->any())
->method('getCache')
->will($this->returnValue($cache));
$manifest->__construct(dirname(__FILE__).'/fixtures/configmanifest', true, false);
// Now test that regenerate is called because the cache is unprimed
$manifest = $this->getManifestMock(array('getCache', 'regenerate', 'buildYamlConfigVariant'));
$cache = $this->getCacheMock();
$cache->expects($this->exactly(2))
->method('load')
->will($this->onConsecutiveCalls(false, false));
$manifest->expects($this->any())
->method('getCache')
->will($this->returnValue($cache));
$manifest->expects($this->once())
->method('regenerate')
->with($this->equalTo(false)); //includeTests = false
$manifest->__construct(dirname(__FILE__).'/fixtures/configmanifest', false, false);
// Now test that when there is a value in the cache that regenerate isn't called
$manifest = $this->getManifestMock(array('getCache', 'regenerate', 'buildYamlConfigVariant'));
$cache = $this->getCacheMock();
$cache->expects($this->exactly(2))
->method('load')
->will($this->onConsecutiveCalls(array(), array()));
$manifest->expects($this->any())
->method('getCache')
->will($this->returnValue($cache));
$manifest->expects($this->never())
->method('regenerate');
$manifest->__construct(dirname(__FILE__).'/fixtures/configmanifest', false, false);
}
/**
* This test checks the processing of before and after reference paths (module-name/filename#fragment)
* This method uses fixture/configmanifest/mysite/_config/addyamlconfigfile.yml as a fixture
*/
public function testAddYAMLConfigFileReferencePathParsing() {
// Use a mock to avoid testing unrelated functionality
$manifest = $this->getManifestMock(array('addModule'));
// This tests that the addModule method is called with the correct value
$manifest->expects($this->once())
->method('addModule')
->with($this->equalTo(dirname(__FILE__).'/fixtures/configmanifest/mysite'));
// Call the method to be tested
$manifest->addYAMLConfigFile(
'addyamlconfigfile.yml',
dirname(__FILE__).'/fixtures/configmanifest/mysite/_config/addyamlconfigfile.yml',
false
);
// There is no getter for yamlConfigFragments
$property = new ReflectionProperty('SS_ConfigManifest', 'yamlConfigFragments');
$property->setAccessible(true);
// Get the result back from the parsing
$result = $property->getValue($manifest);
$this->assertEquals(
array(
array(
'module' => 'mysite',
'file' => 'testfile',
'name' => 'fragment',
),
),
@$result[0]['after'],
$this->getParsedAsMessage('mysite/testfile#fragment')
);
$this->assertEquals(
array(
array(
'module' => 'test-module',
'file' => 'testfile',
'name' => 'fragment',
),
),
@$result[1]['after'],
$this->getParsedAsMessage('test-module/testfile#fragment')
);
$this->assertEquals(
array(
array(
'module' => '*',
'file' => '*',
'name' => '*',
),
),
@$result[2]['after'],
$this->getParsedAsMessage('*')
);
$this->assertEquals(
array(
array(
'module' => '*',
'file' => 'testfile',
'name' => '*'
),
),
@$result[3]['after'],
$this->getParsedAsMessage('*/testfile')
);
$this->assertEquals(
array(
array(
'module' => '*',
'file' => '*',
'name' => 'fragment'
),
),
@$result[4]['after'],
$this->getParsedAsMessage('*/*#fragment')
);
$this->assertEquals(
array(
array(
'module' => '*',
'file' => '*',
'name' => 'fragment'
),
),
@$result[5]['after'],
$this->getParsedAsMessage('#fragment')
);
$this->assertEquals(
array(
array(
'module' => 'test-module',
'file' => '*',
'name' => 'fragment'
),
),
@$result[6]['after'],
$this->getParsedAsMessage('test-module#fragment')
);
$this->assertEquals(
array(
array(
'module' => 'test-module',
'file' => '*',
'name' => '*'
),
),
@$result[7]['after'],
$this->getParsedAsMessage('test-module')
);
$this->assertEquals(
array(
array(
'module' => 'test-module',
'file' => '*',
'name' => '*'
),
),
@$result[8]['after'],
$this->getParsedAsMessage('test-module/*')
);
$this->assertEquals(
array(
array(
'module' => 'test-module',
'file' => '*',
'name' => '*'
),
),
@$result[9]['after'],
$this->getParsedAsMessage('test-module/*/#*')
);
}
public function testClassRules() {
$config = $this->getConfigFixtureValue('Class');
$this->assertEquals(
'Yes', @$config['DirectorExists'],
'Only rule correctly detects existing class'
);
$this->assertEquals(
'No', @$config['NoSuchClassExists'],
'Except rule correctly detects missing class'
);
}
public function testModuleRules() {
$config = $this->getConfigFixtureValue('Module');
$this->assertEquals(
'Yes', @$config['MysiteExists'],
'Only rule correctly detects existing module'
);
$this->assertEquals(
'No', @$config['NoSuchModuleExists'],
'Except rule correctly detects missing module'
);
}
public function testEnvVarSetRules() {
$_ENV['EnvVarSet_Foo'] = 1;
$config = $this->getConfigFixtureValue('EnvVarSet');
$this->assertEquals(
'Yes', @$config['FooSet'],
'Only rule correctly detects set environment variable'
);
$this->assertEquals(
'No', @$config['BarSet'],
'Except rule correctly detects unset environment variable'
);
}
public function testConstantDefinedRules() {
define('ConstantDefined_Foo', 1);
$config = $this->getConfigFixtureValue('ConstantDefined');
$this->assertEquals(
'Yes', @$config['FooDefined'],
'Only rule correctly detects defined constant'
);
$this->assertEquals(
'No', @$config['BarDefined'],
'Except rule correctly detects undefined constant'
);
}
public function testEnvOrConstantMatchesValueRules() {
$_ENV['EnvOrConstantMatchesValue_Foo'] = 'Foo';
define('EnvOrConstantMatchesValue_Bar', 'Bar');
$config = $this->getConfigFixtureValue('EnvOrConstantMatchesValue');
$this->assertEquals(
'Yes', @$config['FooIsFoo'],
'Only rule correctly detects environment variable matches specified value'
);
$this->assertEquals(
'Yes', @$config['BarIsBar'],
'Only rule correctly detects constant matches specified value'
);
$this->assertEquals(
'No', @$config['FooIsQux'],
'Except rule correctly detects environment variable that doesn\'t match specified value'
);
$this->assertEquals(
'No', @$config['BarIsQux'],
'Except rule correctly detects environment variable that doesn\'t match specified value'
);
$this->assertEquals(
'No', @$config['BazIsBaz'],
'Except rule correctly detects undefined variable'
);
}
public function testEnvironmentRules() {
foreach (array('dev', 'test', 'live') as $env) {
Config::nest();
Config::inst()->update('Director', 'environment_type', $env);
$config = $this->getConfigFixtureValue('Environment');
foreach (array('dev', 'test', 'live') as $check) {
$this->assertEquals(
$env == $check ? $check : 'not'.$check, @$config[ucfirst($check).'Environment'],
'Only & except rules correctly detect environment'
);
}
Config::unnest();
}
}
public function testDynamicEnvironmentRules() {
// First, make sure environment_type is live
Config::inst()->update('Director', 'environment_type', 'live');
$this->assertEquals('live', Config::inst()->get('Director', 'environment_type'));
// Then, load in a new manifest, which includes a _config.php that sets environment_type to dev
$manifest = new SS_ConfigManifest(dirname(__FILE__).'/fixtures/configmanifest_dynamicenv', true, true);
Config::inst()->pushConfigYamlManifest($manifest);
// Make sure that stuck
$this->assertEquals('dev', Config::inst()->get('Director', 'environment_type'));
// And that the dynamic rule was calculated correctly
$this->assertEquals('dev', Config::inst()->get('ConfigManifestTest', 'DynamicEnvironment'));
}
public function testMultipleRules() {
$_ENV['MultilpleRules_EnvVariableSet'] = 1;
define('MultilpleRules_DefinedConstant', 'defined');
$config = $this->getConfigFixtureValue('MultipleRules');
$this->assertFalse(
isset($config['TwoOnlyFail']),
'Fragment is not included if one of the Only rules fails.'
);
$this->assertTrue(
isset($config['TwoOnlySucceed']),
'Fragment is included if both Only rules succeed.'
);
$this->assertTrue(
isset($config['TwoExceptSucceed']),
'Fragment is included if one of the Except rules matches.'
);
$this->assertFalse(
isset($config['TwoExceptFail']),
'Fragment is not included if both of the Except rules fail.'
);
$this->assertFalse(
isset($config['TwoBlocksFail']),
'Fragment is not included if one block fails.'
);
$this->assertTrue(
isset($config['TwoBlocksSucceed']),
'Fragment is included if both blocks succeed.'
);
}
public function testRelativeOrder() {
$accessor = new ConfigManifestTest_ConfigManifestAccess(BASE_PATH, true, false);
// A fragment with a fully wildcard before rule
$beforeWildcarded = array(
'module' => 'foo', 'file' => 'alpha', 'name' => '1',
'before' => array(array('module' => '*', 'file' => '*', 'name' => '*'))
);
// A fragment with a fully wildcard before rule and a fully explicit after rule
$beforeWildcardedAfterExplicit = array_merge($beforeWildcarded, array(
'after' => array(array('module' => 'bar', 'file' => 'beta', 'name' => '2'))
));
// A fragment with a fully wildcard before rule and two fully explicit after rules
$beforeWildcardedAfterTwoExplicitRules = array_merge($beforeWildcarded, array(
'after' => array(
array('module' => 'bar', 'file' => 'beta', 'name' => '2'),
array('module' => 'baz', 'file' => 'gamma', 'name' => '3')
)
));
// A fragment with a fully wildcard before rule and a partially explicit after rule
$beforeWildcardedAfterPartialWildcarded = array_merge($beforeWildcarded, array(
'after' => array(array('module' => 'bar', 'file' => 'beta', 'name' => '*'))
));
// Wildcard should match any module
$this->assertEquals($accessor->relativeOrder(
$beforeWildcarded,
array('module' => 'qux', 'file' => 'delta', 'name' => '4')
), 'before');
// Wildcard should match any module even if there is an opposing rule, if opposing rule doesn't match
$this->assertEquals($accessor->relativeOrder(
$beforeWildcardedAfterExplicit,
array('module' => 'qux', 'file' => 'delta', 'name' => '4')
), 'before');
// Wildcard should match any module even if there is an opposing rule, if opposing rule doesn't match, no
// matter how many opposing rules
$this->assertEquals($accessor->relativeOrder(
$beforeWildcardedAfterExplicit,
array('module' => 'qux', 'file' => 'delta', 'name' => '4')
), 'before');
// Wildcard should match any module even if there is an opposing rule, if opposing rule doesn't match
// (even if some portions do)
$this->assertEquals($accessor->relativeOrder(
$beforeWildcardedAfterExplicit,
array('module' => 'bar', 'file' => 'beta', 'name' => 'nomatchy')
), 'before');
// When opposing rule matches, wildcard should be ignored
$this->assertEquals($accessor->relativeOrder(
$beforeWildcardedAfterExplicit,
array('module' => 'bar', 'file' => 'beta', 'name' => '2')
), 'after');
// When any one of mutiple opposing rule exists, wildcard should be ignored
$this->assertEquals($accessor->relativeOrder(
$beforeWildcardedAfterTwoExplicitRules,
array('module' => 'bar', 'file' => 'beta', 'name' => '2')
), 'after');
$this->assertEquals($accessor->relativeOrder(
$beforeWildcardedAfterTwoExplicitRules,
array('module' => 'baz', 'file' => 'gamma', 'name' => '3')
), 'after');
// When two opposed wildcard rules, and more specific one doesn't match, other should win
$this->assertEquals($accessor->relativeOrder(
$beforeWildcardedAfterPartialWildcarded,
array('module' => 'qux', 'file' => 'delta', 'name' => '4')
), 'before');
// When two opposed wildcard rules, and more specific one does match, more specific one should win
$this->assertEquals($accessor->relativeOrder(
$beforeWildcardedAfterPartialWildcarded,
array('module' => 'bar', 'file' => 'beta', 'name' => 'wildcardmatchy')
), 'after');
}
}