generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a3e779
commit 1d4ed69
Showing
7 changed files
with
309 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License\Exception; | ||
|
||
final class InvalidReplacements extends \InvalidArgumentException implements Exception | ||
{ | ||
public static function keys(): self | ||
{ | ||
return new self('Keys need to be strings.'); | ||
} | ||
|
||
public static function values(): self | ||
{ | ||
return new self('Values need to be strings.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License; | ||
|
||
final class Template | ||
{ | ||
private $value; | ||
|
||
private function __construct(string $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public static function fromString(string $value): self | ||
{ | ||
return new self($value); | ||
} | ||
|
||
/** | ||
* @param array $replacements | ||
* | ||
* @throws Exception\InvalidReplacements | ||
* | ||
* @return string | ||
*/ | ||
public function toString(array $replacements): string | ||
{ | ||
$invalidKeys = \array_filter(\array_keys($replacements), static function ($key): bool { | ||
return !\is_string($key); | ||
}); | ||
|
||
if ([] !== $invalidKeys) { | ||
throw Exception\InvalidReplacements::keys(); | ||
} | ||
|
||
$invalidValues = \array_filter($replacements, static function ($value): bool { | ||
return !\is_string($value); | ||
}); | ||
|
||
if ([] !== $invalidValues) { | ||
throw Exception\InvalidReplacements::values(); | ||
} | ||
|
||
return \str_replace( | ||
\array_keys($replacements), | ||
$replacements, | ||
$this->value | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License\Test\Unit\Exception; | ||
|
||
use Ergebnis\License\Exception\InvalidReplacements; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\License\Exception\InvalidReplacements | ||
* | ||
* @uses \Ergebnis\License\Year | ||
*/ | ||
final class InvalidReplacementsTest extends Framework\TestCase | ||
{ | ||
public function testKeysReturnsInvalidReplacements(): void | ||
{ | ||
$exception = InvalidReplacements::keys(); | ||
|
||
self::assertSame('Keys need to be strings.', $exception->getMessage()); | ||
} | ||
|
||
public function testValuesReturnsInvalidReplacements(): void | ||
{ | ||
$exception = InvalidReplacements::values(); | ||
|
||
self::assertSame('Values need to be strings.', $exception->getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License\Test\Unit; | ||
|
||
use Ergebnis\License\Exception; | ||
use Ergebnis\License\Template; | ||
use Ergebnis\Test\Util\Helper; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\License\Template | ||
* | ||
* @uses \Ergebnis\License\Exception\InvalidReplacements | ||
*/ | ||
final class TemplateTest extends Framework\TestCase | ||
{ | ||
use Helper; | ||
|
||
public function testCreateReturnsTemplate(): void | ||
{ | ||
$faker = self::faker(); | ||
|
||
$when = $faker->dateTime->format('Y'); | ||
$who = $faker->name; | ||
|
||
$template = Template::fromString( | ||
<<<'EOF' | ||
Ah! | ||
This was done in <when> by <who>. | ||
Who would have thought? | ||
|
||
EOF | ||
); | ||
|
||
$expected = <<<EOF | ||
Ah! | ||
This was done in {$when} by {$who}. | ||
Who would have thought? | ||
EOF; | ||
|
||
self::assertSame($expected, $template->toString([ | ||
'<when>' => $when, | ||
'<who>' => $who, | ||
])); | ||
} | ||
|
||
/** | ||
* @dataProvider provideReplacementsWithInvalidKeys | ||
* | ||
* @param array $replacements | ||
*/ | ||
public function testToStringRejectsReplacementsWithInvalidKeys(array $replacements): void | ||
{ | ||
$template = Template::fromString(''); | ||
|
||
$this->expectException(Exception\InvalidReplacements::class); | ||
|
||
$template->toString($replacements); | ||
} | ||
|
||
public function provideReplacementsWithInvalidKeys(): \Generator | ||
{ | ||
$faker = self::faker(); | ||
|
||
$values = [ | ||
'float' => $faker->randomFloat(2, 1), | ||
'int' => $faker->numberBetween(1), | ||
]; | ||
|
||
foreach ($values as $key => $value) { | ||
yield $key => [ | ||
[ | ||
$value => 'foo', | ||
], | ||
]; | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider provideReplacementsWithInvalidValues | ||
* | ||
* @param array $replacements | ||
*/ | ||
public function testToStringRejectsReplacementsWithInvalidValues(array $replacements): void | ||
{ | ||
$template = Template::fromString(''); | ||
|
||
$this->expectException(Exception\InvalidReplacements::class); | ||
|
||
$template->toString($replacements); | ||
} | ||
|
||
public function provideReplacementsWithInvalidValues(): \Generator | ||
{ | ||
$faker = self::faker(); | ||
|
||
$values = [ | ||
'array' => $faker->words, | ||
'boolean-false' => false, | ||
'boolean-true' => true, | ||
'float' => $faker->randomFloat(2, 1), | ||
'int' => $faker->numberBetween(1), | ||
'null' => null, | ||
'object' => new \stdClass(), | ||
'resource' => \fopen(__FILE__, 'rb'), | ||
]; | ||
|
||
foreach ($values as $key => $value) { | ||
yield $key => [ | ||
[ | ||
'foo' => $value, | ||
], | ||
]; | ||
} | ||
} | ||
} |