Passed
Pull Request — master (#153)
by Kevin
03:37
created

ORMModelFactoryTest::categoryClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Functional;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category;
7
use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory;
8
use Zenstruck\Foundry\Tests\Fixtures\Factories\CommentFactory;
9
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory;
10
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithInvalidInitialize;
11
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithNullInitialize;
12
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithValidInitialize;
13
use Zenstruck\Foundry\Tests\Fixtures\Factories\TagFactory;
14
use Zenstruck\Foundry\Tests\Fixtures\Factories\UserFactory;
15
16
/**
17
 * @author Kevin Bond <[email protected]>
18
 */
19
final class ORMModelFactoryTest extends ModelFactoryTest
20
{
21
    protected function setUp(): void
22
    {
23
        if (false === \getenv('DATABASE_URL')) {
24
            self::markTestSkipped('doctrine/orm not enabled.');
25
        }
26
    }
27
28
    /**
29
     * @test
30
     */
31
    public function can_override_initialize(): void
32
    {
33
        $this->assertFalse(PostFactory::createOne()->isPublished());
0 ignored issues
show
Bug introduced by
The method isPublished() 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->assertFalse(PostFactory::createOne()->/** @scrutinizer ignore-call */ isPublished());
Loading history...
34
        $this->assertTrue(PostFactoryWithValidInitialize::createOne()->isPublished());
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function initialize_must_return_an_instance_of_the_current_factory(): void
41
    {
42
        $this->expectException(\TypeError::class);
43
        $this->expectExceptionMessage(\sprintf('"%1$s::initialize()" must return an instance of "%1$s".', PostFactoryWithInvalidInitialize::class));
44
45
        PostFactoryWithInvalidInitialize::new();
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function initialize_must_return_a_value(): void
52
    {
53
        $this->expectException(\TypeError::class);
54
        $this->expectExceptionMessage(\sprintf('"%1$s::initialize()" must return an instance of "%1$s".', PostFactoryWithNullInitialize::class));
55
56
        PostFactoryWithNullInitialize::new();
57
    }
58
59
    /**
60
     * @test
61
     */
62
    public function one_to_many_with_nested_collection_relationship(): void
63
    {
64
        $post = PostFactory::createOne([
65
            'comments' => CommentFactory::new()->many(4),
66
        ]);
67
68
        $this->assertCount(4, $post->getComments());
0 ignored issues
show
Bug introduced by
The method getComments() 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

68
        $this->assertCount(4, $post->/** @scrutinizer ignore-call */ getComments());
Loading history...
69
        UserFactory::assert()->count(4);
70
        CommentFactory::assert()->count(4);
71
        PostFactory::assert()->count(1);
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function create_multiple_one_to_many_with_nested_collection_relationship(): void
78
    {
79
        $user = UserFactory::createOne();
80
        $posts = PostFactory::createMany(2, [
81
            'comments' => CommentFactory::new(['user' => $user])->many(4),
82
        ]);
83
84
        $this->assertCount(4, $posts[0]->getComments());
85
        $this->assertCount(4, $posts[1]->getComments());
86
        UserFactory::assert()->count(1);
87
        CommentFactory::assert()->count(8);
88
        PostFactory::assert()->count(2);
89
    }
90
91
    /**
92
     * @test
93
     */
94
    public function many_to_many_with_nested_collection_relationship(): void
95
    {
96
        $post = PostFactory::createOne([
97
            'tags' => TagFactory::new()->many(3),
98
        ]);
99
100
        $this->assertCount(3, $post->getTags());
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

100
        $this->assertCount(3, $post->/** @scrutinizer ignore-call */ getTags());
Loading history...
101
        TagFactory::assert()->count(5); // 3 created by this test and 2 in global state
102
        PostFactory::assert()->count(1);
103
    }
104
105
    /**
106
     * @test
107
     */
108
    public function inverse_many_to_many_with_nested_collection_relationship(): void
109
    {
110
        $tag = TagFactory::createOne([
111
            'posts' => PostFactory::new()->many(3),
112
        ]);
113
114
        $this->assertCount(3, $tag->getPosts());
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

114
        $this->assertCount(3, $tag->/** @scrutinizer ignore-call */ getPosts());
Loading history...
115
        TagFactory::assert()->count(3); // 1 created by this test and 2 in global state
116
        PostFactory::assert()->count(3);
117
    }
118
119
    /**
120
     * @test
121
     */
122
    public function create_multiple_many_to_many_with_nested_collection_relationship(): void
123
    {
124
        $posts = PostFactory::createMany(2, [
125
            'tags' => TagFactory::new()->many(3),
126
        ]);
127
128
        $this->assertCount(3, $posts[0]->getTags());
129
        $this->assertCount(3, $posts[1]->getTags());
130
        TagFactory::assert()->count(8); // 6 created by this test and 2 in global state
131
        PostFactory::assert()->count(2);
132
    }
133
134
    /**
135
     * @test
136
     */
137
    public function unpersisted_one_to_many_with_nested_collection_relationship(): void
138
    {
139
        $post = PostFactory::new()->withoutPersisting()->create([
140
            'comments' => CommentFactory::new()->many(4),
141
        ]);
142
143
        $this->assertCount(4, $post->getComments());
144
        UserFactory::assert()->empty();
145
        CommentFactory::assert()->empty();
146
        PostFactory::assert()->empty();
147
    }
148
149
    /**
150
     * @test
151
     */
152
    public function unpersisted_many_to_many_with_nested_collection_relationship(): void
153
    {
154
        $post = PostFactory::new()->withoutPersisting()->create([
155
            'tags' => TagFactory::new()->many(3),
156
        ]);
157
158
        $this->assertCount(3, $post->getTags());
159
        TagFactory::assert()->count(2); // 2 created in global state
160
        PostFactory::assert()->empty();
161
    }
162
163
    /**
164
     * @test
165
     * @dataProvider dataProvider
166
     */
167
    public function can_use_model_factories_in_a_data_provider(PostFactory $factory, bool $published): void
168
    {
169
        $post = $factory->create();
170
171
        $post->assertPersisted();
172
        $this->assertSame($published, $post->isPublished());
173
    }
174
175
    public static function dataProvider(): array
176
    {
177
        return [
178
            [PostFactory::new(), false],
179
            [PostFactory::new()->published(), true],
0 ignored issues
show
Bug introduced by
The method published() does not exist on Zenstruck\Foundry\ModelFactory. 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

179
            [PostFactory::new()->/** @scrutinizer ignore-call */ published(), true],
Loading history...
180
        ];
181
    }
182
183
    /**
184
     * @test
185
     */
186
    public function many_to_one_unmanaged_entity(): void
187
    {
188
        $category = CategoryFactory::createOne(['name' => 'My Category']);
189
190
        self::$container->get(EntityManagerInterface::class)->clear();
191
192
        $post = PostFactory::createOne(['category' => $category]);
193
194
        $this->assertSame('My Category', $post->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

194
        $this->assertSame('My Category', $post->/** @scrutinizer ignore-call */ getCategory()->getName());
Loading history...
195
    }
196
197
    /**
198
     * @test
199
     */
200
    public function many_to_one_unmanaged_raw_entity(): void
201
    {
202
        $category = CategoryFactory::createOne(['name' => 'My Category'])->object();
203
204
        self::$container->get(EntityManagerInterface::class)->clear();
205
206
        $post = PostFactory::createOne(['category' => $category]);
207
208
        $this->assertSame('My Category', $post->getCategory()->getName());
209
    }
210
211
    protected function categoryClass(): string
212
    {
213
        return Category::class;
214
    }
215
216
    protected function categoryFactoryClass(): string
217
    {
218
        return CategoryFactory::class;
219
    }
220
}
221