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