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