AbstractTestCase   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 195
Duplicated Lines 7.18 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 10
Bugs 3 Features 3
Metric Value
wmc 16
c 10
b 3
f 3
lcom 1
cbo 8
dl 14
loc 195
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setPath() 0 4 1
A setSecurityHeader() 0 4 1
A runAppMocked() 0 5 1
A runApp() 0 5 1
A runInvalid() 0 5 1
A prepareEnvironment() 0 14 2
A runUnsecured() 7 7 1
A runNotHandled() 7 7 1
B assertCommandEnvironment() 0 27 2
A runRequest() 0 14 1
A buildApp() 0 18 2
A prepareRequest() 0 8 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 Tests\Functional;
4
5
use App\Executor;
6
use PHPUnit_Framework_MockObject_Stub_ReturnCallback;
7
use Psr\Http\Message\ResponseInterface;
8
use Slim\App;
9
use Slim\Http\Request;
10
use Slim\Http\RequestBody;
11
use Slim\Http\Response;
12
use Slim\Http\Environment;
13
14
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
15
{
16
	private static $path;
17
	private static $securityHeader;
18
19
	const SECRET = 'alksjdljzcxl';
20
21
22
	/**
23
	 * @param string $path
24
	 */
25
	public static function setPath($path)
26
	{
27
		self::$path = $path;
28
	}
29
30
31
	/**
32
	 * @param string $securityHeader
33
	 */
34
	public static function setSecurityHeader($securityHeader)
35
	{
36
		self::$securityHeader = $securityHeader;
37
	}
38
39
40
	/**
41
	 * @return Response
42
	 */
43
	final protected function runAppMocked($requestData, array $values, $command)
44
    {
45
		$request = $this->prepareRequest(self::SECRET, $requestData);
46
		return $this->assertCommandEnvironment($request, $values, $command);
47
	}
48
49
50
	/**
51
	 * @return Response
52
	 */
53
	final protected function runApp($requestData, array $settings = [])
54
	{
55
		$request = $this->prepareRequest(self::SECRET, $requestData);
56
		return $this->runRequest($request, $this->buildApp($settings));
57
	}
58
59
60
	/**
61
	 * @return Response
62
	 */
63
	final protected function runInvalid()
64
	{
65
		$request = $this->prepareRequest(self::SECRET, []);
66
		return $this->runRequest($request, $this->buildApp());
67
	}
68
69
70
	/**
71
	 * @return Response
72
	 */
73 View Code Duplication
	final protected function runUnsecured()
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...
74
	{
75
		$request = $this->prepareRequest(NULL, [
76
			'object_kind' => 'push'
77
		]);
78
		return $this->runRequest($request, $this->buildApp());
79
	}
80
81
82
	/**
83
	 * @return Response
84
	 */
85 View Code Duplication
	final protected function runNotHandled()
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...
86
	{
87
		$request = $this->prepareRequest(self::SECRET, [
88
			'object_kind' => 'test'
89
		]);
90
		return $this->runRequest($request, $this->buildApp());
91
	}
92
93
94
	/**
95
	 * @param Request $request
96
	 * @param array $values
97
	 * @param string $command
98
	 * @return ResponseInterface|Response
99
	 */
100
	final protected function assertCommandEnvironment(Request $request, array $values, $command)
101
	{
102
		$app = $this->buildApp();
103
104
		$app->getContainer()[Executor::class] = function () use ($values, $command) {
105
			$mock = $this->getMockBuilder(Executor::class)
106
				->setMethods(['executeCommand'])
107
				->getMock();
108
109
			if ($command !== NULL) {
110
				$mock->expects($this->once())
111
					->method('executeCommand')
112
					->with($this->equalTo($command), $this->equalTo($values))
113
					->will(new PHPUnit_Framework_MockObject_Stub_ReturnCallback(function ($command, $values) {
114
						asort($values);
115
						return json_encode($values);
116
					}));
117
			} else {
118
				$mock->expects($this->never())
119
					->method('executeCommand');
120
			}
121
122
			return $mock;
123
		};
124
125
		return $this->runRequest($request, $app);
126
	}
127
128
129
	/**
130
	 * @param Request $request
131
	 * @param App $app
132
	 * @return Response
133
	 */
134
	final protected function runRequest(Request $request, App $app)
135
	{
136
		// Register routes
137
		require __DIR__ . '/../../src/routes.php';
138
139
		// Set up a response object
140
		$response = new Response();
141
142
		// Process the application
143
		$response = $app->process($request, $response);
144
145
		// Return the response
146
		return $response;
147
	}
148
149
150
	/**
151
	 * @param array $settingsOverride
152
	 * @return App
153
	 */
154
	final protected function buildApp(array $settingsOverride = [])
155
	{
156
		// Use the application settings
157
		if ( ! defined('CONFIG_DIR')) {
158
			define('CONFIG_DIR', __DIR__ . '/config');
159
		}
160
		$settings = require __DIR__ . '/../../src/settings.php';
161
		$settings = array_replace_recursive($settings, $settingsOverride);
162
163
		// Instantiate the application
164
		$app = new App($settings);
165
		unset($app->getContainer()['errorHandler']);
166
167
		// Set up dependencies
168
		require __DIR__ . '/../../src/dependencies.php';
169
170
		return $app;
171
	}
172
173
174
	/**
175
	 * @param string|NULL $secret
176
	 * @param array|string $data
177
	 * @return Request
178
	 */
179
	protected function prepareRequest($secret = NULL, $data = '')
180
	{
181
		$body = new RequestBody();
182
		$body->write(is_array($data) ? json_encode($data) : $data);
183
184
		return Request::createFromEnvironment($this->prepareEnvironment($secret))
185
			->withBody($body);
186
	}
187
188
189
	/**
190
	 * @param string|NULL $secret
191
	 * @return Environment
192
	 */
193
	private function prepareEnvironment($secret = NULL)
194
	{
195
		$data = [
196
			'REQUEST_METHOD' => 'POST',
197
			'REQUEST_URI' => self::$path,
198
			'HTTP_CONTENT_TYPE' => 'application/json'
199
		];
200
201
		if ($secret !== NULL) {
202
			$data['HTTP_' . self::$securityHeader] = $secret;
203
		}
204
205
		return Environment::mock($data);
206
	}
207
208
}
209