Passed
Push — master ( 3e1d12...91acb4 )
by Luís
13:51 queued 06:19
created

GH6531OrderItem   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\ORM\Annotation as ORM;
6
use Doctrine\Common\Collections\ArrayCollection;
7
8
final class GH6531Test extends \Doctrine\Tests\OrmFunctionalTestCase
9
{
10
    protected function setUp() : void
11
    {
12
        parent::setup();
13
14
        $this->setUpEntitySchema(
15
            [
16
                GH6531User::class,
17
                GH6531Address::class,
18
                GH6531Article::class,
19
                GH6531ArticleAttribute::class,
20
                GH6531Order::class,
21
                GH6531OrderItem::class,
22
                GH6531Product::class,
23
            ]
24
        );
25
    }
26
27
    /**
28
     * @group 6531
29
     */
30
    public function testSimpleDerivedIdentity() : void
31
    {
32
        $user          = new GH6531User();
33
        $address       = new GH6531Address();
34
        $address->user = $user;
35
36
        $this->em->persist($user);
37
        $this->em->persist($address);
38
        $this->em->flush();
39
40
        self::assertSame($user, $this->em->find(GH6531User::class, $user->id));
41
        self::assertSame($address, $this->em->find(GH6531Address::class, $user));
42
    }
43
44
    /**
45
     * @group 6531
46
     */
47
    public function testDynamicAttributes() : void
48
    {
49
        $article = new GH6531Article();
50
        $article->addAttribute('name', 'value');
51
52
        $this->em->persist($article);
53
        $this->em->flush();
54
55
        self::assertSame(
56
            $article->attributes['name'],
57
            $this->em->find(GH6531ArticleAttribute::class, ['article' => $article, 'attribute' => 'name'])
58
        );
59
    }
60
61
    /**
62
     * @group 6531
63
     */
64
    public function testJoinTableWithMetadata() : void
65
    {
66
        $product = new GH6531Product();
67
        $this->em->persist($product);
68
        $this->em->flush();
69
70
        $order = new GH6531Order();
71
        $order->addItem($product, 2);
72
73
        $this->em->persist($order);
74
        $this->em->flush();
75
76
        self::assertSame(
77
            $order->items->first(),
78
            $this->em->find(GH6531OrderItem::class, ['product' => $product, 'order' => $order])
79
        );
80
    }
81
}
82
83
/**
84
 * @ORM\Entity
85
 */
86
class GH6531User
87
{
88
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
89
    public $id;
90
}
91
92
/**
93
 * @ORM\Entity
94
 */
95
class GH6531Address
96
{
97
    /** @ORM\Id @ORM\OneToOne(targetEntity=GH6531User::class) */
98
    public $user;
99
}
100
101
/**
102
 * @ORM\Entity
103
 */
104
class GH6531Article
105
{
106
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
107
    public $id;
108
109
    /** @ORM\OneToMany(targetEntity=GH6531ArticleAttribute::class, mappedBy="article", cascade={"ALL"}, indexBy="attribute") */
110
    public $attributes;
111
112
    public function addAttribute(string $name, string $value)
113
    {
114
        $this->attributes[$name] = new GH6531ArticleAttribute($name, $value, $this);
115
    }
116
}
117
118
/**
119
 * @ORM\Entity
120
 */
121
class GH6531ArticleAttribute
122
{
123
    /** @ORM\Id @ORM\ManyToOne(targetEntity=GH6531Article::class, inversedBy="attributes") */
124
    public $article;
125
126
    /** @ORM\Id @ORM\Column(type="string") */
127
    public $attribute;
128
129
    /** @ORM\Column(type="string") */
130
    public $value;
131
132
    public function __construct(string $name, string $value, GH6531Article $article)
133
    {
134
        $this->attribute = $name;
135
        $this->value     = $value;
136
        $this->article   = $article;
137
    }
138
}
139
140
/**
141
 * @ORM\Entity
142
 */
143
class GH6531Order
144
{
145
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
146
    public $id;
147
148
    /** @ORM\OneToMany(targetEntity=GH6531OrderItem::class, mappedBy="order", cascade={"ALL"}) */
149
    public $items;
150
151
    public function __construct()
152
    {
153
        $this->items = new ArrayCollection();
154
    }
155
156
    public function addItem(GH6531Product $product, int $amount) : void
157
    {
158
        $this->items->add(new GH6531OrderItem($this, $product, $amount));
159
    }
160
}
161
162
/**
163
 * @ORM\Entity
164
 */
165
class GH6531Product
166
{
167
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
168
    public $id;
169
}
170
171
/**
172
 * @ORM\Entity
173
 */
174
class GH6531OrderItem
175
{
176
    /** @ORM\Id @ORM\ManyToOne(targetEntity=GH6531Order::class) */
177
    public $order;
178
179
    /** @ORM\Id @ORM\ManyToOne(targetEntity=GH6531Product::class) */
180
    public $product;
181
182
    /** @ORM\Column(type="integer") */
183
    public $amount = 1;
184
185
    public function __construct(GH6531Order $order, GH6531Product $product, int $amount = 1)
186
    {
187
        $this->order   = $order;
188
        $this->product = $product;
189
        $this->amount  = $amount;
190
    }
191
}
192