Passed
Pull Request — 2.6 (#7082)
by Luís
12:04
created

GH7062Season::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
10
class GH7062Test extends OrmFunctionalTestCase
11
{
12
    private const SEASON_ID = 'season_18';
13
    private const TEAM_ID   = 'team_A';
14
15
    protected function setUp() : void
16
    {
17
        parent::setUp();
18
19
        $this->setUpEntitySchema(
20
            [
21
                GH7062Team::class,
22
                GH7062Season::class,
23
                GH7062Ranking::class,
24
                GH7062RankingPosition::class
25
            ]
26
        );
27
    }
28
29
    /**
30
     * @group 7062
31
     */
32
    public function testEntityWithAssociationKeyIdentityCanBeUpdated() : void
33
    {
34
        $this->createInitialRankingWithRelatedEntities();
35
        $this->modifyRanking();
36
        $this->verifyRanking();
37
    }
38
39
    private function createInitialRankingWithRelatedEntities() : void
40
    {
41
        $team    = new GH7062Team(self::TEAM_ID);
42
        $season  = new GH7062Season(self::SEASON_ID);
43
        $ranking = $season->start([$team]);
44
45
        $this->_em->persist($team);
46
        $this->_em->persist($season);
47
        $this->_em->persist($ranking);
48
        $this->_em->flush();
49
        $this->_em->clear();
50
51
        foreach ($ranking->getPositions() as $position) {
52
            self::assertFalse($position->hasPoints());
53
        }
54
    }
55
56
    private function modifyRanking() : void
57
    {
58
        /** @var GH7062Ranking $ranking */
59
        $ranking = $this->_em->find(GH7062Ranking::class, self::SEASON_ID);
60
61
        $ranking->everybodyWins();
62
63
        foreach ($ranking->getPositions() as $position) {
64
            self::assertTrue($position->hasPoints());
65
        }
66
67
        $this->_em->flush();
68
        $this->_em->clear();
69
    }
70
71
    private function verifyRanking() : void
72
    {
73
        /** @var GH7062Season $season */
74
        $season = $this->_em->find(GH7062Season::class, self::SEASON_ID);
75
        self::assertInstanceOf(GH7062Season::class, $season);
76
77
        $ranking = $season->getRanking();
78
        self::assertInstanceOf(GH7062Ranking::class, $ranking);
79
80
        foreach ($ranking->getPositions() as $position) {
81
            self::assertTrue($position->hasPoints());
82
        }
83
    }
84
}
85
86
/**
87
 * Simple Entity whose identity is defined through another Entity (Season)
88
 *
89
 * @Entity
90
 * @Table(name="soccer_rankings")
91
 */
92
class GH7062Ranking
93
{
94
    /**
95
     * @Id
96
     * @OneToOne(targetEntity=GH7062Season::class, inversedBy="ranking")
97
     * @JoinColumn(name="season", referencedColumnName="id")
98
     *
99
     * @var GH7062Season
100
     */
101
    private $season;
102
103
    /**
104
     * @OneToMany(targetEntity=GH7062RankingPosition::class, mappedBy="ranking", cascade={"all"})
105
     *
106
     * @var Collection
107
     */
108
    private $positions;
109
110
    /**
111
     * @param GH7062Team[] $teams
112
     */
113
    public function __construct(GH7062Season $season, array $teams)
114
    {
115
        $this->season    = $season;
116
        $this->positions = new ArrayCollection();
117
118
        foreach ($teams as $team) {
119
            $this->positions[] = new GH7062RankingPosition($this, $team);
120
        }
121
    }
122
123
    /**
124
     * Adds a victory for every team :)
125
     */
126
    public function everybodyWins() : void
127
    {
128
        foreach ($this->positions as $position) {
129
            $position->addVictory();
130
        }
131
    }
132
133
    /**
134
     * @return GH7062RankingPosition[]
135
     */
136
    public function getPositions() : array
137
    {
138
        return $this->positions->toArray();
139
    }
140
}
141
142
/**
143
 * Entity which serves as a identity provider for other entities
144
 *
145
 * @Entity
146
 * @Table(name="soccer_seasons")
147
 */
148
class GH7062Season
149
{
150
    /**
151
     * @Id
152
     * @Column(type="string")
153
     *
154
     * @var string
155
     */
156
    private $id;
157
158
    /**
159
     * @OneToOne(targetEntity=GH7062Ranking::class, mappedBy="season", cascade={"all"})
160
     *
161
     * @var GH7062Ranking|null
162
     */
163
    private $ranking;
164
165
    public function __construct(string $id)
166
    {
167
        $this->id = $id;
168
    }
169
170
    public function start(array $teams) : GH7062Ranking
171
    {
172
        return $this->ranking = new GH7062Ranking($this, $teams);
173
    }
174
175
    /**
176
     * @return GH7062Ranking|null
177
     */
178
    public function getRanking(): ?GH7062Ranking
179
    {
180
        return $this->ranking;
181
    }
182
}
183
184
/**
185
 * Entity which serves as a identity provider for other entities
186
 *
187
 * @Entity
188
 * @Table(name="soccer_teams")
189
 */
190
class GH7062Team
191
{
192
    /**
193
     * @Id
194
     * @Column(type="string")
195
     *
196
     * @var string
197
     */
198
    private $id;
199
200
    public function __construct($id)
201
    {
202
        $this->id = $id;
203
    }
204
}
205
206
/**
207
 * Entity whose identity is defined through two other entities
208
 *
209
 * @Entity
210
 * @Table(name="soccer_ranking_positions")
211
 */
212
class GH7062RankingPosition
213
{
214
    /**
215
     * @Id
216
     * @ManyToOne(targetEntity=GH7062Ranking::class, inversedBy="positions")
217
     * @JoinColumn(name="season", referencedColumnName="season")
218
     *
219
     * @var GH7062Ranking
220
     */
221
    private $ranking;
222
223
    /**
224
     * @Id
225
     * @ManyToOne(targetEntity=GH7062Team::class)
226
     * @JoinColumn(name="team_id", referencedColumnName="id")
227
     *
228
     * @var GH7062Team
229
     */
230
    private $team;
231
232
    /**
233
     * @Column(type="integer")
234
     *
235
     * @var int
236
     */
237
    private $points;
238
239
    public function __construct(GH7062Ranking $ranking, GH7062Team $team)
240
    {
241
        $this->ranking = $ranking;
242
        $this->team    = $team;
243
        $this->points  = 0;
244
    }
245
246
    public function addVictory() : void
247
    {
248
        $this->points += 3;
249
    }
250
251
    public function hasPoints() : bool
252
    {
253
        return $this->points > 0;
254
    }
255
}
256