|
14 | 14 | namespace CodeIgniter\Test;
|
15 | 15 |
|
16 | 16 | use CodeIgniter\Config\Factories;
|
| 17 | +use CodeIgniter\Model; |
17 | 18 | use Tests\Support\Models\EntityModel;
|
18 | 19 | use Tests\Support\Models\EventModel;
|
19 | 20 | use Tests\Support\Models\FabricatorModel;
|
@@ -491,4 +492,60 @@ public function testResetClearsValue(): void
|
491 | 492 |
|
492 | 493 | $this->assertSame(0, Fabricator::getCount('giants'));
|
493 | 494 | }
|
| 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->assertSame( |
| 545 | + 0, |
| 546 | + $digit % 2, |
| 547 | + sprintf('Failed asserting that %s is even.', number_format($digit)) |
| 548 | + ); |
| 549 | + } |
| 550 | + } |
494 | 551 | }
|
0 commit comments