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 ConcatenatingValueGenerator
- Loading branch information
1 parent
f4516dc
commit 8a120cb
Showing
5 changed files
with
201 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,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
|
||
throw new \InvalidArgumentException('Need at least one generator'); | ||
} | ||
|
||
$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
|
||
$this->secondGenerator = new self(...$stringGenerators); | ||
} | ||
} | ||
|
||
public function generate(): \Generator | ||
{ | ||
foreach ($this->firstGenerator->generate() as $firstValue) { | ||
foreach ($this->secondGenerator->generate() as $secondValue) { | ||
yield $firstValue . $secondValue; | ||
} | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} |