Passed
Pull Request — master (#181)
by Mathieu
03:28
created

one_to_one_relationship()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

144
        $this->assertNotNull($product->/** @scrutinizer ignore-call */ getReview()->getId());
Loading history...
145
146
        $this->assertSame(4, $product->getReview()->getRank());
147
    }
148
}
149