2012-08-27 06:42:25 +02:00
|
|
|
<?php
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
namespace SilverStripe\Core\Tests\Manifest;
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
use Dotenv\Loader;
|
|
|
|
use SilverStripe\Config\Collections\MemoryConfigCollection;
|
2016-12-16 05:34:21 +01:00
|
|
|
use SilverStripe\Control\Director;
|
2017-02-26 04:52:33 +01:00
|
|
|
use SilverStripe\Core\Config\CoreConfigFactory;
|
2017-02-27 02:38:06 +01:00
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
|
|
|
use SilverStripe\Core\Manifest\ModuleManifest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
class ConfigManifestTest extends SapphireTest
|
|
|
|
{
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2017-02-27 02:38:06 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$moduleManifest = new ModuleManifest(dirname(__FILE__) . '/fixtures/configmanifest');
|
|
|
|
ModuleLoader::instance()->pushManifest($moduleManifest);
|
|
|
|
}
|
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function tearDown()
|
2017-02-27 02:38:06 +01:00
|
|
|
{
|
|
|
|
ModuleLoader::instance()->popManifest();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* This is a helper method for getting a new manifest
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function getConfigFixtureValue($name)
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
return $this->getTestConfig()->get(__CLASS__, $name);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-22 04:15:08 +01:00
|
|
|
* Build a new config based on YMl manifest
|
2016-12-16 05:34:21 +01:00
|
|
|
*
|
2017-02-22 04:15:08 +01:00
|
|
|
* @return MemoryConfigCollection
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
2017-02-22 04:15:08 +01:00
|
|
|
public function getTestConfig()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
$config = new MemoryConfigCollection();
|
2017-02-26 04:52:33 +01:00
|
|
|
$transformer = CoreConfigFactory::inst()->buildYamlTransformerForPath(dirname(__FILE__) . '/fixtures/configmanifest');
|
2017-02-22 04:15:08 +01:00
|
|
|
$config->transform([$transformer]);
|
|
|
|
return $config;
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 13:23:41 +01:00
|
|
|
/**
|
2017-02-22 04:15:08 +01:00
|
|
|
* This is a helper method for displaying a relevant message about a parsing failure
|
2017-01-17 13:23:41 +01:00
|
|
|
*
|
2017-02-22 04:15:08 +01:00
|
|
|
* @param string $path
|
|
|
|
* @return string
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
2017-02-22 04:15:08 +01:00
|
|
|
protected function getParsedAsMessage($path)
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
return sprintf('Reference path "%s" failed to parse correctly', $path);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
$loader = new Loader(null);
|
2017-01-30 16:33:56 +01:00
|
|
|
$loader->setEnvironmentVariable('ENVVARSET_FOO', 1);
|
2016-12-16 05:34:21 +01:00
|
|
|
$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()
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
$loader = new Loader(null);
|
2017-01-30 16:33:56 +01:00
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
$loader->setEnvironmentVariable('CONSTANTMATCHESVALUE_FOO', 'Foo');
|
|
|
|
define('CONSTANTMATCHESVALUE_BAR', 'Bar');
|
2016-12-16 05:34:21 +01:00
|
|
|
$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) {
|
2017-02-22 04:15:08 +01:00
|
|
|
Director::set_environment_type($env);
|
2016-12-16 05:34:21 +01:00
|
|
|
$config = $this->getConfigFixtureValue('Environment');
|
|
|
|
|
|
|
|
foreach (array('dev', 'test', 'live') as $check) {
|
|
|
|
$this->assertEquals(
|
|
|
|
$env == $check ? $check : 'not'.$check,
|
|
|
|
@$config[ucfirst($check).'Environment'],
|
2017-02-22 04:15:08 +01:00
|
|
|
'Only & except rules correctly detect environment in env ' . $env
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleRules()
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
$loader = new Loader(null);
|
2017-01-30 16:33:56 +01:00
|
|
|
|
|
|
|
$loader->setEnvironmentVariable('MULTIPLERULES_ENVVARIABLESET', 1);
|
2016-12-16 05:34:21 +01:00
|
|
|
define('MULTIPLERULES_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.'
|
|
|
|
);
|
|
|
|
}
|
2013-07-02 05:51:53 +02:00
|
|
|
}
|