Skip to content

Commit

Permalink
Merge pull request #19 from ergebnis/feature/named
Browse files Browse the repository at this point in the history
Enhancement: Extract named constructors for FakeVariables
  • Loading branch information
localheinz authored Feb 23, 2020
2 parents 973dead + eac4b45 commit a0b209a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ For a full diff see [`c0c63bb...master`][c0c63bb...master].
* 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]
* Adjusted `Ergebnis\Environment\TestVariables` so it implements the `Ergebnis\Environment\Variables` interface as well ([#17]), by [@localheinz]
* Extracted named constructors `Ergebnis\Environment\FakeVariables::empty()` `Ergebnis\Environment\FakeVariables::fromArray()` ([#19]), by [@localheinz]

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

Expand All @@ -40,5 +41,6 @@ For a full diff see [`c0c63bb...master`][c0c63bb...master].
[#16]: https://github.com/ergebnis/environment-variables/pull/16
[#17]: https://github.com/ergebnis/environment-variables/pull/17
[#18]: https://github.com/ergebnis/environment-variables/pull/18
[#19]: https://github.com/ergebnis/environment-variables/pull/19

[@localheinz]: https://github.com/localheinz
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ final class BuildEnvironmentTest extends Framework\TestCase
{
public function testIsGitHubActionsReturnsFalseWhenNoneOfTheExpectedEnvironmentVariablesAreAvailable(): void
{
$environmentVariables = new Environment\FakeVariables();
$environmentVariables = Environment\FakeVariables::empty();

$buildEnvironment = new BuildEnvironment($environmentVariables);

Expand All @@ -93,7 +93,7 @@ final class BuildEnvironmentTest extends Framework\TestCase

public function testIsGitHubActionsReturnsFalseWhenValueOfGitHubActionsEnvironmentVariableIsNotTrue(): void
{
$environmentVariables = new Environment\FakeVariables([
$environmentVariables = Environment\FakeVariables::fromArray([
'GITHUB_ACTIONS' => 'false',
]);

Expand All @@ -104,7 +104,7 @@ final class BuildEnvironmentTest extends Framework\TestCase

public function testIsGitHubActionsReturnsTrueWhenValueOfGitHubActionsEnvironmentVariableIsTrue(): void
{
$environmentVariables = new Environment\FakeVariables([
$environmentVariables = Environment\FakeVariables::fromArray([
'GITHUB_ACTIONS' => 'true',
]);

Expand Down
7 changes: 1 addition & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Constructor in Ergebnis\\\\Environment\\\\FakeVariables has parameter \\$values with default value\\.$#"
count: 1
path: src/FakeVariables.php

-
message: "#^Call to function is_string\\(\\) with string will always evaluate to true\\.$#"
count: 1
Expand Down Expand Up @@ -41,7 +36,7 @@ parameters:
path: src/ReadOnlyVariables.php

-
message: "#^Parameter \\#1 \\$values of class Ergebnis\\\\Environment\\\\FakeVariables constructor expects array\\<string, string\\|false\\>, array\\<int\\|string, string\\> given\\.$#"
message: "#^Parameter \\#1 \\$values of static method Ergebnis\\\\Environment\\\\FakeVariables\\:\\:fromArray\\(\\) expects array\\<string, string\\|false\\>, array\\<int\\|string, string\\> given\\.$#"
count: 1
path: test/Unit/FakeVariablesTest.php

Expand Down
20 changes: 15 additions & 5 deletions src/FakeVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@

final class FakeVariables implements Variables
{
private $values;

/**
* @var array<string, string>
* @param array<string, string> $values
*/
private $values = [];
private function __construct(array $values)
{
$this->values = $values;
}

public static function empty(): self
{
return new self([]);
}

/**
* @param array<string, false|string> $values
*
* @throws Exception\InvalidName
* @throws Exception\InvalidValue
*/
public function __construct(array $values = [])
public static function fromArray(array $values = []): self
{
$invalidNames = \array_filter(\array_keys($values), static function ($name): bool {
return !\is_string($name) || '' === $name || \trim($name) !== $name;
Expand All @@ -44,9 +54,9 @@ public function __construct(array $values = [])
throw Exception\InvalidValue::create();
}

$this->values = \array_filter($values, static function ($value): bool {
return new self(\array_filter($values, static function ($value): bool {
return \is_string($value);
});
}));
}

public function has(string $name): bool
Expand Down
45 changes: 26 additions & 19 deletions test/Unit/FakeVariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,26 @@ final class FakeVariablesTest extends Framework\TestCase

private const NAME = 'FOO';

public function testEmptyReturnsEmptyVariables(): void
{
$variables = FakeVariables::empty();

self::assertSame([], $variables->toArray());
}

/**
* @dataProvider \Ergebnis\Environment\Test\DataProvider\Name::invalidValue()
* @dataProvider \Ergebnis\Environment\Test\DataProvider\Name::invalidType()
*
* @param mixed $name
*/
public function testConstructorRejectsValuesWhenTheyHaveInvalidNames($name): void
public function testFromArrayRejectsValuesWhenTheyHaveInvalidNames($name): void
{
$value = self::faker()->sentence;

$this->expectException(Exception\InvalidName::class);

new FakeVariables([
FakeVariables::fromArray([
$name => $value,
]);
}
Expand All @@ -57,11 +64,11 @@ public function testConstructorRejectsValuesWhenTheyHaveInvalidNames($name): voi
*
* @param mixed $value
*/
public function testConstructorRejectsValuesWhenTheyHaveInvalidValues($value): void
public function testFromArrayRejectsValuesWhenTheyHaveInvalidValues($value): void
{
$this->expectException(Exception\InvalidValue::class);

new FakeVariables([
FakeVariables::fromArray([
self::NAME => $value,
]);
}
Expand All @@ -73,7 +80,7 @@ public function testConstructorRejectsValuesWhenTheyHaveInvalidValues($value): v
*/
public function testHasThrowsInvalidNameWhenNameIsInvalid(string $name): void
{
$variables = new FakeVariables();
$variables = FakeVariables::empty();

$this->expectException(Exception\InvalidName::class);

Expand All @@ -82,14 +89,14 @@ public function testHasThrowsInvalidNameWhenNameIsInvalid(string $name): void

public function testHasReturnsFalseWhenEnvironmentVariableHasNotBeenInjected(): void
{
$variables = new FakeVariables();
$variables = FakeVariables::empty();

self::assertFalse($variables->has(self::NAME));
}

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

Expand All @@ -98,7 +105,7 @@ public function testHasReturnsFalseWhenEnvironmentVariableHasBeenInjectedButValu

public function testHasReturnsTrueWhenEnvironmentVariableHasBeenInjectedAndValueIsNotFalse(): void
{
$variables = new FakeVariables([
$variables = FakeVariables::fromArray([
self::NAME => self::faker()->sentence,
]);

Expand All @@ -112,7 +119,7 @@ public function testHasReturnsTrueWhenEnvironmentVariableHasBeenInjectedAndValue
*/
public function testGetThrowsInvalidNameWhenNameIsInvalid(string $name): void
{
$variables = new FakeVariables();
$variables = FakeVariables::empty();

$this->expectException(Exception\InvalidName::class);

Expand All @@ -121,7 +128,7 @@ public function testGetThrowsInvalidNameWhenNameIsInvalid(string $name): void

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

$this->expectException(Exception\NotSet::class);

Expand All @@ -130,7 +137,7 @@ public function testGetThrowsNotSetWhenEnvironmentVariableHasNotBeenInjected():

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

Expand All @@ -143,7 +150,7 @@ public function testGetReturnsValueWhenEnvironmentVariableHasBeenInjectedAndValu
{
$value = self::faker()->sentence;

$variables = new FakeVariables([
$variables = FakeVariables::fromArray([
self::NAME => $value,
]);

Expand All @@ -159,7 +166,7 @@ public function testSetThrowsInvalidNameWhenNameIsInvalid(string $name): void
{
$value = self::faker()->sentence;

$variables = new FakeVariables();
$variables = FakeVariables::empty();

$this->expectException(Exception\InvalidName::class);

Expand All @@ -173,7 +180,7 @@ public function testSetSetsValue(): void
{
$value = self::faker()->sentence;

$variables = new FakeVariables();
$variables = FakeVariables::empty();

$variables->set(
self::NAME,
Expand All @@ -190,7 +197,7 @@ public function testSetSetsValue(): void
*/
public function testUnsetThrowsInvalidNameWhenNameIsInvalid(string $name): void
{
$variables = new FakeVariables();
$variables = FakeVariables::empty();

$this->expectException(Exception\InvalidName::class);

Expand All @@ -201,7 +208,7 @@ public function testUnsetUnsetsVariable(): void
{
$value = self::faker()->sentence;

$variables = new FakeVariables([
$variables = FakeVariables::fromArray([
self::NAME => $value,
]);

Expand All @@ -218,7 +225,7 @@ public function testToArrayReturnsInjectedValues(): void
'BAZ' => 'aha',
];

$variables = new FakeVariables($values);
$variables = FakeVariables::fromArray($values);

self::assertSame($values, $variables->toArray());
}
Expand All @@ -231,7 +238,7 @@ public function testToArrayReturnsValuesWhenValueHasBeenSet(): void
'BAZ' => 'aha',
];

$variables = new FakeVariables($values);
$variables = FakeVariables::fromArray($values);

$variables->set(
'FOO',
Expand All @@ -255,7 +262,7 @@ public function testToArrayReturnsValuesWhenValueHasBeenUnset(): void
'BAZ' => 'aha',
];

$variables = new FakeVariables($values);
$variables = FakeVariables::fromArray($values);

$variables->unset('FOO');

Expand Down

0 comments on commit a0b209a

Please sign in to comment.