GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SystemMessagesTest::testAddSystemMessage()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Server\Tests\Api;
11
12
use DateTime;
13
use PDO;
14
use PHPUnit_Framework_TestCase;
15
use SURFnet\VPN\Common\Http\BasicAuthenticationHook;
16
use SURFnet\VPN\Common\Http\Request;
17
use SURFnet\VPN\Common\Http\Service;
18
use SURFnet\VPN\Server\Api\SystemMessagesModule;
19
use SURFnet\VPN\Server\Storage;
20
21
class SystemMessagesTest extends PHPUnit_Framework_TestCase
22
{
23
    /** @var \SURFnet\VPN\Common\Http\Service */
24
    private $service;
25
26
    public function setUp()
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
27
    {
28
        $storage = new Storage(
29
            new PDO(
30
                $GLOBALS['DB_DSN'],
31
                $GLOBALS['DB_USER'],
32
                $GLOBALS['DB_PASSWD']
33
            ),
34
            new DateTime('2016-01-01 08:00:00')
35
        );
36
        $storage->init();
37
        $storage->addSystemMessage('motd', 'Hello World!');
38
39
        $this->service = new Service();
40
        $this->service->addModule(
41
            new SystemMessagesModule(
42
                $storage
43
            )
44
        );
45
46
        $bearerAuthentication = new BasicAuthenticationHook(
47
            [
48
                'vpn-admin-portal' => 'aabbcc',
49
            ]
50
        );
51
52
        $this->service->addBeforeHook('auth', $bearerAuthentication);
53
    }
54
55
    public function testGetSystemMessages()
56
    {
57
        $this->assertSame(
58
            [
59
                [
60
                    'id' => '1',
61
                    'message' => 'Hello World!',
62
                    'date_time' => '2016-01-01 08:00:00',
63
                ],
64
            ],
65
            $this->makeRequest(
66
                ['vpn-admin-portal', 'aabbcc'],
67
                'GET',
68
                'system_messages',
69
                ['message_type' => 'motd'],
70
                []
71
            )
72
        );
73
    }
74
75
    public function testAddSystemMessage()
76
    {
77
        $this->assertTrue(
78
            $this->makeRequest(
79
                ['vpn-admin-portal', 'aabbcc'],
80
                'POST',
81
                'add_system_message',
82
                [],
83
                ['message_type' => 'motd', 'message_body' => 'foo']
84
            )
85
        );
86
        $this->assertSame(
87
            [
88
                [
89
                    'id' => '1',
90
                    'message' => 'Hello World!',
91
                    'date_time' => '2016-01-01 08:00:00',
92
                ],
93
                [
94
                    'id' => '2',
95
                    'message' => 'foo',
96
                    'date_time' => '2016-01-01 08:00:00',
97
                ],
98
            ],
99
            $this->makeRequest(
100
                ['vpn-admin-portal', 'aabbcc'],
101
                'GET',
102
                'system_messages',
103
                ['message_type' => 'motd'],
104
                []
105
            )
106
        );
107
    }
108
109
    public function testDeleteSystemMessage()
110
    {
111
        $this->assertTrue(
112
            $this->makeRequest(
113
                ['vpn-admin-portal', 'aabbcc'],
114
                'POST',
115
                'delete_system_message',
116
                [],
117
                ['message_id' => 1]
118
            )
119
        );
120
    }
121
122 View Code Duplication
    private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = [])
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...
123
    {
124
        $response = $this->service->run(
125
            new Request(
126
                [
127
                    'SERVER_PORT' => 80,
128
                    'SERVER_NAME' => 'vpn.example',
129
                    'REQUEST_METHOD' => $requestMethod,
130
                    'SCRIPT_NAME' => '/index.php',
131
                    'REQUEST_URI' => sprintf('/%s', $pathInfo),
132
                    'PHP_AUTH_USER' => $basicAuth[0],
133
                    'PHP_AUTH_PW' => $basicAuth[1],
134
                ],
135
                $getData,
136
                $postData
137
            )
138
        );
139
140
        $responseArray = json_decode($response->getBody(), true)[$pathInfo];
141
        if ($responseArray['ok']) {
142
            if (array_key_exists('data', $responseArray)) {
143
                return $responseArray['data'];
144
            }
145
146
            return true;
147
        }
148
149
        // in case of errors...
150
        return $responseArray;
151
    }
152
}
153