Completed
Push — master ( e0d167...0f42e0 )
by Josef
02:33
created

GetSlotsTest::testFailures()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 105
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 74
nc 1
nop 0
dl 0
loc 105
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace HelePartnerSyncApi\Method;
4
5
use DateTime;
6
7
class GetSlotsTest extends MethodTestCase
8
{
9
10
	public function testSuccess()
11
	{
12
		$today = new DateTime('today');
13
		$request = $this->getRequestMock(array(
14
			'date' => $today->format(DateTime::W3C),
15
			'parameters' => array(),
16
		));
17
18
		$startDateTime = new DateTime('+1 hour');
19
		$endDateTime = new DateTime('+2 hours');
20
		$closure = function () use ($startDateTime, $endDateTime) {
21
			return array(
22
				array(
23
					'startDateTime' => $startDateTime,
24
					'endDateTime' => $endDateTime,
25
					'capacity' => 1,
26
				),
27
			);
28
		};
29
		$method = new GetSlots($closure);
30
		$response = $method->call($request);
31
32
		$this->assertSame(array(
33
			array(
34
				'startDateTime' => $startDateTime->format(DateTime::W3C),
35
				'endDateTime' => $endDateTime->format(DateTime::W3C),
36
				'capacity' => 1,
37
			),
38
		), $response);
39
	}
40
41
	public function testFailures()
42
	{
43
		$now = new DateTime();
44
		$before = new DateTime('-1 hour');
45
		$tomorrow = new DateTime('tomorrow');
46
		$nowString = $now->format(DateTime::W3C);
47
48
		$this->checkException(
49
			array(),
50
			array(),
51
			'Missing keys (date, parameters)'
52
		);
53
		$this->checkException(
54
			array(
55
				'date' => 'now',
56
				'parameters' => array(),
57
			),
58
			array(
59
				array(),
60
			),
61
			'W3C datetime expected, string (now) given.'
62
		);
63
		$this->checkException(
64
			array(
65
				'date' => $nowString,
66
				'parameters' => array(),
67
			),
68
			array(
69
				array(
70
					'startDateTime' => $now,
71
				)
72
			),
73
			'Missing keys (endDateTime, capacity)'
74
		);
75
		$this->checkException(
76
			array(
77
				'date' => $nowString,
78
				'parameters' => array(),
79
			),
80
			array(
81
				array(
82
					'startDateTime' => $now,
83
					'endDateTime' => $now,
84
					'capacity' => 'string',
85
				)
86
			),
87
			'Int expected, string (string) given.'
88
		);
89
		$this->checkException(
90
			array(
91
				'date' => $nowString,
92
				'parameters' => array(),
93
			),
94
			array(
95
				array(
96
					'startDateTime' => 'not-a-datetime',
97
					'endDateTime' => $now,
98
					'capacity' => 1,
99
				)
100
			),
101
			'DateTime expected, string (not-a-datetime) given.'
102
		);
103
		$this->checkException(
104
			array(
105
				'date' => $nowString,
106
				'parameters' => array(),
107
			),
108
			array(
109
				array(
110
					'startDateTime' => $before,
111
					'endDateTime' => $now,
112
					'capacity' => -1,
113
				)
114
			),
115
			'Slot capacity (-1) must be non-negative (slot #1)'
116
		);
117
		$this->checkException(
118
			array(
119
				'date' => $nowString,
120
				'parameters' => array(),
121
			),
122
			array(
123
				array(
124
					'startDateTime' => $now,
125
					'endDateTime' => $before,
126
					'capacity' => 1,
127
				)
128
			),
129
			sprintf('Slot startDateTime (%s) must be before endDateTime (%s) (slot #1)', $nowString, $before->format(DateTime::W3C))
130
		);
131
		$this->checkException(
132
			array(
133
				'date' => $tomorrow->format(DateTime::W3C),
134
				'parameters' => array(),
135
			),
136
			array(
137
				array(
138
					'startDateTime' => $before,
139
					'endDateTime' => $now,
140
					'capacity' => 1,
141
				)
142
			),
143
			sprintf('Slot startDateTime (%s) does not match requested day (%s) (slot #1)', $now->format('Y-m-d'), $tomorrow->format('Y-m-d'))
144
		);
145
	}
146
147
	/**
148
	 * @param array $requestData
149
	 * @param array $responseData
150
	 * @param string $error
151
	 */
152 View Code Duplication
	private function checkException(array $requestData, array $responseData, $error)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
	{
154
		try {
155
			$request = $this->getRequestMock($requestData);
156
			$method = new GetSlots(function () use ($responseData) {
157
				return $responseData;
158
			});
159
			$method->call($request);
160
			$this->fail(sprintf('Expected exception with "%s" to be thrown', $error));
161
162
		} catch (MethodException $e) {
163
			$this->assertContains($error, $e->getMessage());
164
		}
165
	}
166
167
}
168