Passed
Push — master ( 327bdc...4cb164 )
by Jan
02:33
created

BashRestTest::testParsedBodyObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace Tests\Functional;
4
5
use InvalidArgumentException;
6
use Slim\Http\RequestBody;
7
8
9
class BashRestTest extends AbstractTestCase
10
{
11
12
	public static function setUpBeforeClass()
13
	{
14
		self::setSecurityHeader('X-Secret');
15
	}
16
17
18
	public function testAction1()
19
	{
20
		self::setPath('/bash-rest/test-app/action1');
21
		$this->simpleTest('', [] , 'test');
22
	}
23
24
25
	public function testAction2()
26
	{
27
		self::setPath('/bash-rest/test-app/action2');
28
		$this->simpleTest('[]', [] , [
0 ignored issues
show
Documentation introduced by
array('cwd' => 'dir', 'test1', 'test2') is of type array<string|integer,str..."string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
29
			'cwd' => 'dir',
30
			'test1',
31
			'test2'
32
		]);
33
	}
34
35
36
	public function testAction2Env()
37
	{
38
		self::setPath('/bash-rest/test-app/action2');
39
		$input = [
40
			'test' => 'a',
41
			'down' => [
42
				'test' => 'b',
43
				'more' => [
44
					'test' => 3
45
				]
46
			],
47
			'array' => ['a', 'b', 'c'],
48
			'last' => 'zxc'
49
		];
50
51
		$this->simpleTest(json_encode($input), [
52
			'HOOK_test' => 'a',
53
			'HOOK_down_test' => 'b',
54
			'HOOK_down_more_test' => 3,
55
			'HOOK_array_0' => 'a',
56
			'HOOK_array_1' => 'b',
57
			'HOOK_array_2' => 'c',
58
			'HOOK_last' => 'zxc'
59
		] , [
0 ignored issues
show
Documentation introduced by
array('cwd' => 'dir', 'test1', 'test2') is of type array<string|integer,str..."string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
			'cwd' => 'dir',
61
			'test1',
62
			'test2'
63
		]);
64
	}
65
66
67
	public function testUnsecured()
68
	{
69
		self::setPath('/bash-rest/test-app/noAction');
70
		$response = $this->runUnsecured();
71
72
		$this->assertEquals(403, $response->getStatusCode());
73
	}
74
75
76
	public function testNotHandled()
77
	{
78
		self::setPath('/bash-rest/test-app/noAction');
79
		$response = $this->runAppMocked('[]', [], NULL);
80
81
		$this->assertEquals(404, $response->getStatusCode());
82
	}
83
84
85
	public function testParsedBodyObject()
86
	{
87
		$this->setExpectedExceptionRegExp(InvalidArgumentException::class, '#Unexpected parser result.#');
88
89
		$body = new RequestBody();
90
		$body->write(json_encode(['data' => 'abc']));
91
92
		self::setPath('/bash-rest/test-app/action1');
93
		$request = $this->prepareRequest(self::SECRET)
94
			->withBody($body);
95
96
		$request->registerMediaTypeParser('application/json', function ($input) {
97
				return json_decode($input);
98
			});
99
100
		$response  = $this->runRequest($request, $this->buildApp());
101
		$this->assertEquals(200, $response->getStatusCode());
102
	}
103
104
105
	/**
106
	 * @param string $data
107
	 * @param array $env
108
	 * @param string $command
109
	 */
110
	protected function simpleTest($data, array $env, $command)
111
	{
112
		$response = $this->runAppMocked($data, $env, $command);
113
		$this->assertEquals(200, $response->getStatusCode());
114
		$responseBody = (string)$response->getBody();
115
		$this->assertEquals(
116
			[
117
				'result' => $env
118
			],
119
			json_decode($responseBody, TRUE)
120
		);
121
	}
122
123
}
124