|
| 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