1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use Zenstruck\Foundry\FactoryCollection; |
6
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category; |
7
|
|
|
use Zenstruck\Foundry\Tests\UnitTestCase; |
8
|
|
|
use function Zenstruck\Foundry\factory; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Kevin Bond <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class FactoryCollectionTest extends UnitTestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @test |
17
|
|
|
*/ |
18
|
|
|
public function can_create_with_static_size(): void |
19
|
|
|
{ |
20
|
|
|
$collection = new FactoryCollection(factory(Category::class)->withoutPersisting(), 2); |
21
|
|
|
|
22
|
|
|
$this->assertCount(2, $collection->create()); |
23
|
|
|
$this->assertCount(2, $collection->create()); |
24
|
|
|
$this->assertCount(2, $collection->create()); |
25
|
|
|
$this->assertCount(2, $collection->create()); |
26
|
|
|
$this->assertCount(2, $collection->create()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
*/ |
32
|
|
|
public function can_create_with_random_range(): void |
33
|
|
|
{ |
34
|
|
|
$collection = new FactoryCollection(factory(Category::class)->withoutPersisting(), 0, 3); |
35
|
|
|
$counts = []; |
36
|
|
|
|
37
|
|
|
while (4 !== \count(\array_unique($counts))) { |
38
|
|
|
$counts[] = \count($collection->create()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->assertCount(4, \array_unique($counts)); |
42
|
|
|
$this->assertContains(0, $counts); |
43
|
|
|
$this->assertContains(1, $counts); |
44
|
|
|
$this->assertContains(2, $counts); |
45
|
|
|
$this->assertContains(3, $counts); |
46
|
|
|
$this->assertNotContains(4, $counts); |
47
|
|
|
$this->assertNotContains(5, $counts); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @test |
52
|
|
|
*/ |
53
|
|
|
public function min_must_be_less_than_or_equal_to_max(): void |
54
|
|
|
{ |
55
|
|
|
$this->expectException(\InvalidArgumentException::class); |
56
|
|
|
$this->expectDeprecationMessage('Min must be less than max.'); |
57
|
|
|
|
58
|
|
|
new FactoryCollection(factory(Category::class)->withoutPersisting(), 4, 3); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|