Passed
Pull Request — master (#181)
by Mathieu
02:31
created

FactoryDoctrineCascadeTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 40
c 2
b 0
f 0
dl 0
loc 95
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A many_to_many_reverse_relationship() 0 11 1
A one_to_one_relationship() 0 10 1
A many_to_many_relationship() 0 11 1
A many_to_one_relationship() 0 10 1
A one_to_one_reverse_relationship() 0 10 1
A one_to_many_relationship() 0 11 1
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Functional;
4
5
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6
use Zenstruck\Foundry\Test\Factories;
7
use Zenstruck\Foundry\Test\ResetDatabase;
8
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Brand;
9
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Category;
10
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Image;
11
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Product;
12
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Review;
13
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Tag;
14
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Variant;
15
use function Zenstruck\Foundry\create;
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 = create(Product::class, [
31
            'name' => 'foo',
32
            'brand' => factory(Brand::class, ['name' => 'bar']),
33
        ]);
34
35
        $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

35
        $this->assertNotNull($product->/** @scrutinizer ignore-call */ getBrand()->getId());
Loading history...
36
        $this->assertNull($product->getBrand()->getPrePersistedId());
37
        $this->assertSame('bar', $product->getBrand()->getName());
38
    }
39
40
    /**
41
     * @test
42
     */
43
    public function one_to_many_relationship(): void
44
    {
45
        $product = create(Product::class, [
46
            'name' => 'foo',
47
            'variants' => [factory(Variant::class, ['name' => 'bar'])],
48
        ]);
49
50
        $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

50
        $this->assertCount(1, $product->/** @scrutinizer ignore-call */ getVariants());
Loading history...
51
        $this->assertNotNull($product->getVariants()->first()->getId());
52
        $this->assertNull($product->getVariants()->first()->getPrePersistedId());
53
        $this->assertSame('bar', $product->getVariants()->first()->getName());
54
    }
55
56
    /**
57
     * @test
58
     */
59
    public function many_to_many_relationship(): void
60
    {
61
        $product = create(Product::class, [
62
            'name' => 'foo',
63
            'tags' => [factory(Tag::class, ['name' => 'bar'])],
64
        ]);
65
66
        $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

66
        $this->assertCount(1, $product->/** @scrutinizer ignore-call */ getTags());
Loading history...
67
        $this->assertNotNull($product->getTags()->first()->getId());
68
        $this->assertNull($product->getTags()->first()->getPrePersistedId());
69
        $this->assertSame('bar', $product->getTags()->first()->getName());
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function many_to_many_reverse_relationship(): void
76
    {
77
        $product = create(Product::class, [
78
            'name' => 'foo',
79
            'categories' => [factory(Category::class, ['name' => 'bar'])],
80
        ]);
81
82
        $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

82
        $this->assertCount(1, $product->/** @scrutinizer ignore-call */ getCategories());
Loading history...
83
        $this->assertNotNull($product->getCategories()->first()->getId());
84
        $this->assertNull($product->getCategories()->first()->getPrePersistedId());
85
        $this->assertSame('bar', $product->getCategories()->first()->getName());
86
    }
87
88
    /**
89
     * @test
90
     */
91
    public function one_to_one_relationship(): void
92
    {
93
        $variant = create(Variant::class, [
94
            'name' => 'foo',
95
            'image' => factory(Image::class, ['path' => '/path/to/file.extension']),
96
        ]);
97
98
        $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

98
        $this->assertNotNull($variant->/** @scrutinizer ignore-call */ getImage()->getId());
Loading history...
99
        $this->assertNull($variant->getImage()->getPrePersistedId());
100
        $this->assertSame('/path/to/file.extension', $variant->getImage()->getPath());
101
    }
102
103
    /**
104
     * @test
105
     */
106
    public function one_to_one_reverse_relationship(): void
107
    {
108
        $product = create(Product::class, [
109
            'name' => 'foo',
110
            'review' => factory(Review::class, ['rank' => 4]),
111
        ]);
112
113
        $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

113
        $this->assertNotNull($product->/** @scrutinizer ignore-call */ getReview()->getId());
Loading history...
114
        $this->assertNull($product->getReview()->getPrePersistedId());
115
        $this->assertSame(4, $product->getReview()->getRank());
116
    }
117
}
118