|
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(fn($post) => $post->getTitle(), $category->getPosts()->toArray()); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$this->assertCount(2, $posts); |
|
48
|
|
|
$this->assertContains('Post A', $posts); |
|
49
|
|
|
$this->assertContains('Post B', $posts); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @test |
|
54
|
|
|
*/ |
|
55
|
|
|
public function many_to_many_relationship(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$post = create(Post::class, [ |
|
58
|
|
|
'title' => 'title', |
|
59
|
|
|
'body' => 'body', |
|
60
|
|
|
'tags' => [ |
|
61
|
|
|
factory(Tag::class, ['name' => 'Tag A']), |
|
62
|
|
|
create(Tag::class, ['name' => 'Tag B']), |
|
63
|
|
|
], |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
$tags = \array_map(fn($tag) => $tag->getName(), $post->getTags()->toArray()); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
$this->assertCount(2, $tags); |
|
69
|
|
|
$this->assertContains('Tag A', $tags); |
|
70
|
|
|
$this->assertContains('Tag B', $tags); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @test |
|
75
|
|
|
*/ |
|
76
|
|
|
public function many_to_many_reverse_relationship(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$tag = create(Tag::class, [ |
|
79
|
|
|
'name' => 'bar', |
|
80
|
|
|
'posts' => [ |
|
81
|
|
|
factory(Post::class, ['title' => 'Post A', 'body' => 'body']), |
|
82
|
|
|
create(Post::class, ['title' => 'Post B', 'body' => 'body']), |
|
83
|
|
|
], |
|
84
|
|
|
]); |
|
85
|
|
|
|
|
86
|
|
|
$posts = \array_map(fn($post) => $post->getTitle(), $tag->getPosts()->toArray()); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertCount(2, $posts); |
|
89
|
|
|
$this->assertContains('Post A', $posts); |
|
90
|
|
|
$this->assertContains('Post B', $posts); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @test |
|
95
|
|
|
*/ |
|
96
|
|
|
public function creating_with_factory_attribute_persists_the_factory(): void |
|
97
|
|
|
{ |
|
98
|
|
|
$object = (new Factory(Post::class))->create([ |
|
99
|
|
|
'title' => 'title', |
|
100
|
|
|
'body' => 'body', |
|
101
|
|
|
'category' => new Factory(Category::class, ['name' => 'name']), |
|
102
|
|
|
]); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertNotNull($object->getCategory()->getId()); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|