f7faf74848b6fa6b7d7d37643dcaf7516a92d26e
[yaffs-website] / vendor / consolidation / config / tests / ConfigOverlayTest.php
1 <?php
2 namespace Consolidation\Config\Util;
3
4 use Consolidation\Config\Config;
5 use Consolidation\Config\Loader\ConfigProcessor;
6 use Consolidation\Config\Loader\YamlConfigLoader;
7
8 class ConfigOverlayTest extends \PHPUnit_Framework_TestCase
9 {
10     protected $overlay;
11
12     protected function setUp()
13     {
14         $aliasContext = new Config();
15         $aliasContext->import([
16             'hidden-by-a' => 'alias hidden-by-a',
17             'hidden-by-process' => 'alias hidden-by-process',
18             'options' =>[
19                 'a-a' => 'alias-a',
20             ],
21             'command' => [
22                 'foo' => [
23                     'bar' => [
24                         'command' => [
25                             'options' => [
26                                 'a-b' => 'alias-b',
27                             ],
28                         ],
29                     ],
30                 ],
31             ],
32         ]);
33
34         $configFileContext = new Config();
35         $configFileContext->import([
36             'hidden-by-cf' => 'config-file hidden-by-cf',
37             'hidden-by-a' => 'config-file hidden-by-a',
38             'hidden-by-process' => 'config-file hidden-by-process',
39             'options' =>[
40                 'cf-a' => 'config-file-a',
41             ],
42             'command' => [
43                 'foo' => [
44                     'bar' => [
45                         'command' => [
46                             'options' => [
47                                 'cf-b' => 'config-file-b',
48                             ],
49                         ],
50                     ],
51                 ],
52             ],
53         ]);
54
55         $this->overlay = new ConfigOverlay();
56         $this->overlay->set('hidden-by-process', 'process-h');
57         $this->overlay->addContext('cf', $configFileContext);
58         $this->overlay->addContext('a', $aliasContext);
59         $this->overlay->setDefault('df-a', 'default');
60         $this->overlay->setDefault('hidden-by-a', 'default hidden-by-a');
61         $this->overlay->setDefault('hidden-by-cf', 'default hidden-by-cf');
62         $this->overlay->setDefault('hidden-by-process', 'default hidden-by-process');
63     }
64
65     public function testGetPriority()
66     {
67         $this->assertEquals('process-h', $this->overlay->get('hidden-by-process'));
68         $this->assertEquals('config-file hidden-by-cf', $this->overlay->get('hidden-by-cf'));
69         $this->assertEquals('alias hidden-by-a', $this->overlay->get('hidden-by-a'));
70     }
71
72     public function testDefault()
73     {
74         $this->assertEquals('alias-a', $this->overlay->get('options.a-a'));
75         $this->assertEquals('alias-a', $this->overlay->get('options.a-a', 'ignored'));
76         $this->assertEquals('default', $this->overlay->getDefault('df-a', 'ignored'));
77         $this->assertEquals('nsv', $this->overlay->getDefault('a-a', 'nsv'));
78
79         $this->overlay->setDefault('df-a', 'new value');
80         $this->assertEquals('new value', $this->overlay->getDefault('df-a', 'ignored'));
81     }
82
83     public function testExport()
84     {
85         $data = $this->overlay->export();
86
87         $this->assertEquals('config-file-a', $data['options']['cf-a']);
88         $this->assertEquals('alias-a', $data['options']['a-a']);
89     }
90
91     /**
92      * @expectedException Exception
93      */
94     public function testImport()
95     {
96         $data = $this->overlay->import(['a' => 'value']);
97     }
98
99     public function testMaintainPriority()
100     {
101         // Get and re-add the 'cf' context. Its priority should not change.
102         $configFileContext = $this->overlay->getContext('cf');
103         $this->overlay->addContext('cf', $configFileContext);
104
105         // These asserts are the same as in testGetPriority
106         $this->assertEquals('process-h', $this->overlay->get('hidden-by-process'));
107         $this->assertEquals('config-file hidden-by-cf', $this->overlay->get('hidden-by-cf'));
108         $this->assertEquals('alias hidden-by-a', $this->overlay->get('hidden-by-a'));
109     }
110
111     public function testChangePriority()
112     {
113         // Increase the priority of the 'cf' context. Now, it should have a higher
114         // priority than the 'alias' context, but should still have a lower
115         // priority than the 'process' context.
116         $this->overlay->increasePriority('cf');
117
118         // These asserts are the same as in testGetPriority
119         $this->assertEquals('process-h', $this->overlay->get('hidden-by-process'));
120         $this->assertEquals('config-file hidden-by-cf', $this->overlay->get('hidden-by-cf'));
121
122         // This one has changed: the config-file value is now found instead
123         // of the alias value.
124         $this->assertEquals('config-file hidden-by-a', $this->overlay->get('hidden-by-a'));
125     }
126
127     public function testPlaceholder()
128     {
129         $this->overlay->addPlaceholder('lower');
130
131         $higherContext = new Config();
132         $higherContext->import(['priority-test' => 'higher']);
133
134         $lowerContext = new Config();
135         $lowerContext->import(['priority-test' => 'lower']);
136
137         // Usually 'lower' would have the highest priority, since it is
138         // added last. However, our earlier call to 'addPlaceholder' reserves
139         // a spot for it, so the 'higher' context will end up with a higher
140         // priority.
141         $this->overlay->addContext('higher', $higherContext);
142         $this->overlay->addContext('lower', $lowerContext);
143         $this->assertEquals('higher', $this->overlay->get('priority-test', 'neither'));
144
145         // Test to see that we can change the value of the 'higher' context,
146         // and the change will be reflected in the overlay.
147         $higherContext->set('priority-test', 'changed');
148         $this->assertEquals('changed', $this->overlay->get('priority-test', 'neither'));
149
150         // Test to see that the 'process' context still has the highest priority.
151         $this->overlay->set('priority-test', 'process');
152         $higherContext->set('priority-test', 'ignored');
153         $this->assertEquals('process', $this->overlay->get('priority-test', 'neither'));
154     }
155
156     public function testDoesNotHave()
157     {
158         $context = $this->overlay->getContext('no-such-context');
159         $data = $context->export();
160         $this->assertEquals('[]', json_encode($data));
161
162         $this->assertTrue(!$this->overlay->has('no-such-key'));
163         $this->assertTrue(!$this->overlay->hasDefault('no-such-default'));
164
165         $this->assertEquals('no-such-value', $this->overlay->get('no-such-key', 'no-such-value'));
166
167     }
168 }