Passed
Push — master ( 5f39d8...1dccda )
by Kevin
02:49
created

FactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 53
c 1
b 0
f 1
dl 0
loc 118
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A many_to_many_reverse_relationship() 0 20 1
A can_create_embeddable() 0 7 1
A one_to_many_relationship() 0 20 1
A many_to_one_relationship() 0 9 1
A creating_with_factory_attribute_persists_the_factory() 0 9 1
A many_to_many_relationship() 0 21 1
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
    /**
24
     * @test
25
     */
26
    public function many_to_one_relationship(): void
27
    {
28
        $categoryFactory = factory(Category::class, ['name' => 'foo']);
29
        $category = create(Category::class, ['name' => 'bar']);
30
        $postA = create(Post::class, ['title' => 'title', 'body' => 'body', 'category' => $categoryFactory]);
31
        $postB = create(Post::class, ['title' => 'title', 'body' => 'body', 'category' => $category]);
32
33
        $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

33
        $this->assertSame('foo', $postA->/** @scrutinizer ignore-call */ getCategory()->getName());
Loading history...
34
        $this->assertSame('bar', $postB->getCategory()->getName());
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function one_to_many_relationship(): void
41
    {
42
        $category = create(Category::class, [
43
            'name' => 'bar',
44
            'posts' => [
45
                factory(Post::class, ['title' => 'Post A', 'body' => 'body']),
46
                create(Post::class, ['title' => 'Post B', 'body' => 'body']),
47
            ],
48
        ]);
49
50
        $posts = \array_map(
51
            static function($post) {
52
                return $post->getTitle();
53
            },
54
            $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

54
            $category->/** @scrutinizer ignore-call */ 
55
                       getPosts()->toArray()
Loading history...
55
        );
56
57
        $this->assertCount(2, $posts);
58
        $this->assertContains('Post A', $posts);
59
        $this->assertContains('Post B', $posts);
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function many_to_many_relationship(): void
66
    {
67
        $post = create(Post::class, [
68
            'title' => 'title',
69
            'body' => 'body',
70
            'tags' => [
71
                factory(Tag::class, ['name' => 'Tag A']),
72
                create(Tag::class, ['name' => 'Tag B']),
73
            ],
74
        ]);
75
76
        $tags = \array_map(
77
            static function($tag) {
78
                return $tag->getName();
79
            },
80
            $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

80
            $post->/** @scrutinizer ignore-call */ 
81
                   getTags()->toArray()
Loading history...
81
        );
82
83
        $this->assertCount(2, $tags);
84
        $this->assertContains('Tag A', $tags);
85
        $this->assertContains('Tag B', $tags);
86
    }
87
88
    /**
89
     * @test
90
     */
91
    public function many_to_many_reverse_relationship(): void
92
    {
93
        $tag = create(Tag::class, [
94
            'name' => 'bar',
95
            'posts' => [
96
                factory(Post::class, ['title' => 'Post A', 'body' => 'body']),
97
                create(Post::class, ['title' => 'Post B', 'body' => 'body']),
98
            ],
99
        ]);
100
101
        $posts = \array_map(
102
            static function($post) {
103
                return $post->getTitle();
104
            },
105
            $tag->getPosts()->toArray()
106
        );
107
108
        $this->assertCount(2, $posts);
109
        $this->assertContains('Post A', $posts);
110
        $this->assertContains('Post B', $posts);
111
    }
112
113
    /**
114
     * @test
115
     */
116
    public function creating_with_factory_attribute_persists_the_factory(): void
117
    {
118
        $object = (new AnonymousFactory(Post::class))->create([
119
            'title' => 'title',
120
            'body' => 'body',
121
            'category' => new AnonymousFactory(Category::class, ['name' => 'name']),
122
        ]);
123
124
        $this->assertNotNull($object->getCategory()->getId());
125
    }
126
127
    /**
128
     * @test
129
     */
130
    public function can_create_embeddable(): void
131
    {
132
        $object1 = (new AnonymousFactory(Address::class))->create();
133
        $object2 = (new AnonymousFactory(Address::class))->create(['value' => 'an address']);
134
135
        $this->assertNull($object1->getValue());
0 ignored issues
show
Bug introduced by
The method getValue() 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

135
        $this->assertNull($object1->/** @scrutinizer ignore-call */ getValue());
Loading history...
136
        $this->assertSame('an address', $object2->getValue());
137
    }
138
}
139