generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement ValueGenerator
- Loading branch information
1 parent
5183980
commit af98463
Showing
8 changed files
with
180 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2017-2023 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/data-generator | ||
*/ | ||
|
||
namespace Ergebnis\DataGenerator\Exception; | ||
|
||
final class InvalidValuesException extends \InvalidArgumentException | ||
{ | ||
public static function empty(): self | ||
{ | ||
return new self('Values can not be empty.'); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2017-2023 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/data-generator | ||
*/ | ||
|
||
namespace Ergebnis\DataGenerator; | ||
|
||
final class ValueGenerator implements StringGenerator | ||
{ | ||
/** | ||
* @var array<string> | ||
*/ | ||
private readonly array $values; | ||
|
||
/** | ||
* @throws Exception\InvalidValuesException | ||
*/ | ||
public function __construct(string ...$values) | ||
{ | ||
if (0 === \count($values)) { | ||
throw Exception\InvalidValuesException::empty(); | ||
} | ||
|
||
$this->values = $values; | ||
} | ||
|
||
public function generate(): \Generator | ||
{ | ||
foreach ($this->values as $value) { | ||
yield $value; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2017-2023 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/data-generator | ||
*/ | ||
|
||
namespace Ergebnis\DataGenerator\Test\Unit\Exception; | ||
|
||
use Ergebnis\DataGenerator\Exception; | ||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversClass(Exception\InvalidValuesException::class)] | ||
final class InvalidValuesExceptionTest extends Framework\TestCase | ||
{ | ||
public function testEmptyReturnsException(): void | ||
{ | ||
$exception = Exception\InvalidValuesException::empty(); | ||
|
||
$message = 'Values can not be empty.'; | ||
|
||
self::assertSame($message, $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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2017-2023 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/data-generator | ||
*/ | ||
|
||
namespace Ergebnis\DataGenerator\Test\Unit; | ||
|
||
use Ergebnis\DataGenerator\Exception; | ||
use Ergebnis\DataGenerator\Test; | ||
use Ergebnis\DataGenerator\ValueGenerator; | ||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversClass(ValueGenerator::class)] | ||
#[Framework\Attributes\UsesClass(Exception\InvalidValuesException::class)] | ||
final class ValueGeneratorTest extends Framework\TestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
public function testConstructorRejectsEmptyValues(): void | ||
{ | ||
$this->expectException(Exception\InvalidValuesException::class); | ||
|
||
new ValueGenerator(); | ||
} | ||
|
||
public function testGenerateReturnsGeneratorThatYieldsValues(): void | ||
{ | ||
/** @var list<string> $values */ | ||
$values = self::faker()->words(); | ||
|
||
$generator = new ValueGenerator(...$values); | ||
|
||
$generated = \iterator_to_array($generator->generate()); | ||
|
||
self::assertSame($values, $generated); | ||
} | ||
} |