Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment configuration model TOOL-14 #1

Merged
merged 3 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -21,6 +30,6 @@
"autoload-dev": {
"psr-4": {
"Space48\\EnvironmentConfiguration\\Test\\": "test"
}
}
}
}
45 changes: 45 additions & 0 deletions src/ConfigValue.php
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);
}
}
26 changes: 26 additions & 0 deletions src/ConfigValueRepository.php
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()
);
}
}
55 changes: 55 additions & 0 deletions src/ConfigValueSet.php
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()
{
}
}
17 changes: 17 additions & 0 deletions src/Environment.php
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()
{
}
}
29 changes: 29 additions & 0 deletions src/EnvironmentConfigValues.php
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()
{
}
}
27 changes: 27 additions & 0 deletions src/Scope.php
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;
}
}
48 changes: 48 additions & 0 deletions test/ConfigValueRepositoryTest.php
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.'
);
}
}
50 changes: 50 additions & 0 deletions test/EnvironmentConfigValuesTest.php
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.'
);
}
}