Completed
Push — 2.6 ( a912fc...30a063 )
by Luís
13s
created

GH6531ArticleAttribute::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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