2 namespace Consolidation\Config\Util;
4 use Consolidation\Config\Config;
5 use Consolidation\Config\Loader\ConfigProcessor;
6 use Consolidation\Config\Loader\YamlConfigLoader;
8 class ConfigOverlayTest extends \PHPUnit_Framework_TestCase
12 protected function setUp()
14 $aliasContext = new Config();
15 $aliasContext->import([
16 'hidden-by-a' => 'alias hidden-by-a',
17 'hidden-by-process' => 'alias hidden-by-process',
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',
40 'cf-a' => 'config-file-a',
47 'cf-b' => 'config-file-b',
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');
65 public function testGetPriority()
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'));
72 public function testDefault()
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'));
79 $this->overlay->setDefault('df-a', 'new value');
80 $this->assertEquals('new value', $this->overlay->getDefault('df-a', 'ignored'));
83 public function testExport()
85 $data = $this->overlay->export();
87 $this->assertEquals('config-file-a', $data['options']['cf-a']);
88 $this->assertEquals('alias-a', $data['options']['a-a']);
92 * @expectedException Exception
94 public function testImport()
96 $data = $this->overlay->import(['a' => 'value']);
99 public function testMaintainPriority()
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);
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'));
111 public function testChangePriority()
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');
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'));
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'));
127 public function testPlaceholder()
129 $this->overlay->addPlaceholder('lower');
131 $higherContext = new Config();
132 $higherContext->import(['priority-test' => 'higher']);
134 $lowerContext = new Config();
135 $lowerContext->import(['priority-test' => 'lower']);
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
141 $this->overlay->addContext('higher', $higherContext);
142 $this->overlay->addContext('lower', $lowerContext);
143 $this->assertEquals('higher', $this->overlay->get('priority-test', 'neither'));
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'));
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'));
156 public function testDoesNotHave()
158 $context = $this->overlay->getContext('no-such-context');
159 $data = $context->export();
160 $this->assertEquals('[]', json_encode($data));
162 $this->assertTrue(!$this->overlay->has('no-such-key'));
163 $this->assertTrue(!$this->overlay->hasDefault('no-such-default'));
165 $this->assertEquals('no-such-value', $this->overlay->get('no-such-key', 'no-such-value'));