|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ntb\RestAPI; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Rest test class can work as base class for your functional tests. It provides some helpful methods to test your rest |
|
7
|
|
|
* api more easily. |
|
8
|
|
|
*/ |
|
9
|
|
|
abstract class RestTest extends \SapphireTest { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The namespace of your api. |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $namespace = 'v/1'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The route to the session without the namespace. |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $sessionRoute = 'sessions'; |
|
22
|
|
|
|
|
23
|
|
|
public function setUp() { |
|
24
|
|
|
parent::setUp(); |
|
25
|
|
|
// clear cache |
|
26
|
|
|
\SS_Cache::factory('rest_cache')->clean(\Zend_Cache::CLEANING_MODE_ALL); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Perform an api request with the given options |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $path the request path; can consist of resource name, identifier and GET params |
|
34
|
|
|
* @param array $options |
|
35
|
|
|
* * string `body` the data |
|
36
|
|
|
* * int `code` the expected response code |
|
37
|
|
|
* * string `method` the http method |
|
38
|
|
|
* * ApiSession `session` the test session |
|
39
|
|
|
* * string `token` the auth token |
|
40
|
|
|
* * array `postVars` the post data, eg. multi form or files |
|
41
|
|
|
* @return array |
|
42
|
|
|
* @throws \SS_HTTPResponse_Exception |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function makeApiRequest($path, $options=[]) { |
|
45
|
|
|
$settings = array_merge([ |
|
46
|
|
|
'session' => null, |
|
47
|
|
|
'token' => null, |
|
48
|
|
|
'method' => 'GET', |
|
49
|
|
|
'body' => null, |
|
50
|
|
|
'postVars' => null, |
|
51
|
|
|
'code' => 200 |
|
52
|
|
|
], $options); |
|
53
|
|
|
$headers = [ |
|
54
|
|
|
'Accept' => 'application/json' |
|
55
|
|
|
]; |
|
56
|
|
|
if($settings['token']) { |
|
57
|
|
|
$headers['Authorization'] = "Bearer {$settings['token']}"; |
|
58
|
|
|
} |
|
59
|
|
|
$response = \Director::test( |
|
60
|
|
|
\Controller::join_links($this->namespace, $path), |
|
61
|
|
|
$settings['postVars'], |
|
62
|
|
|
$settings['session'], |
|
|
|
|
|
|
63
|
|
|
$settings['method'], |
|
64
|
|
|
$settings['body'], |
|
65
|
|
|
$headers |
|
66
|
|
|
); |
|
67
|
|
|
$this->assertEquals($settings['code'], $response->getStatusCode(), "Wrong status code: {$response->getBody()}"); |
|
|
|
|
|
|
68
|
|
|
return json_decode($response->getBody(), true); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Creates a session for the api. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $email the email of the user |
|
75
|
|
|
* @param string $password the password for the user |
|
76
|
|
|
* @return array the current session with `token` |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function createSession($email='[email protected]', $password='password') { |
|
79
|
|
|
$data = [ |
|
80
|
|
|
'email' => $email, |
|
81
|
|
|
'password' => $password |
|
82
|
|
|
]; |
|
83
|
|
|
$dataString = json_encode($data); |
|
84
|
|
|
$result = $this->makeApiRequest('SessionRoute', ['body' => $dataString, 'method' => 'POST']); |
|
85
|
|
|
return $result['session']; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
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: