1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory; |
6
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\CommentFactory; |
7
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory; |
8
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithInvalidInitialize; |
9
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithNullInitialize; |
10
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithValidInitialize; |
11
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\UserFactory; |
12
|
|
|
use Zenstruck\Foundry\Tests\FunctionalTestCase; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Kevin Bond <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class ModelFactoryTest extends FunctionalTestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @test |
21
|
|
|
*/ |
22
|
|
|
public function can_find_or_create(): void |
23
|
|
|
{ |
24
|
|
|
CategoryFactory::repository()->assertCount(0); |
25
|
|
|
CategoryFactory::findOrCreate(['name' => 'php']); |
26
|
|
|
CategoryFactory::repository()->assertCount(1); |
27
|
|
|
CategoryFactory::findOrCreate(['name' => 'php']); |
28
|
|
|
CategoryFactory::repository()->assertCount(1); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @test |
33
|
|
|
*/ |
34
|
|
|
public function can_override_initialize(): void |
35
|
|
|
{ |
36
|
|
|
$this->assertFalse(PostFactory::new()->create()->isPublished()); |
|
|
|
|
37
|
|
|
$this->assertTrue(PostFactoryWithValidInitialize::new()->create()->isPublished()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @test |
42
|
|
|
*/ |
43
|
|
|
public function initialize_must_return_an_instance_of_the_current_factory(): void |
44
|
|
|
{ |
45
|
|
|
$this->expectException(\TypeError::class); |
46
|
|
|
$this->expectExceptionMessage(\sprintf('"%1$s::initialize()" must return an instance of "%1$s".', PostFactoryWithInvalidInitialize::class)); |
47
|
|
|
|
48
|
|
|
PostFactoryWithInvalidInitialize::new(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
*/ |
54
|
|
|
public function initialize_must_return_a_value(): void |
55
|
|
|
{ |
56
|
|
|
$this->expectException(\TypeError::class); |
57
|
|
|
$this->expectExceptionMessage(\sprintf('"%1$s::initialize()" must return an instance of "%1$s".', PostFactoryWithNullInitialize::class)); |
58
|
|
|
|
59
|
|
|
PostFactoryWithNullInitialize::new(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
*/ |
65
|
|
|
public function can_find_random_object(): void |
66
|
|
|
{ |
67
|
|
|
CategoryFactory::new()->createMany(5); |
68
|
|
|
|
69
|
|
|
$ids = []; |
70
|
|
|
|
71
|
|
|
while (5 !== \count(\array_unique($ids))) { |
72
|
|
|
$ids[] = CategoryFactory::random()->getId(); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->assertCount(5, \array_unique($ids)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
*/ |
81
|
|
|
public function can_find_random_set_of_objects(): void |
82
|
|
|
{ |
83
|
|
|
CategoryFactory::new()->createMany(5); |
84
|
|
|
|
85
|
|
|
$objects = CategoryFactory::randomSet(3); |
86
|
|
|
|
87
|
|
|
$this->assertCount(3, $objects); |
88
|
|
|
$this->assertCount(3, \array_unique(\array_map(static function($category) { return $category->getId(); }, $objects))); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @test |
93
|
|
|
*/ |
94
|
|
|
public function can_find_random_range_of_objects(): void |
95
|
|
|
{ |
96
|
|
|
CategoryFactory::new()->createMany(5); |
97
|
|
|
|
98
|
|
|
$counts = []; |
99
|
|
|
|
100
|
|
|
while (4 !== \count(\array_unique($counts))) { |
101
|
|
|
$counts[] = \count(CategoryFactory::randomRange(0, 3)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->assertCount(4, \array_unique($counts)); |
105
|
|
|
$this->assertContains(0, $counts); |
106
|
|
|
$this->assertContains(1, $counts); |
107
|
|
|
$this->assertContains(2, $counts); |
108
|
|
|
$this->assertContains(3, $counts); |
109
|
|
|
$this->assertNotContains(4, $counts); |
110
|
|
|
$this->assertNotContains(5, $counts); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @test |
115
|
|
|
*/ |
116
|
|
|
public function one_to_many_with_nested_relationship(): void |
117
|
|
|
{ |
118
|
|
|
$post = PostFactory::new()->create([ |
119
|
|
|
'comments' => CommentFactory::collection(4), |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
$this->assertCount(4, $post->getComments()); |
|
|
|
|
123
|
|
|
UserFactory::repository()->assertCount(4); |
124
|
|
|
CommentFactory::repository()->assertCount(4); |
125
|
|
|
PostFactory::repository()->assertCount(1); // fails (count=5, 1 primary, 1 for each comment) |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|