testDataEncodingAndDecodingRoundtrips()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Store\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use WMDE\Fundraising\Entities\Donation;
9
use WMDE\Fundraising\Store\DonationData;
10
11
/**
12
 * @covers WMDE\Fundraising\Entities\Donation
13
 * @covers WMDE\Fundraising\Store\DonationData
14
 *
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class DonationTest extends TestCase {
19
20
	public function testDataEncodingAndDecodingRoundtrips() {
21
		$donation = new Donation();
22
23
		$someData = [
24
			'nyan' => 'cat',
25
			'foo' => null,
26
			'bar' => 9000.01,
27
			'baz' => [ true ]
28
		];
29
30
		$donation->encodeAndSetData( $someData );
31
32
		$this->assertSame( $someData, $donation->getDecodedData() );
33
	}
34
35
	public function testGivenNoData_getDecodedDataReturnsEmptyArray() {
36
		$donation = new Donation();
37
38
		$this->assertSame( [], $donation->getDecodedData() );
39
	}
40
41
	public function testWhenSettingIdToAnInteger_getIdReturnsIt() {
42
		$donation = new Donation();
43
		$donation->setId( 1337 );
44
45
		$this->assertSame( 1337, $donation->getId() );
46
	}
47
48
	public function testWhenSettingIdToNull_getIdReturnsNull() {
49
		$donation = new Donation();
50
		$donation->setId( 1337 );
51
		$donation->setId( null );
52
53
		$this->assertNull( $donation->getId() );
54
	}
55
56
	public function testWhenIdIsNotSet_getIdReturnsNull() {
57
		$donation = new Donation();
58
59
		$this->assertNull( $donation->getId() );
60
	}
61
62
	public function testGivenNoData_getDataObjectReturnsObjectWithNullValues() {
63
		$donation = new Donation();
64
65
		$this->assertNull( $donation->getDataObject()->getAccessToken() );
66
		$this->assertNull( $donation->getDataObject()->getUpdateToken() );
67
		$this->assertNull( $donation->getDataObject()->getUpdateTokenExpiry() );
68
	}
69
70
	public function testGivenData_getDataObjectReturnsTheValues() {
71
		$donation = new Donation();
72
		$donation->encodeAndSetData( [
73
			'token' => 'foo',
74
			'utoken' => 'bar',
75
			'uexpiry' => 'baz',
76
		] );
77
78
		$this->assertSame( 'foo', $donation->getDataObject()->getAccessToken() );
79
		$this->assertSame( 'bar', $donation->getDataObject()->getUpdateToken() );
80
		$this->assertSame( 'baz', $donation->getDataObject()->getUpdateTokenExpiry() );
81
	}
82
83
	public function testWhenProvidingData_setDataObjectSetsData() {
84
		$data = new DonationData();
85
		$data->setAccessToken( 'foo' );
86
		$data->setUpdateToken( 'bar' );
87
		$data->setUpdateTokenExpiry( 'baz' );
88
89
		$donation = new Donation();
90
		$donation->setDataObject( $data );
91
92
		$this->assertSame(
93
			[
94
				'token' => 'foo',
95
				'utoken' => 'bar',
96
				'uexpiry' => 'baz',
97
			],
98
			$donation->getDecodedData()
99
		);
100
	}
101
102
	public function testWhenProvidingNullData_setObjectDoesNotSetFields() {
103
		$donation = new Donation();
104
		$donation->setDataObject( new DonationData() );
105
106
		$this->assertSame(
107
			[],
108
			$donation->getDecodedData()
109
		);
110
	}
111
112
	public function testWhenDataAlreadyExists_setDataObjectRetainsAndUpdatesData() {
113
		$donation = new Donation();
114
		$donation->encodeAndSetData( [
115
			'nyan' => 'cat',
116
			'token' => 'wee',
117
			'pink' => 'fluffy',
118
		] );
119
120
		$data = new DonationData();
121
		$data->setAccessToken( 'foo' );
122
		$data->setUpdateToken( 'bar' );
123
124
		$donation->setDataObject( $data );
125
126
		$this->assertSame(
127
			[
128
				'nyan' => 'cat',
129
				'token' => 'foo',
130
				'pink' => 'fluffy',
131
				'utoken' => 'bar',
132
			],
133
			$donation->getDecodedData()
134
		);
135
	}
136
137
	public function testWhenModifyingTheDataObject_modificationsAreReflected() {
138
		$donation = new Donation();
139
		$donation->encodeAndSetData( [
140
			'nyan' => 'cat',
141
			'token' => 'wee',
142
			'pink' => 'fluffy',
143
		] );
144
145
		$donation->modifyDataObject( function( DonationData $data ) {
146
			$data->setAccessToken( 'foo' );
147
			$data->setUpdateToken( 'bar' );
148
		} );
149
150
		$this->assertSame(
151
			[
152
				'nyan' => 'cat',
153
				'token' => 'foo',
154
				'pink' => 'fluffy',
155
				'utoken' => 'bar',
156
			],
157
			$donation->getDecodedData()
158
		);
159
	}
160
161
	public function testStatusConstantsExist() {
162
		$this->assertNotNull( Donation::STATUS_NEW );
163
		$this->assertNotNull( Donation::STATUS_CANCELLED );
164
		$this->assertNotNull( Donation::STATUS_EXTERNAL_BOOKED );
165
		$this->assertNotNull( Donation::STATUS_EXTERNAL_INCOMPLETE );
166
		$this->assertNotNull( Donation::STATUS_MODERATION );
167
		$this->assertNotNull( Donation::STATUS_PROMISE );
168
		$this->assertNotNull( Donation::STATUS_EXPORTED );
0 ignored issues
show
Deprecated Code introduced by
The constant WMDE\Fundraising\Entitie...nation::STATUS_EXPORTED has been deprecated with message: since 6.1; This status is defined for historical reasons. It should not be used to define a
donation's status anymore.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
169
	}
170
171
}
172