RestTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 79
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A makeApiRequest() 0 26 2
A createSession() 0 9 1
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'],
0 ignored issues
show
Documentation introduced by
$settings['session'] is of type null, but the function expects a object<Session>|array.

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...
63
            $settings['method'],
64
            $settings['body'],
65
            $headers
66
        );
67
        $this->assertEquals($settings['code'], $response->getStatusCode(), "Wrong status code: {$response->getBody()}");
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<Ntb\RestAPI\RestTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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