CreateReservationTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 123
Duplicated Lines 26.02 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 32
loc 123
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSuccess() 18 18 1
A testFailure() 0 23 2
A testExceptions() 0 55 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
use Exception;
7
8
class CreateReservationTest extends MethodTestCase
9
{
10
11 View Code Duplication
	public function testSuccess()
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...
12
	{
13
		$startDateTime = new DateTime('+1 hour');
14
		$endDateTime = new DateTime('+2 hours');
15
		$request = $this->getRequestMock(array(
16
			'startDateTime' => $startDateTime->format(DateTime::W3C),
17
			'endDateTime' => $endDateTime->format(DateTime::W3C),
18
			'quantity' => 1,
19
			'parameters' => array(),
20
		));
21
22
		$method = new CreateReservation(function () {
23
			// no action
24
		});
25
		$response = $method->call($request);
26
27
		$this->assertNull($response);
28
	}
29
30
	public function testFailure()
31
	{
32
		$startDateTime = new DateTime('+1 hour');
33
		$endDateTime = new DateTime('+2 hours');
34
		$request = $this->getRequestMock(array(
35
			'startDateTime' => $startDateTime->format(DateTime::W3C),
36
			'endDateTime' => $endDateTime->format(DateTime::W3C),
37
			'quantity' => 1,
38
			'parameters' => array(),
39
		));
40
41
		$exception = new Exception('Cannot create reservation');
42
		$method = new CreateReservation(function () use ($exception) {
43
			throw $exception;
44
		});
45
		try {
46
			$method->call($request);
47
			$this->fail('Exception expected');
48
49
		} catch (Exception $e) {
50
			$this->assertSame($exception, $e);
51
		}
52
	}
53
54
	public function testExceptions()
55
	{
56
		$startDateTime = new DateTime('+1 hour');
57
		$endDateTime = new DateTime('+2 hours');
58
		$this->checkException(
59
			null,
60
			null,
61
			'Array expected, NULL given.'
62
		);
63
		$this->checkException(
64
			array(),
65
			null,
66
			'Missing keys (startDateTime, endDateTime, quantity, parameters)'
67
		);
68
		$this->checkException(
69
			array(
70
				'startDateTime' => $startDateTime->format(DateTime::W3C),
71
				'endDateTime' => $endDateTime->format(DateTime::W3C),
72
				'quantity' => 1,
73
				'parameters' => array(),
74
			),
75
			'response-data',
76
			'Null expected, string (response-data) given.'
77
		);
78
		$this->checkException(
79
			array(
80
				'startDateTime' => '1',
81
				'endDateTime' => $endDateTime->format(DateTime::W3C),
82
				'quantity' => 1,
83
				'parameters' => array(),
84
			),
85
			null,
86
			'W3C datetime expected, string (1) given.'
87
		);
88
		$this->checkException(
89
			array(
90
				'startDateTime' => $startDateTime->format(DateTime::W3C),
91
				'endDateTime' => $endDateTime->format(DateTime::W3C),
92
				'quantity' => 'quantity',
93
				'parameters' => array(),
94
			),
95
			null,
96
			'Int expected, string (quantity) given.'
97
		);
98
		$this->checkException(
99
			array(
100
				'startDateTime' => $startDateTime->format(DateTime::W3C),
101
				'endDateTime' => $endDateTime->format(DateTime::W3C),
102
				'quantity' => 1,
103
				'parameters' => 'param',
104
			),
105
			null,
106
			'Array expected, string (param) given.'
107
		);
108
	}
109
110
	/**
111
	 * @param mixed $requestData
112
	 * @param mixed $responseData
113
	 * @param string $error
114
	 */
115 View Code Duplication
	private function checkException($requestData, $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...
116
	{
117
		try {
118
			$request = $this->getRequestMock($requestData);
119
			$method = new CreateReservation(function () use ($responseData) {
120
				return $responseData;
121
			});
122
			$method->call($request);
123
			$this->fail('Expected exception to be thrown');
124
125
		} catch (MethodException $e) {
126
			$this->assertContains($error, $e->getMessage());
127
		}
128
	}
129
130
}
131