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 ValueGenerator #1

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
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 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
8 changes: 7 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19"/>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19">
<file src="test/Unit/ValueGeneratorTest.php">
<MixedArgument>
<code>$values</code>
</MixedArgument>
</file>
</files>
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());
}
}
44 changes: 44 additions & 0 deletions test/Unit/ValueGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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
{
$values = self::faker()->words();

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

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

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