Skip to content

Commit

Permalink
Add support for Faker modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Mar 29, 2024
1 parent 96ce795 commit 765720c
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion system/Test/Fabricator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Test;

use Closure;
use CodeIgniter\Exceptions\FrameworkException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Model;
Expand Down Expand Up @@ -88,6 +89,17 @@ class Fabricator
*/
protected $tempOverrides;

/**
* Fields to be modified before applying any formatter.
*
* @var array{
* unique: array<non-empty-string, array{reset: bool, maxRetries: int}>,
* optional: array<non-empty-string, array{weight: float, default: mixed}>,
* valid: array<non-empty-string, array{validator: Closure(mixed): bool|null, maxRetries: int}>
* }
*/
private array $modifiedFields = ['unique' => [], 'optional' => [], 'valid' => []];

/**
* Default formatter to use when nothing is detected
*
Expand Down Expand Up @@ -251,6 +263,46 @@ public function setOverrides(array $overrides = [], $persist = true): self
return $this;
}

/**
* Set a field to be unique.
*
* @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.
*/
public function setUnique(string $field, bool $reset = false, int $maxRetries = 10000): static
{
$this->modifiedFields['unique'][$field] = compact('reset', 'maxRetries');

return $this;
}

/**
* Set a field to be optional.
*
* @param float $weight A probability between 0 and 1, 0 means that we always get the default value.
*/
public function setOptional(string $field, float $weight = 0.5, mixed $default = null): static
{
$this->modifiedFields['optional'][$field] = compact('weight', 'default');

return $this;
}

/**
* Set a field to be valid using a callback.
*
* @param Closure(mixed): bool|null $validator A function returning true for valid values
* @param int $maxRetries Maximum number of retries to find a valid value,
* After which an OverflowException is thrown.
*/
public function setValid(string $field, ?Closure $validator = null, int $maxRetries = 10000): static
{
$this->modifiedFields['valid'][$field] = compact('validator', 'maxRetries');

return $this;
}

/**
* Returns the current formatters
*/
Expand Down Expand Up @@ -380,7 +432,30 @@ public function makeArray()
$result = [];

foreach ($this->formatters as $field => $formatter) {
$result[$field] = $this->faker->{$formatter}();
$faker = $this->faker;

if (isset($this->modifiedFields['unique'][$field])) {
$faker = $faker->unique(
$this->modifiedFields['unique'][$field]['reset'],
$this->modifiedFields['unique'][$field]['maxRetries']
);
}

if (isset($this->modifiedFields['optional'][$field])) {
$faker = $faker->optional(
$this->modifiedFields['optional'][$field]['weight'],
$this->modifiedFields['optional'][$field]['default']
);
}

if (isset($this->modifiedFields['valid'][$field])) {
$faker = $faker->valid(
$this->modifiedFields['valid'][$field]['validator'],
$this->modifiedFields['valid'][$field]['maxRetries']
);
}

$result[$field] = $faker->format($formatter);
}
}
// If no formatters were defined then look for a model fake() method
Expand Down

0 comments on commit 765720c

Please sign in to comment.