Added the possibility of 'protected' env parameters. (+test).

This commit is contained in:
Hubert Viktor 2015-05-21 09:58:01 +02:00
parent 3e8f6ea361
commit 24d680b15b
2 changed files with 30 additions and 4 deletions

View File

@ -25,7 +25,14 @@ class Environment
* @var \Deployer\Type\DotArray
*/
private $values = null;
/**
* Values represented by their keys here are protected, and cannot be
* changed by calling the `set` method.
* @var array
*/
private $protectedValueKeys = [];
/**
* Constructor
*/
@ -37,10 +44,19 @@ class Environment
/**
* @param string $name
* @param bool|int|string|array $value
* @param bool $isProtected
*/
public function set($name, $value)
public function set($name, $value, $isProtected = false)
{
if (in_array($name, $this->protectedValueKeys)) {
throw new \RuntimeException("The environment parameter `$name` has already been set, and is protected from changes.");
}
$this->values[$name] = $value;
if ($isProtected === true) {
$this->protectedValueKeys[] = $name;
}
}
/**

View File

@ -4,7 +4,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\Server;
class EnvironmentTest extends \PHPUnit_Framework_TestCase
@ -30,7 +30,7 @@ class EnvironmentTest extends \PHPUnit_Framework_TestCase
$env->set('string', 'value');
$env->set('array', [1, 'two']);
$env->set('parse', 'is {{int}}');
$this->assertEquals(42, $env->get('int'));
$this->assertEquals('value', $env->get('string'));
$this->assertEquals([1, 'two'], $env->get('array'));
@ -45,4 +45,14 @@ class EnvironmentTest extends \PHPUnit_Framework_TestCase
$this->setExpectedException('RuntimeException', 'Environment parameter `so` does not exists.');
$env->get('so');
}
public function testProtectedParameters()
{
$env = new Environment();
$env->set('protected', 'protected-value', true);
$this->setExpectedException('\RuntimeException', 'The environment parameter `protected` has already been set, and is protected from changes.');
$env->set('protected', 'some-other-value');
}
}