Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Implement SequentialValueGenerator #4

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ For a full diff see [`f4208b3...main`][f4208b3...main].
- Added `ValueGenerator` ([#1]), by [@localheinz]
- Added `OptionalValueGenerator` ([#2]), by [@localheinz]
- Added `ConcatenatingValueGenerator` ([#3]), by [@localheinz]
- Added `SequentialValueGenerator` ([#4]), by [@localheinz]

[f4208b3...main]: https://github.com/ergebnis/data-generator/compare/f4208b3...main

[#1]: https://github.com/ergebnis/data-generator/pull/1
[#2]: https://github.com/ergebnis/data-generator/pull/2
[#3]: https://github.com/ergebnis/data-generator/pull/3
[#4]: https://github.com/ergebnis/data-generator/pull/4

[@localheinz]: https://github.com/localheinz
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This project comes with the following data generators:

- [`Ergebnis\DataGenerator\ConcatenatingValueGenerator`](#ConcatenatingValuegenerator)
- [`Ergebnis\DataGenerator\OptionalValueGenerator`](#optionalvaluegenerator)
- [`Ergebnis\DataGenerator\SequentialValueGenerator`](#sequentialvaluegenerator)
- [`Ergebnis\DataGenerator\ValueGenerator`](#valuegenerator)

### `ConcatenatingValueGenerator`
Expand Down Expand Up @@ -93,6 +94,40 @@ foreach ($generator->generate() as $value) {
// baz
```

### `SequentialValueGenerator`

Use the `SequentialValueGenerator` to generate one or more values from one or more `StringGenerator`s:

```php
<?php

declare(strict_types=1);

use Ergebnis\DataGenerator;

$generator = new DataGenerator\SequentialValueGenerator(
new DataGenerator\ValueGenerator(
'foo',
'bar',
'baz',
),
new DataGenerator\ValueGenerator(
'qux',
'quux',
),
);

foreach ($generator->generate() as $value) {
echo $value . PHP_EOL
}

// foo
// bar
// baz
// qux
// quux
```

### `ValueGenerator`

Use the `ValueGenerator` to generate one or more values from a list of `string` values:
Expand Down
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<code>$values</code>
</MixedArgument>
</file>
<file src="test/Unit/SequentialValueGeneratorTest.php">
<MixedArgument>
<code>$values</code>
</MixedArgument>
</file>
<file src="test/Unit/ValueGeneratorTest.php">
<MixedArgument>
<code>$values</code>
Expand Down
43 changes: 43 additions & 0 deletions src/SequentialValueGenerator.php
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;
}
}
}
}
71 changes: 71 additions & 0 deletions test/Unit/SequentialValueGeneratorTest.php
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);
}
}