1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Doctrine\Tests\ORM\Functional\Ticket; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
use Doctrine\Common\Collections\Collection; |
8
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
9
|
|
|
use Doctrine\ORM\Annotation as ORM; |
10
|
|
|
|
11
|
|
|
class GH7062Test extends OrmFunctionalTestCase |
12
|
|
|
{ |
13
|
|
|
private const SEASON_ID = 'season_18'; |
14
|
|
|
private const TEAM_ID = 'team_A'; |
15
|
|
|
|
16
|
|
|
protected function setUp() : void |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
|
20
|
|
|
$this->setUpEntitySchema( |
21
|
|
|
[ |
22
|
|
|
GH7062Team::class, |
23
|
|
|
GH7062Season::class, |
24
|
|
|
GH7062Ranking::class, |
25
|
|
|
GH7062RankingPosition::class |
26
|
|
|
] |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @group 7062 |
32
|
|
|
*/ |
33
|
|
|
public function testEntityWithAssociationKeyIdentityCanBeUpdated() : void |
34
|
|
|
{ |
35
|
|
|
$this->createInitialRankingWithRelatedEntities(); |
36
|
|
|
$this->modifyRanking(); |
37
|
|
|
$this->verifyRanking(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function createInitialRankingWithRelatedEntities() : void |
41
|
|
|
{ |
42
|
|
|
$team = new GH7062Team(self::TEAM_ID); |
43
|
|
|
$season = new GH7062Season(self::SEASON_ID); |
44
|
|
|
|
45
|
|
|
$season->ranking = new GH7062Ranking($season, [$team]); |
46
|
|
|
|
47
|
|
|
$this->em->persist($team); |
48
|
|
|
$this->em->persist($season); |
49
|
|
|
$this->em->flush(); |
50
|
|
|
$this->em->clear(); |
51
|
|
|
|
52
|
|
|
foreach ($season->ranking->positions as $position) { |
53
|
|
|
self::assertSame(0, $position->points); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function modifyRanking() : void |
58
|
|
|
{ |
59
|
|
|
/** @var GH7062Ranking $ranking */ |
60
|
|
|
$ranking = $this->em->find(GH7062Ranking::class, self::SEASON_ID); |
61
|
|
|
|
62
|
|
|
foreach ($ranking->positions as $position) { |
63
|
|
|
$position->points += 3; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->em->flush(); |
67
|
|
|
$this->em->clear(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function verifyRanking() : void |
71
|
|
|
{ |
72
|
|
|
/** @var GH7062Season $season */ |
73
|
|
|
$season = $this->em->find(GH7062Season::class, self::SEASON_ID); |
74
|
|
|
self::assertInstanceOf(GH7062Season::class, $season); |
75
|
|
|
|
76
|
|
|
$ranking = $season->ranking; |
77
|
|
|
self::assertInstanceOf(GH7062Ranking::class, $ranking); |
78
|
|
|
|
79
|
|
|
foreach ($ranking->positions as $position) { |
80
|
|
|
self::assertSame(3, $position->points); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Simple Entity whose identity is defined through another Entity (Season) |
87
|
|
|
* |
88
|
|
|
* @ORM\Entity |
89
|
|
|
* @ORM\Table(name="soccer_rankings") |
90
|
|
|
*/ |
91
|
|
|
class GH7062Ranking |
92
|
|
|
{ |
93
|
|
|
/** |
94
|
|
|
* @ORM\Id |
95
|
|
|
* @ORM\OneToOne(targetEntity=GH7062Season::class, inversedBy="ranking") |
96
|
|
|
* @ORM\JoinColumn(name="season", referencedColumnName="id") |
97
|
|
|
* |
98
|
|
|
* @var GH7062Season |
99
|
|
|
*/ |
100
|
|
|
public $season; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @ORM\OneToMany(targetEntity=GH7062RankingPosition::class, mappedBy="ranking", cascade={"all"}) |
104
|
|
|
* |
105
|
|
|
* @var Collection|GH7062RankingPosition[] |
106
|
|
|
*/ |
107
|
|
|
public $positions; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param GH7062Team[] $teams |
111
|
|
|
*/ |
112
|
|
|
public function __construct(GH7062Season $season, array $teams) |
113
|
|
|
{ |
114
|
|
|
$this->season = $season; |
115
|
|
|
$this->positions = new ArrayCollection(); |
116
|
|
|
|
117
|
|
|
foreach ($teams as $team) { |
118
|
|
|
$this->positions[] = new GH7062RankingPosition($this, $team); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Entity which serves as a identity provider for other entities |
125
|
|
|
* |
126
|
|
|
* @ORM\Entity |
127
|
|
|
* @ORM\Table(name="soccer_seasons") |
128
|
|
|
*/ |
129
|
|
|
class GH7062Season |
130
|
|
|
{ |
131
|
|
|
/** |
132
|
|
|
* @ORM\Id |
133
|
|
|
* @ORM\Column(type="string") |
134
|
|
|
* |
135
|
|
|
* @var string |
136
|
|
|
*/ |
137
|
|
|
public $id; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @ORM\OneToOne(targetEntity=GH7062Ranking::class, mappedBy="season", cascade={"all"}) |
141
|
|
|
* |
142
|
|
|
* @var GH7062Ranking|null |
143
|
|
|
*/ |
144
|
|
|
public $ranking; |
145
|
|
|
|
146
|
|
|
public function __construct(string $id) |
147
|
|
|
{ |
148
|
|
|
$this->id = $id; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Entity which serves as a identity provider for other entities |
154
|
|
|
* |
155
|
|
|
* @ORM\Entity |
156
|
|
|
* @ORM\Table(name="soccer_teams") |
157
|
|
|
*/ |
158
|
|
|
class GH7062Team |
159
|
|
|
{ |
160
|
|
|
/** |
161
|
|
|
* @ORM\Id |
162
|
|
|
* @ORM\Column(type="string") |
163
|
|
|
* |
164
|
|
|
* @var string |
165
|
|
|
*/ |
166
|
|
|
public $id; |
167
|
|
|
|
168
|
|
|
public function __construct(string $id) |
169
|
|
|
{ |
170
|
|
|
$this->id = $id; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Entity whose identity is defined through two other entities |
176
|
|
|
* |
177
|
|
|
* @ORM\Entity |
178
|
|
|
* @ORM\Table(name="soccer_ranking_positions") |
179
|
|
|
*/ |
180
|
|
|
class GH7062RankingPosition |
181
|
|
|
{ |
182
|
|
|
/** |
183
|
|
|
* @ORM\Id |
184
|
|
|
* @ORM\ManyToOne(targetEntity=GH7062Ranking::class, inversedBy="positions") |
185
|
|
|
* @ORM\JoinColumn(name="season", referencedColumnName="season") |
186
|
|
|
* |
187
|
|
|
* @var GH7062Ranking |
188
|
|
|
*/ |
189
|
|
|
public $ranking; |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @ORM\Id |
193
|
|
|
* @ORM\ManyToOne(targetEntity=GH7062Team::class) |
194
|
|
|
* @ORM\JoinColumn(name="team_id", referencedColumnName="id") |
195
|
|
|
* |
196
|
|
|
* @var GH7062Team |
197
|
|
|
*/ |
198
|
|
|
public $team; |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @ORM\Column(type="integer") |
202
|
|
|
* |
203
|
|
|
* @var int |
204
|
|
|
*/ |
205
|
|
|
public $points; |
206
|
|
|
|
207
|
|
|
public function __construct(GH7062Ranking $ranking, GH7062Team $team) |
208
|
|
|
{ |
209
|
|
|
$this->ranking = $ranking; |
210
|
|
|
$this->team = $team; |
211
|
|
|
$this->points = 0; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|