Passed
Pull Request — master (#1)
by Kevin
02:38
created

FactoryTest::many_to_many_relationship()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 13
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 21
rs 9.8333
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());
0 ignored issues
show
Bug introduced by
The method getCategory() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $this->assertSame('foo', $postA->/** @scrutinizer ignore-call */ getCategory()->getName());
Loading history...
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()
0 ignored issues
show
Bug introduced by
The method getPosts() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
            $category->/** @scrutinizer ignore-call */ 
50
                       getPosts()->toArray()
Loading history...
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()
0 ignored issues
show
Bug introduced by
The method getTags() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            $post->/** @scrutinizer ignore-call */ 
76
                   getTags()->toArray()
Loading history...
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