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

Feature: Unique key #848

Open
wants to merge 2 commits into
base: 1.23
Choose a base branch
from
Open
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
19 changes: 10 additions & 9 deletions src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,9 @@ class Generator
private $container;

/**
* @var UniqueGenerator
* @var array<UniqueGenerator>
*/
private $uniqueGenerator;
private $uniqueGenerators = [];

public function __construct(ContainerInterface $container = null)
{
Expand Down Expand Up @@ -618,21 +618,22 @@ public function getProviders()
* $faker->unique()->randomElement(array(1, 2, 3));
* </code>
*
* @param bool $reset If set to true, resets the list of existing values
* @param int $maxRetries Maximum number of retries to find a unique value,
* After which an OverflowException is thrown.
* @param bool $reset If set to true, resets the list of existing values
* @param int $maxRetries Maximum number of retries to find a unique value,
* After which an OverflowException is thrown.
* @param string $key Unique identifier to allow multiple independent UniqueGenerator instances
*
* @throws \OverflowException When no unique value can be found by iterating $maxRetries times
*
* @return self A proxy class returning only non-existing values
*/
public function unique($reset = false, $maxRetries = 10000)
public function unique($reset = false, $maxRetries = 10000, $key = 'default')
Copy link

Choose a reason for hiding this comment

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

I think we could also pass &$uniques = [] to init generator

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, I can't follow.

{
if ($reset || $this->uniqueGenerator === null) {
$this->uniqueGenerator = new UniqueGenerator($this, $maxRetries);
if ($reset || !array_key_exists($key, $this->uniqueGenerators)) {
$this->uniqueGenerators[$key] = new UniqueGenerator($this, $maxRetries);
}

return $this->uniqueGenerator;
return $this->uniqueGenerators[$key];
}

/**
Expand Down
13 changes: 13 additions & 0 deletions test/Faker/UniqueGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@ public function testUniqueGeneratorRetries(): void

$this->addToAssertionCount(1);
}

public function testUniqueGeneratorWithKey(): void
{
for ($i = 0; $i < 10; ++$i) {
$this->faker->unique(key: 'id')->ext(NumberExtension::class)->numberBetween(0, 9);
}

for ($i = 0; $i < 10; ++$i) {
$this->faker->unique(key: 'order')->ext(NumberExtension::class)->numberBetween(0, 9);
}

$this->addToAssertionCount(1);
}
}