Skip to content

Commit

Permalink
Merge pull request #20 from ergebnis/feature/named
Browse files Browse the repository at this point in the history
Enhancement: Extract named constructors for ReadOnlyVariables
  • Loading branch information
localheinz authored Feb 23, 2020
2 parents a0b209a + 7e825b6 commit 147cc02
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ on:
- "master"

env:
MIN_COVERED_MSI: 93
MIN_MSI: 86
MIN_COVERED_MSI: 94
MIN_MSI: 87
REQUIRED_PHP_EXTENSIONS: "mbstring"

jobs:
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ 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]
* Extracted named constructors `Ergebnis\Environment\FakeVariables::empty()` and `Ergebnis\Environment\FakeVariables::fromArray()` ([#19]), by [@localheinz]
* Extracted named constructors `Ergebnis\Environment\ReadOnlyVariables::empty()` and `Ergebnis\Environment\ReadOnlyVariables::fromArray()` ([#20]), by [@localheinz]

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

Expand All @@ -42,5 +43,6 @@ For a full diff see [`c0c63bb...master`][c0c63bb...master].
[#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
[#20]: https://github.com/ergebnis/environment-variables/pull/20

[@localheinz]: https://github.com/localheinz
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MIN_COVERED_MSI:=93
MIN_MSI:=86
MIN_COVERED_MSI:=94
MIN_MSI:=87

.PHONY: it
it: coding-standards dependency-analysis static-code-analysis tests ## Runs the coding-standards, dependency-analysis, static-code-analysis, and tests targets
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ final class BuildEnvironmentTest extends Framework\TestCase
{
public function testIsGitHubActionsReturnsFalseWhenNoneOfTheExpectedEnvironmentVariablesAreAvailable(): void
{
$environmentVariables = new Environment\ReadOnlyVariables();
$environmentVariables = Environment\ReadOnlyVariables::empty();

$buildEnvironment = new BuildEnvironment($environmentVariables);

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

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

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

public function testIsGitHubActionsReturnsTrueWhenValueOfGitHubActionsEnvironmentVariableIsTrue(): void
{
$environmentVariables = new Environment\ReadOnlyVariables([
$environmentVariables = Environment\ReadOnlyVariables::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
Expand Up @@ -15,11 +15,6 @@ parameters:
count: 1
path: src/FakeVariables.php

-
message: "#^Constructor in Ergebnis\\\\Environment\\\\ReadOnlyVariables has parameter \\$values with default value\\.$#"
count: 1
path: src/ReadOnlyVariables.php

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

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

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

final class ReadOnlyVariables 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
37 changes: 22 additions & 15 deletions test/Unit/ReadOnlyVariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,26 @@ final class ReadOnlyVariablesTest extends Framework\TestCase

private const NAME = 'FOO';

public function testEmptyReturnsEmptyVariables(): void
{
$variables = ReadOnlyVariables::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 ReadOnlyVariables([
ReadOnlyVariables::fromArray([
$name => $value,
]);
}
Expand All @@ -58,11 +65,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 ReadOnlyVariables([
ReadOnlyVariables::fromArray([
self::NAME => $value,
]);
}
Expand All @@ -74,7 +81,7 @@ public function testConstructorRejectsValuesWhenTheyHaveInvalidValues($value): v
*/
public function testHasThrowsInvalidNameWhenNameIsInvalid(string $name): void
{
$variables = new ReadOnlyVariables();
$variables = ReadOnlyVariables::empty();

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

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

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

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

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

Expand All @@ -99,7 +106,7 @@ public function testHasReturnsFalseWhenEnvironmentVariableHasBeenInjectedButValu

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

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

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

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

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

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

Expand All @@ -131,7 +138,7 @@ public function testGetThrowsNotSetWhenEnvironmentVariableHasNotBeenInjected():

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

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

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

Expand All @@ -155,7 +162,7 @@ public function testSetThrowsShouldNotBeUsed(): void
{
$value = self::faker()->sentence;

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

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

Expand All @@ -169,7 +176,7 @@ public function testUnsetThrowsShouldNotBeUsed(): void
{
$value = self::faker()->sentence;

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

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

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

self::assertSame($values, $variables->toArray());
}
Expand Down

0 comments on commit 147cc02

Please sign in to comment.