Skip to content

Commit

Permalink
Merge pull request #16 from ergebnis/feature/not-set
Browse files Browse the repository at this point in the history
Enhancement: Throw exception when retrieving value of variable that is not set
  • Loading branch information
localheinz committed Feb 23, 2020
2 parents c68a887 + f04619f commit f6f6d37
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,7 @@ For a full diff see [`c0c63bb...master`][c0c63bb...master].
* Renamed `Ergebnis\Environment\Test` to `Ergebnis\Environment\TestVariables` ([#9]), by [@localheinz]
* Started throwing `Ergebnis\Environment\Exception\CouldNotSet` when a system environment variable could not be set ([#14]), by [@localheinz]
* Started throwing `Ergebnis\Environment\Exception\CouldNotUnset` when a system environment variable could not be unset ([#15]), by [@localheinz]
* Started throwing `Ergebnis\Environment\Exception\NotSet` when attempting to retrieve the value of an environment variable that is not set ([#16]), by [@localheinz]

[c0c63bb...master]: https://github.com/ergebnis/environment-variables/compare/c0c63bb...master

Expand All @@ -34,5 +35,6 @@ For a full diff see [`c0c63bb...master`][c0c63bb...master].
[#9]: https://github.com/ergebnis/environment-variables/pull/9
[#14]: https://github.com/ergebnis/environment-variables/pull/14
[#15]: https://github.com/ergebnis/environment-variables/pull/15
[#16]: https://github.com/ergebnis/environment-variables/pull/16

[@localheinz]: https://github.com/localheinz
25 changes: 25 additions & 0 deletions src/Exception/NotSet.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020 Andreas M枚ller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/environment-variables
*/

namespace Ergebnis\Environment\Exception;

final class NotSet extends \InvalidArgumentException implements Exception
{
public static function name(string $name): self
{
return new self(\sprintf(
'The environment variable "%s" is not set.',
$name
));
}
}
4 changes: 2 additions & 2 deletions src/FakeVariables.php
Expand Up @@ -58,14 +58,14 @@ public function has(string $name): bool
return \array_key_exists($name, $this->values);
}

public function get(string $name)
public function get(string $name): string
{
if ('' === $name || \trim($name) !== $name) {
throw Exception\InvalidName::create();
}

if (!\array_key_exists($name, $this->values)) {
return false;
throw Exception\NotSet::name($name);
}

return $this->values[$name];
Expand Down
7 changes: 5 additions & 2 deletions src/ReadOnlyVariables.php
Expand Up @@ -15,6 +15,9 @@

final class ReadOnlyVariables implements Variables
{
/**
* @var array<string, string>
*/
private $values = [];

/**
Expand Down Expand Up @@ -55,14 +58,14 @@ public function has(string $name): bool
return \array_key_exists($name, $this->values);
}

public function get(string $name)
public function get(string $name): string
{
if ('' === $name || \trim($name) !== $name) {
throw Exception\InvalidName::create();
}

if (!\array_key_exists($name, $this->values)) {
return false;
throw Exception\NotSet::name($name);
}

return $this->values[$name];
Expand Down
10 changes: 8 additions & 2 deletions src/SystemVariables.php
Expand Up @@ -24,13 +24,19 @@ public function has(string $name): bool
return false !== \getenv($name);
}

public function get(string $name)
public function get(string $name): string
{
if ('' === $name || \trim($name) !== $name) {
throw Exception\InvalidName::create();
}

return \getenv($name);
$value = \getenv($name);

if (!\is_string($value)) {
throw Exception\NotSet::name($name);
}

return $value;
}

public function set(string $name, string $value): void
Expand Down
4 changes: 2 additions & 2 deletions src/Variables.php
Expand Up @@ -29,9 +29,9 @@ public function has(string $name): bool;
*
* @throws Exception\InvalidName
*
* @return bool|string
* @return string
*/
public function get(string $name);
public function get(string $name): string;

/**
* @param string $name
Expand Down
42 changes: 42 additions & 0 deletions test/Unit/Exception/NotSetTest.php
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020 Andreas M枚ller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/environment-variables
*/

namespace Ergebnis\Environment\Test\Unit\Exception;

use Ergebnis\Environment\Exception\NotSet;
use Ergebnis\Test\Util\Helper;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\Environment\Exception\NotSet
*/
final class NotSetTest extends Framework\TestCase
{
use Helper;

public function testNameReturnsException(): void
{
$name = self::faker()->word;

$exception = NotSet::name($name);

$message = \sprintf(
'The environment variable "%s" is not set.',
$name
);

self::assertSame($message, $exception->getMessage());
}
}
15 changes: 10 additions & 5 deletions test/Unit/FakeVariablesTest.php
Expand Up @@ -25,6 +25,7 @@
*
* @uses \Ergebnis\Environment\Exception\InvalidName
* @uses \Ergebnis\Environment\Exception\InvalidValue
* @uses \Ergebnis\Environment\Exception\NotSet
* @uses \Ergebnis\Environment\TestVariables
*/
final class FakeVariablesTest extends Framework\TestCase
Expand Down Expand Up @@ -118,20 +119,24 @@ public function testGetThrowsInvalidNameWhenNameIsInvalid(string $name): void
$variables->get($name);
}

public function testGetReturnsFalseWhenEnvironmentVariableHasNotBeenInjected(): void
public function testGetThrowsNotSetWhenEnvironmentVariableHasNotBeenInjected(): void
{
$variables = new FakeVariables();

self::assertFalse($variables->get(self::NAME));
$this->expectException(Exception\NotSet::class);

$variables->get(self::NAME);
}

public function testGetReturnsFalseWhenEnvironmentVariableHasBeenInjectedButValueIsFalse(): void
public function testGetThrowsNotSetWhenEnvironmentVariableHasBeenInjectedButValueIsFalse(): void
{
$variables = new FakeVariables([
self::NAME => false,
]);

self::assertFalse($variables->get(self::NAME));
$this->expectException(Exception\NotSet::class);

$variables->get(self::NAME);
}

public function testGetReturnsValueWhenEnvironmentVariableHasBeenInjectedAndValueIsNotFalse(): void
Expand Down Expand Up @@ -202,6 +207,6 @@ public function testUnsetUnsetsVariable(): void

$variables->unset(self::NAME);

self::assertFalse($variables->get(self::NAME));
self::assertFalse($variables->has(self::NAME));
}
}
13 changes: 9 additions & 4 deletions test/Unit/ReadOnlyVariablesTest.php
Expand Up @@ -25,6 +25,7 @@
*
* @uses \Ergebnis\Environment\Exception\InvalidName
* @uses \Ergebnis\Environment\Exception\InvalidValue
* @uses \Ergebnis\Environment\Exception\NotSet
* @uses \Ergebnis\Environment\Exception\ShouldNotBeUsed
* @uses \Ergebnis\Environment\TestVariables
*/
Expand Down Expand Up @@ -119,20 +120,24 @@ public function testGetThrowsInvalidNameWhenNameIsInvalid(string $name): void
$variables->get($name);
}

public function testGetReturnsFalseWhenEnvironmentVariableHasNotBeenInjected(): void
public function testGetThrowsNotSetWhenEnvironmentVariableHasNotBeenInjected(): void
{
$variables = new ReadOnlyVariables();

self::assertFalse($variables->get(self::NAME));
$this->expectException(Exception\NotSet::class);

$variables->get(self::NAME);
}

public function testGetReturnsFalseWhenEnvironmentVariableHasBeenInjectedButValueIsFalse(): void
public function testGetThrowsNotSetWhenEnvironmentVariableHasBeenInjectedButValueIsFalse(): void
{
$variables = new ReadOnlyVariables([
self::NAME => false,
]);

self::assertFalse($variables->get(self::NAME));
$this->expectException(Exception\NotSet::class);

$variables->get(self::NAME);
}

public function testGetReturnsValueWhenEnvironmentVariableHasBeenInjectedAndValueIsNotFalse(): void
Expand Down
7 changes: 5 additions & 2 deletions test/Unit/SystemVariablesTest.php
Expand Up @@ -25,6 +25,7 @@
* @covers \Ergebnis\Environment\SystemVariables
*
* @uses \Ergebnis\Environment\Exception\InvalidName
* @uses \Ergebnis\Environment\Exception\NotSet
* @uses \Ergebnis\Environment\TestVariables
*/
final class SystemVariablesTest extends Framework\TestCase
Expand Down Expand Up @@ -98,11 +99,13 @@ public function testGetThrowsInvalidNameWhenNameIsInvalid(string $name): void
$variables->get($name);
}

public function testGetReturnsFalseWhenEnvironmentVariableIsNotSet(): void
public function testGetThrowsNotSetFalseWhenEnvironmentVariableIsNotSet(): void
{
$variables = new SystemVariables();

self::assertFalse($variables->get(self::NAME));
$this->expectException(Exception\NotSet::class);

$variables->get(self::NAME);
}

public function testGetReturnsValueWhenEnvironmentVariableIsSet(): void
Expand Down

0 comments on commit f6f6d37

Please sign in to comment.