Fix: Add test to assert unique() behaviour#233
Conversation
Codecov Report
@@ Coverage Diff @@
## main #233 +/- ##
=========================================
Coverage 58.40% 58.40%
Complexity 2044 2044
=========================================
Files 315 315
Lines 5104 5104
=========================================
Hits 2981 2981
Misses 2123 2123 Continue to review full report at Codecov.
|
Nyholm
left a comment
There was a problem hiding this comment.
Thank you.
Im not sure we should have the "reset" argument in the future. It makes things unpredictable. But that is not stopping us from adding these tests.
| $generator->addProvider(new class(...$words) { | ||
| private $words; | ||
|
|
||
| public function __construct(string ...$words) |
There was a problem hiding this comment.
Why the array deconstruction and not just pass the word array?
There was a problem hiding this comment.
@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.
|
Thank you, @Nyholm! |
This PR
Generator::unique()Related to #155.
💁♂️ As a user of
fakerphp/faker, I would like to continue to be able to useto generate a unique word.
I don't care if it internally uses extensions or providers, but I would like to continue using this behaviour. The tests added here ensure that this is the case.