1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
6
|
|
|
use Zenstruck\Foundry\Instantiator; |
7
|
|
|
use Zenstruck\Foundry\Test\Factories; |
8
|
|
|
use Zenstruck\Foundry\Test\ResetDatabase; |
9
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Brand; |
10
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Category; |
11
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Image; |
12
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Product; |
13
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Review; |
14
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Tag; |
15
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Variant; |
16
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\CascadeProductCommentFactory; |
17
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\CascadeProductFactory; |
18
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\CascadeVariantFactory; |
19
|
|
|
use function Zenstruck\Foundry\factory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Kevin Bond <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class FactoryDoctrineCascadeTest extends KernelTestCase |
25
|
|
|
{ |
26
|
|
|
use Factories, ResetDatabase; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @test |
30
|
|
|
*/ |
31
|
|
|
public function many_to_one_relationship(): void |
32
|
|
|
{ |
33
|
|
|
$product = factory(Product::class, [ |
34
|
|
|
'name' => 'foo', |
35
|
|
|
'brand' => factory(Brand::class, ['name' => 'bar']), |
36
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
37
|
|
|
$this->assertNull($attibutes['brand']->getId()); |
38
|
|
|
|
39
|
|
|
return (new Instantiator())($attibutes, $class); |
40
|
|
|
})->create(); |
41
|
|
|
|
42
|
|
|
$this->assertNotNull($product->getBrand()->getId()); |
|
|
|
|
43
|
|
|
$this->assertSame('bar', $product->getBrand()->getName()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
*/ |
49
|
|
|
public function one_to_many_relationship(): void |
50
|
|
|
{ |
51
|
|
|
$product = factory(Product::class, [ |
52
|
|
|
'name' => 'foo', |
53
|
|
|
'variants' => [factory(Variant::class, ['name' => 'bar'])], |
54
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
55
|
|
|
$this->assertNull($attibutes['variants'][0]->getId()); |
56
|
|
|
|
57
|
|
|
return (new Instantiator())($attibutes, $class); |
58
|
|
|
})->create(); |
59
|
|
|
|
60
|
|
|
$this->assertCount(1, $product->getVariants()); |
|
|
|
|
61
|
|
|
$this->assertNotNull($product->getVariants()->first()->getId()); |
62
|
|
|
$this->assertSame('bar', $product->getVariants()->first()->getName()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
|
public function many_to_many_relationship(): void |
69
|
|
|
{ |
70
|
|
|
$product = factory(Product::class, [ |
71
|
|
|
'name' => 'foo', |
72
|
|
|
'tags' => [factory(Tag::class, ['name' => 'bar'])], |
73
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
74
|
|
|
$this->assertNull($attibutes['tags'][0]->getId()); |
75
|
|
|
|
76
|
|
|
return (new Instantiator())($attibutes, $class); |
77
|
|
|
})->create(); |
78
|
|
|
|
79
|
|
|
$this->assertCount(1, $product->getTags()); |
|
|
|
|
80
|
|
|
$this->assertNotNull($product->getTags()->first()->getId()); |
81
|
|
|
$this->assertSame('bar', $product->getTags()->first()->getName()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @test |
86
|
|
|
*/ |
87
|
|
|
public function many_to_many_reverse_relationship(): void |
88
|
|
|
{ |
89
|
|
|
$product = factory(Product::class, [ |
90
|
|
|
'name' => 'foo', |
91
|
|
|
'categories' => [factory(Category::class, ['name' => 'bar'])], |
92
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
93
|
|
|
$this->assertNull($attibutes['categories'][0]->getId()); |
94
|
|
|
|
95
|
|
|
return (new Instantiator())($attibutes, $class); |
96
|
|
|
})->create(); |
97
|
|
|
|
98
|
|
|
$this->assertCount(1, $product->getCategories()); |
|
|
|
|
99
|
|
|
$this->assertNotNull($product->getCategories()->first()->getId()); |
100
|
|
|
$this->assertSame('bar', $product->getCategories()->first()->getName()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @test |
105
|
|
|
*/ |
106
|
|
|
public function one_to_one_relationship(): void |
107
|
|
|
{ |
108
|
|
|
$variant = factory(Variant::class, [ |
109
|
|
|
'name' => 'foo', |
110
|
|
|
'image' => factory(Image::class, ['path' => '/path/to/file.extension']), |
111
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
112
|
|
|
$this->assertNull($attibutes['image']->getId()); |
113
|
|
|
|
114
|
|
|
return (new Instantiator())($attibutes, $class); |
115
|
|
|
})->create(); |
116
|
|
|
|
117
|
|
|
$this->assertNotNull($variant->getImage()->getId()); |
|
|
|
|
118
|
|
|
$this->assertSame('/path/to/file.extension', $variant->getImage()->getPath()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @test |
123
|
|
|
*/ |
124
|
|
|
public function one_to_one_reverse_relationship(): void |
125
|
|
|
{ |
126
|
|
|
$product = factory(Product::class, [ |
127
|
|
|
'name' => 'foo', |
128
|
|
|
'review' => factory(Review::class, ['rank' => 4]), |
129
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
130
|
|
|
$this->assertNull($attibutes['review']->getId()); |
131
|
|
|
|
132
|
|
|
return (new Instantiator())($attibutes, $class); |
133
|
|
|
})->create(); |
134
|
|
|
|
135
|
|
|
$this->assertNotNull($product->getReview()->getId()); |
|
|
|
|
136
|
|
|
$this->assertSame(4, $product->getReview()->getRank()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @test |
141
|
|
|
*/ |
142
|
|
|
public function default_not_created_when_cascading(): void |
143
|
|
|
{ |
144
|
|
|
$product = CascadeProductFactory::createOne([ |
145
|
|
|
'variants' => CascadeVariantFactory::new()->many(5), |
146
|
|
|
]); |
147
|
|
|
|
148
|
|
|
CascadeVariantFactory::assert()->count(5); |
149
|
|
|
|
150
|
|
|
foreach (CascadeVariantFactory::all() as $variant) { |
151
|
|
|
$this->assertSame($variant->getProduct()->getId(), $product->getId()); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
CascadeProductFactory::assert()->count(1); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @test |
159
|
|
|
*/ |
160
|
|
|
public function default_not_created_when_cascading_2(): void |
161
|
|
|
{ |
162
|
|
|
$product = CascadeProductFactory::createOne([ |
163
|
|
|
'comments' => CascadeProductCommentFactory::new()->many(5), |
164
|
|
|
]); |
165
|
|
|
|
166
|
|
|
CascadeProductCommentFactory::assert()->count(5); |
167
|
|
|
|
168
|
|
|
foreach (CascadeProductCommentFactory::all() as $comment) { |
169
|
|
|
$this->assertSame($comment->getProduct()->getId(), $product->getId()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
CascadeProductFactory::assert()->count(1); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|