Passed
Pull Request — master (#84)
by Kevin
02:40
created

FactoryTest::can_delay_flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
c 1
b 0
f 1
dl 0
loc 18
rs 9.9
cc 1
nc 1
nop 0
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\Category;
10
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post;
11
use Zenstruck\Foundry\Tests\Fixtures\Entity\Tag;
12
use function Zenstruck\Foundry\create;
13
use function Zenstruck\Foundry\factory;
14
15
/**
16
 * @author Kevin Bond <[email protected]>
17
 */
18
final class FactoryTest extends KernelTestCase
19
{
20
    use Factories, ResetDatabase;
21
22
    /**
23
     * @test
24
     */
25
    public function many_to_one_relationship(): void
26
    {
27
        $categoryFactory = factory(Category::class, ['name' => 'foo']);
28
        $category = create(Category::class, ['name' => 'bar']);
29
        $postA = create(Post::class, ['title' => 'title', 'body' => 'body', 'category' => $categoryFactory]);
30
        $postB = create(Post::class, ['title' => 'title', 'body' => 'body', 'category' => $category]);
31
32
        $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

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

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

79
            $post->/** @scrutinizer ignore-call */ 
80
                   getTags()->toArray()
Loading history...
80
        );
81
82
        $this->assertCount(2, $tags);
83
        $this->assertContains('Tag A', $tags);
84
        $this->assertContains('Tag B', $tags);
85
    }
86
87
    /**
88
     * @test
89
     */
90
    public function many_to_many_reverse_relationship(): void
91
    {
92
        $tag = create(Tag::class, [
93
            'name' => 'bar',
94
            'posts' => [
95
                factory(Post::class, ['title' => 'Post A', 'body' => 'body']),
96
                create(Post::class, ['title' => 'Post B', 'body' => 'body']),
97
            ],
98
        ]);
99
100
        $posts = \array_map(
101
            static function($post) {
102
                return $post->getTitle();
103
            },
104
            $tag->getPosts()->toArray()
105
        );
106
107
        $this->assertCount(2, $posts);
108
        $this->assertContains('Post A', $posts);
109
        $this->assertContains('Post B', $posts);
110
    }
111
112
    /**
113
     * @test
114
     */
115
    public function creating_with_factory_attribute_persists_the_factory(): void
116
    {
117
        $object = (new AnonymousFactory(Post::class))->create([
118
            'title' => 'title',
119
            'body' => 'body',
120
            'category' => new AnonymousFactory(Category::class, ['name' => 'name']),
121
        ]);
122
123
        $this->assertNotNull($object->getCategory()->getId());
124
    }
125
126
    /**
127
     * @test
128
     */
129
    public function can_delay_flush(): void
130
    {
131
        AnonymousFactory::new(Post::class)->assert()->empty();
132
        AnonymousFactory::new(Category::class)->assert()->empty();
133
134
        AnonymousFactory::delayFlush(function() {
135
            AnonymousFactory::new(Post::class)->create([
136
                'title' => 'title',
137
                'body' => 'body',
138
                'category' => AnonymousFactory::new(Category::class, ['name' => 'name']),
139
            ]);
140
141
            AnonymousFactory::new(Post::class)->assert()->empty();
142
            AnonymousFactory::new(Category::class)->assert()->empty();
143
        });
144
145
        AnonymousFactory::new(Post::class)->assert()->count(1);
146
        AnonymousFactory::new(Category::class)->assert()->count(1);
147
    }
148
149
    /**
150
     * @test
151
     */
152
    public function auto_refresh_is_disabled_during_delay_flush(): void
153
    {
154
        AnonymousFactory::new(Post::class)->assert()->empty();
155
        AnonymousFactory::new(Category::class)->assert()->empty();
156
157
        AnonymousFactory::delayFlush(function() {
158
            $post = AnonymousFactory::new(Post::class)->create([
159
                'title' => 'title',
160
                'body' => 'body',
161
                'category' => AnonymousFactory::new(Category::class, ['name' => 'name']),
162
            ])->enableAutoRefresh();
163
164
            $post->setTitle('new title');
0 ignored issues
show
Bug introduced by
The method setTitle() 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

164
            $post->/** @scrutinizer ignore-call */ 
165
                   setTitle('new title');
Loading history...
165
            $post->setBody('new body');
0 ignored issues
show
Bug introduced by
The method setBody() 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

165
            $post->/** @scrutinizer ignore-call */ 
166
                   setBody('new body');
Loading history...
166
167
            AnonymousFactory::new(Post::class)->assert()->empty();
168
            AnonymousFactory::new(Category::class)->assert()->empty();
169
        });
170
171
        AnonymousFactory::new(Post::class)->assert()->count(1);
172
        AnonymousFactory::new(Category::class)->assert()->count(1);
173
    }
174
}
175