Skip to content

Commit 2d20690

Browse files
committed
Add tests
1 parent 37e4dd2 commit 2d20690

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

tests/system/Database/Live/FabricatorLiveTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testCreateAddsCountToDatabase(): void
5151

5252
// Some countries violate the 40 character limit so override that
5353
$fabricator->setOverrides(['country' => 'France']);
54-
54+
$fabricator->setUnique('email');
5555
$fabricator->create($count);
5656

5757
$this->seeNumRecords($count, 'user', []);

tests/system/Test/FabricatorTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Test;
1515

1616
use CodeIgniter\Config\Factories;
17+
use CodeIgniter\Model;
1718
use Tests\Support\Models\EntityModel;
1819
use Tests\Support\Models\EventModel;
1920
use Tests\Support\Models\FabricatorModel;
@@ -491,4 +492,59 @@ public function testResetClearsValue(): void
491492

492493
$this->assertSame(0, Fabricator::getCount('giants'));
493494
}
495+
496+
public function testUniqueSetsOutUniqueFieldValues(): void
497+
{
498+
$model = new class () extends Model {
499+
protected $allowedFields = ['email'];
500+
protected $returnType = 'array';
501+
};
502+
503+
$result = (new Fabricator($model))
504+
->setUnique('email')
505+
->make(5000);
506+
507+
$result = array_map(static fn (array $email): string => $email['email'], $result);
508+
509+
$this->assertSame(array_unique($result), $result);
510+
}
511+
512+
public function testOptionalSetsOutOptionalFieldValues(): void
513+
{
514+
$model = new class () extends Model {
515+
protected $allowedFields = ['email'];
516+
protected $returnType = 'array';
517+
};
518+
519+
$result = (new Fabricator($model))
520+
->setOptional('email', 0.5, false) // 50% probability of email being `false`
521+
->make(5000);
522+
523+
$result = array_map(static fn (array $email) => $email['email'], $result);
524+
525+
$this->assertLessThan(
526+
count($result),
527+
count(array_filter($result))
528+
);
529+
}
530+
531+
public function testValidSetsOutValidValuesUsingCallback(): void
532+
{
533+
$model = new class () extends Model {
534+
protected $allowedFields = ['digit'];
535+
protected $returnType = 'array';
536+
};
537+
538+
$result = (new Fabricator($model, ['digit' => 'numberBetween']))
539+
->setValid('digit', static fn (int $digit): bool => $digit % 2 === 0)
540+
->make(5000);
541+
$result = array_map(static fn (array $digit): int => $digit['digit'], $result);
542+
543+
foreach ($result as $digit) {
544+
$this->assertTrue(
545+
$digit % 2 === 0,
546+
sprintf('Failed asserting that %s is even.', number_format($digit))
547+
);
548+
}
549+
}
494550
}

0 commit comments

Comments
 (0)