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
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post; |
11
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Tag; |
12
|
|
|
use function Zenstruck\Foundry\create; |
13
|
|
|
use function Zenstruck\Foundry\factory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Kevin Bond <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class FactoryTest extends KernelTestCase |
19
|
|
|
{ |
20
|
|
|
use Factories, ResetDatabase; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @test |
24
|
|
|
*/ |
25
|
|
|
public function many_to_one_relationship(): void |
26
|
|
|
{ |
27
|
|
|
$categoryFactory = factory(Category::class, ['name' => 'foo']); |
28
|
|
|
$category = create(Category::class, ['name' => 'bar']); |
29
|
|
|
$postA = create(Post::class, ['title' => 'title', 'body' => 'body', 'category' => $categoryFactory]); |
30
|
|
|
$postB = create(Post::class, ['title' => 'title', 'body' => 'body', 'category' => $category]); |
31
|
|
|
|
32
|
|
|
$this->assertSame('foo', $postA->getCategory()->getName()); |
|
|
|
|
33
|
|
|
$this->assertSame('bar', $postB->getCategory()->getName()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @test |
38
|
|
|
*/ |
39
|
|
|
public function one_to_many_relationship(): void |
40
|
|
|
{ |
41
|
|
|
$category = create(Category::class, [ |
42
|
|
|
'name' => 'bar', |
43
|
|
|
'posts' => [ |
44
|
|
|
factory(Post::class, ['title' => 'Post A', 'body' => 'body']), |
45
|
|
|
create(Post::class, ['title' => 'Post B', 'body' => 'body']), |
46
|
|
|
], |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
$posts = \array_map( |
50
|
|
|
static function($post) { |
51
|
|
|
return $post->getTitle(); |
52
|
|
|
}, |
53
|
|
|
$category->getPosts()->toArray() |
|
|
|
|
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$this->assertCount(2, $posts); |
57
|
|
|
$this->assertContains('Post A', $posts); |
58
|
|
|
$this->assertContains('Post B', $posts); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @test |
63
|
|
|
*/ |
64
|
|
|
public function many_to_many_relationship(): void |
65
|
|
|
{ |
66
|
|
|
$post = create(Post::class, [ |
67
|
|
|
'title' => 'title', |
68
|
|
|
'body' => 'body', |
69
|
|
|
'tags' => [ |
70
|
|
|
factory(Tag::class, ['name' => 'Tag A']), |
71
|
|
|
create(Tag::class, ['name' => 'Tag B']), |
72
|
|
|
], |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$tags = \array_map( |
76
|
|
|
static function($tag) { |
77
|
|
|
return $tag->getName(); |
78
|
|
|
}, |
79
|
|
|
$post->getTags()->toArray() |
|
|
|
|
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$this->assertCount(2, $tags); |
83
|
|
|
$this->assertContains('Tag A', $tags); |
84
|
|
|
$this->assertContains('Tag B', $tags); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @test |
89
|
|
|
*/ |
90
|
|
|
public function many_to_many_reverse_relationship(): void |
91
|
|
|
{ |
92
|
|
|
$tag = create(Tag::class, [ |
93
|
|
|
'name' => 'bar', |
94
|
|
|
'posts' => [ |
95
|
|
|
factory(Post::class, ['title' => 'Post A', 'body' => 'body']), |
96
|
|
|
create(Post::class, ['title' => 'Post B', 'body' => 'body']), |
97
|
|
|
], |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
$posts = \array_map( |
101
|
|
|
static function($post) { |
102
|
|
|
return $post->getTitle(); |
103
|
|
|
}, |
104
|
|
|
$tag->getPosts()->toArray() |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$this->assertCount(2, $posts); |
108
|
|
|
$this->assertContains('Post A', $posts); |
109
|
|
|
$this->assertContains('Post B', $posts); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @test |
114
|
|
|
*/ |
115
|
|
|
public function creating_with_factory_attribute_persists_the_factory(): void |
116
|
|
|
{ |
117
|
|
|
$object = (new AnonymousFactory(Post::class))->create([ |
118
|
|
|
'title' => 'title', |
119
|
|
|
'body' => 'body', |
120
|
|
|
'category' => new AnonymousFactory(Category::class, ['name' => 'name']), |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
$this->assertNotNull($object->getCategory()->getId()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @test |
128
|
|
|
*/ |
129
|
|
|
public function can_delay_flush(): void |
130
|
|
|
{ |
131
|
|
|
AnonymousFactory::new(Post::class)->assert()->empty(); |
132
|
|
|
AnonymousFactory::new(Category::class)->assert()->empty(); |
133
|
|
|
|
134
|
|
|
AnonymousFactory::delayFlush(function() { |
135
|
|
|
AnonymousFactory::new(Post::class)->create([ |
136
|
|
|
'title' => 'title', |
137
|
|
|
'body' => 'body', |
138
|
|
|
'category' => AnonymousFactory::new(Category::class, ['name' => 'name']), |
139
|
|
|
]); |
140
|
|
|
|
141
|
|
|
AnonymousFactory::new(Post::class)->assert()->empty(); |
142
|
|
|
AnonymousFactory::new(Category::class)->assert()->empty(); |
143
|
|
|
}); |
144
|
|
|
|
145
|
|
|
AnonymousFactory::new(Post::class)->assert()->count(1); |
146
|
|
|
AnonymousFactory::new(Category::class)->assert()->count(1); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @test |
151
|
|
|
*/ |
152
|
|
|
public function auto_refresh_is_disabled_during_delay_flush(): void |
153
|
|
|
{ |
154
|
|
|
AnonymousFactory::new(Post::class)->assert()->empty(); |
155
|
|
|
AnonymousFactory::new(Category::class)->assert()->empty(); |
156
|
|
|
|
157
|
|
|
AnonymousFactory::delayFlush(function() { |
158
|
|
|
$post = AnonymousFactory::new(Post::class)->create([ |
159
|
|
|
'title' => 'title', |
160
|
|
|
'body' => 'body', |
161
|
|
|
'category' => AnonymousFactory::new(Category::class, ['name' => 'name']), |
162
|
|
|
])->enableAutoRefresh(); |
163
|
|
|
|
164
|
|
|
$post->setTitle('new title'); |
|
|
|
|
165
|
|
|
$post->setBody('new body'); |
|
|
|
|
166
|
|
|
|
167
|
|
|
AnonymousFactory::new(Post::class)->assert()->empty(); |
168
|
|
|
AnonymousFactory::new(Category::class)->assert()->empty(); |
169
|
|
|
}); |
170
|
|
|
|
171
|
|
|
AnonymousFactory::new(Post::class)->assert()->count(1); |
172
|
|
|
AnonymousFactory::new(Category::class)->assert()->count(1); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|