Completed
Pull Request — 2.6 (#7082)
by
unknown
10:07
created

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