Skip to content

Commit 75ad28a

Browse files
committed
make the relation more consistent
1 parent e8af4fd commit 75ad28a

5 files changed

Lines changed: 30 additions & 16 deletions

File tree

phpstan.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ parameters:
99
-
1010
message: '#PHPDoc tag @var for variable \$entityRepository contains generic class Doctrine\\ORM\\EntityRepository but does not specify its types\: T#'
1111
path: %currentWorkingDirectory%/src/Repository/PetRepository.php
12-
-
13-
message: '#Property App\\Model\\Vaccination\:\:\$pet is never read, only written\.#'
14-
path: %currentWorkingDirectory%/src/Model/Vaccination.php
1512
-
1613
message: '#Slim\\Interfaces\\RouteParserInterface\:\:urlFor\(\)#'
1714
path: %currentWorkingDirectory%/src/Parsing/PetParsing.php

src/Model/Pet.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ public function getTag(): ?string
7777
*/
7878
public function setVaccinations(array $vaccinations): void
7979
{
80-
$this->vaccinations->clear();
80+
foreach ($this->vaccinations as $i => $vaccination) {
81+
$vaccination->setPet(null);
82+
$this->vaccinations->remove($i);
83+
}
84+
8185
foreach ($vaccinations as $vaccination) {
8286
$vaccination->setPet($this);
8387
$this->vaccinations->add($vaccination);

src/Model/Vaccination.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function setPet(?Pet $pet): void
3939
$this->pet = $pet;
4040
}
4141

42+
public function getPet(): ?Pet
43+
{
44+
return $this->pet;
45+
}
46+
4247
/**
4348
* @return array{name: null|string}
4449
*/

tests/Unit/Model/PetTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use App\Model\ModelInterface;
88
use App\Model\Pet;
99
use App\Model\Vaccination;
10-
use App\Tests\Helper\AssertHelper;
1110
use PHPUnit\Framework\TestCase;
1211

1312
/**
@@ -37,12 +36,24 @@ public function testGetSet(): void
3736
$vaccination2 = new Vaccination();
3837
$vaccination2->setName('Feline Acquired Immune Deficiency Syndrome');
3938

39+
$vaccination3 = new Vaccination();
40+
$vaccination3->setName('Panleukopenia');
41+
4042
$pet->setUpdatedAt($now);
4143
$pet->setName('Lucas');
4244
$pet->setTag('2018 OHIO DOG 87123 LUCAS');
43-
$pet->setVaccinations([$vaccination2]);
45+
$pet->setVaccinations([$vaccination2, $vaccination3]);
46+
47+
self::assertNull($vaccination1->getPet());
48+
self::assertSame($pet, $vaccination2->getPet());
49+
self::assertSame($pet, $vaccination3->getPet());
50+
4451
$pet->setVaccinations([$vaccination1, $vaccination2]);
4552

53+
self::assertSame($pet, $vaccination1->getPet());
54+
self::assertSame($pet, $vaccination2->getPet());
55+
self::assertNull($vaccination3->getPet());
56+
4657
self::assertSame($now, $pet->getUpdatedAt());
4758
self::assertSame('Lucas', $pet->getName());
4859
self::assertSame('2018 OHIO DOG 87123 LUCAS', $pet->getTag());
@@ -54,14 +65,11 @@ public function testGetSet(): void
5465
self::assertSame($vaccination1, array_shift($vaccinations));
5566
self::assertSame($vaccination2, array_shift($vaccinations));
5667

57-
self::assertSame('Rabies', AssertHelper::readProperty('name', $vaccination1));
58-
self::assertSame($pet, AssertHelper::readProperty('pet', $vaccination1));
68+
self::assertSame('Rabies', $vaccination1->getName());
69+
self::assertSame($pet, $vaccination1->getPet());
5970

60-
self::assertSame(
61-
'Feline Acquired Immune Deficiency Syndrome',
62-
AssertHelper::readProperty('name', $vaccination2)
63-
);
64-
self::assertSame($pet, AssertHelper::readProperty('pet', $vaccination2));
71+
self::assertSame('Feline Acquired Immune Deficiency Syndrome', $vaccination2->getName());
72+
self::assertSame($pet, $vaccination2->getPet());
6573

6674
self::assertSame([
6775
'id' => $pet->getId(),

tests/Unit/Model/VaccinationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use App\Model\Pet;
88
use App\Model\Vaccination;
9-
use App\Tests\Helper\AssertHelper;
109
use PHPUnit\Framework\TestCase;
1110

1211
/**
@@ -24,13 +23,14 @@ public function testGetSet(): void
2423

2524
$pet = new Pet();
2625

27-
self::assertNull(AssertHelper::readProperty('pet', $vaccination));
26+
self::assertNull($vaccination->getPet());
2827

2928
$vaccination->setName('Rabies');
3029
$vaccination->setPet($pet);
3130

3231
self::assertSame('Rabies', $vaccination->getName());
33-
self::assertSame($pet, AssertHelper::readProperty('pet', $vaccination));
32+
33+
self::assertSame($pet, $vaccination->getPet());
3434

3535
self::assertSame(['name' => $vaccination->getName()], $vaccination->jsonSerialize());
3636
}

0 commit comments

Comments
 (0)