FIX Only and Except rules in Configs not working

This commit is contained in:
Hamish Friedlander 2013-06-28 13:46:32 +12:00
parent 03aa9e4b41
commit e74c002647
7 changed files with 320 additions and 20 deletions

View File

@ -396,10 +396,17 @@ class SS_ConfigManifest {
$matchingFragments = array(); $matchingFragments = array();
foreach ($this->yamlConfigFragments as $i => $fragment) { foreach ($this->yamlConfigFragments as $i => $fragment) {
$failsonly = isset($fragment['only']) && !$this->matchesPrefilterVariantRules($fragment['only']); $matches = true;
$matchesexcept = isset($fragment['except']) && $this->matchesPrefilterVariantRules($fragment['except']);
if (!$failsonly && !$matchesexcept) $matchingFragments[] = $fragment; if (isset($fragment['only'])) {
$matches = $matches && ($this->matchesPrefilterVariantRules($fragment['only']) !== false);
}
if (isset($fragment['except'])) {
$matches = $matches && ($this->matchesPrefilterVariantRules($fragment['except']) !== true);
}
if ($matches) $matchingFragments[] = $fragment;
} }
$this->yamlConfigFragments = $matchingFragments; $this->yamlConfigFragments = $matchingFragments;
@ -414,22 +421,26 @@ class SS_ConfigManifest {
* which values means accept or reject a fragment * which values means accept or reject a fragment
*/ */
public function matchesPrefilterVariantRules($rules) { public function matchesPrefilterVariantRules($rules) {
$matches = "undefined"; // Needs to be truthy, but not true
foreach ($rules as $k => $v) { foreach ($rules as $k => $v) {
switch (strtolower($k)) { switch (strtolower($k)) {
case 'classexists': case 'classexists':
if (!ClassInfo::exists($v)) return false; $matches = $matches && ClassInfo::exists($v);
break; break;
case 'moduleexists': case 'moduleexists':
if (!$this->moduleExists($v)) return false; $matches = $matches && $this->moduleExists($v);
break; break;
default: default:
// NOP // NOP
} }
if ($matches === false) return $matches;
} }
return true; return $matches;
} }
/** /**
@ -487,10 +498,17 @@ class SS_ConfigManifest {
$this->yamlConfig = array(); $this->yamlConfig = array();
foreach ($this->yamlConfigFragments as $i => $fragment) { foreach ($this->yamlConfigFragments as $i => $fragment) {
$failsonly = isset($fragment['only']) && !$this->matchesVariantRules($fragment['only']); $matches = true;
$matchesexcept = isset($fragment['except']) && $this->matchesVariantRules($fragment['except']);
if (!$failsonly && !$matchesexcept) $this->mergeInYamlFragment($this->yamlConfig, $fragment['fragment']); if (isset($fragment['only'])) {
$matches = $matches && ($this->matchesVariantRules($fragment['only']) !== false);
}
if (isset($fragment['except'])) {
$matches = $matches && ($this->matchesVariantRules($fragment['except']) !== true);
}
if ($matches) $this->mergeInYamlFragment($this->yamlConfig, $fragment['fragment']);
} }
if ($cache) { if ($cache) {
@ -502,6 +520,8 @@ class SS_ConfigManifest {
* Returns false if the non-prefilterable parts of the rule aren't met, and true if they are * Returns false if the non-prefilterable parts of the rule aren't met, and true if they are
*/ */
public function matchesVariantRules($rules) { public function matchesVariantRules($rules) {
$matches = "undefined"; // Needs to be truthy, but not true
foreach ($rules as $k => $v) { foreach ($rules as $k => $v) {
switch (strtolower($k)) { switch (strtolower($k)) {
case 'classexists': case 'classexists':
@ -511,13 +531,13 @@ class SS_ConfigManifest {
case 'environment': case 'environment':
switch (strtolower($v)) { switch (strtolower($v)) {
case 'live': case 'live':
if (!Director::isLive()) return false; $matches = $matches && Director::isLive();
break; break;
case 'test': case 'test':
if (!Director::isTest()) return false; $matches = $matches && Director::isTest();
break; break;
case 'dev': case 'dev':
if (!Director::isDev()) return false; $matches = $matches && Director::isDev();
break; break;
default: default:
user_error('Unknown environment '.$v.' in config fragment', E_USER_ERROR); user_error('Unknown environment '.$v.' in config fragment', E_USER_ERROR);
@ -525,21 +545,25 @@ class SS_ConfigManifest {
break; break;
case 'envvarset': case 'envvarset':
if (isset($_ENV[$k])) break; $matches = $matches && isset($_ENV[$v]);
return false; break;
case 'constantdefined': case 'constantdefined':
if (defined($k)) break; $matches = $matches && defined($v);
return false; break;
default: default:
if (isset($_ENV[$k]) && $_ENV[$k] == $v) break; $matches = $matches && (
if (defined($k) && constant($k) == $v) break; (isset($_ENV[$k]) && $_ENV[$k] == $v) ||
return false; (defined($k) && constant($k) == $v)
} );
break;
} }
return true; if ($matches === false) return $matches;
}
return $matches;
} }
/** /**

View File

@ -8,6 +8,100 @@ class ConfigManifestTest_ConfigManifestAccess extends SS_ConfigManifest {
class ConfigManifestTest extends SapphireTest { class ConfigManifestTest extends SapphireTest {
protected function getConfigFixture() {
$manifest = new SS_ConfigManifest(dirname(__FILE__).'/fixtures/configmanifest', true, true);
return $manifest->yamlConfig;
}
public function testClassRules() {
$config = $this->getConfigFixture();
$this->assertEquals(
'Yes', @$config['ConfigManifestTest']['Class']['DirectorExists'],
'Only rule correctly detects existing class'
);
$this->assertEquals(
'No', @$config['ConfigManifestTest']['Class']['NoSuchClassExists'],
'Except rule correctly detects missing class'
);
}
public function testModuleRules() {
$config = $this->getConfigFixture();
$this->assertEquals(
'Yes', @$config['ConfigManifestTest']['Module']['MysiteExists'],
'Only rule correctly detects existing module'
);
$this->assertEquals(
'No', @$config['ConfigManifestTest']['Module']['NoSuchModuleExists'],
'Except rule correctly detects missing module'
);
}
public function testEnvVarSetRules() {
$_ENV['EnvVarSet_Foo'] = 1;
$config = $this->getConfigFixture();
$this->assertEquals(
'Yes', @$config['ConfigManifestTest']['EnvVarSet']['FooSet'],
'Only rule correctly detects set environment variable'
);
$this->assertEquals(
'No', @$config['ConfigManifestTest']['EnvVarSet']['BarSet'],
'Except rule correctly detects unset environment variable'
);
}
public function testConstantDefinedRules() {
define('ConstantDefined_Foo', 1);
$config = $this->getConfigFixture();
$this->assertEquals(
'Yes', @$config['ConfigManifestTest']['ConstantDefined']['FooDefined'],
'Only rule correctly detects defined constant'
);
$this->assertEquals(
'No', @$config['ConfigManifestTest']['ConstantDefined']['BarDefined'],
'Except rule correctly detects undefined constant'
);
}
public function testEnvOrConstantMatchesValueRules() {
$_ENV['EnvOrConstantMatchesValue_Foo'] = 'Foo';
define('EnvOrConstantMatchesValue_Bar', 'Bar');
$config = $this->getConfigFixture();
$this->assertEquals(
'Yes', @$config['ConfigManifestTest']['EnvOrConstantMatchesValue']['FooIsFoo'],
'Only rule correctly detects environment variable matches specified value'
);
$this->assertEquals(
'Yes', @$config['ConfigManifestTest']['EnvOrConstantMatchesValue']['BarIsBar'],
'Only rule correctly detects constant matches specified value'
);
$this->assertEquals(
'No', @$config['ConfigManifestTest']['EnvOrConstantMatchesValue']['FooIsQux'],
'Except rule correctly detects environment variable that doesn\'t match specified value'
);
$this->assertEquals(
'No', @$config['ConfigManifestTest']['EnvOrConstantMatchesValue']['BarIsQux'],
'Except rule correctly detects environment variable that doesn\'t match specified value'
);
$this->assertEquals(
'No', @$config['ConfigManifestTest']['EnvOrConstantMatchesValue']['BazIsBaz'],
'Except rule correctly detects undefined variable'
);
}
public function testRelativeOrder() { public function testRelativeOrder() {
$accessor = new ConfigManifestTest_ConfigManifestAccess(BASE_PATH, true, false); $accessor = new ConfigManifestTest_ConfigManifestAccess(BASE_PATH, true, false);

View File

@ -0,0 +1,28 @@
---
Only:
ClassExists: Director
---
ConfigManifestTest:
Class:
DirectorExists: Yes
---
Only:
ClassExists: NoSuchClass
---
ConfigManifestTest:
Class:
NoSuchClassExists: Yes
---
Except:
ClassExists: Director
---
ConfigManifestTest:
Class:
DirectorExists: No
---
Except:
ClassExists: NoSuchClass
---
ConfigManifestTest:
Class:
NoSuchClassExists: No

View File

@ -0,0 +1,28 @@
---
Only:
ConstantDefined: ConstantDefined_Foo
---
ConfigManifestTest:
ConstantDefined:
FooDefined: Yes
---
Only:
ConstantDefined: ConstantDefined_Bar
---
ConfigManifestTest:
ConstantDefined:
BarDefined: Yes
---
Except:
ConstantDefined: ConstantDefined_Foo
---
ConfigManifestTest:
ConstantDefined:
FooDefined: No
---
Except:
ConstantDefined: ConstantDefined_Bar
---
ConfigManifestTest:
ConstantDefined:
BarDefined: No

View File

@ -0,0 +1,28 @@
---
Only:
EnvVarSet: EnvVarSet_Foo
---
ConfigManifestTest:
EnvVarSet:
FooSet: Yes
---
Only:
EnvVarSet: EnvVarSet_Bar
---
ConfigManifestTest:
EnvVarSet:
BarSet: Yes
---
Except:
EnvVarSet: EnvVarSet_Foo
---
ConfigManifestTest:
EnvVarSet:
FooSet: No
---
Except:
EnvVarSet: EnvVarSet_Bar
---
ConfigManifestTest:
EnvVarSet:
BarSet: No

View File

@ -0,0 +1,28 @@
---
Only:
ModuleExists: mysite
---
ConfigManifestTest:
Module:
MysiteExists: Yes
---
Only:
ModuleExists: nosuchmodule
---
ConfigManifestTest:
Module:
NoSuchModuleExists: Yes
---
Except:
ModuleExists: mysite
---
ConfigManifestTest:
Module:
MysiteExists: No
---
Except:
ModuleExists: nosuchmodule
---
ConfigManifestTest:
Module:
NoSuchModuleExists: No

View File

@ -0,0 +1,70 @@
---
Only:
EnvOrConstantMatchesValue_Foo: Foo
---
ConfigManifestTest:
EnvOrConstantMatchesValue:
FooIsFoo: Yes
---
Only:
EnvOrConstantMatchesValue_Foo: Qux
---
ConfigManifestTest:
EnvOrConstantMatchesValue:
FooIsQux: Yes
---
Only:
EnvOrConstantMatchesValue_Bar: Bar
---
ConfigManifestTest:
EnvOrConstantMatchesValue:
BarIsBar: Yes
---
Only:
EnvOrConstantMatchesValue_Bar: Qux
---
ConfigManifestTest:
EnvOrConstantMatchesValue:
BarIsQux: Yes
---
Only:
EnvOrConstantMatchesValue_Baz: Baz
---
ConfigManifestTest:
EnvOrConstantMatchesValue:
BazIsBaz: Yes
---
Except:
EnvOrConstantMatchesValue_Foo: Foo
---
ConfigManifestTest:
EnvOrConstantMatchesValue:
FooIsFoo: No
---
Except:
EnvOrConstantMatchesValue_Foo: Qux
---
ConfigManifestTest:
EnvOrConstantMatchesValue:
FooIsQux: No
---
Except:
EnvOrConstantMatchesValue_Bar: Bar
---
ConfigManifestTest:
EnvOrConstantMatchesValue:
BarIsBar: No
---
Except:
EnvOrConstantMatchesValue_Bar: Qux
---
ConfigManifestTest:
EnvOrConstantMatchesValue:
BarIsQux: No
---
Except:
EnvOrConstantMatchesValue_Baz: Baz
---
ConfigManifestTest:
EnvOrConstantMatchesValue:
BazIsBaz: No