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

Fix: Add test to assert unique() behaviour #233

Merged
merged 1 commit into from
Dec 26, 2020
Merged
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
127 changes: 127 additions & 0 deletions test/Faker/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Faker\Extension\ExtensionNotFound;
use Faker\Extension\FileExtension;
use Faker\Generator;
use Faker\Provider;
use Faker\UniqueGenerator;

/**
* @covers \Faker\Generator
Expand Down Expand Up @@ -157,6 +159,131 @@ public function testSeed()
$this->faker->seed('10');
self::assertTrue(true, 'seeding with a non int value doesn\'t throw an exception');
}

public function testUniqueReturnsUniqueGenerator(): void
{
$generator = new Generator();

$generator->addProvider(new Provider\Base($generator));

$uniqueGenerator = $generator->unique();

self::assertInstanceOf(UniqueGenerator::class, $uniqueGenerator);
}

public function testUniqueReturnsSameUniqueGeneratorWhenUsedWithoutArguments(): void
{
$generator = new Generator();

$generator->addProvider(new Provider\Base($generator));

$uniqueGenerator = $generator->unique();

self::assertSame($uniqueGenerator, $generator->unique());
}

public function testUniqueReturnsSameUniqueGeneratorWhenResetIsFalse(): void
{
$generator = new Generator();

$generator->addProvider(new Provider\Base($generator));

$uniqueGenerator = $generator->unique();

self::assertSame($uniqueGenerator, $generator->unique(false));
}

public function testUniqueReturnsDifferentUniqueGeneratorWhenResetIsTrue(): void
{
$generator = new Generator();

$generator->addProvider(new Provider\Base($generator));

$uniqueGenerator = $generator->unique();

self::assertNotSame($uniqueGenerator, $generator->unique(true));
}

public function testUniqueReturnsUniqueGeneratorThatGeneratesUniqueValues(): void
{
$words = [
'foo',
'bar',
'baz',
];

$generator = new Generator();

$generator->addProvider(new Provider\Base($generator));
$generator->addProvider(new class(...$words) {
private $words;

public function __construct(string ...$words)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the array deconstruction and not just pass the word array?

Copy link
Member Author

@localheinz localheinz Dec 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bramceulemans

Because when passing an array, it would be necessary to write more code to ensure that every element of the array is a string:

/**
 * @param array<int, string> $words
 */
public function __construct(array $words)
{
    $this->words = array_values(array_map(static function (string $word): string {
        return $word;
    }, $words));
}

or

/**
 * @param array<int, string> $words
 * 
 * @throws \InvalidArgumentException
 */
public function __construct(array $words)
{
    $notString = array_filter(static function ($word): bool {
        return !is_string($word);
    }, $words);

    if ([] !== $notString) {
        throw new \InvalidArgumentException('Every word needs to be a string');
    }

    $this->words = array_values($words);
}

Using variadics here allows me not to write this code.

{
$this->words = $words;
}

public function word(): string
{
$key = array_rand($this->words);

return $this->words[$key];
}
});

$uniqueGenerator = $generator->unique();

$generatedWords = [
$uniqueGenerator->word(),
$uniqueGenerator->word(),
$uniqueGenerator->word(),
];

self::assertEquals($words, $generatedWords);
}

public function testUniqueReturnsUniqueGeneratorThatThrowsWhenItCanNotGenerateUniqueValuesAnymore(): void
{
$words = [
'foo',
];

$maxRetries = 90;

$generator = new Generator();

$generator->addProvider(new Provider\Base($generator));
$generator->addProvider(new class(...$words) {
private $words;

public function __construct(string ...$words)
{
$this->words = $words;
}

public function word(): string
{
$key = array_rand($this->words);

return $this->words[$key];
}
});

$uniqueGenerator = $generator->unique(
false,
$maxRetries
);

$uniqueGenerator->word();

$this->expectException(\OverflowException::class);
$this->expectExceptionMessage(sprintf(
'Maximum retries of %d reached without finding a unique value',
$maxRetries
));

$uniqueGenerator->word();
}
}

final class FooProvider
Expand Down