-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Space48/feature/model
Environment configuration model TOOL-14
- Loading branch information
Showing
9 changed files
with
307 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,18 @@ | |
"email": "[email protected]" | ||
} | ||
], | ||
"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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Space48\EnvironmentConfiguration; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
|
||
class ConfigValue | ||
{ | ||
private $path; | ||
private $value; | ||
private $scope; | ||
|
||
public function __construct(string $path, $value) | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Space48\EnvironmentConfiguration; | ||
|
||
use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter; | ||
|
||
class ConfigValueRepository | ||
{ | ||
/** @var ConfigWriter */ | ||
private $configWriter; | ||
|
||
public function __construct(ConfigWriter $configWriter) | ||
{ | ||
$this->configWriter = $configWriter; | ||
} | ||
|
||
public function save(ConfigValue $configValue) | ||
{ | ||
$this->configWriter->save( | ||
$configValue->getPath(), | ||
$configValue->getValue(), | ||
$configValue->getScope()->getScopeType(), | ||
$configValue->getScope()->getScopeCode() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Space48\EnvironmentConfiguration; | ||
|
||
|
||
class ConfigValueSet implements \IteratorAggregate | ||
{ | ||
private $values = []; | ||
|
||
public static function of(array $configValues): self | ||
{ | ||
$instance = new self; | ||
|
||
foreach ($configValues as $configValue) { | ||
if (!$configValue instanceof ConfigValue) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'Members of %s must be instances of %s, got %s', | ||
self::class, | ||
ConfigValue::class, | ||
gettype($configValue) | ||
)); | ||
} | ||
|
||
$instance->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() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Space48\EnvironmentConfiguration; | ||
|
||
final class Environment | ||
{ | ||
const | ||
PRODUCTION = 'production', | ||
STAGING = 'staging', | ||
DEVELOPMENT = 'development', | ||
LOCAL = 'local' | ||
; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Space48\EnvironmentConfiguration; | ||
|
||
class EnvironmentConfigValues | ||
{ | ||
private $environments = []; | ||
|
||
public function withConfigValuesForEnvironment(ConfigValueSet $configValues, string $environment): self | ||
{ | ||
$clone = clone $this; | ||
$clone->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() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Space48\EnvironmentConfiguration; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
|
||
class Scope | ||
{ | ||
private $scopeType; | ||
private $scopeCode; | ||
|
||
public function __construct(string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null) | ||
{ | ||
$this->scopeType = $scopeType; | ||
$this->scopeCode = $scopeCode; | ||
} | ||
|
||
public function getScopeType(): string | ||
{ | ||
return $this->scopeType; | ||
} | ||
|
||
public function getScopeCode() | ||
{ | ||
return $this->scopeCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Space48\EnvironmentConfiguration\Test; | ||
|
||
use Magento\Framework\App\Config\{ScopeConfigInterface, Storage\WriterInterface}; | ||
use PHPUnit\Framework\TestCase; | ||
use Space48\EnvironmentConfiguration\ConfigValue; | ||
use Space48\EnvironmentConfiguration\ConfigValueRepository; | ||
|
||
class ConfigValueRepositoryTest extends TestCase | ||
{ | ||
public function testSave() | ||
{ | ||
$config = new class implements WriterInterface, ScopeConfigInterface | ||
{ | ||
private $values = []; | ||
|
||
public function getValue($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null) | ||
{ | ||
return $this->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.' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Space48\EnvironmentConfiguration\Test; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Space48\EnvironmentConfiguration\ConfigValue; | ||
use Space48\EnvironmentConfiguration\ConfigValueSet; | ||
use Space48\EnvironmentConfiguration\Environment; | ||
use Space48\EnvironmentConfiguration\EnvironmentConfigValues; | ||
|
||
class EnvironmentConfigValuesTest extends TestCase | ||
{ | ||
public function testGetConfigValuesByEnvironment() | ||
{ | ||
$path = 'space48/module/config'; | ||
$localConfigValue = new ConfigValue($path, 'localValue'); | ||
$productionConfigValue = new ConfigValue($path, 'productionValue'); | ||
|
||
$environmentConfig = EnvironmentConfigValues::create() | ||
->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.' | ||
); | ||
} | ||
} |