diff options
author | diosmosis <benaka@piwik.pro> | 2015-06-02 21:20:09 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-06-06 00:35:44 +0300 |
commit | cc9ee9f6111a8d7522dce014a47fda0730aabcaa (patch) | |
tree | b4d82f2c739cbc4a3a6358db98de064976053856 | |
parent | 70d8a9fe42e14305b80ded80d73ef4f729c5743e (diff) |
Use custom DefinitionSource to access TestingEnvironmentVariables so those variables can be easily overridden by test classes through provideContainerConfig(BeforeClass).
-rw-r--r-- | config/environment/test.php | 21 | ||||
-rw-r--r-- | core/Container/ContainerFactory.php | 5 | ||||
-rw-r--r-- | tests/PHPUnit/Framework/TestingEnvironmentVariablesDefinitionSource.php | 58 | ||||
-rw-r--r-- | tests/PHPUnit/System/AutoSuggestAPITest.php | 1 |
4 files changed, 74 insertions, 11 deletions
diff --git a/config/environment/test.php b/config/environment/test.php index ce71427b50..8174e8b057 100644 --- a/config/environment/test.php +++ b/config/environment/test.php @@ -4,7 +4,6 @@ use Interop\Container\ContainerInterface; use Piwik\Common; use Piwik\Tests\Framework\Mock\FakeAccess; use Piwik\Tests\Framework\Mock\TestConfig; -use Piwik\Tests\Framework\TestingEnvironmentVariables; return array( @@ -18,8 +17,8 @@ return array( // Disable loading core translations 'Piwik\Translation\Translator' => DI\decorate(function ($previous, ContainerInterface $c) { - $testingEnvironment = $c->get('Piwik\Tests\Framework\TestingEnvironmentVariables'); - if (!$testingEnvironment->loadRealTranslations) { + $loadRealTranslations = $c->get('test.vars.loadRealTranslations'); + if (!$loadRealTranslations) { return new \Piwik\Translation\Translator($c->get('Piwik\Translation\Loader\LoaderInterface'), $directories = array()); } else { return $previous; @@ -28,7 +27,9 @@ return array( 'Piwik\Config' => DI\decorate(function ($previous, ContainerInterface $c) { $testingEnvironment = $c->get('Piwik\Tests\Framework\TestingEnvironmentVariables'); - if (!$testingEnvironment->dontUseTestConfig) { + + $dontUseTestConfig = $c->get('test.vars.dontUseTestConfig'); + if (!$dontUseTestConfig) { $settingsProvider = $c->get('Piwik\Application\Kernel\GlobalSettingsProvider'); return new TestConfig($settingsProvider, $testingEnvironment, $allowSave = false, $doSetTestEnvironment = true); } else { @@ -37,8 +38,8 @@ return array( }), 'Piwik\Access' => DI\decorate(function ($previous, ContainerInterface $c) { - $testingEnvironment = $c->get('Piwik\Tests\Framework\TestingEnvironmentVariables'); - if ($testingEnvironment->testUseMockAuth) { + $testUseMockAuth = $c->get('test.vars.testUseMockAuth'); + if ($testUseMockAuth) { $access = new FakeAccess(); FakeAccess::$superUser = true; FakeAccess::$superUserLogin = 'superUserLogin'; @@ -51,15 +52,15 @@ return array( 'observers.global' => DI\add(array( array('AssetManager.getStylesheetFiles', function (&$stylesheets) { - $testingEnvironment = new TestingEnvironmentVariables(); - if ($testingEnvironment->useOverrideCss) { + $useOverrideCss = \Piwik\Container\StaticContainer::get('test.vars.useOverrideCss'); + if ($useOverrideCss) { $stylesheets[] = 'tests/resources/screenshot-override/override.css'; } }), array('AssetManager.getJavaScriptFiles', function (&$jsFiles) { - $testingEnvironment = new TestingEnvironmentVariables(); - if ($testingEnvironment->useOverrideJs) { + $useOverrideJs = \Piwik\Container\StaticContainer::get('test.vars.useOverrideJs'); + if ($useOverrideJs) { $jsFiles[] = 'tests/resources/screenshot-override/override.js'; } }), diff --git a/core/Container/ContainerFactory.php b/core/Container/ContainerFactory.php index 83a081f448..9586fe098e 100644 --- a/core/Container/ContainerFactory.php +++ b/core/Container/ContainerFactory.php @@ -14,6 +14,8 @@ use Doctrine\Common\Cache\ArrayCache; use Piwik\Application\Kernel\GlobalSettingsProvider; use Piwik\Application\Kernel\PluginList; use Piwik\Plugin\Manager; +use Piwik\Tests\Framework\TestingEnvironmentVariables; +use Piwik\Tests\Framework\TestingEnvironmentVariablesDefinitionSource; /** * Creates a configured DI container. @@ -92,6 +94,9 @@ class ContainerFactory // Test config if (defined('PIWIK_TEST_MODE')) { + $vars = new TestingEnvironmentVariables(); + $builder->addDefinitions(new TestingEnvironmentVariablesDefinitionSource($vars)); + $this->addEnvironmentConfig($builder, 'test'); } diff --git a/tests/PHPUnit/Framework/TestingEnvironmentVariablesDefinitionSource.php b/tests/PHPUnit/Framework/TestingEnvironmentVariablesDefinitionSource.php new file mode 100644 index 0000000000..41034dd0d9 --- /dev/null +++ b/tests/PHPUnit/Framework/TestingEnvironmentVariablesDefinitionSource.php @@ -0,0 +1,58 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Tests\Framework; + +use DI\Definition\Source\DefinitionSource; +use DI\Definition\ValueDefinition; + +/** + * PHP DI definition source that accesses variables defined in TestingEnvironmentVariables. + */ +class TestingEnvironmentVariablesDefinitionSource implements DefinitionSource +{ + /** + * @var TestingEnvironmentVariables + */ + private $vars; + + /** + * @var string + */ + private $prefix; + + /** + * @param TestingEnvironmentVariables $vars + * @param string $prefix + */ + public function __construct(TestingEnvironmentVariables $vars, $prefix = 'test.vars.') + { + $this->vars = $vars; + $this->prefix = $prefix; + } + + /** + * {@inheritdoc} + */ + public function getDefinition($name) + { + if (strpos($name, $this->prefix) !== 0) { + return null; + } + + $variableName = $this->parseVariableName($name); + + return new ValueDefinition($name, $this->vars->$variableName); + } + + private function parseVariableName($name) + { + $parts = explode('.', $name, 3); + return @$parts[2]; + } +}
\ No newline at end of file diff --git a/tests/PHPUnit/System/AutoSuggestAPITest.php b/tests/PHPUnit/System/AutoSuggestAPITest.php index 2b08d2ef70..d08209b1d8 100644 --- a/tests/PHPUnit/System/AutoSuggestAPITest.php +++ b/tests/PHPUnit/System/AutoSuggestAPITest.php @@ -164,7 +164,6 @@ class AutoSuggestAPITest extends SystemTestCase $segments = \Piwik\Plugins\API\API::getInstance()->getSegmentsMetadata($idSite); return $segments; } - } AutoSuggestAPITest::$fixture = new ManyVisitsWithGeoIP(); |