AddressChangeTest   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 19
eloc 73
c 8
b 0
f 0
dl 0
loc 150
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A testWhenNewAddressChangeIsCreated_itIsNotModified() 0 3 1
A testWhenAddressIsUpdated_dataIsProperlyAssigned() 0 7 1
A testWhenAddressChangeIsPerformed_exportStateIsReset() 0 6 1
A newPersonAddressChange() 0 8 1
A setUp() 0 3 1
A testMarkingAsExportedDoesNotChangeModificationDate() 0 4 1
A testUpdatingAddressMarksAddressChangeAsModified() 0 9 1
A testOptingOutOfReceiptMarksAddressChangeAsModified() 0 9 1
A testNewAddressChangeIsNotExported() 0 3 1
A testAddressChangeCanBeMarkedAsExported() 0 4 1
A testAddressChangeCannotBeExportedTwice() 0 7 1
A testUsedAndExportedAddressReturnsCorrectExportState() 0 6 1
A testAddressChangesWithAddressesAreUsed() 0 5 1
A testNewAddressChangesAreUnused() 0 4 1
A testUnusedAddressReturnsCorrectExportState() 0 4 1
A testUsedAddressReturnsCorrectExportState() 0 5 1
A testReferenceTypeIsValidated() 0 4 1
A testMultipleModificationsWithTheSameIdentifierKeepsIdentifiers() 0 9 1
A testAddressChangesWithOptOutAreUsed() 0 5 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\AddressChangeContext\Tests\Unit\Domain\Model;
6
7
use PHPUnit\Framework\Attributes\CoversClass;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CoversClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PHPUnit\Framework\TestCase;
9
use WMDE\Fundraising\AddressChangeContext\Domain\Model\AddressChange;
10
use WMDE\Fundraising\AddressChangeContext\Domain\Model\AddressChangeId;
11
use WMDE\Fundraising\AddressChangeContext\Domain\Model\AddressType;
12
use WMDE\Fundraising\AddressChangeContext\Tests\Data\ValidAddress;
13
14
#[CoversClass( AddressChange::class )]
15
class AddressChangeTest extends TestCase {
16
17
	private const DUMMY_DONATION_ID = 0;
18
19
	private AddressChangeId $identifier;
20
	private AddressChangeId $newIdentifier;
21
22
	public function setUp(): void {
23
		$this->identifier = AddressChangeId::fromString( 'c956688a-89e8-41b7-b93e-7e4cf3d6c826' );
24
		$this->newIdentifier = AddressChangeId::fromString( 'e0c4db0b-9049-462c-8c76-c4f6a3a75091' );
25
	}
26
27
	public function testWhenNewAddressChangeIsCreated_itIsNotModified(): void {
28
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
29
		$this->assertFalse( $addressChange->isModified() );
30
	}
31
32
	public function testWhenAddressIsUpdated_dataIsProperlyAssigned(): void {
33
		$addressChange = $this->newPersonAddressChange();
34
		$address = ValidAddress::newValidPersonalAddress();
35
36
		$addressChange->performAddressChange( $address, $this->newIdentifier );
37
38
		$this->assertSame( $address, $addressChange->getAddress() );
39
	}
40
41
	private function newPersonAddressChange(): AddressChange {
42
		return new AddressChange(
43
			AddressType::Person,
44
			AddressChange::EXTERNAL_ID_TYPE_DONATION,
45
			self::DUMMY_DONATION_ID,
46
			$this->identifier,
47
			null,
48
			new \DateTime( '1970-01-01' ),
49
		);
50
	}
51
52
	public function testUpdatingAddressMarksAddressChangeAsModified(): void {
53
		$addressChange = $this->newPersonAddressChange();
54
		$initialIdentifier = $addressChange->getCurrentIdentifier();
55
56
		$addressChange->performAddressChange( ValidAddress::newValidPersonalAddress(), $this->newIdentifier );
57
58
		$this->assertNotSame( $initialIdentifier, $addressChange->getCurrentIdentifier() );
59
		$this->assertSame( $initialIdentifier, $addressChange->getPreviousIdentifier() );
60
		$this->assertTrue( $addressChange->isModified() );
61
	}
62
63
	public function testOptingOutOfReceiptMarksAddressChangeAsModified(): void {
64
		$addressChange = $this->newPersonAddressChange();
65
		$initialIdentifier = $addressChange->getCurrentIdentifier();
66
67
		$addressChange->optOutOfDonationReceipt( $this->newIdentifier );
68
69
		$this->assertNotSame( $initialIdentifier, $addressChange->getCurrentIdentifier() );
70
		$this->assertSame( $initialIdentifier, $addressChange->getPreviousIdentifier() );
71
		$this->assertTrue( $addressChange->isModified() );
72
	}
73
74
	public function testNewAddressChangeIsNotExported(): void {
75
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
76
		$this->assertFalse( $addressChange->isExported() );
77
	}
78
79
	public function testAddressChangeCanBeMarkedAsExported(): void {
80
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
81
		$addressChange->markAsExported();
82
		$this->assertTrue( $addressChange->isExported() );
83
	}
84
85
	public function testAddressChangeCannotBeExportedTwice(): void {
86
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
87
		$addressChange->markAsExported();
88
89
		$this->expectException( \LogicException::class );
90
91
		$addressChange->markAsExported();
92
	}
93
94
	public function testMarkingAsExportedDoesNotChangeModificationDate(): void {
95
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
96
		$addressChange->markAsExported();
97
		$this->assertFalse( $addressChange->isModified() );
98
	}
99
100
	public function testWhenAddressChangeIsPerformed_exportStateIsReset(): void {
101
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
102
		$addressChange->markAsExported();
103
		$addressChange->performAddressChange( ValidAddress::newValidCompanyAddress(), $this->newIdentifier );
104
105
		$this->assertFalse( $addressChange->isExported() );
106
	}
107
108
	public function testReferenceTypeIsValidated(): void {
109
		$this->expectException( \InvalidArgumentException::class );
110
111
		new AddressChange( AddressType::Person, 'dogs!', self::DUMMY_DONATION_ID, $this->identifier );
112
	}
113
114
	public function testUnusedAddressReturnsCorrectExportState(): void {
115
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
116
117
		$this->assertEquals( AddressChange::EXPORT_STATE_NO_DATA, $addressChange->getExportState() );
118
	}
119
120
	public function testUsedAddressReturnsCorrectExportState(): void {
121
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
122
		$addressChange->performAddressChange( ValidAddress::newValidPersonalAddress(), $this->newIdentifier );
123
124
		$this->assertEquals( AddressChange::EXPORT_STATE_USED_NOT_EXPORTED, $addressChange->getExportState() );
125
	}
126
127
	public function testUsedAndExportedAddressReturnsCorrectExportState(): void {
128
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
129
		$addressChange->performAddressChange( ValidAddress::newValidPersonalAddress(), $this->newIdentifier );
130
		$addressChange->markAsExported();
131
132
		$this->assertEquals( AddressChange::EXPORT_STATE_USED_EXPORTED, $addressChange->getExportState() );
133
	}
134
135
	public function testNewAddressChangesAreUnused(): void {
136
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
137
138
		$this->assertFalse( $addressChange->hasBeenUsed() );
139
	}
140
141
	public function testAddressChangesWithAddressesAreUsed(): void {
142
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
143
		$addressChange->performAddressChange( ValidAddress::newValidPersonalAddress(), $this->newIdentifier );
144
145
		$this->assertTrue( $addressChange->hasBeenUsed() );
146
	}
147
148
	public function testAddressChangesWithOptOutAreUsed(): void {
149
		$addressChange = new AddressChange( AddressType::Person, AddressChange::EXTERNAL_ID_TYPE_DONATION, self::DUMMY_DONATION_ID, $this->identifier );
150
		$addressChange->optOutOfDonationReceipt( $this->newIdentifier );
151
152
		$this->assertTrue( $addressChange->hasBeenUsed() );
153
	}
154
155
	public function testMultipleModificationsWithTheSameIdentifierKeepsIdentifiers(): void {
156
		$addressChange = $this->newPersonAddressChange();
157
		$initialIdentifier = $addressChange->getCurrentIdentifier();
158
159
		$addressChange->performAddressChange( ValidAddress::newValidPersonalAddress(), $this->newIdentifier );
160
		$addressChange->optOutOfDonationReceipt( $this->newIdentifier );
161
162
		$this->assertEquals( $initialIdentifier, $addressChange->getPreviousIdentifier() );
163
		$this->assertEquals( $this->newIdentifier, $addressChange->getCurrentIdentifier() );
164
	}
165
}
166