CheckSlotsTest::testFailures()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 9.5555
c 0
b 0
f 0
cc 1
eloc 41
nc 1
nop 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 CheckSlotsTest 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 CheckSlots($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
45
		$this->checkException(
46
			array(),
47
			array(),
48
			'Missing keys (date, parameters)'
49
		);
50
		$this->checkException(
51
			array(
52
				'date' => 'now',
53
				'parameters' => array(),
54
			),
55
			array(
56
				array(),
57
			),
58
			'W3C datetime expected, string (now) given.'
59
		);
60
		$this->checkException(
61
			array(
62
				'date' => $now->format(DateTime::W3C),
63
				'parameters' => array(),
64
			),
65
			array(
66
				array(
67
					'startDateTime' => new DateTime(),
68
				)
69
			),
70
			'Missing keys (endDateTime, capacity)'
71
		);
72
		$this->checkException(
73
			array(
74
				'date' => $now->format(DateTime::W3C),
75
				'parameters' => array(),
76
			),
77
			array(
78
				array(
79
					'startDateTime' => new DateTime(),
80
					'endDateTime' => new DateTime(),
81
					'capacity' => 'string',
82
				)
83
			),
84
			'Int expected, string (string) given.'
85
		);
86
		$this->checkException(
87
			array(
88
				'date' => $now->format(DateTime::W3C),
89
				'parameters' => array(),
90
			),
91
			array(
92
				array(
93
					'startDateTime' => 'not-a-datetime',
94
					'endDateTime' => new DateTime(),
95
					'capacity' => 1,
96
				)
97
			),
98
			'DateTime expected, string (not-a-datetime) given.'
99
		);
100
	}
101
102
	/**
103
	 * @param array $requestData
104
	 * @param array $responseData
105
	 * @param string $error
106
	 */
107 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...
108
	{
109
		try {
110
			$request = $this->getRequestMock($requestData);
111
			$method = new CheckSlots(function () use ($responseData) {
112
				return $responseData;
113
			});
114
			$method->call($request);
115
			$this->fail('Expected exception to be thrown');
116
117
		} catch (MethodException $e) {
118
			$this->assertContains($error, $e->getMessage());
119
		}
120
	}
121
122
}
123