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

FactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 39
c 1
b 0
f 1
dl 0
loc 89
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A many_to_many_reverse_relationship() 0 15 1
A one_to_many_relationship() 0 15 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 16 1
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(fn($post) => $post->getTitle(), $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

45
        $posts = \array_map(fn($post) => $post->getTitle(), $category->/** @scrutinizer ignore-call */ getPosts()->toArray());
Loading history...
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());
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

66
        $tags = \array_map(fn($tag) => $tag->getName(), $post->/** @scrutinizer ignore-call */ getTags()->toArray());
Loading history...
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