Skip to content

Commit

Permalink
Enhancement: Implement ValueGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 30, 2023
1 parent 5183980 commit af98463
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 48 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

For a full diff see [`f4208b3...main`][f4208b3...main].

### Added

- Added `ValueGenerator` ([#1]), by [@localheinz]

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

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

[@localheinz]: https://github.com/localheinz
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,35 @@ composer require ergebnis/data-generator

## Usage

💡 This is a great place for showing a few usage examples!
This project comes with the following data generators:

- [`Ergebnis\DataGenerator\ValueGenerator`](#valuegenerator)

### `ValueGenerator`

#### Generate one or more values from a known list of `string` values

```php
<?php

declare(strict_types=1);

use Ergebnis\DataGenerator;

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

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

// foo
// bar
// baz
```

## Changelog

Expand Down
22 changes: 22 additions & 0 deletions src/Exception/InvalidValuesException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Exception;

final class InvalidValuesException extends \InvalidArgumentException
{
public static function empty(): self
{
return new self('Values can not be empty.');
}
}
19 changes: 5 additions & 14 deletions src/Example.php → src/StringGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,10 @@

namespace Ergebnis\DataGenerator;

final class Example
interface StringGenerator
{
private function __construct(private readonly string $value)
{
}

public static function fromString(string $value): self
{
return new self($value);
}

public function toString(): string
{
return $this->value;
}
/**
* @return \Generator<string>
*/
public function generate(): \Generator;
}
41 changes: 41 additions & 0 deletions src/ValueGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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 ValueGenerator implements StringGenerator
{
/**
* @var array<string>
*/
private readonly array $values;

/**
* @throws Exception\InvalidValuesException
*/
public function __construct(string ...$values)
{
if (0 === \count($values)) {
throw Exception\InvalidValuesException::empty();
}

$this->values = $values;
}

public function generate(): \Generator
{
foreach ($this->values as $value) {
yield $value;
}
}
}
33 changes: 0 additions & 33 deletions test/Unit/ExampleTest.php

This file was deleted.

30 changes: 30 additions & 0 deletions test/Unit/Exception/InvalidValuesExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\Test\Unit\Exception;

use Ergebnis\DataGenerator\Exception;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(Exception\InvalidValuesException::class)]
final class InvalidValuesExceptionTest extends Framework\TestCase
{
public function testEmptyReturnsException(): void
{
$exception = Exception\InvalidValuesException::empty();

$message = 'Values can not be empty.';

self::assertSame($message, $exception->getMessage());
}
}
45 changes: 45 additions & 0 deletions test/Unit/ValueGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\Test\Unit;

use Ergebnis\DataGenerator\Exception;
use Ergebnis\DataGenerator\Test;
use Ergebnis\DataGenerator\ValueGenerator;
use PHPUnit\Framework;

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

public function testConstructorRejectsEmptyValues(): void
{
$this->expectException(Exception\InvalidValuesException::class);

new ValueGenerator();
}

public function testGenerateReturnsGeneratorThatYieldsValues(): void
{
/** @var list<string> $values */
$values = self::faker()->words();

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

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

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

0 comments on commit af98463

Please sign in to comment.