Passed
Pull Request — master (#211)
by Kevin
03:26
created

default_not_created_when_cascading()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 2
nc 2
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\CascadeProductFactory;
17
use Zenstruck\Foundry\Tests\Fixtures\Factories\CascadeVariantFactory;
18
use function Zenstruck\Foundry\factory;
19
20
/**
21
 * @author Kevin Bond <[email protected]>
22
 */
23
final class FactoryDoctrineCascadeTest extends KernelTestCase
24
{
25
    use Factories, ResetDatabase;
26
27
    /**
28
     * @test
29
     */
30
    public function many_to_one_relationship(): void
31
    {
32
        $product = factory(Product::class, [
33
            'name' => 'foo',
34
            'brand' => factory(Brand::class, ['name' => 'bar']),
35
        ])->instantiateWith(function(array $attibutes, string $class): object {
36
            $this->assertNull($attibutes['brand']->getId());
37
38
            return (new Instantiator())($attibutes, $class);
39
        })->create();
40
41
        $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

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

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

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

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

116
        $this->assertNotNull($variant->/** @scrutinizer ignore-call */ getImage()->getId());
Loading history...
117
        $this->assertSame('/path/to/file.extension', $variant->getImage()->getPath());
118
    }
119
120
    /**
121
     * @test
122
     */
123
    public function one_to_one_reverse_relationship(): void
124
    {
125
        $product = factory(Product::class, [
126
            'name' => 'foo',
127
            'review' => factory(Review::class, ['rank' => 4]),
128
        ])->instantiateWith(function(array $attibutes, string $class): object {
129
            $this->assertNull($attibutes['review']->getId());
130
131
            return (new Instantiator())($attibutes, $class);
132
        })->create();
133
134
        $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

134
        $this->assertNotNull($product->/** @scrutinizer ignore-call */ getReview()->getId());
Loading history...
135
        $this->assertSame(4, $product->getReview()->getRank());
136
    }
137
138
    /**
139
     * @test
140
     */
141
    public function default_not_created_when_cascading(): void
142
    {
143
        $product = CascadeProductFactory::createOne([
144
            'variants' => CascadeVariantFactory::new()->many(5),
145
        ]);
146
147
        CascadeVariantFactory::assert()->count(5);
148
149
        foreach (CascadeVariantFactory::all() as $variant) {
150
            $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

150
            $this->assertSame($variant->getProduct()->getId(), $product->/** @scrutinizer ignore-call */ getId());
Loading history...
151
        }
152
153
        CascadeProductFactory::assert()->count(1);
154
    }
155
}
156