Failed Conditions
Pull Request — 2.6 (#7082)
by Luís
11:10
created

GH7062Team   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Importance

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

1 Method

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