1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use Zenstruck\Foundry\Factory; |
6
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category; |
7
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post; |
8
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Tag; |
9
|
|
|
use Zenstruck\Foundry\Tests\FunctionalTestCase; |
10
|
|
|
use function Zenstruck\Foundry\create; |
11
|
|
|
use function Zenstruck\Foundry\factory; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Kevin Bond <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
final class FactoryTest extends FunctionalTestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @test |
20
|
|
|
*/ |
21
|
|
|
public function many_to_one_relationship(): void |
22
|
|
|
{ |
23
|
|
|
$categoryFactory = factory(Category::class, ['name' => 'foo']); |
24
|
|
|
$category = create(Category::class, ['name' => 'bar']); |
25
|
|
|
$postA = create(Post::class, ['title' => 'title', 'body' => 'body', 'category' => $categoryFactory]); |
26
|
|
|
$postB = create(Post::class, ['title' => 'title', 'body' => 'body', 'category' => $category]); |
27
|
|
|
|
28
|
|
|
$this->assertSame('foo', $postA->getCategory()->getName()); |
|
|
|
|
29
|
|
|
$this->assertSame('bar', $postB->getCategory()->getName()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @test |
34
|
|
|
*/ |
35
|
|
|
public function one_to_many_relationship(): void |
36
|
|
|
{ |
37
|
|
|
$category = create(Category::class, [ |
38
|
|
|
'name' => 'bar', |
39
|
|
|
'posts' => [ |
40
|
|
|
factory(Post::class, ['title' => 'Post A', 'body' => 'body']), |
41
|
|
|
create(Post::class, ['title' => 'Post B', 'body' => 'body']), |
42
|
|
|
], |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
$posts = \array_map( |
46
|
|
|
static function($post) { |
47
|
|
|
return $post->getTitle(); |
48
|
|
|
}, |
49
|
|
|
$category->getPosts()->toArray() |
|
|
|
|
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$this->assertCount(2, $posts); |
53
|
|
|
$this->assertContains('Post A', $posts); |
54
|
|
|
$this->assertContains('Post B', $posts); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @test |
59
|
|
|
*/ |
60
|
|
|
public function many_to_many_relationship(): void |
61
|
|
|
{ |
62
|
|
|
$post = create(Post::class, [ |
63
|
|
|
'title' => 'title', |
64
|
|
|
'body' => 'body', |
65
|
|
|
'tags' => [ |
66
|
|
|
factory(Tag::class, ['name' => 'Tag A']), |
67
|
|
|
create(Tag::class, ['name' => 'Tag B']), |
68
|
|
|
], |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
$tags = \array_map( |
72
|
|
|
static function($tag) { |
73
|
|
|
return $tag->getName(); |
74
|
|
|
}, |
75
|
|
|
$post->getTags()->toArray() |
|
|
|
|
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$this->assertCount(2, $tags); |
79
|
|
|
$this->assertContains('Tag A', $tags); |
80
|
|
|
$this->assertContains('Tag B', $tags); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
*/ |
86
|
|
|
public function many_to_many_reverse_relationship(): void |
87
|
|
|
{ |
88
|
|
|
$tag = create(Tag::class, [ |
89
|
|
|
'name' => 'bar', |
90
|
|
|
'posts' => [ |
91
|
|
|
factory(Post::class, ['title' => 'Post A', 'body' => 'body']), |
92
|
|
|
create(Post::class, ['title' => 'Post B', 'body' => 'body']), |
93
|
|
|
], |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
$posts = \array_map( |
97
|
|
|
static function($post) { |
98
|
|
|
return $post->getTitle(); |
99
|
|
|
}, |
100
|
|
|
$tag->getPosts()->toArray() |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$this->assertCount(2, $posts); |
104
|
|
|
$this->assertContains('Post A', $posts); |
105
|
|
|
$this->assertContains('Post B', $posts); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @test |
110
|
|
|
*/ |
111
|
|
|
public function creating_with_factory_attribute_persists_the_factory(): void |
112
|
|
|
{ |
113
|
|
|
$object = (new Factory(Post::class))->create([ |
114
|
|
|
'title' => 'title', |
115
|
|
|
'body' => 'body', |
116
|
|
|
'category' => new Factory(Category::class, ['name' => 'name']), |
117
|
|
|
]); |
118
|
|
|
|
119
|
|
|
$this->assertNotNull($object->getCategory()->getId()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|