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 #3 from ergebnis/feature/concatenating-value-gener…
…ator Enhancement: Implement `ConcatenatingValueGenerator`
- Loading branch information
Showing
7 changed files
with
248 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,51 @@ | ||
<?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 ConcatenatingValueGenerator implements StringGenerator | ||
{ | ||
private readonly StringGenerator $firstGenerator; | ||
private readonly StringGenerator $secondGenerator; | ||
|
||
/** | ||
* @throws Exception\InvalidGeneratorsException | ||
*/ | ||
public function __construct(StringGenerator ...$generators) | ||
{ | ||
$count = \count($generators); | ||
|
||
if (0 === $count) { | ||
throw Exception\InvalidGeneratorsException::empty(); | ||
} | ||
|
||
$this->firstGenerator = \array_shift($generators); | ||
|
||
if (1 === $count) { | ||
$this->secondGenerator = new ValueGenerator(''); | ||
|
||
return; | ||
} | ||
|
||
$this->secondGenerator = new self(...$generators); | ||
} | ||
|
||
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,22 @@ | ||
<?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\Exception; | ||
|
||
final class InvalidGeneratorsException extends \InvalidArgumentException | ||
{ | ||
public static function empty(): self | ||
{ | ||
return new self('Generators can not be empty.'); | ||
} | ||
} |
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,101 @@ | ||
<?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\ConcatenatingValueGenerator; | ||
use Ergebnis\DataGenerator\Exception; | ||
use Ergebnis\DataGenerator\Test; | ||
use Ergebnis\DataGenerator\ValueGenerator; | ||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversClass(ConcatenatingValueGenerator::class)] | ||
#[Framework\Attributes\UsesClass(Exception\InvalidGeneratorsException::class)] | ||
#[Framework\Attributes\UsesClass(ValueGenerator::class)] | ||
final class ConcatenatingValueGeneratorTest extends Framework\TestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
public function testConstructorRejectsEmptyGenerators(): void | ||
{ | ||
$this->expectException(Exception\InvalidGeneratorsException::class); | ||
|
||
new ConcatenatingValueGenerator(); | ||
} | ||
|
||
public function testGenerateReturnsGeneratorThatYieldsValuesFromOneGenerator(): void | ||
{ | ||
$values = self::faker()->words(); | ||
|
||
$generator = new ConcatenatingValueGenerator(new ValueGenerator(...$values)); | ||
|
||
$generated = \iterator_to_array($generator->generate()); | ||
|
||
self::assertSame($values, $generated); | ||
} | ||
|
||
public function testGenerateReturnsGeneratorThatYieldsValuesFromTwoGenerators(): 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 testGenerateReturnsGeneratorThatYieldsValuesFromThreeGenerators(): 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); | ||
} | ||
} |
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,30 @@ | ||
<?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\Test\Unit\Exception; | ||
|
||
use Ergebnis\DataGenerator\Exception; | ||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversClass(Exception\InvalidGeneratorsException::class)] | ||
final class InvalidGeneratorsExceptionTest extends Framework\TestCase | ||
{ | ||
public function testEmptyReturnsException(): void | ||
{ | ||
$exception = Exception\InvalidGeneratorsException::empty(); | ||
|
||
$message = 'Generators can not be empty.'; | ||
|
||
self::assertSame($message, $exception->getMessage()); | ||
} | ||
} |