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 #4 from ergebnis/feature/sequential-value-generator
Enhancement: Implement `SequentialValueGenerator`
- Loading branch information
Showing
5 changed files
with
156 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) 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 SequentialValueGenerator implements StringGenerator | ||
{ | ||
/** | ||
* @var array<StringGenerator> | ||
*/ | ||
private readonly array $generators; | ||
|
||
/** | ||
* @throws Exception\InvalidGeneratorsException | ||
*/ | ||
public function __construct(StringGenerator ...$generators) | ||
{ | ||
if ([] === $generators) { | ||
throw Exception\InvalidGeneratorsException::empty(); | ||
} | ||
|
||
$this->generators = $generators; | ||
} | ||
|
||
public function generate(): \Generator | ||
{ | ||
foreach ($this->generators as $generator) { | ||
foreach ($generator->generate() 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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 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\SequentialValueGenerator; | ||
use Ergebnis\DataGenerator\Test; | ||
use Ergebnis\DataGenerator\ValueGenerator; | ||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversClass(SequentialValueGenerator::class)] | ||
#[Framework\Attributes\UsesClass(Exception\InvalidGeneratorsException::class)] | ||
#[Framework\Attributes\UsesClass(ValueGenerator::class)] | ||
final class SequentialValueGeneratorTest extends Framework\TestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
public function testConstructorRejectsEmptyGenerators(): void | ||
{ | ||
$this->expectException(Exception\InvalidGeneratorsException::class); | ||
|
||
new SequentialValueGenerator(); | ||
} | ||
|
||
public function testGenerateReturnsGeneratorThatYieldsValuesFromOneGenerator(): void | ||
{ | ||
$values = self::faker()->words(); | ||
|
||
$generator = new SequentialValueGenerator(new ValueGenerator(...$values)); | ||
|
||
$generated = \iterator_to_array($generator->generate()); | ||
|
||
self::assertSame($values, $generated); | ||
} | ||
|
||
public function testGenerateReturnsGeneratorThatYieldsValuesFromTwoGenerators(): void | ||
{ | ||
$generator = new SequentialValueGenerator( | ||
new ValueGenerator( | ||
'foo', | ||
'bar', | ||
'baz', | ||
), | ||
new ValueGenerator( | ||
'qux', | ||
'quux', | ||
), | ||
); | ||
|
||
$generated = \iterator_to_array($generator->generate()); | ||
|
||
$expected = [ | ||
'foo', | ||
'bar', | ||
'baz', | ||
'qux', | ||
'quux', | ||
]; | ||
|
||
self::assertSame($expected, $generated); | ||
} | ||
} |