Skip to content

Commit

Permalink
Enhancement: Implement ConcatenatingValueGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 30, 2023
1 parent f4516dc commit 8a120cb
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ For a full diff see [`f4208b3...main`][f4208b3...main].

- Added `ValueGenerator` ([#1]), by [@localheinz]
- Added `OptionalValueGenerator` ([#2]), by [@localheinz]
- Added `ConcatenatingValueGenerator` ([#3]), 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

[@localheinz]: https://github.com/localheinz
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,46 @@ composer require ergebnis/data-generator

This project comes with the following data generators:

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

### `ConcatenatingValueGenerator`

Use the `ConcatenatingValueGenerator` to generate values by concatenating values generated from one or more `StringGenerator`s:

```php
<?php

declare(strict_types=1);

use Ergebnis\DataGenerator;

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

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

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

### `OptionalValueGenerator`

Use the `OptionalValueGenerator` to generate an empty string and one or more values from a list of `string` values:
Expand Down
10 changes: 10 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19">
<file src="src/ConcatenatingValueGenerator.php">
<PropertyNotSetInConstructor>
<code>$secondGenerator</code>
</PropertyNotSetInConstructor>
</file>
<file src="test/Unit/ConcatenatingValueGeneratorTest.php">
<MixedArgument>
<code>$values</code>
</MixedArgument>
</file>
<file src="test/Unit/OptionalValueGeneratorTest.php">
<MixedArgument>
<code>$values</code>
Expand Down
59 changes: 59 additions & 0 deletions src/ConcatenatingValueGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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 ConcatenatingValueGenerator implements StringGenerator
{
private readonly StringGenerator $firstGenerator;
private readonly StringGenerator $secondGenerator;

/**
* @throws \InvalidArgumentException
*/
public function __construct(StringGenerator ...$stringGenerators)
{
$count = \count($stringGenerators);

if (0 === $count) {

Check warning on line 28 in src/ConcatenatingValueGenerator.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (8.1, locked)

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ public function __construct(StringGenerator ...$stringGenerators) { $count = \count($stringGenerators); - if (0 === $count) { + if (-1 === $count) { throw new \InvalidArgumentException('Need at least one generator'); } $this->firstGenerator = \array_shift($stringGenerators);
throw new \InvalidArgumentException('Need at least one generator');

Check warning on line 29 in src/ConcatenatingValueGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/ConcatenatingValueGenerator.php#L29

Added line #L29 was not covered by tests
}

$this->firstGenerator = \array_shift($stringGenerators);

if (1 === $count) {
$this->secondGenerator = new ValueGenerator('');

return;
}

if (2 === $count) {
$this->secondGenerator = \array_shift($stringGenerators);

return;
}

if (2 < $count) {

Check warning on line 46 in src/ConcatenatingValueGenerator.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (8.1, locked)

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ $this->secondGenerator = \array_shift($stringGenerators); return; } - if (2 < $count) { + if (2 <= $count) { $this->secondGenerator = new self(...$stringGenerators); } }
$this->secondGenerator = new self(...$stringGenerators);
}
}

public function generate(): \Generator
{
foreach ($this->firstGenerator->generate() as $firstValue) {
foreach ($this->secondGenerator->generate() as $secondValue) {
yield $firstValue . $secondValue;
}
}
}
}
93 changes: 93 additions & 0 deletions test/Unit/ConcatenatingValueGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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\ConcatenatingValueGenerator;
use Ergebnis\DataGenerator\Test;
use Ergebnis\DataGenerator\ValueGenerator;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(ConcatenatingValueGenerator::class)]
#[Framework\Attributes\UsesClass(ValueGenerator::class)]
final class ConcatenatingValueGeneratorTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testGenerateReturnsGeneratorThatYieldsValuesFromSingleStringGenerator(): void
{
$values = self::faker()->words();

$generator = new ConcatenatingValueGenerator(new ValueGenerator(...$values));

$generated = \iterator_to_array($generator->generate());

self::assertSame($values, $generated);
}

public function testGenerateReturnsGeneratorThatYieldsValuesFromTwoStringGenerators(): void
{
$generator = new ConcatenatingValueGenerator(
new ValueGenerator(
'foo',
'bar',
),
new ValueGenerator(
'1',
'2',
'3',
),
);

$generated = \iterator_to_array($generator->generate());

$expected = [
'foo1',
'foo2',
'foo3',
'bar1',
'bar2',
'bar3',
];

self::assertSame($expected, $generated);
}

public function testGenerateReturnsGeneratorThatYieldsValuesFromThreeStringGenerators(): void
{
$generator = new ConcatenatingValueGenerator(
new ValueGenerator(
'foo',
'bar',
'baz',
),
new ValueGenerator('-'),
new ValueGenerator(
'qux',
'quux',
),
);

$generated = \iterator_to_array($generator->generate());

$expected = [
'foo-qux',
'foo-quux',

'bar-qux',
'bar-quux',
'baz-qux',
'baz-quux',
];

self::assertSame($expected, $generated);
}
}

0 comments on commit 8a120cb

Please sign in to comment.