Passed
Push — master ( 1e7639...3fc4e5 )
by Tonina
51s queued 10s
created

testAddressChangeCannotBeExportedTwice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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 = $this->newPersonAddressChange();
39
		$address = ValidAddress::newValidPersonalAddress();
40
41
		$addressChange->performAddressChange( $address );
42
43
		$this->assertSame( $address, $addressChange->getAddress() );
44
	}
45
46
	private function newPersonAddressChange(): AddressChange {
47
		return AddressChange::createNewPersonAddressChange( null, null, new \DateTime( '1970-01-01' ) );
48
	}
49
50
	public function testUpdatingAddressMarksAddressChangeAsModified() {
51
		$addressChange = $this->newPersonAddressChange();
52
		$initialIdentifier = $addressChange->getCurrentIdentifier();
53
54
		$addressChange->performAddressChange( ValidAddress::newValidPersonalAddress() );
55
56
		$this->assertNotSame( $initialIdentifier, $addressChange->getCurrentIdentifier() );
57
		$this->assertSame( $initialIdentifier, $addressChange->getPreviousIdentifier() );
58
		$this->assertTrue( $addressChange->isModified() );
59
	}
60
61
	public function testAddressChangeCannotBePerformedTwice() {
62
		$addressChange = AddressChange::createNewPersonAddressChange();
63
		$addressChange->performAddressChange( ValidAddress::newValidCompanyAddress() );
64
65
		$this->expectException( \LogicException::class );
66
67
		$addressChange->performAddressChange( ValidAddress::newValidPersonalAddress() );
68
	}
69
70
	public function testOptingOutOfReceiptMarksAddressChangeAsModified() {
71
		$addressChange = $this->newPersonAddressChange();
72
		$initialIdentifier = $addressChange->getCurrentIdentifier();
73
74
		$addressChange->optOutOfDonationReceipt();
75
76
		$this->assertNotSame( $initialIdentifier, $addressChange->getCurrentIdentifier() );
77
		$this->assertSame( $initialIdentifier, $addressChange->getPreviousIdentifier() );
78
		$this->assertTrue( $addressChange->isModified() );
79
	}
80
81
	public function testNewAddressChangeIsNotExported() {
82
		$addressChange = AddressChange::createNewPersonAddressChange();
83
		$this->assertFalse( $addressChange->isExported() );
84
	}
85
86
	public function testAddressChangeCanBeMarkedAsExported() {
87
		$addressChange = AddressChange::createNewPersonAddressChange();
88
		$addressChange->markAsExported();
89
		$this->assertTrue( $addressChange->isExported() );
90
	}
91
92
	public function testAddressChangeCannotBeExportedTwice() {
93
		$addressChange = AddressChange::createNewPersonAddressChange();
94
		$addressChange->markAsExported();
95
96
		$this->expectException( \LogicException::class );
97
98
		$addressChange->markAsExported();
99
	}
100
101
	public function testMarkingAsExportedDoesNotChangeModificationDate() {
102
		$addressChange = AddressChange::createNewPersonAddressChange();
103
		$addressChange->markAsExported();
104
		$this->assertFalse( $addressChange->isModified() );
105
	}
106
107
	public function testMultipleChangesModifyIdentifiersOnlyOnce() {
108
		$addressChange = AddressChange::createNewPersonAddressChange();
109
110
		$originalIdentifier = $addressChange->getCurrentIdentifier();
111
		$addressChange->performAddressChange( ValidAddress::newValidPersonalAddress() );
112
		$identifierAfterFirstChange = $addressChange->getCurrentIdentifier();
113
		$previousIdentifierAfterFirstChange = $addressChange->getPreviousIdentifier();
114
115
		$addressChange->optOutOfDonationReceipt();
116
		$identifierAfterSecondChange = $addressChange->getCurrentIdentifier();
117
		$previousIdentifierAfterSecondChange = $addressChange->getPreviousIdentifier();
118
119
		$this->assertSame( $previousIdentifierAfterFirstChange, $originalIdentifier, 'The original identifier must become the previous identifier' );
120
		$this->assertSame( $previousIdentifierAfterSecondChange, $previousIdentifierAfterFirstChange, 'The previous identifier must not change after the first modification' );
121
		$this->assertSame( $identifierAfterFirstChange, $identifierAfterSecondChange, 'The current identifier must not change after the first modification' );
122
	}
123
124
	public function testWhenAddressChangeIsPerformed_exportStateIsReset() {
125
		$addressChange = AddressChange::createNewPersonAddressChange();
126
		$addressChange->markAsExported();
127
		$addressChange->performAddressChange( ValidAddress::newValidCompanyAddress() );
128
129
		$this->assertFalse( $addressChange->isExported() );
130
	}
131
132
	public function testConstructorsAcceptValidUuids() {
133
		$uuid = '72dfed91-fa40-4af0-9e80-c6010ab29cd1';
134
		$personAddressChange = AddressChange::createNewPersonAddressChange( $uuid );
135
		$companyAddressChange = AddressChange::createNewCompanyAddressChange( $uuid );
136
137
		$this->assertSame( $uuid, $personAddressChange->getCurrentIdentifier() );
138
		$this->assertSame( $uuid, $companyAddressChange->getCurrentIdentifier() );
139
	}
140
141
	/**
142
	 * @dataProvider invalidUUIDProvider
143
	 */
144
	public function testPersonalAddressChangeThrowsExceptionsWhenUUIDIsInvalid( string $invalidUUID ) {
145
		$this->expectException( \InvalidArgumentException::class );
146
		AddressChange::createNewPersonAddressChange( $invalidUUID );
147
	}
148
149
	/**
150
	 * @dataProvider invalidUUIDProvider
151
	 */
152
	public function testCompanyAddressChangeThrowsExceptionsWhenUUIDIsInvalid( string $invalidUUID ) {
153
		$this->expectException( \InvalidArgumentException::class );
154
		AddressChange::createNewCompanyAddressChange( $invalidUUID );
155
	}
156
157
	public function invalidUUIDProvider(): \Generator {
158
		yield [ '' ];
159
		yield [ 'just a string' ];
160
		yield [ '1111222233334444-1111222233334444-1111222233334444-1111222233334444-1111222233334444' ];
161
		yield [ 'e-f-f-e-d' ];
162
		yield [ 'This-is-not-a-UUID' ];
163
	}
164
}
165