Completed
Push — dev ( ce9639...c420bb )
by Josef
04:04
created

CheckSlotsTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 116
Duplicated Lines 12.07 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 14
loc 116
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testSuccess() 0 30 1
A testFailures() 0 60 1
A checkException() 14 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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