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