|
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 function Zenstruck\Foundry\factory; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Kevin Bond <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
final class FactoryDoctrineCascadeTest extends KernelTestCase |
|
22
|
|
|
{ |
|
23
|
|
|
use Factories, ResetDatabase; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @test |
|
27
|
|
|
*/ |
|
28
|
|
|
public function many_to_one_relationship(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$product = factory(Product::class, [ |
|
31
|
|
|
'name' => 'foo', |
|
32
|
|
|
'brand' => factory(Brand::class, ['name' => 'bar']), |
|
33
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
|
34
|
|
|
$this->assertNull($attibutes['brand']->getId()); |
|
35
|
|
|
|
|
36
|
|
|
return (new Instantiator())($attibutes, $class); |
|
37
|
|
|
})->create(); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertNotNull($product->getBrand()->getId()); |
|
|
|
|
|
|
40
|
|
|
$this->assertSame('bar', $product->getBrand()->getName()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @test |
|
45
|
|
|
*/ |
|
46
|
|
|
public function one_to_many_relationship(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$product = factory(Product::class, [ |
|
49
|
|
|
'name' => 'foo', |
|
50
|
|
|
'variants' => [factory(Variant::class, ['name' => 'bar'])], |
|
51
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
|
52
|
|
|
$this->assertNull($attibutes['variants'][0]->getId()); |
|
53
|
|
|
|
|
54
|
|
|
return (new Instantiator())($attibutes, $class); |
|
55
|
|
|
})->create(); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertCount(1, $product->getVariants()); |
|
|
|
|
|
|
58
|
|
|
$this->assertNotNull($product->getVariants()->first()->getId()); |
|
59
|
|
|
$this->assertSame('bar', $product->getVariants()->first()->getName()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @test |
|
64
|
|
|
*/ |
|
65
|
|
|
public function many_to_many_relationship(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$product = factory(Product::class, [ |
|
68
|
|
|
'name' => 'foo', |
|
69
|
|
|
'tags' => [factory(Tag::class, ['name' => 'bar'])], |
|
70
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
|
71
|
|
|
$this->assertNull($attibutes['tags'][0]->getId()); |
|
72
|
|
|
|
|
73
|
|
|
return (new Instantiator())($attibutes, $class); |
|
74
|
|
|
})->create(); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertCount(1, $product->getTags()); |
|
|
|
|
|
|
77
|
|
|
$this->assertNotNull($product->getTags()->first()->getId()); |
|
78
|
|
|
$this->assertSame('bar', $product->getTags()->first()->getName()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @test |
|
83
|
|
|
*/ |
|
84
|
|
|
public function many_to_many_reverse_relationship(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$product = factory(Product::class, [ |
|
87
|
|
|
'name' => 'foo', |
|
88
|
|
|
'categories' => [factory(Category::class, ['name' => 'bar'])], |
|
89
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
|
90
|
|
|
$this->assertNull($attibutes['categories'][0]->getId()); |
|
91
|
|
|
|
|
92
|
|
|
return (new Instantiator())($attibutes, $class); |
|
93
|
|
|
})->create(); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertCount(1, $product->getCategories()); |
|
|
|
|
|
|
96
|
|
|
$this->assertNotNull($product->getCategories()->first()->getId()); |
|
97
|
|
|
$this->assertSame('bar', $product->getCategories()->first()->getName()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @test |
|
102
|
|
|
*/ |
|
103
|
|
|
public function one_to_one_relationship(): void |
|
104
|
|
|
{ |
|
105
|
|
|
$variant = factory(Variant::class, [ |
|
106
|
|
|
'name' => 'foo', |
|
107
|
|
|
'image' => factory(Image::class, ['path' => '/path/to/file.extension']), |
|
108
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
|
109
|
|
|
$this->assertNull($attibutes['image']->getId()); |
|
110
|
|
|
|
|
111
|
|
|
return (new Instantiator())($attibutes, $class); |
|
112
|
|
|
})->create(); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertNotNull($variant->getImage()->getId()); |
|
|
|
|
|
|
115
|
|
|
$this->assertSame('/path/to/file.extension', $variant->getImage()->getPath()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @test |
|
120
|
|
|
*/ |
|
121
|
|
|
public function one_to_one_reverse_relationship(): void |
|
122
|
|
|
{ |
|
123
|
|
|
$product = factory(Product::class, [ |
|
124
|
|
|
'name' => 'foo', |
|
125
|
|
|
'review' => factory(Review::class, ['rank' => 4]), |
|
126
|
|
|
])->instantiateWith(function(array $attibutes, string $class): object { |
|
127
|
|
|
$this->assertNull($attibutes['review']->getId()); |
|
128
|
|
|
|
|
129
|
|
|
return (new Instantiator())($attibutes, $class); |
|
130
|
|
|
})->create(); |
|
131
|
|
|
|
|
132
|
|
|
$this->assertNotNull($product->getReview()->getId()); |
|
|
|
|
|
|
133
|
|
|
$this->assertSame(4, $product->getReview()->getRank()); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|