Passed
Push — master ( c65b3c...fcdaef )
by Jan
03:18
created

AbstractTestCase::runInvalid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 19
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 19
loc 19
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
1
<?php
2
3
namespace Tests\Functional;
4
5
use App\Executor;
6
use Interop\Container\ContainerInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Slim\App;
9
use Slim\Http\Request;
10
use Slim\Http\Response;
11
use Slim\Http\Environment;
12
13
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
14
{
15
16
	final protected function runApp($requestData = null, array $values, $command)
17
    {
18
        $environment = Environment::mock(
19
            [
20
                'REQUEST_METHOD' => 'POST',
21
                'REQUEST_URI' => '/',
22
				'HTTP_CONTENT_TYPE' => 'application/json',
23
				'HTTP_X-Gitlab-Token' => 'alksjdljzcxl'
24
            ]
25
        );
26
27
		$request = Request::createFromEnvironment($environment);
28
29
		if (isset($requestData)) {
30
			$request = $request->withParsedBody(json_decode($requestData, TRUE));
31
		}
32
33
		return $this->assertComandEnvironment($request, $values, $command);
34
35
	}
36
37
38 View Code Duplication
	final protected function runInvalid()
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...
39
	{
40
		$environment = Environment::mock(
41
			[
42
				'REQUEST_METHOD' => 'POST',
43
				'REQUEST_URI' => '/',
44
				'HTTP_CONTENT_TYPE' => 'application/json',
45
				'HTTP_X-Gitlab-Token' => 'alksjdljzcxl'
46
			]
47
		);
48
49
		$request = Request::createFromEnvironment($environment);
50
51
		if (isset($requestData)) {
0 ignored issues
show
Bug introduced by
The variable $requestData does not exist. Did you mean $request?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
52
			$request = $request->withParsedBody([]);
53
		}
54
55
		return $this->runRequest($request, $this->buildApp());
56
	}
57
58
59 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...
60
	{
61
		$environment = Environment::mock(
62
			[
63
				'REQUEST_METHOD' => 'POST',
64
				'REQUEST_URI' => '/',
65
				'HTTP_CONTENT_TYPE' => 'application/json',
66
			]
67
		);
68
69
		$request = Request::createFromEnvironment($environment);
70
71
		$request = $request->withParsedBody([
72
			'object_kind' => 'push'
73
		]);
74
75
		return $this->runRequest($request, $this->buildApp());
76
	}
77
78
79 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...
80
	{
81
		$environment = Environment::mock(
82
			[
83
				'REQUEST_METHOD' => 'POST',
84
				'REQUEST_URI' => '/',
85
				'HTTP_CONTENT_TYPE' => 'application/json',
86
				'HTTP_X-Gitlab-Token' => 'alksjdljzcxl'
87
			]
88
		);
89
90
		$request = Request::createFromEnvironment($environment);
91
92
		$request = $request->withParsedBody([
93
			'object_kind' => 'test'
94
		]);
95
96
		return $this->runRequest($request, $this->buildApp());
97
	}
98
99
100
	/**
101
	 * @param Request $request
102
	 * @param array $values
103
	 * @param string $command
104
	 * @return ResponseInterface|Response
105
	 */
106
	private function assertComandEnvironment(Request $request, array $values, $command)
107
	{
108
109
		$app = $this->buildApp();
110
111
		if ($command !== NULL) {
112
			$app->getContainer()[Executor::class] = function (ContainerInterface $c) use ($values, $command) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
				$mock = $this->getMockBuilder(Executor::class)
114
					->setMethods(['executeCommand'])
115
					->getMock();
116
117
				$mock->expects($this->once())
118
					->method('executeCommand')
119
					->with($this->equalTo($command), $this->equalTo($values));
120
121
				return $mock;
122
			};
123
		}
124
125
		return $this->runRequest($request, $app);
126
	}
127
128
129
	/**
130
	 * @return App
131
	 */
132
	private function buildApp()
133
	{
134
		// Use the application settings
135
		if ( ! defined('CONFIG_DIR')) {
136
			define('CONFIG_DIR', __DIR__ . '/config');
137
		}
138
		$settings = require __DIR__ . '/../../src/settings.php';
139
140
		// Instantiate the application
141
		$app = new App($settings);
142
143
		// Set up dependencies
144
		require __DIR__ . '/../../src/dependencies.php';
145
146
		return $app;
147
	}
148
149
150
	/**
151
	 * @param Request $request
152
	 * @param App $app
153
	 * @return Response
154
	 */
155
	private function runRequest(Request $request, App $app)
156
	{
157
		// Register routes
158
		require __DIR__ . '/../../src/routes.php';
159
160
		// Set up a response object
161
		$response = new Response();
162
163
		// Process the application
164
		$response = $app->process($request, $response);
165
166
		// Return the response
167
		return $response;
168
	}
169
}
170