Passed
Push — 4.0 ( 5a8314...d5e2d3 )
by Daniel
07:31
created

ConfigManifestTest::getParsedAsMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Core\Tests\Manifest;
4
5
use SilverStripe\Config\Collections\MemoryConfigCollection;
6
use SilverStripe\Core\Config\CoreConfigFactory;
7
use SilverStripe\Core\Environment;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Core\Kernel;
10
use SilverStripe\Core\Manifest\ModuleLoader;
11
use SilverStripe\Core\Manifest\ModuleManifest;
12
use SilverStripe\Dev\SapphireTest;
13
14
class ConfigManifestTest extends SapphireTest
15
{
16
    protected function setUp()
17
    {
18
        parent::setUp();
19
20
        $moduleManifest = new ModuleManifest(dirname(__FILE__) . '/fixtures/configmanifest');
21
        $moduleManifest->init();
22
        ModuleLo    ader::inst()->pushManifest($moduleManifest);
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING on line 22 at column 20
Loading history...
23
    }
24
25
    protected function tearDown()
26
    {
27
        ModuleLoader::inst()->popManifest();
28
        parent::tearDown();
29
    }
30
31
    /**
32
     * This is a helper method for getting a new manifest
33
     *
34
     * @param string $name
35
     * @return mixed
36
     */
37
    protected function getConfigFixtureValue($name)
38
    {
39
        return $this->getTestConfig()->get(__CLASS__, $name);
40
    }
41
42
    /**
43
     * Build a new config based on YMl manifest
44
     *
45
     * @return MemoryConfigCollection
46
     */
47
    public function getTestConfig()
48
    {
49
        $config = new MemoryConfigCollection();
50
        $factory = new CoreConfigFactory();
51
        $transformer = $factory->buildYamlTransformerForPath(dirname(__FILE__) . '/fixtures/configmanifest');
52
        $config->transform([$transformer]);
53
        return $config;
54
    }
55
56
    /**
57
     * This is a helper method for displaying a relevant message about a parsing failure
58
     *
59
     * @param string $path
60
     * @return string
61
     */
62
    protected function getParsedAsMessage($path)
63
    {
64
        return sprintf('Reference path "%s" failed to parse correctly', $path);
65
    }
66
67
    public function testClassRules()
68
    {
69
        $config = $this->getConfigFixtureValue('Class');
70
71
        $this->assertEquals(
72
            'Yes',
73
            @$config['DirectorExists'],
74
            'Only rule correctly detects existing class'
75
        );
76
77
        $this->assertEquals(
78
            'No',
79
            @$config['NoSuchClassExists'],
80
            'Except rule correctly detects missing class'
81
        );
82
    }
83
84
    public function testModuleRules()
85
    {
86
        $config = $this->getConfigFixtureValue('Module');
87
88
        $this->assertEquals(
89
            'Yes',
90
            @$config['MysiteExists'],
91
            'Only rule correctly detects existing module'
92
        );
93
94
        $this->assertEquals(
95
            'No',
96
            @$config['NoSuchModuleExists'],
97
            'Except rule correctly detects missing module'
98
        );
99
    }
100
101
    public function testEnvVarSetRules()
102
    {
103
        Environment::setEnv('ENVVARSET_FOO', '1');
104
        $config = $this->getConfigFixtureValue('EnvVarSet');
105
106
        $this->assertEquals(
107
            'Yes',
108
            @$config['FooSet'],
109
            'Only rule correctly detects set environment variable'
110
        );
111
112
        $this->assertEquals(
113
            'No',
114
            @$config['BarSet'],
115
            'Except rule correctly detects unset environment variable'
116
        );
117
    }
118
119
    public function testConstantDefinedRules()
120
    {
121
        define('CONSTANTDEFINED_FOO', 1);
122
        $config = $this->getConfigFixtureValue('ConstantDefined');
123
124
        $this->assertEquals(
125
            'Yes',
126
            @$config['FooDefined'],
127
            'Only rule correctly detects defined constant'
128
        );
129
130
        $this->assertEquals(
131
            'No',
132
            @$config['BarDefined'],
133
            'Except rule correctly detects undefined constant'
134
        );
135
    }
136
137
    public function testEnvOrConstantMatchesValueRules()
138
    {
139
        Environment::setEnv('CONSTANTMATCHESVALUE_FOO', 'Foo');
140
        define('CONSTANTMATCHESVALUE_BAR', 'Bar');
141
        $config = $this->getConfigFixtureValue('EnvOrConstantMatchesValue');
142
143
        $this->assertEquals(
144
            'Yes',
145
            @$config['FooIsFoo'],
146
            'Only rule correctly detects environment variable matches specified value'
147
        );
148
149
        $this->assertEquals(
150
            'Yes',
151
            @$config['BarIsBar'],
152
            'Only rule correctly detects constant matches specified value'
153
        );
154
155
        $this->assertEquals(
156
            'No',
157
            @$config['FooIsQux'],
158
            'Except rule correctly detects environment variable that doesn\'t match specified value'
159
        );
160
161
        $this->assertEquals(
162
            'No',
163
            @$config['BarIsQux'],
164
            'Except rule correctly detects environment variable that doesn\'t match specified value'
165
        );
166
167
        $this->assertEquals(
168
            'No',
169
            @$config['BazIsBaz'],
170
            'Except rule correctly detects undefined variable'
171
        );
172
    }
173
174
    public function testEnvironmentRules()
175
    {
176
        /** @var Kernel $kernel */
177
        $kernel = Injector::inst()->get(Kernel::class);
178
        foreach (array('dev', 'test', 'live') as $env) {
179
            $kernel->setEnvironment($env);
180
            $config = $this->getConfigFixtureValue('Environment');
181
182
            foreach (array('dev', 'test', 'live') as $check) {
183
                $this->assertEquals(
184
                    $env == $check ? $check : 'not' . $check,
185
                    @$config[ucfirst($check) . 'Environment'],
186
                    'Only & except rules correctly detect environment in env ' . $env
187
                );
188
            }
189
        }
190
    }
191
192
    public function testMultipleRules()
193
    {
194
        Environment::setEnv('MULTIPLERULES_ENVVARIABLESET', '1');
195
        define('MULTIPLERULES_DEFINEDCONSTANT', 'defined');
196
        $config = $this->getConfigFixtureValue('MultipleRules');
197
198
        $this->assertFalse(
199
            isset($config['TwoOnlyFail']),
200
            'Fragment is not included if one of the Only rules fails.'
201
        );
202
203
        $this->assertTrue(
204
            isset($config['TwoOnlySucceed']),
205
            'Fragment is included if both Only rules succeed.'
206
        );
207
208
        $this->assertFalse(
209
            isset($config['OneExceptFail']),
210
            'Fragment is not included if one of the Except rules fails.'
211
        );
212
213
        $this->assertFalse(
214
            isset($config['TwoExceptFail']),
215
            'Fragment is not included if both of the Except rules fail.'
216
        );
217
218
        $this->assertFalse(
219
            isset($config['TwoBlocksFail']),
220
            'Fragment is not included if one block fails.'
221
        );
222
223
        $this->assertTrue(
224
            isset($config['TwoBlocksSucceed']),
225
            'Fragment is included if both blocks succeed.'
226
        );
227
    }
228
}
229