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