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.
Completed
Push — master ( 932629...d7a7a8 )
by François
03:12
created

SystemMessagesTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace SURFnet\VPN\Server\Tests\Api;
20
21
use DateTime;
22
use PDO;
23
use PHPUnit_Framework_TestCase;
24
use SURFnet\VPN\Common\Http\BasicAuthenticationHook;
25
use SURFnet\VPN\Common\Http\Request;
26
use SURFnet\VPN\Common\Http\Service;
27
use SURFnet\VPN\Server\Api\SystemMessagesModule;
28
use SURFnet\VPN\Server\Storage;
29
30
class SystemMessagesTest extends PHPUnit_Framework_TestCase
31
{
32
    /** @var \SURFnet\VPN\Common\Http\Service */
33
    private $service;
34
35
    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...
36
    {
37
        $storage = new Storage(
38
            new PDO(
39
                $GLOBALS['DB_DSN'],
40
                $GLOBALS['DB_USER'],
41
                $GLOBALS['DB_PASSWD']
42
            ),
43
            new DateTime('2016-01-01 08:00:00')
44
        );
45
        $storage->init();
46
        $storage->addSystemMessage('motd', 'Hello World!');
47
48
        $this->service = new Service();
49
        $this->service->addModule(
50
            new SystemMessagesModule(
51
                $storage
52
            )
53
        );
54
55
        $bearerAuthentication = new BasicAuthenticationHook(
56
            [
57
                'vpn-admin-portal' => 'aabbcc',
58
            ]
59
        );
60
61
        $this->service->addBeforeHook('auth', $bearerAuthentication);
62
    }
63
64
    public function testGetSystemMessages()
65
    {
66
        $this->assertSame(
67
            [
68
                [
69
                    'id' => '1',
70
                    'message' => 'Hello World!',
71
                    'date_time' => '2016-01-01 08:00:00',
72
                ],
73
            ],
74
            $this->makeRequest(
75
                ['vpn-admin-portal', 'aabbcc'],
76
                'GET',
77
                'system_messages',
78
                ['message_type' => 'motd'],
79
                []
80
            )
81
        );
82
    }
83
84
    public function testAddSystemMessage()
85
    {
86
        $this->assertTrue(
87
            $this->makeRequest(
88
                ['vpn-admin-portal', 'aabbcc'],
89
                'POST',
90
                'add_system_message',
91
                [],
92
                ['message_type' => 'motd', 'message_body' => 'foo']
93
            )
94
        );
95
        $this->assertSame(
96
            [
97
                [
98
                    'id' => '1',
99
                    'message' => 'Hello World!',
100
                    'date_time' => '2016-01-01 08:00:00',
101
                ],
102
                [
103
                    'id' => '2',
104
                    'message' => 'foo',
105
                    'date_time' => '2016-01-01 08:00:00',
106
                ],
107
            ],
108
            $this->makeRequest(
109
                ['vpn-admin-portal', 'aabbcc'],
110
                'GET',
111
                'system_messages',
112
                ['message_type' => 'motd'],
113
                []
114
            )
115
        );
116
    }
117
118
    public function testDeleteSystemMessage()
119
    {
120
        $this->assertTrue(
121
            $this->makeRequest(
122
                ['vpn-admin-portal', 'aabbcc'],
123
                'POST',
124
                'delete_system_message',
125
                [],
126
                ['message_id' => 1]
127
            )
128
        );
129
    }
130
131 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...
132
    {
133
        $response = $this->service->run(
134
            new Request(
135
                [
136
                    'SERVER_PORT' => 80,
137
                    'SERVER_NAME' => 'vpn.example',
138
                    'REQUEST_METHOD' => $requestMethod,
139
                    'SCRIPT_NAME' => '/index.php',
140
                    'REQUEST_URI' => sprintf('/%s', $pathInfo),
141
                    'PHP_AUTH_USER' => $basicAuth[0],
142
                    'PHP_AUTH_PW' => $basicAuth[1],
143
                ],
144
                $getData,
145
                $postData
146
            )
147
        );
148
149
        $responseArray = json_decode($response->getBody(), true)[$pathInfo];
150
        if ($responseArray['ok']) {
151
            if (array_key_exists('data', $responseArray)) {
152
                return $responseArray['data'];
153
            }
154
155
            return true;
156
        }
157
158
        // in case of errors...
159
        return $responseArray;
160
    }
161
}
162