Issues (131)

tests/Functional/FactoryDoctrineCascadeTest.php (6 issues)

Labels
Severity
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
    protected function setUp(): void
26
    {
27
        if (false === \getenv('DATABASE_URL')) {
28
            self::markTestSkipped('doctrine/orm not enabled.');
29
        }
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function many_to_one_relationship(): void
36
    {
37
        $product = factory(Product::class, [
38
            'name' => 'foo',
39
            'brand' => factory(Brand::class, ['name' => 'bar']),
40
        ])->instantiateWith(function(array $attibutes, string $class): object {
41
            $this->assertNull($attibutes['brand']->getId());
42
43
            return (new Instantiator())($attibutes, $class);
44
        })->create();
45
46
        $this->assertNotNull($product->getBrand()->getId());
0 ignored issues
show
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

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

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

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

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

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

139
        $this->assertNotNull($product->/** @scrutinizer ignore-call */ getReview()->getId());
Loading history...
140
        $this->assertSame(4, $product->getReview()->getRank());
141
    }
142
}
143