Passed
Pull Request — master (#211)
by Kevin
02:57
created

one_to_many_relationship()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 14
rs 9.9332
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\Instantiator;
7
use Zenstruck\Foundry\Test\Factories;
8
use Zenstruck\Foundry\Test\ResetDatabase;
9
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Brand;
10
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Category;
11
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Image;
12
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Product;
13
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Review;
14
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Tag;
15
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Variant;
16
use Zenstruck\Foundry\Tests\Fixtures\Factories\CascadeProductCommentFactory;
17
use Zenstruck\Foundry\Tests\Fixtures\Factories\CascadeProductFactory;
18
use Zenstruck\Foundry\Tests\Fixtures\Factories\CascadeVariantFactory;
19
use function Zenstruck\Foundry\factory;
20
21
/**
22
 * @author Kevin Bond <[email protected]>
23
 */
24
final class FactoryDoctrineCascadeTest extends KernelTestCase
25
{
26
    use Factories, ResetDatabase;
27
28
    /**
29
     * @test
30
     */
31
    public function many_to_one_relationship(): void
32
    {
33
        $product = factory(Product::class, [
34
            'name' => 'foo',
35
            'brand' => factory(Brand::class, ['name' => 'bar']),
36
        ])->instantiateWith(function(array $attibutes, string $class): object {
37
            $this->assertNull($attibutes['brand']->getId());
38
39
            return (new Instantiator())($attibutes, $class);
40
        })->create();
41
42
        $this->assertNotNull($product->getBrand()->getId());
0 ignored issues
show
Bug introduced by
The method getBrand() 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

42
        $this->assertNotNull($product->/** @scrutinizer ignore-call */ getBrand()->getId());
Loading history...
43
        $this->assertSame('bar', $product->getBrand()->getName());
44
    }
45
46
    /**
47
     * @test
48
     */
49
    public function one_to_many_relationship(): void
50
    {
51
        $product = factory(Product::class, [
52
            'name' => 'foo',
53
            'variants' => [factory(Variant::class, ['name' => 'bar'])],
54
        ])->instantiateWith(function(array $attibutes, string $class): object {
55
            $this->assertNull($attibutes['variants'][0]->getId());
56
57
            return (new Instantiator())($attibutes, $class);
58
        })->create();
59
60
        $this->assertCount(1, $product->getVariants());
0 ignored issues
show
Bug introduced by
The method getVariants() 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

60
        $this->assertCount(1, $product->/** @scrutinizer ignore-call */ getVariants());
Loading history...
61
        $this->assertNotNull($product->getVariants()->first()->getId());
62
        $this->assertSame('bar', $product->getVariants()->first()->getName());
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function many_to_many_relationship(): void
69
    {
70
        $product = factory(Product::class, [
71
            'name' => 'foo',
72
            'tags' => [factory(Tag::class, ['name' => 'bar'])],
73
        ])->instantiateWith(function(array $attibutes, string $class): object {
74
            $this->assertNull($attibutes['tags'][0]->getId());
75
76
            return (new Instantiator())($attibutes, $class);
77
        })->create();
78
79
        $this->assertCount(1, $product->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

79
        $this->assertCount(1, $product->/** @scrutinizer ignore-call */ getTags());
Loading history...
80
        $this->assertNotNull($product->getTags()->first()->getId());
81
        $this->assertSame('bar', $product->getTags()->first()->getName());
82
    }
83
84
    /**
85
     * @test
86
     */
87
    public function many_to_many_reverse_relationship(): void
88
    {
89
        $product = factory(Product::class, [
90
            'name' => 'foo',
91
            'categories' => [factory(Category::class, ['name' => 'bar'])],
92
        ])->instantiateWith(function(array $attibutes, string $class): object {
93
            $this->assertNull($attibutes['categories'][0]->getId());
94
95
            return (new Instantiator())($attibutes, $class);
96
        })->create();
97
98
        $this->assertCount(1, $product->getCategories());
0 ignored issues
show
Bug introduced by
The method getCategories() 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

98
        $this->assertCount(1, $product->/** @scrutinizer ignore-call */ getCategories());
Loading history...
99
        $this->assertNotNull($product->getCategories()->first()->getId());
100
        $this->assertSame('bar', $product->getCategories()->first()->getName());
101
    }
102
103
    /**
104
     * @test
105
     */
106
    public function one_to_one_relationship(): void
107
    {
108
        $variant = factory(Variant::class, [
109
            'name' => 'foo',
110
            'image' => factory(Image::class, ['path' => '/path/to/file.extension']),
111
        ])->instantiateWith(function(array $attibutes, string $class): object {
112
            $this->assertNull($attibutes['image']->getId());
113
114
            return (new Instantiator())($attibutes, $class);
115
        })->create();
116
117
        $this->assertNotNull($variant->getImage()->getId());
0 ignored issues
show
Bug introduced by
The method getImage() 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

117
        $this->assertNotNull($variant->/** @scrutinizer ignore-call */ getImage()->getId());
Loading history...
118
        $this->assertSame('/path/to/file.extension', $variant->getImage()->getPath());
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function one_to_one_reverse_relationship(): void
125
    {
126
        $product = factory(Product::class, [
127
            'name' => 'foo',
128
            'review' => factory(Review::class, ['rank' => 4]),
129
        ])->instantiateWith(function(array $attibutes, string $class): object {
130
            $this->assertNull($attibutes['review']->getId());
131
132
            return (new Instantiator())($attibutes, $class);
133
        })->create();
134
135
        $this->assertNotNull($product->getReview()->getId());
0 ignored issues
show
Bug introduced by
The method getReview() 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->assertNotNull($product->/** @scrutinizer ignore-call */ getReview()->getId());
Loading history...
136
        $this->assertSame(4, $product->getReview()->getRank());
137
    }
138
139
    /**
140
     * @test
141
     */
142
    public function default_not_created_when_cascading(): void
143
    {
144
        $product = CascadeProductFactory::createOne([
145
            'variants' => CascadeVariantFactory::new()->many(5),
146
        ]);
147
148
        CascadeVariantFactory::assert()->count(5);
149
150
        foreach (CascadeVariantFactory::all() as $variant) {
151
            $this->assertSame($variant->getProduct()->getId(), $product->getId());
0 ignored issues
show
Bug introduced by
The method getId() 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

151
            $this->assertSame($variant->getProduct()->getId(), $product->/** @scrutinizer ignore-call */ getId());
Loading history...
152
        }
153
154
        CascadeProductFactory::assert()->count(1);
155
    }
156
157
    /**
158
     * @test
159
     */
160
    public function default_not_created_when_cascading_2(): void
161
    {
162
        $product = CascadeProductFactory::createOne([
163
            'comments' => CascadeProductCommentFactory::new()->many(5),
164
        ]);
165
166
        CascadeProductCommentFactory::assert()->count(5);
167
168
        foreach (CascadeProductCommentFactory::all() as $comment) {
169
            $this->assertSame($comment->getProduct()->getId(), $product->getId());
170
        }
171
172
        CascadeProductFactory::assert()->count(1);
173
    }
174
}
175