Skip to content

Commit 5de84d4

Browse files
authored
Merge pull request #52 from wmde/improve-api
Improve Address change ID handling
2 parents b7b8b72 + 44a541d commit 5de84d4

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

src/Domain/Model/AddressChange.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,15 @@ public function isModified(): bool {
115115
return $this->createdAt < $this->modifiedAt;
116116
}
117117

118+
public function hasBeenUsed(): bool {
119+
return !$this->getCurrentIdentifier()->equals( $this->previousIdentifier );
120+
}
121+
118122
private function markAsModified( AddressChangeId $newIdentifier ): void {
119-
$this->previousIdentifier = $this->getCurrentIdentifier();
120-
$this->identifier = $newIdentifier;
123+
if ( !$this->getCurrentIdentifier()->equals( $newIdentifier ) ) {
124+
$this->previousIdentifier = $this->getCurrentIdentifier();
125+
$this->identifier = $newIdentifier;
126+
}
121127

122128
$this->modifiedAt = new \DateTime();
123129
$this->resetExportState();

src/Domain/Model/AddressChangeId.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ public function __toString(): string {
2525
return $this->identifier;
2626
}
2727

28+
public function equals( string|AddressChangeId $id ): bool {
29+
return is_string( $id ) ? $this->identifier === $id : $this->identifier === $id->identifier;
30+
}
31+
2832
}

tests/Unit/Domain/Model/AddressChangeIdTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testConstructorAcceptValidUuids(): void {
2222
/**
2323
* @dataProvider invalidUUIDProvider
2424
*/
25-
public function testPersonalAddressChangeThrowsExceptionsWhenUUIDIsInvalid( string $invalidUUID ): void {
25+
public function testThrowsExceptionsWhenUUIDIsInvalid( string $invalidUUID ): void {
2626
$this->expectException( \InvalidArgumentException::class );
2727
AddressChangeId::fromString( $invalidUUID );
2828
}
@@ -37,4 +37,19 @@ public static function invalidUUIDProvider(): \Generator {
3737
yield [ 'e-f-f-e-d' ];
3838
yield [ 'This-is-not-a-UUID' ];
3939
}
40+
41+
public function testCanCheckEqualityWithStrings(): void {
42+
$uuid = '72dfed91-fa40-4af0-9e80-c6010ab29cd1';
43+
$addressChangeId = AddressChangeId::fromString( $uuid );
44+
45+
$this->assertTrue( $addressChangeId->equals( $uuid ), 'IDs should compare equals' );
46+
}
47+
48+
public function testCanCheckEqualityWithOtherID(): void {
49+
$uuid = '72dfed91-fa40-4af0-9e80-c6010ab29cd1';
50+
$addressChangeId = AddressChangeId::fromString( $uuid );
51+
$addressChangeIdWithSameUUID = AddressChangeId::fromString( $uuid );
52+
53+
$this->assertTrue( $addressChangeId->equals( $addressChangeIdWithSameUUID ), 'IDs should compare equals' );
54+
}
4055
}

tests/Unit/Domain/Model/AddressChangeTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,35 @@ public function testUsedAndExportedAddressReturnsCorrectExportState(): void {
132132

133133
$this->assertEquals( AddressChange::EXPORT_STATE_USED_EXPORTED, $addressChange->getExportState() );
134134
}
135+
136+
public function testNewAddressChangesAreUnused(): void {
137+
$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
138+
139+
$this->assertFalse( $addressChange->hasBeenUsed() );
140+
}
141+
142+
public function testAddressChangesWithAddressesAreUsed(): void {
143+
$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
144+
$addressChange->performAddressChange( ValidAddress::newValidPersonalAddress(), $this->newIdentifier );
145+
146+
$this->assertTrue( $addressChange->hasBeenUsed() );
147+
}
148+
149+
public function testAddressChangesWithOptOutAreUsed(): void {
150+
$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
151+
$addressChange->optOutOfDonationReceipt( $this->newIdentifier );
152+
153+
$this->assertTrue( $addressChange->hasBeenUsed() );
154+
}
155+
156+
public function testMultipleModificationsWithTheSameIdentifierKeepsIdentifiers(): void {
157+
$addressChange = $this->newPersonAddressChange();
158+
$initialIdentifier = $addressChange->getCurrentIdentifier();
159+
160+
$addressChange->performAddressChange( ValidAddress::newValidPersonalAddress(), $this->newIdentifier );
161+
$addressChange->optOutOfDonationReceipt( $this->newIdentifier );
162+
163+
$this->assertEquals( $initialIdentifier, $addressChange->getPreviousIdentifier() );
164+
$this->assertEquals( $this->newIdentifier, $addressChange->getCurrentIdentifier() );
165+
}
135166
}

0 commit comments

Comments
 (0)