1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
6
|
|
|
use Zenstruck\Foundry\AnonymousFactory; |
7
|
|
|
use Zenstruck\Foundry\Test\Factories; |
8
|
|
|
use Zenstruck\Foundry\Test\ResetDatabase; |
9
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Kevin Bond <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
final class AnonymousFactoryTest extends KernelTestCase |
15
|
|
|
{ |
16
|
|
|
use Factories, ResetDatabase; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @test |
20
|
|
|
*/ |
21
|
|
|
public function can_find_or_create(): void |
22
|
|
|
{ |
23
|
|
|
$factory = AnonymousFactory::new(Category::class); |
24
|
|
|
|
25
|
|
|
$factory->assert()->count(0); |
26
|
|
|
$factory->findOrCreate(['name' => 'php']); |
27
|
|
|
$factory->assert()->count(1); |
28
|
|
|
$factory->findOrCreate(['name' => 'php']); |
29
|
|
|
$factory->assert()->count(1); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @test |
34
|
|
|
*/ |
35
|
|
|
public function can_find_random_object(): void |
36
|
|
|
{ |
37
|
|
|
$factory = AnonymousFactory::new(Category::class, ['name' => 'php']); |
38
|
|
|
$factory->many(5)->create(); |
39
|
|
|
|
40
|
|
|
$ids = []; |
41
|
|
|
|
42
|
|
|
while (5 !== \count(\array_unique($ids))) { |
43
|
|
|
$ids[] = $factory->random()->getId(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->assertCount(5, \array_unique($ids)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @test |
51
|
|
|
*/ |
52
|
|
|
public function can_create_random_object_if_none_exists(): void |
53
|
|
|
{ |
54
|
|
|
$factory = AnonymousFactory::new(Category::class, ['name' => 'php']); |
55
|
|
|
|
56
|
|
|
$factory->assert()->count(0); |
57
|
|
|
$this->assertInstanceOf(Category::class, $factory->randomOrCreate()->object()); |
58
|
|
|
$factory->assert()->count(1); |
59
|
|
|
$this->assertInstanceOf(Category::class, $factory->randomOrCreate()->object()); |
60
|
|
|
$factory->assert()->count(1); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @test |
65
|
|
|
*/ |
66
|
|
|
public function can_get_or_create_random_object_with_attributes(): void |
67
|
|
|
{ |
68
|
|
|
$factory = AnonymousFactory::new(Category::class, ['name' => 'name1']); |
69
|
|
|
$factory->many(5)->create(); |
70
|
|
|
|
71
|
|
|
$factory->assert()->count(5); |
72
|
|
|
$this->assertSame('name2', $factory->randomOrCreate(['name' => 'name2'])->getName()); |
|
|
|
|
73
|
|
|
$factory->assert()->count(6); |
74
|
|
|
$this->assertSame('name2', $factory->randomOrCreate(['name' => 'name2'])->getName()); |
75
|
|
|
$factory->assert()->count(6); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
*/ |
81
|
|
|
public function can_find_random_set_of_objects(): void |
82
|
|
|
{ |
83
|
|
|
$factory = AnonymousFactory::new(Category::class, ['name' => 'php']); |
84
|
|
|
$factory->many(5)->create(); |
85
|
|
|
|
86
|
|
|
$objects = $factory->randomSet(3); |
87
|
|
|
|
88
|
|
|
$this->assertCount(3, $objects); |
89
|
|
|
$this->assertCount(3, \array_unique(\array_map(static function($category) { return $category->getId(); }, $objects))); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @test |
94
|
|
|
*/ |
95
|
|
|
public function can_find_random_set_of_objects_with_attributes(): void |
96
|
|
|
{ |
97
|
|
|
$factory = AnonymousFactory::new(Category::class); |
98
|
|
|
$factory->many(20)->create(['name' => 'name1']); |
99
|
|
|
$factory->many(5)->create(['name' => 'name2']); |
100
|
|
|
|
101
|
|
|
$objects = $factory->randomSet(2, ['name' => 'name2']); |
102
|
|
|
|
103
|
|
|
$this->assertCount(2, $objects); |
104
|
|
|
$this->assertSame('name2', $objects[0]->getName()); |
105
|
|
|
$this->assertSame('name2', $objects[1]->getName()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @test |
110
|
|
|
*/ |
111
|
|
|
public function can_find_random_range_of_objects(): void |
112
|
|
|
{ |
113
|
|
|
$factory = AnonymousFactory::new(Category::class, ['name' => 'php']); |
114
|
|
|
$factory->many(5)->create(); |
115
|
|
|
|
116
|
|
|
$counts = []; |
117
|
|
|
|
118
|
|
|
while (4 !== \count(\array_unique($counts))) { |
119
|
|
|
$counts[] = \count($factory->randomRange(0, 3)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->assertCount(4, \array_unique($counts)); |
123
|
|
|
$this->assertContains(0, $counts); |
124
|
|
|
$this->assertContains(1, $counts); |
125
|
|
|
$this->assertContains(2, $counts); |
126
|
|
|
$this->assertContains(3, $counts); |
127
|
|
|
$this->assertNotContains(4, $counts); |
128
|
|
|
$this->assertNotContains(5, $counts); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @test |
133
|
|
|
*/ |
134
|
|
|
public function can_find_random_range_of_objects_with_attributes(): void |
135
|
|
|
{ |
136
|
|
|
$factory = AnonymousFactory::new(Category::class); |
137
|
|
|
$factory->many(20)->create(['name' => 'name1']); |
138
|
|
|
$factory->many(5)->create(['name' => 'name2']); |
139
|
|
|
|
140
|
|
|
$objects = $factory->randomRange(2, 4, ['name' => 'name2']); |
141
|
|
|
|
142
|
|
|
$this->assertGreaterThanOrEqual(2, \count($objects)); |
143
|
|
|
$this->assertLessThanOrEqual(4, \count($objects)); |
144
|
|
|
|
145
|
|
|
foreach ($objects as $object) { |
146
|
|
|
$this->assertSame('name2', $object->getName()); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @test |
152
|
|
|
*/ |
153
|
|
|
public function first_and_last_return_the_correct_object(): void |
154
|
|
|
{ |
155
|
|
|
$factory = AnonymousFactory::new(Category::class); |
156
|
|
|
$categoryA = $factory->create(['name' => '3']); |
157
|
|
|
$categoryB = $factory->create(['name' => '2']); |
|
|
|
|
158
|
|
|
$categoryC = $factory->create(['name' => '1']); |
159
|
|
|
|
160
|
|
|
$this->assertSame($categoryA->getId(), $factory->first()->getId()); |
161
|
|
|
$this->assertSame($categoryC->getId(), $factory->first('name')->getId()); |
162
|
|
|
$this->assertSame($categoryC->getId(), $factory->last()->getId()); |
163
|
|
|
$this->assertSame($categoryA->getId(), $factory->last('name')->getId()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @test |
168
|
|
|
*/ |
169
|
|
|
public function first_throws_exception_if_no_entities_exist(): void |
170
|
|
|
{ |
171
|
|
|
$this->expectException(\RuntimeException::class); |
172
|
|
|
|
173
|
|
|
AnonymousFactory::new(Category::class)->first(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @test |
178
|
|
|
*/ |
179
|
|
|
public function last_throws_exception_if_no_entities_exist(): void |
180
|
|
|
{ |
181
|
|
|
$this->expectException(\RuntimeException::class); |
182
|
|
|
|
183
|
|
|
AnonymousFactory::new(Category::class)->last(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @test |
188
|
|
|
*/ |
189
|
|
|
public function can_count_and_truncate_model_factory(): void |
190
|
|
|
{ |
191
|
|
|
$factory = AnonymousFactory::new(Category::class, ['name' => 'php']); |
192
|
|
|
|
193
|
|
|
$this->assertSame(0, $factory->count()); |
194
|
|
|
$this->assertCount(0, $factory); |
195
|
|
|
|
196
|
|
|
$factory->many(4)->create(); |
197
|
|
|
|
198
|
|
|
$this->assertSame(4, $factory->count()); |
199
|
|
|
$this->assertCount(4, $factory); |
200
|
|
|
|
201
|
|
|
$factory->truncate(); |
202
|
|
|
|
203
|
|
|
$this->assertSame(0, $factory->count()); |
204
|
|
|
$this->assertCount(0, $factory); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @test |
209
|
|
|
*/ |
210
|
|
|
public function can_get_all_entities(): void |
211
|
|
|
{ |
212
|
|
|
$factory = AnonymousFactory::new(Category::class, ['name' => 'php']); |
213
|
|
|
|
214
|
|
|
$this->assertSame([], $factory->all()); |
215
|
|
|
|
216
|
|
|
$factory->many(4)->create(); |
217
|
|
|
|
218
|
|
|
$categories = $factory->all(); |
219
|
|
|
|
220
|
|
|
$this->assertCount(4, $categories); |
221
|
|
|
$this->assertCount(4, \iterator_to_array($factory)); |
222
|
|
|
$this->assertInstanceOf(Category::class, $categories[0]->object()); |
223
|
|
|
$this->assertInstanceOf(Category::class, $categories[1]->object()); |
224
|
|
|
$this->assertInstanceOf(Category::class, $categories[2]->object()); |
225
|
|
|
$this->assertInstanceOf(Category::class, $categories[3]->object()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @test |
230
|
|
|
*/ |
231
|
|
|
public function can_find_entity(): void |
232
|
|
|
{ |
233
|
|
|
$factory = AnonymousFactory::new(Category::class); |
234
|
|
|
|
235
|
|
|
$factory->create(['name' => 'first']); |
236
|
|
|
$factory->create(['name' => 'second']); |
237
|
|
|
$category = $factory->create(['name' => 'third']); |
238
|
|
|
|
239
|
|
|
$this->assertSame('second', $factory->find(['name' => 'second'])->getName()); |
240
|
|
|
$this->assertSame('third', $factory->find(['id' => $category->getId()])->getName()); |
241
|
|
|
$this->assertSame('third', $factory->find($category->getId())->getName()); |
242
|
|
|
$this->assertSame('third', $factory->find($category->object())->getName()); |
243
|
|
|
$this->assertSame('third', $factory->find($category)->getName()); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @test |
248
|
|
|
*/ |
249
|
|
|
public function find_throws_exception_if_no_entities_exist(): void |
250
|
|
|
{ |
251
|
|
|
$this->expectException(\RuntimeException::class); |
252
|
|
|
|
253
|
|
|
AnonymousFactory::new(Category::class)->find(99); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @test |
258
|
|
|
*/ |
259
|
|
|
public function can_find_by(): void |
260
|
|
|
{ |
261
|
|
|
$factory = AnonymousFactory::new(Category::class); |
262
|
|
|
|
263
|
|
|
$this->assertSame([], $factory->findBy(['name' => 'name2'])); |
264
|
|
|
|
265
|
|
|
$factory->create(['name' => 'name1']); |
266
|
|
|
$factory->create(['name' => 'name2']); |
267
|
|
|
$factory->create(['name' => 'name2']); |
268
|
|
|
|
269
|
|
|
$categories = $factory->findBy(['name' => 'name2']); |
270
|
|
|
|
271
|
|
|
$this->assertCount(2, $categories); |
272
|
|
|
$this->assertSame('name2', $categories[0]->getName()); |
273
|
|
|
$this->assertSame('name2', $categories[1]->getName()); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|