From eac4b45bb6d7d14212face69240e7018fd19dc83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 23 Feb 2020 17:30:49 +0100 Subject: [PATCH] Enhancement: Extract named constructors for FakeVariables --- CHANGELOG.md | 2 ++ README.md | 6 ++--- phpstan-baseline.neon | 7 +---- src/FakeVariables.php | 20 +++++++++++---- test/Unit/FakeVariablesTest.php | 45 +++++++++++++++++++-------------- 5 files changed, 47 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fe6781..ccf060e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/README.md b/README.md index b5710bb..7e2e794 100644 --- a/README.md +++ b/README.md @@ -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); @@ -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', ]); @@ -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', ]); diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 5bdea96..1185ae3 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 @@ -41,7 +36,7 @@ parameters: path: src/ReadOnlyVariables.php - - message: "#^Parameter \\#1 \\$values of class Ergebnis\\\\Environment\\\\FakeVariables constructor expects array\\, array\\ given\\.$#" + message: "#^Parameter \\#1 \\$values of static method Ergebnis\\\\Environment\\\\FakeVariables\\:\\:fromArray\\(\\) expects array\\, array\\ given\\.$#" count: 1 path: test/Unit/FakeVariablesTest.php diff --git a/src/FakeVariables.php b/src/FakeVariables.php index 5ec5622..dc216f9 100644 --- a/src/FakeVariables.php +++ b/src/FakeVariables.php @@ -15,10 +15,20 @@ final class FakeVariables implements Variables { + private $values; + /** - * @var array + * @param array $values */ - private $values = []; + private function __construct(array $values) + { + $this->values = $values; + } + + public static function empty(): self + { + return new self([]); + } /** * @param array $values @@ -26,7 +36,7 @@ final class FakeVariables implements Variables * @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; @@ -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 diff --git a/test/Unit/FakeVariablesTest.php b/test/Unit/FakeVariablesTest.php index 4ce49cb..6d6cb98 100644 --- a/test/Unit/FakeVariablesTest.php +++ b/test/Unit/FakeVariablesTest.php @@ -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, ]); } @@ -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, ]); } @@ -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); @@ -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, ]); @@ -98,7 +105,7 @@ public function testHasReturnsFalseWhenEnvironmentVariableHasBeenInjectedButValu public function testHasReturnsTrueWhenEnvironmentVariableHasBeenInjectedAndValueIsNotFalse(): void { - $variables = new FakeVariables([ + $variables = FakeVariables::fromArray([ self::NAME => self::faker()->sentence, ]); @@ -112,7 +119,7 @@ public function testHasReturnsTrueWhenEnvironmentVariableHasBeenInjectedAndValue */ public function testGetThrowsInvalidNameWhenNameIsInvalid(string $name): void { - $variables = new FakeVariables(); + $variables = FakeVariables::empty(); $this->expectException(Exception\InvalidName::class); @@ -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); @@ -130,7 +137,7 @@ public function testGetThrowsNotSetWhenEnvironmentVariableHasNotBeenInjected(): public function testGetThrowsNotSetWhenEnvironmentVariableHasBeenInjectedButValueIsFalse(): void { - $variables = new FakeVariables([ + $variables = FakeVariables::fromArray([ self::NAME => false, ]); @@ -143,7 +150,7 @@ public function testGetReturnsValueWhenEnvironmentVariableHasBeenInjectedAndValu { $value = self::faker()->sentence; - $variables = new FakeVariables([ + $variables = FakeVariables::fromArray([ self::NAME => $value, ]); @@ -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); @@ -173,7 +180,7 @@ public function testSetSetsValue(): void { $value = self::faker()->sentence; - $variables = new FakeVariables(); + $variables = FakeVariables::empty(); $variables->set( self::NAME, @@ -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); @@ -201,7 +208,7 @@ public function testUnsetUnsetsVariable(): void { $value = self::faker()->sentence; - $variables = new FakeVariables([ + $variables = FakeVariables::fromArray([ self::NAME => $value, ]); @@ -218,7 +225,7 @@ public function testToArrayReturnsInjectedValues(): void 'BAZ' => 'aha', ]; - $variables = new FakeVariables($values); + $variables = FakeVariables::fromArray($values); self::assertSame($values, $variables->toArray()); } @@ -231,7 +238,7 @@ public function testToArrayReturnsValuesWhenValueHasBeenSet(): void 'BAZ' => 'aha', ]; - $variables = new FakeVariables($values); + $variables = FakeVariables::fromArray($values); $variables->set( 'FOO', @@ -255,7 +262,7 @@ public function testToArrayReturnsValuesWhenValueHasBeenUnset(): void 'BAZ' => 'aha', ]; - $variables = new FakeVariables($values); + $variables = FakeVariables::fromArray($values); $variables->unset('FOO');