Completed
Pull Request — master (#2)
by Gabriel
127:24 queued 62:20
created

assertDatePropertyIsSet()   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 4
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\DataAccess;
6
7
use Doctrine\ORM\EntityManager;
8
use PHPUnit\Framework\TestCase;
9
use WMDE\Fundraising\AddressChange\DataAccess\DoctrineAddressChangeRepository;
10
use WMDE\Fundraising\AddressChange\Domain\Model\Address;
11
use WMDE\Fundraising\AddressChange\Domain\Model\AddressChange;
12
use WMDE\Fundraising\AddressChangeContext\Tests\TestEnvironment;
13
14
/**
15
 * @covers \WMDE\Fundraising\AddressChange\DataAccess\DoctrineAddressChangeRepository
16
 */
17
class DoctrineAddressChangeRepositoryTest extends TestCase {
18
19
	const VALID_UPDATE_TOKEN_PERSONAL_DONATION = '2a54c0a1-fc94-4ef8-8b0a-7c2ed8565521';
20
	const VALID_UPDATE_TOKEN_PERSONAL_MEMBERSHIP = 'ce4449f9-8317-41fa-acc3-4a878e26845d';
21
	const VALID_UPDATE_TOKEN_COMPANY_DONATION = 'c52258ba-fed1-476a-a7e5-c721df087c12';
22
	const VALID_UPDATE_TOKEN_COMPANY_MEMBERSHIP = '8d11d2ba-5ec5-4ec8-a08c-0ac7b8654b59';
23
	const INVALID_UPDATE_TOKEN = '72dfed91-fa40-4af0-9e80-c6010ab29cd1';
24
25
	/** @var EntityManager */
26
	private $em;
27
28
	public function setUp(): void {
29
		$this->em = TestEnvironment::newInstance()->getFactory()->getEntityManager();
30
		parent::setUp();
31
	}
32
33
	public function testGivenValidPersonalDonationUuid_addressChangeIsReturned() {
34
		$this->storeAddressChange( self::VALID_UPDATE_TOKEN_PERSONAL_DONATION, true );
35
		$retrievedAddressChange = $this->retrieveAddressChangeByUuid( self::VALID_UPDATE_TOKEN_PERSONAL_DONATION );
36
37
		$this->assertNotNull( $retrievedAddressChange );
38
		$this->assertSame(
39
			self::VALID_UPDATE_TOKEN_PERSONAL_DONATION,
40
			$retrievedAddressChange->getCurrentIdentifier()
41
		);
42
		$this->assertTrue( $retrievedAddressChange->isPersonalAddress() );
43
	}
44
45
	public function testGivenValidCompanyDonationUuid_addressChangeIsReturned() {
46
		$this->storeAddressChange( self::VALID_UPDATE_TOKEN_COMPANY_DONATION, false );
47
		$retrievedAddressChange = $this->retrieveAddressChangeByUuid( self::VALID_UPDATE_TOKEN_COMPANY_DONATION );
48
49
		$this->assertNotNull( $retrievedAddressChange );
50
		$this->assertSame(
51
			self::VALID_UPDATE_TOKEN_COMPANY_DONATION,
52
			$retrievedAddressChange->getCurrentIdentifier()
53
		);
54
		$this->assertTrue( $retrievedAddressChange->isCompanyAddress() );
55
	}
56
57
	public function testGivenInvalidDonationUuid_nullIsReturned() {
58
		$addressChangeRepository = new DoctrineAddressChangeRepository( $this->em );
59
		$addressChange = $addressChangeRepository->getAddressChangeByUuid( self::INVALID_UPDATE_TOKEN );
60
		$this->assertNull( $addressChange );
61
	}
62
63
	public function testGivenAddressChangeWithAddress_itIsStoredCorrectly() {
64
		$addressChangeRepository = new DoctrineAddressChangeRepository( $this->em );
65
		$addressChange = AddressChange::createNewPersonAddressChange( null, $this->newPersonalAddress() );
66
		$addressChangeRepository->storeAddressChange( $addressChange );
67
		$now = new \DateTime();
68
69
		$retrievedAddressChange = $addressChangeRepository->getAddressChangeByUuid( $addressChange->getCurrentIdentifier() );
70
		$this->assertNotNull( $retrievedAddressChange );
71
		$this->assertNotNull( $retrievedAddressChange->getAddress() );
72
		$this->assertNotNull( $addressChange->getAddress() ); // avoid PHPStan errors when accessing address later
73
		$this->assertFalse( $retrievedAddressChange->isExported() );
74
		$this->assertDatePropertyIsSet( $now, $retrievedAddressChange, 'createdAt' );
0 ignored issues
show
Bug introduced by
It seems like $retrievedAddressChange can also be of type null; however, parameter $addressChange of WMDE\Fundraising\Address...sertDatePropertyIsSet() does only seem to accept WMDE\Fundraising\Address...ain\Model\AddressChange, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
		$this->assertDatePropertyIsSet( $now, /** @scrutinizer ignore-type */ $retrievedAddressChange, 'createdAt' );
Loading history...
75
		$this->assertDatePropertyIsSet( $now, $retrievedAddressChange, 'modifiedAt' );
76
		$this->assertSame( $addressChange->getCurrentIdentifier(), $retrievedAddressChange->getCurrentIdentifier() );
77
		$this->assertSame( $addressChange->getAddress()->isPersonalAddress(), $retrievedAddressChange->getAddress()->isPersonalAddress() );
78
	}
79
80
	public function testGivenExportedAddressChange_itsStateIsStoredCorrectly() {
81
		$addressChangeRepository = new DoctrineAddressChangeRepository( $this->em );
82
		$addressChange = AddressChange::createNewPersonAddressChange( null, $this->newPersonalAddress() );
83
		$addressChange->markAsExported();
84
		$addressChangeRepository->storeAddressChange( $addressChange );
85
		$now = new \DateTime();
86
87
		$retrievedAddressChange = $addressChangeRepository->getAddressChangeByUuid( $addressChange->getCurrentIdentifier() );
88
		$this->assertNotNull( $retrievedAddressChange );
89
		$this->assertTrue( $retrievedAddressChange->isExported() );
90
		$this->assertDatePropertyIsSet( $now, $retrievedAddressChange, 'exportDate' );
0 ignored issues
show
Bug introduced by
It seems like $retrievedAddressChange can also be of type null; however, parameter $addressChange of WMDE\Fundraising\Address...sertDatePropertyIsSet() does only seem to accept WMDE\Fundraising\Address...ain\Model\AddressChange, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
		$this->assertDatePropertyIsSet( $now, /** @scrutinizer ignore-type */ $retrievedAddressChange, 'exportDate' );
Loading history...
91
	}
92
93
	private function storeAddressChange( string $uuid, bool $isPersonal = true ): void {
94
		if ( $isPersonal ) {
95
			$addressChange = AddressChange::createNewPersonAddressChange(
96
				$uuid,
97
				$this->newPersonalAddress()
98
			);
99
		} else {
100
			$addressChange = AddressChange::createNewCompanyAddressChange(
101
				$uuid,
102
				$this->newCompanyAddress()
103
			);
104
		}
105
		$this->em->persist( $addressChange );
106
		$this->em->flush();
107
	}
108
109
	private function retrieveAddressChangeByUuid( string $uuid ): ?AddressChange {
110
		$addressChangeRepository = new DoctrineAddressChangeRepository( $this->em );
111
		return $addressChangeRepository->getAddressChangeByUuid(
112
			$uuid
113
		);
114
	}
115
116
	private function newPersonalAddress(): Address {
117
		return Address::newPersonalAddress(
118
			'Herr',
119
			'Prof. Dr.',
120
			'Test',
121
			'User',
122
			'Teststreet 12345',
123
			'98765',
124
			'Berlin',
125
			'Germany'
126
		);
127
	}
128
129
	private function newCompanyAddress(): Address {
130
		return Address::newCompanyAddress(
131
			'Test Company',
132
			'Teststreet 123',
133
			'324324',
134
			'Not Berlin',
135
			'Somewhere'
136
		);
137
	}
138
139
	private function assertDatePropertyIsSet( \DateTime $expectedDate, AddressChange $addressChange, string $propertyName, float $delta = 1.0 ) {
140
		// We're peeking into private properties to make sure the dates, which are not exposed through getters at the domain level,
141
		// are properly written at the DB level
142
		$dateField = new \ReflectionProperty( AddressChange::class, $propertyName );
143
		$dateField->setAccessible( true );
144
		$actualDate = $dateField->getValue( $addressChange );
145
		$this->assertEqualsWithDelta( $actualDate->getTimestamp(), $expectedDate->getTimestamp(), $delta, 'Dates do not match.' );
146
	}
147
}
148