Skip to content

Commit d2b4b2b

Browse files
committed
Add ReadAddressChangeUseCase
1 parent f0b66ec commit d2b4b2b

File tree

5 files changed

+203
-1
lines changed

5 files changed

+203
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Address change use case for fundraising application",
44
"license": "GPL-2.0-or-later",
55
"require": {
6-
"php": ">=7.4",
6+
"php": ">=8.0",
77
"doctrine/dbal": "^2.10",
88
"doctrine/orm": "^2.7",
99
"ramsey/uuid": "^4.0",

src/Domain/AddressChangeRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ interface AddressChangeRepository {
1313

1414
public function getAddressChangeByUuid( string $uuid ): ?AddressChange;
1515

16+
public function getAddressChangeByUuids( string $currentIdentifier, string $previousIdentifier ): ?AddressChange;
17+
1618
public function storeAddressChange( AddressChange $addressChange ): void;
1719

1820
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare( strict_types = 1 );
4+
5+
namespace WMDE\Fundraising\AddressChangeContext\UseCases\ReadAddressChange;
6+
7+
class AddressChangeData {
8+
9+
/**
10+
* @param string $identifier
11+
* @param string $previousIdentifier
12+
* @param array<string, mixed> $address
13+
* @param bool $donationReceipt
14+
* @param string $exportState
15+
*/
16+
public function __construct(
17+
public string $identifier,
18+
public string $previousIdentifier,
19+
public array $address,
20+
public bool $donationReceipt,
21+
public string $exportState
22+
) {
23+
}
24+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare( strict_types = 1 );
4+
5+
namespace WMDE\Fundraising\AddressChangeContext\UseCases\ReadAddressChange;
6+
7+
use WMDE\Fundraising\AddressChangeContext\Domain\AddressChangeRepository;
8+
use WMDE\Fundraising\AddressChangeContext\Domain\Model\AddressChange;
9+
10+
class ReadAddressChangeUseCase {
11+
12+
private AddressChangeRepository $addressChangeRepository;
13+
14+
public function __construct( AddressChangeRepository $addressChangeRepository ) {
15+
$this->addressChangeRepository = $addressChangeRepository;
16+
}
17+
18+
public function getAddressChangeByUuids( string $currentIdentifier, string $previousIdentifier ): ?AddressChangeData {
19+
$addressChange = $this->addressChangeRepository->getAddressChangeByUuids( $currentIdentifier, $previousIdentifier );
20+
21+
if ( !$addressChange ) {
22+
return null;
23+
}
24+
25+
return new AddressChangeData(
26+
$addressChange->getCurrentIdentifier()->__toString(),
27+
$addressChange->getPreviousIdentifier()->__toString(),
28+
$this->getAddressData( $addressChange ),
29+
$addressChange->isOptedIntoDonationReceipt(),
30+
$addressChange->getExportState(),
31+
);
32+
}
33+
34+
/**
35+
* @param AddressChange $addressChange
36+
*
37+
* @return array<string, mixed>
38+
*/
39+
private function getAddressData( AddressChange $addressChange ): array {
40+
if ( !$addressChange->getAddress() ) {
41+
return [];
42+
}
43+
44+
return [
45+
'salutation' => $addressChange->getAddress()->getSalutation(),
46+
'company' => $addressChange->getAddress()->getCompany(),
47+
'title' => $addressChange->getAddress()->getTitle(),
48+
'firstName' => $addressChange->getAddress()->getFirstName(),
49+
'lastName' => $addressChange->getAddress()->getLastName(),
50+
'street' => $addressChange->getAddress()->getAddress(),
51+
'postcode' => $addressChange->getAddress()->getPostcode(),
52+
'city' => $addressChange->getAddress()->getCity(),
53+
'country' => $addressChange->getAddress()->getCountry(),
54+
'isPersonalAddress' => $addressChange->getAddress()->isPersonalAddress(),
55+
'isCompanyAddress' => $addressChange->getAddress()->isCompanyAddress(),
56+
];
57+
}
58+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
declare( strict_types = 1 );
4+
5+
namespace WMDE\Fundraising\AddressChangeContext\Tests\Unit\UseCases;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use WMDE\Fundraising\AddressChangeContext\Domain\AddressChangeRepository;
9+
use WMDE\Fundraising\AddressChangeContext\Domain\Model\Address;
10+
use WMDE\Fundraising\AddressChangeContext\Domain\Model\AddressChange;
11+
use WMDE\Fundraising\AddressChangeContext\Domain\Model\AddressChangeId;
12+
use WMDE\Fundraising\AddressChangeContext\UseCases\ReadAddressChange\ReadAddressChangeUseCase;
13+
14+
/**
15+
* @covers \WMDE\Fundraising\AddressChangeContext\UseCases\ReadAddressChange\ReadAddressChangeUseCase
16+
* @covers \WMDE\Fundraising\AddressChangeContext\UseCases\ReadAddressChange\AddressChangeData
17+
*/
18+
class ReadAddressChangeUseCaseTest extends TestCase {
19+
20+
private const VALID_UUID = '2a54c0a1-fc94-4ef8-8b0a-7c2ed8565521';
21+
private const UPDATE_UUID = 'c52258ba-fed1-476a-a7e5-c721df087c12';
22+
private const INVALID_UUID = 'NOT A VALID UUID';
23+
private const DUMMY_DONATION_ID = 0;
24+
25+
public function testGivenValidUuid_returnsAddressChangeData(): void {
26+
$mockAddressChangeRepository = $this->createMock( AddressChangeRepository::class );
27+
$mockAddressChangeRepository->method( 'getAddressChangeByUuids' )->willReturn(
28+
$this->createUsedAddressChange()
29+
);
30+
31+
$readAddressUseCase = new ReadAddressChangeUseCase( $mockAddressChangeRepository );
32+
$expectedAddress = [
33+
'salutation' => 'Herr',
34+
'company' => '',
35+
'title' => 'Prof. Dr.',
36+
'firstName' => 'Test',
37+
'lastName' => 'User',
38+
'street' => 'Teststreet 12345',
39+
'postcode' => '98765',
40+
'city' => 'Berlin',
41+
'country' => 'Germany',
42+
'isPersonalAddress' => true,
43+
'isCompanyAddress' => false,
44+
];
45+
46+
$addressChangeData = $readAddressUseCase->getAddressChangeByUuids( self::VALID_UUID, self::VALID_UUID );
47+
48+
$this->assertEquals( self::UPDATE_UUID, $addressChangeData->identifier );
49+
$this->assertEquals( self::VALID_UUID, $addressChangeData->previousIdentifier );
50+
$this->assertEquals( $expectedAddress, $addressChangeData->address );
51+
$this->assertTrue( $addressChangeData->donationReceipt );
52+
$this->assertEquals( AddressChange::EXPORT_STATE_USED_NOT_EXPORTED, $addressChangeData->exportState );
53+
}
54+
55+
public function testGivenValidUuidForUnusedAddressChange_returnsAddressChangeData(): void {
56+
$mockAddressChangeRepository = $this->createMock( AddressChangeRepository::class );
57+
$mockAddressChangeRepository->method( 'getAddressChangeByUuids' )->willReturn(
58+
$this->createUnusedAddressChange()
59+
);
60+
61+
$readAddressUseCase = new ReadAddressChangeUseCase( $mockAddressChangeRepository );
62+
63+
$addressChangeData = $readAddressUseCase->getAddressChangeByUuids( self::VALID_UUID, self::VALID_UUID );
64+
65+
$this->assertEquals( self::VALID_UUID, $addressChangeData->identifier );
66+
$this->assertEquals( self::VALID_UUID, $addressChangeData->previousIdentifier );
67+
$this->assertEquals( [], $addressChangeData->address );
68+
$this->assertTrue( $addressChangeData->donationReceipt );
69+
$this->assertEquals( AddressChange::EXPORT_STATE_NO_DATA, $addressChangeData->exportState );
70+
}
71+
72+
public function testGivenInvalidDonationUuids_returnsNull(): void {
73+
$mockAddressChangeRepository = $this->createMock( AddressChangeRepository::class );
74+
$mockAddressChangeRepository->method( 'getAddressChangeByUuids' )->willReturn( null );
75+
76+
$readAddressUseCase = new ReadAddressChangeUseCase( $mockAddressChangeRepository );
77+
78+
$this->assertNull( $readAddressUseCase->getAddressChangeByUuids( self::INVALID_UUID, self::INVALID_UUID ) );
79+
}
80+
81+
private function createUnusedAddressChange(): AddressChange {
82+
return new AddressChange(
83+
AddressChange::ADDRESS_TYPE_PERSON,
84+
AddressChange::EXTERNAL_ID_TYPE_DONATION,
85+
self::DUMMY_DONATION_ID,
86+
AddressChangeId::fromString( self::VALID_UUID )
87+
);
88+
}
89+
90+
private function createUsedAddressChange(): AddressChange {
91+
$addressChange = new AddressChange(
92+
AddressChange::ADDRESS_TYPE_PERSON,
93+
AddressChange::EXTERNAL_ID_TYPE_DONATION,
94+
self::DUMMY_DONATION_ID,
95+
AddressChangeId::fromString( self::VALID_UUID )
96+
);
97+
98+
$addressChange->performAddressChange(
99+
$this->newPersonalAddress(),
100+
AddressChangeId::fromString( self::UPDATE_UUID )
101+
);
102+
103+
return $addressChange;
104+
}
105+
106+
private function newPersonalAddress(): Address {
107+
return Address::newPersonalAddress(
108+
'Herr',
109+
'Prof. Dr.',
110+
'Test',
111+
'User',
112+
'Teststreet 12345',
113+
'98765',
114+
'Berlin',
115+
'Germany'
116+
);
117+
}
118+
}

0 commit comments

Comments
 (0)