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.
Merge pull request #2 from ergebnis/feature/optional-value-generator
Enhancement: Implement `OptionalValueGenerator`
- Loading branch information
Showing
5 changed files
with
127 additions
and
0 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,43 @@ | ||
<?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 OptionalValueGenerator 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 | ||
{ | ||
yield ''; | ||
|
||
foreach ($this->values as $value) { | ||
yield $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,49 @@ | ||
<?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 | ||
*/ | ||
|
||
use Ergebnis\DataGenerator\Exception; | ||
use Ergebnis\DataGenerator\OptionalValueGenerator; | ||
use Ergebnis\DataGenerator\Test; | ||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversClass(OptionalValueGenerator::class)] | ||
#[Framework\Attributes\UsesClass(Exception\InvalidValuesException::class)] | ||
final class OptionalValueGeneratorTest extends Framework\TestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
public function testConstructorRejectsEmptyValues(): void | ||
{ | ||
$this->expectException(Exception\InvalidValuesException::class); | ||
|
||
new OptionalValueGenerator(); | ||
} | ||
|
||
public function testGenerateReturnsGeneratorThatYieldsValues(): void | ||
{ | ||
$values = self::faker()->words(); | ||
|
||
$generator = new OptionalValueGenerator(...$values); | ||
|
||
$generated = \iterator_to_array($generator->generate()); | ||
|
||
$expected = \array_merge( | ||
[ | ||
'', | ||
], | ||
$values, | ||
); | ||
|
||
self::assertSame($expected, $generated); | ||
} | ||
} |