| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare( strict_types = 1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace WMDE\Fundraising\AddressChangeContext\Tests\Unit\Domain\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use WMDE\Fundraising\AddressChange\Domain\Model\AddressChange; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use WMDE\Fundraising\AddressChangeContext\Tests\Data\ValidAddress; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use WMDE\Fundraising\AddressChangeContext\Tests\TestEnvironment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @covers \WMDE\Fundraising\AddressChange\Domain\Model\AddressChange | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class AddressChangeTest extends TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @var EntityManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	private $entityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	public function setUp(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		$this->entityManager = TestEnvironment::newInstance()->getFactory()->getEntityManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	public function testWhenNewAddressChangeIsCreated_uuidIsGenerated() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		$addressChange = AddressChange::createNewPersonAddressChange(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		$this->assertNotEmpty( $addressChange->getCurrentIdentifier() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	public function testWhenNewAddressChangeIsCreated_itIsNotModified() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		$addressChange = AddressChange::createNewPersonAddressChange(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		$this->assertFalse( $addressChange->isModified() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 	public function testWhenAddressIsUpdated_dataIsProperlyAssigned() { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 		$addressChange = AddressChange::createNewPersonAddressChange( null, null, new \DateTime( '1970-01-01' ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 		$initialIdentifier = $addressChange->getCurrentIdentifier(); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 		$address = ValidAddress::newValidPersonalAddress(); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 		$addressChange->performAddressChange( $address ); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		$this->assertSame( $initialIdentifier, $addressChange->getPreviousIdentifier() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 		$this->assertNotSame( $initialIdentifier, $addressChange->getCurrentIdentifier() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 		$this->assertSame( $address, $addressChange->getAddress() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 		$this->assertTrue( $addressChange->isModified() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	public function testAddressChangeCannotBePerformedTwice() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$addressChange = AddressChange::createNewPersonAddressChange(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$addressChange->performAddressChange( ValidAddress::newValidCompanyAddress() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		$this->expectException( \LogicException::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$addressChange->performAddressChange( ValidAddress::newValidPersonalAddress() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	public function testNewAddressChangeIsNotExported() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$addressChange = AddressChange::createNewPersonAddressChange(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$this->assertFalse( $addressChange->isExported() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	public function testAddressChangeCanBeMarkedAsExported() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$addressChange = AddressChange::createNewPersonAddressChange(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$addressChange->markAsExported(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$this->assertTrue( $addressChange->isExported() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	public function testAddressChangeCannotBeExportedTwice() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$addressChange = AddressChange::createNewPersonAddressChange(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$addressChange->markAsExported(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		$this->expectException( \LogicException::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		$addressChange->markAsExported(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	public function testMarkingAsExportedDoesNotChangeModificationDate() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		$addressChange = AddressChange::createNewPersonAddressChange(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$addressChange->markAsExported(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$this->assertFalse( $addressChange->isModified() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	public function testWhenAddressChangeIsPerformed_exportStateIsReset() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$addressChange = AddressChange::createNewPersonAddressChange(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$addressChange->markAsExported(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$addressChange->performAddressChange( ValidAddress::newValidCompanyAddress() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		$this->assertFalse( $addressChange->isExported() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	public function testConstructorsAcceptValidUuids() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		$uuid = '72dfed91-fa40-4af0-9e80-c6010ab29cd1'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$personAddressChange = AddressChange::createNewPersonAddressChange( $uuid ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$companyAddressChange = AddressChange::createNewCompanyAddressChange( $uuid ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		$this->assertSame( $uuid, $personAddressChange->getCurrentIdentifier() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$this->assertSame( $uuid, $companyAddressChange->getCurrentIdentifier() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 * @dataProvider invalidUUIDProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	public function testPersonalAddressChangeThrowsExceptionsWhenUUIDIsInvalid( string $invalidUUID ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		$this->expectException( \InvalidArgumentException::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		AddressChange::createNewPersonAddressChange( $invalidUUID ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * @dataProvider invalidUUIDProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	public function testCompanyAddressChangeThrowsExceptionsWhenUUIDIsInvalid( string $invalidUUID ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		$this->expectException( \InvalidArgumentException::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		AddressChange::createNewCompanyAddressChange( $invalidUUID ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	public function invalidUUIDProvider(): \Generator { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		yield [ '' ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		yield [ 'just a string' ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		yield [ '1111222233334444-1111222233334444-1111222233334444-1111222233334444-1111222233334444' ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		yield [ 'e-f-f-e-d' ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		yield [ 'This-is-not-a-UUID' ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |  |