Completed
Pull Request — master (#71)
by Jeroen De
04:20
created

testWhenDataAlreadyExists_setDataObjectRetainsAndUpdatesData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
rs 8.9713
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace WMDE\Fundraising\Store\Tests;
4
5
use WMDE\Fundraising\Entities\MembershipApplication;
6
use WMDE\Fundraising\Store\MembershipApplicationData;
7
8
/**
9
 * @covers WMDE\Fundraising\Entities\MembershipApplication
10
 * @covers WMDE\Fundraising\Store\MembershipApplicationData
11
 *
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class MembershipApplicationTest extends \PHPUnit_Framework_TestCase {
16
17
	public function testWhenSettingIdToAnInteger_getIdReturnsIt() {
18
		$application = new MembershipApplication();
19
		$application->setId( 1337 );
20
21
		$this->assertSame( 1337, $application->getId() );
22
	}
23
24
	public function testWhenSettingIdToNull_getIdReturnsNull() {
25
		$application = new MembershipApplication();
26
		$application->setId( 1337 );
27
		$application->setId( null );
28
29
		$this->assertNull( $application->getId() );
30
	}
31
32
	public function testWhenIdIsNotSet_getIdReturnsNull() {
33
		$application = new MembershipApplication();
34
35
		$this->assertNull( $application->getId() );
36
	}
37
38
	public function testGivenNoData_getDataObjectReturnsObjectWithNullValues() {
39
		$application = new MembershipApplication();
40
41
		$this->assertNull( $application->getDataObject()->getAccessToken() );
42
		$this->assertNull( $application->getDataObject()->getUpdateToken() );
43
	}
44
45
	public function testWhenProvidingData_setDataObjectSetsData() {
46
		$data = new MembershipApplicationData();
47
		$data->setAccessToken( 'foo' );
48
		$data->setUpdateToken( 'bar' );
49
50
		$application = new MembershipApplication();
51
		$application->setDataObject( $data );
52
53
		$this->assertSame(
54
			[
55
				'token' => 'foo',
56
				'utoken' => 'bar',
57
			],
58
			$application->getDecodedData()
59
		);
60
	}
61
62
	public function testWhenProvidingNullData_setObjectDoesNotSetFields() {
63
		$application = new MembershipApplication();
64
		$application->setDataObject( new MembershipApplicationData() );
65
66
		$this->assertSame(
67
			[],
68
			$application->getDecodedData()
69
		);
70
	}
71
72
	public function testWhenDataAlreadyExists_setDataObjectRetainsAndUpdatesData() {
73
		$application = new MembershipApplication();
74
		$application->encodeAndSetData( [
75
			'nyan' => 'cat',
76
			'token' => 'wee',
77
			'pink' => 'fluffy',
78
		] );
79
80
		$data = new MembershipApplicationData();
81
		$data->setAccessToken( 'foo' );
82
		$data->setUpdateToken( 'bar' );
83
84
		$application->setDataObject( $data );
85
86
		$this->assertSame(
87
			[
88
				'nyan' => 'cat',
89
				'token' => 'foo',
90
				'pink' => 'fluffy',
91
				'utoken' => 'bar',
92
			],
93
			$application->getDecodedData()
94
		);
95
	}
96
97
	public function testWhenModifyingTheDataObject_modificationsAreReflected() {
98
		$application = new MembershipApplication();
99
		$application->encodeAndSetData( [
100
			'nyan' => 'cat',
101
			'token' => 'wee',
102
			'pink' => 'fluffy',
103
		] );
104
105
		$application->modifyDataObject( function( MembershipApplicationData $data ) {
106
			$data->setAccessToken( 'foo' );
107
			$data->setUpdateToken( 'bar' );
108
		} );
109
110
		$this->assertSame(
111
			[
112
				'nyan' => 'cat',
113
				'token' => 'foo',
114
				'pink' => 'fluffy',
115
				'utoken' => 'bar',
116
			],
117
			$application->getDecodedData()
118
		);
119
	}
120
121
}
122