Skip to content

Commit

Permalink
Merge pull request #52 from wmde/improve-api
Browse files Browse the repository at this point in the history
Improve Address change ID handling
  • Loading branch information
gbirke authored Jun 17, 2024
2 parents b7b8b72 + 44a541d commit 5de84d4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/Domain/Model/AddressChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/Domain/Model/AddressChangeId.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
17 changes: 16 additions & 1 deletion tests/Unit/Domain/Model/AddressChangeIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand All @@ -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' );
}
}
31 changes: 31 additions & 0 deletions tests/Unit/Domain/Model/AddressChangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
}

0 comments on commit 5de84d4

Please sign in to comment.