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('', ['HOOK_PROJECT_PATH' => 'bash-rest/test-app', 'HOOK_ACTION' => 'action1'] , 'test'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
public function testAction2() |
26
|
|
|
{ |
27
|
|
|
self::setPath('/bash-rest/test-app/action2'); |
28
|
|
|
$this->simpleTest('[]', ['HOOK_PROJECT_PATH' => 'bash-rest/test-app', 'HOOK_ACTION' => 'action2'] , [ |
|
|
|
|
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_PROJECT_PATH' => 'bash-rest/test-app', |
53
|
|
|
'HOOK_ACTION' => 'action2', |
54
|
|
|
'HOOK_test' => 'a', |
55
|
|
|
'HOOK_down_test' => 'b', |
56
|
|
|
'HOOK_down_more_test' => 3, |
57
|
|
|
'HOOK_array_0' => 'a', |
58
|
|
|
'HOOK_array_1' => 'b', |
59
|
|
|
'HOOK_array_2' => 'c', |
60
|
|
|
'HOOK_last' => 'zxc' |
61
|
|
|
] , [ |
|
|
|
|
62
|
|
|
'cwd' => 'dir', |
63
|
|
|
'test1', |
64
|
|
|
'test2' |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public function testUnsecured() |
70
|
|
|
{ |
71
|
|
|
self::setPath('/bash-rest/test-app/noAction'); |
72
|
|
|
$response = $this->runUnsecured(); |
73
|
|
|
|
74
|
|
|
$this->assertEquals(403, $response->getStatusCode()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
public function testNotHandled() |
79
|
|
|
{ |
80
|
|
|
self::setPath('/bash-rest/test-app/noAction'); |
81
|
|
|
$response = $this->runAppMocked('[]', [], NULL); |
82
|
|
|
|
83
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
public function testParsedBodyObject() |
88
|
|
|
{ |
89
|
|
|
$this->setExpectedExceptionRegExp(InvalidArgumentException::class, '#Unexpected parser result.#'); |
90
|
|
|
|
91
|
|
|
$body = new RequestBody(); |
92
|
|
|
$body->write(json_encode(['data' => 'abc'])); |
93
|
|
|
|
94
|
|
|
self::setPath('/bash-rest/test-app/action1'); |
95
|
|
|
$request = $this->prepareRequest(self::SECRET) |
96
|
|
|
->withBody($body); |
97
|
|
|
|
98
|
|
|
$request->registerMediaTypeParser('application/json', function ($input) { |
99
|
|
|
return json_decode($input); |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
$response = $this->runRequest($request, $this->buildApp()); |
103
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
View Code Duplication |
public function testExecutorPushTag() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
self::setPath('/groupName/projectName/deploy'); |
110
|
|
|
$result = shell_exec("bash -c \"echo abc\""); |
111
|
|
|
if ($result === "abc\n") { |
112
|
|
|
$response = $this->runApp('{"ENV":"production"}'); |
113
|
|
|
|
114
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
115
|
|
|
$this->assertEquals("Application groupName/projectName action deploy called with ENV set to production", (string)$response->getBody()); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
View Code Duplication |
public function testExecutorPushErrorTag() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
self::setPath('/groupName/projectName/error'); |
123
|
|
|
$result = shell_exec("bash -c \"echo abc\""); |
124
|
|
|
if ($result === "abc\n") { |
125
|
|
|
$response = $this->runApp('{"ENV":"production"}'); |
126
|
|
|
|
127
|
|
|
$this->assertEquals(500, $response->getStatusCode()); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $data |
134
|
|
|
* @param array $env |
135
|
|
|
* @param string $command |
136
|
|
|
*/ |
137
|
|
|
protected function simpleTest($data, array $env, $command) |
138
|
|
|
{ |
139
|
|
|
$response = $this->runAppMocked($data, $env, $command); |
140
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
141
|
|
|
$responseBody = (string)$response->getBody(); |
142
|
|
|
$this->assertEquals($env, json_decode($responseBody, TRUE)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
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: