|
| 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 | + |
| 44 | + $season->ranking = new GH7062Ranking($season, [$team]); |
| 45 | + |
| 46 | + $this->_em->persist($team); |
| 47 | + $this->_em->persist($season); |
| 48 | + $this->_em->flush(); |
| 49 | + $this->_em->clear(); |
| 50 | + |
| 51 | + foreach ($season->ranking->positions as $position) { |
| 52 | + self::assertSame(0, $position->points); |
| 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 | + foreach ($ranking->positions as $position) { |
| 62 | + $position->points += 3; |
| 63 | + } |
| 64 | + |
| 65 | + $this->_em->flush(); |
| 66 | + $this->_em->clear(); |
| 67 | + } |
| 68 | + |
| 69 | + private function verifyRanking() : void |
| 70 | + { |
| 71 | + /** @var GH7062Season $season */ |
| 72 | + $season = $this->_em->find(GH7062Season::class, self::SEASON_ID); |
| 73 | + self::assertInstanceOf(GH7062Season::class, $season); |
| 74 | + |
| 75 | + $ranking = $season->ranking; |
| 76 | + self::assertInstanceOf(GH7062Ranking::class, $ranking); |
| 77 | + |
| 78 | + foreach ($ranking->positions as $position) { |
| 79 | + self::assertSame(3, $position->points); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Simple Entity whose identity is defined through another Entity (Season) |
| 86 | + * |
| 87 | + * @Entity |
| 88 | + * @Table(name="soccer_rankings") |
| 89 | + */ |
| 90 | +class GH7062Ranking |
| 91 | +{ |
| 92 | + /** |
| 93 | + * @Id |
| 94 | + * @OneToOne(targetEntity=GH7062Season::class, inversedBy="ranking") |
| 95 | + * @JoinColumn(name="season", referencedColumnName="id") |
| 96 | + * |
| 97 | + * @var GH7062Season |
| 98 | + */ |
| 99 | + public $season; |
| 100 | + |
| 101 | + /** |
| 102 | + * @OneToMany(targetEntity=GH7062RankingPosition::class, mappedBy="ranking", cascade={"all"}) |
| 103 | + * |
| 104 | + * @var Collection|GH7062RankingPosition[] |
| 105 | + */ |
| 106 | + public $positions; |
| 107 | + |
| 108 | + /** |
| 109 | + * @param GH7062Team[] $teams |
| 110 | + */ |
| 111 | + public function __construct(GH7062Season $season, array $teams) |
| 112 | + { |
| 113 | + $this->season = $season; |
| 114 | + $this->positions = new ArrayCollection(); |
| 115 | + |
| 116 | + foreach ($teams as $team) { |
| 117 | + $this->positions[] = new GH7062RankingPosition($this, $team); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Entity which serves as a identity provider for other entities |
| 124 | + * |
| 125 | + * @Entity |
| 126 | + * @Table(name="soccer_seasons") |
| 127 | + */ |
| 128 | +class GH7062Season |
| 129 | +{ |
| 130 | + /** |
| 131 | + * @Id |
| 132 | + * @Column(type="string") |
| 133 | + * |
| 134 | + * @var string |
| 135 | + */ |
| 136 | + public $id; |
| 137 | + |
| 138 | + /** |
| 139 | + * @OneToOne(targetEntity=GH7062Ranking::class, mappedBy="season", cascade={"all"}) |
| 140 | + * |
| 141 | + * @var GH7062Ranking|null |
| 142 | + */ |
| 143 | + public $ranking; |
| 144 | + |
| 145 | + public function __construct(string $id) |
| 146 | + { |
| 147 | + $this->id = $id; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Entity which serves as a identity provider for other entities |
| 153 | + * |
| 154 | + * @Entity |
| 155 | + * @Table(name="soccer_teams") |
| 156 | + */ |
| 157 | +class GH7062Team |
| 158 | +{ |
| 159 | + /** |
| 160 | + * @Id |
| 161 | + * @Column(type="string") |
| 162 | + * |
| 163 | + * @var string |
| 164 | + */ |
| 165 | + public $id; |
| 166 | + |
| 167 | + public function __construct(string $id) |
| 168 | + { |
| 169 | + $this->id = $id; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * Entity whose identity is defined through two other entities |
| 175 | + * |
| 176 | + * @Entity |
| 177 | + * @Table(name="soccer_ranking_positions") |
| 178 | + */ |
| 179 | +class GH7062RankingPosition |
| 180 | +{ |
| 181 | + /** |
| 182 | + * @Id |
| 183 | + * @ManyToOne(targetEntity=GH7062Ranking::class, inversedBy="positions") |
| 184 | + * @JoinColumn(name="season", referencedColumnName="season") |
| 185 | + * |
| 186 | + * @var GH7062Ranking |
| 187 | + */ |
| 188 | + public $ranking; |
| 189 | + |
| 190 | + /** |
| 191 | + * @Id |
| 192 | + * @ManyToOne(targetEntity=GH7062Team::class) |
| 193 | + * @JoinColumn(name="team_id", referencedColumnName="id") |
| 194 | + * |
| 195 | + * @var GH7062Team |
| 196 | + */ |
| 197 | + public $team; |
| 198 | + |
| 199 | + /** |
| 200 | + * @Column(type="integer") |
| 201 | + * |
| 202 | + * @var int |
| 203 | + */ |
| 204 | + public $points; |
| 205 | + |
| 206 | + public function __construct(GH7062Ranking $ranking, GH7062Team $team) |
| 207 | + { |
| 208 | + $this->ranking = $ranking; |
| 209 | + $this->team = $team; |
| 210 | + $this->points = 0; |
| 211 | + } |
| 212 | +} |
0 commit comments