From 9d1dac06d03ccb482058b6dc35c1e76e44760138 Mon Sep 17 00:00:00 2001 From: dank00 Date: Thu, 4 Jan 2018 12:02:05 +0000 Subject: [PATCH 1/3] Add classes to facilitate environment config saving --- src/ConfigValue.php | 45 +++++++++++++++++++++++ src/ConfigValueRepository.php | 26 +++++++++++++ src/ConfigValueSet.php | 55 ++++++++++++++++++++++++++++ src/Environment.php | 17 +++++++++ src/EnvironmentConfigValues.php | 29 +++++++++++++++ src/Scope.php | 27 ++++++++++++++ test/ConfigValueRepositoryTest.php | 48 ++++++++++++++++++++++++ test/EnvironmentConfigValuesTest.php | 50 +++++++++++++++++++++++++ 8 files changed, 297 insertions(+) create mode 100644 src/ConfigValue.php create mode 100644 src/ConfigValueRepository.php create mode 100644 src/ConfigValueSet.php create mode 100644 src/Environment.php create mode 100644 src/EnvironmentConfigValues.php create mode 100644 src/Scope.php create mode 100644 test/ConfigValueRepositoryTest.php create mode 100644 test/EnvironmentConfigValuesTest.php diff --git a/src/ConfigValue.php b/src/ConfigValue.php new file mode 100644 index 0000000..cece656 --- /dev/null +++ b/src/ConfigValue.php @@ -0,0 +1,45 @@ +path = $path; + $this->value = $value; + } + + public function isNull(): bool + { + return $this->value === null; + } + + public function getPath(): string + { + return $this->path; + } + + public function getValue() + { + return $this->value; + } + + public function withScope(Scope $scope): self + { + $result = clone $this; + $result->scope = $scope->getScopeType(); + return $result; + } + + public function getScope(): Scope + { + return $this->scope ?? new Scope(ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null); + } +} diff --git a/src/ConfigValueRepository.php b/src/ConfigValueRepository.php new file mode 100644 index 0000000..929c3f1 --- /dev/null +++ b/src/ConfigValueRepository.php @@ -0,0 +1,26 @@ +configWriter = $configWriter; + } + + public function save(ConfigValue $configValue) + { + $this->configWriter->save( + $configValue->getPath(), + $configValue->getValue(), + $configValue->getScope()->getScopeType(), + $configValue->getScope()->getScopeCode() + ); + } +} \ No newline at end of file diff --git a/src/ConfigValueSet.php b/src/ConfigValueSet.php new file mode 100644 index 0000000..d6c67c3 --- /dev/null +++ b/src/ConfigValueSet.php @@ -0,0 +1,55 @@ +values[$configValue->getPath()] = $configValue; + } + + return $instance; + } + + public function withConfigValue(ConfigValue $configValue): self + { + $instance = clone $this; + $instance->values[$configValue->getPath()] = $configValue; + return $instance; + } + + public function getConfigValueByPath(string $path): ConfigValue + { + return $this->values[$path] ?? new ConfigValue($path, null); + } + + public function getIterator() + { + return new \ArrayIterator($this->values); + } + + public static function create(): self + { + return new self; + } + + private function __construct() + { + } +} \ No newline at end of file diff --git a/src/Environment.php b/src/Environment.php new file mode 100644 index 0000000..3562d4d --- /dev/null +++ b/src/Environment.php @@ -0,0 +1,17 @@ +environments[$environment] = $configValues; + return $clone; + } + + public function getConfigValuesByEnvironment(string $environment): ConfigValueSet + { + return $this->environments[$environment] ?? ConfigValueSet::create(); + } + + public static function create(): self + { + return new self; + } + + private function __construct() + { + } +} \ No newline at end of file diff --git a/src/Scope.php b/src/Scope.php new file mode 100644 index 0000000..a36b23f --- /dev/null +++ b/src/Scope.php @@ -0,0 +1,27 @@ +scopeType = $scopeType; + $this->scopeCode = $scopeCode; + } + + public function getScopeType(): string + { + return $this->scopeType; + } + + public function getScopeCode() + { + return $this->scopeCode; + } +} diff --git a/test/ConfigValueRepositoryTest.php b/test/ConfigValueRepositoryTest.php new file mode 100644 index 0000000..f220798 --- /dev/null +++ b/test/ConfigValueRepositoryTest.php @@ -0,0 +1,48 @@ +values[$path] ?? false; + } + + public function isSetFlag($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null) + { + return isset($this->values[$path]); + } + + public function delete($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0) + { + unset($this->values[$path]); + } + + public function save($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0) + { + $this->values[$path] = $value; + } + }; + + $repository = new ConfigValueRepository($config); + $repository->save(new ConfigValue('path', 'value')); + + self::assertEquals( + 'value', + $config->getValue('path'), + 'We should be able to retrieve the saved config value by it\'s path.' + ); + } +} \ No newline at end of file diff --git a/test/EnvironmentConfigValuesTest.php b/test/EnvironmentConfigValuesTest.php new file mode 100644 index 0000000..1c45fba --- /dev/null +++ b/test/EnvironmentConfigValuesTest.php @@ -0,0 +1,50 @@ +withConfigValuesForEnvironment( + ConfigValueSet::create()->withConfigValue($productionConfigValue), + Environment::PRODUCTION) + ->withConfigValuesForEnvironment( + ConfigValueSet::create()->withConfigValue($localConfigValue), + Environment::LOCAL); + + self::assertEquals( + 'productionValue', + $environmentConfig + ->getConfigValuesByEnvironment(Environment::PRODUCTION) + ->getConfigValueByPath($path)->getValue(), + 'The production environment should return the production value.' + ); + + self::assertEquals( + 'localValue', + $environmentConfig + ->getConfigValuesByEnvironment(Environment::LOCAL) + ->getConfigValueByPath($path)->getValue(), + 'The local environment should return the local value.' + ); + + self::assertTrue( + $environmentConfig + ->getConfigValuesByEnvironment(Environment::DEVELOPMENT) + ->getConfigValueByPath($path)->isNull(), + 'The development environment should not be set.' + ); + } +} From b0b85004809b098eb642688c8385423f48d270f5 Mon Sep 17 00:00:00 2001 From: dank00 Date: Thu, 4 Jan 2018 12:03:10 +0000 Subject: [PATCH 2/3] compsoer require magento/framework --- composer.json | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b2b89f9..c6b324c 100644 --- a/composer.json +++ b/composer.json @@ -7,9 +7,18 @@ "email": "dan.kenny@space48.com" } ], + "repositories": { + "magento": { + "type": "composer", + "url": "https://repo.magento.com/" + } + }, "scripts": { "test": "vendor/bin/phpunit -c phpunit.xml" }, + "require": { + "magento/framework": "^101.0" + }, "require-dev": { "phpunit/phpunit": "^6.4" }, @@ -21,6 +30,6 @@ "autoload-dev": { "psr-4": { "Space48\\EnvironmentConfiguration\\Test\\": "test" - } + } } } From 0b04ff84430dcc1773f4a8beb45823068ea7a8fe Mon Sep 17 00:00:00 2001 From: dank00 Date: Thu, 4 Jan 2018 12:10:00 +0000 Subject: [PATCH 3/3] :lipstick: --- src/ConfigValueSet.php | 4 ++-- src/EnvironmentConfigValues.php | 2 +- test/ConfigValueRepositoryTest.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ConfigValueSet.php b/src/ConfigValueSet.php index d6c67c3..629dde7 100644 --- a/src/ConfigValueSet.php +++ b/src/ConfigValueSet.php @@ -17,8 +17,8 @@ public static function of(array $configValues): self 'Members of %s must be instances of %s, got %s', self::class, ConfigValue::class, - gettype($configValue)) - ); + gettype($configValue) + )); } $instance->values[$configValue->getPath()] = $configValue; diff --git a/src/EnvironmentConfigValues.php b/src/EnvironmentConfigValues.php index a257909..cc9e26c 100644 --- a/src/EnvironmentConfigValues.php +++ b/src/EnvironmentConfigValues.php @@ -26,4 +26,4 @@ public static function create(): self private function __construct() { } -} \ No newline at end of file +} diff --git a/test/ConfigValueRepositoryTest.php b/test/ConfigValueRepositoryTest.php index f220798..47c5d2a 100644 --- a/test/ConfigValueRepositoryTest.php +++ b/test/ConfigValueRepositoryTest.php @@ -45,4 +45,4 @@ public function save($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DE 'We should be able to retrieve the saved config value by it\'s path.' ); } -} \ No newline at end of file +}