diff --git a/src/Domain/Model/AddressChange.php b/src/Domain/Model/AddressChange.php index 76dec1d..6e4489f 100644 --- a/src/Domain/Model/AddressChange.php +++ b/src/Domain/Model/AddressChange.php @@ -115,9 +115,15 @@ public function isModified(): bool { return $this->createdAt < $this->modifiedAt; } + public function hasBeenUsed(): bool { + return !$this->getCurrentIdentifier()->equals( $this->previousIdentifier ); + } + private function markAsModified( AddressChangeId $newIdentifier ): void { - $this->previousIdentifier = $this->getCurrentIdentifier(); - $this->identifier = $newIdentifier; + if ( !$this->getCurrentIdentifier()->equals( $newIdentifier ) ) { + $this->previousIdentifier = $this->getCurrentIdentifier(); + $this->identifier = $newIdentifier; + } $this->modifiedAt = new \DateTime(); $this->resetExportState(); diff --git a/src/Domain/Model/AddressChangeId.php b/src/Domain/Model/AddressChangeId.php index 302f6f3..b32dcfa 100644 --- a/src/Domain/Model/AddressChangeId.php +++ b/src/Domain/Model/AddressChangeId.php @@ -25,4 +25,8 @@ public function __toString(): string { return $this->identifier; } + public function equals( string|AddressChangeId $id ): bool { + return is_string( $id ) ? $this->identifier === $id : $this->identifier === $id->identifier; + } + } diff --git a/tests/Unit/Domain/Model/AddressChangeIdTest.php b/tests/Unit/Domain/Model/AddressChangeIdTest.php index 0982bf6..46e1edc 100644 --- a/tests/Unit/Domain/Model/AddressChangeIdTest.php +++ b/tests/Unit/Domain/Model/AddressChangeIdTest.php @@ -22,7 +22,7 @@ public function testConstructorAcceptValidUuids(): void { /** * @dataProvider invalidUUIDProvider */ - public function testPersonalAddressChangeThrowsExceptionsWhenUUIDIsInvalid( string $invalidUUID ): void { + public function testThrowsExceptionsWhenUUIDIsInvalid( string $invalidUUID ): void { $this->expectException( \InvalidArgumentException::class ); AddressChangeId::fromString( $invalidUUID ); } @@ -37,4 +37,19 @@ public static function invalidUUIDProvider(): \Generator { yield [ 'e-f-f-e-d' ]; yield [ 'This-is-not-a-UUID' ]; } + + public function testCanCheckEqualityWithStrings(): void { + $uuid = '72dfed91-fa40-4af0-9e80-c6010ab29cd1'; + $addressChangeId = AddressChangeId::fromString( $uuid ); + + $this->assertTrue( $addressChangeId->equals( $uuid ), 'IDs should compare equals' ); + } + + public function testCanCheckEqualityWithOtherID(): void { + $uuid = '72dfed91-fa40-4af0-9e80-c6010ab29cd1'; + $addressChangeId = AddressChangeId::fromString( $uuid ); + $addressChangeIdWithSameUUID = AddressChangeId::fromString( $uuid ); + + $this->assertTrue( $addressChangeId->equals( $addressChangeIdWithSameUUID ), 'IDs should compare equals' ); + } } diff --git a/tests/Unit/Domain/Model/AddressChangeTest.php b/tests/Unit/Domain/Model/AddressChangeTest.php index e24c288..73ee99c 100644 --- a/tests/Unit/Domain/Model/AddressChangeTest.php +++ b/tests/Unit/Domain/Model/AddressChangeTest.php @@ -132,4 +132,35 @@ public function testUsedAndExportedAddressReturnsCorrectExportState(): void { $this->assertEquals( AddressChange::EXPORT_STATE_USED_EXPORTED, $addressChange->getExportState() ); } + + public function testNewAddressChangesAreUnused(): void { + $addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier ); + + $this->assertFalse( $addressChange->hasBeenUsed() ); + } + + public function testAddressChangesWithAddressesAreUsed(): void { + $addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier ); + $addressChange->performAddressChange( ValidAddress::newValidPersonalAddress(), $this->newIdentifier ); + + $this->assertTrue( $addressChange->hasBeenUsed() ); + } + + public function testAddressChangesWithOptOutAreUsed(): void { + $addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier ); + $addressChange->optOutOfDonationReceipt( $this->newIdentifier ); + + $this->assertTrue( $addressChange->hasBeenUsed() ); + } + + public function testMultipleModificationsWithTheSameIdentifierKeepsIdentifiers(): void { + $addressChange = $this->newPersonAddressChange(); + $initialIdentifier = $addressChange->getCurrentIdentifier(); + + $addressChange->performAddressChange( ValidAddress::newValidPersonalAddress(), $this->newIdentifier ); + $addressChange->optOutOfDonationReceipt( $this->newIdentifier ); + + $this->assertEquals( $initialIdentifier, $addressChange->getPreviousIdentifier() ); + $this->assertEquals( $this->newIdentifier, $addressChange->getCurrentIdentifier() ); + } }