Issues (9)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/JsonTest.php (9 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace BrofistTest\ApiClient;
4
5
use Brofist\ApiClient\Exception;
6
use Brofist\ApiClient\Json;
7
use Brofist\ApiClient\JsonInterface;
8
use GuzzleHttp\Client as HttpClient;
9
use GuzzleHttp\Exception\RequestException;
10
use PHPUnit_Framework_TestCase;
11
use Psr\Http\Message\RequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
14
/**
15
 * @SuppressWarnings(PHPMD)
16
 */
17
class JsonTest extends PHPUnit_Framework_TestCase
18
{
19
    /** @var Json */
20
    protected $client;
21
22
    /** @var HttpClient | \Prophecy\Prophecy\ObjectProphecy */
23
    protected $mockClient;
24
25
    /**
26
     * @before
27
     */
28
    public function initialize()
29
    {
30
        $this->setClient();
31
    }
32
33
    /**
34
     * @test
35
     * @expectedException \InvalidArgumentException
36
     * @expectedExceptionMessage Endpoint not set
37
     */
38
    public function throwsExceptionWhenNoEndpointIsGiven()
39
    {
40
        new Json();
41
    }
42
43
    /**
44
     * @test
45
     */
46
    public function implementsTheCorrectInterface()
47
    {
48
        $this->assertInstanceOf(JsonInterface::class, $this->client);
49
    }
50
51
    /**
52
     * @test
53
     */
54 View Code Duplication
    public function canResolvePathWithNonLeadingSlash()
0 ignored issues
show
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...
55
    {
56
        $query = ['foo' => 'bar'];
57
58
        $this->mockClient()
59
            ->request(
60
                'GET',
61
                $this->url('/foo'),
62
                ['query' => $query]
63
            )
64
            ->willReturn($this->fooBarResponse());
65
66
        $data = $this->client->get('foo', $query);
67
68
        $this->assertFooBarResponse($data);
69
    }
70
71
    /**
72
     * @test
73
     */
74
    public function canMutateEndpoint()
75
    {
76
        $this->client = new Json(['endpoint' => 'https://test.foo.bar/v3/']);
77
        $this->assertEquals('https://test.foo.bar/v3', $this->client->getEndpoint());
78
    }
79
80
    /**
81
     * @test
82
     */
83 View Code Duplication
    public function canMakeGetRequests()
0 ignored issues
show
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...
84
    {
85
        $query = ['foo' => 'bar'];
86
87
        $this->mockClient()->request(
88
            'GET',
89
            $this->url('/foo'),
90
            [
91
                'query' => $query,
92
            ]
93
        )->willReturn($this->fooBarResponse());
94
95
        $data = $this->client->get('/foo', $query);
96
97
        $this->assertFooBarResponse($data);
98
    }
99
100
    /**
101
     * @test
102
     */
103 View Code Duplication
    public function canMakeGetRequestsWithAuthentication()
0 ignored issues
show
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...
104
    {
105
        $this->setClient(['authToken' => 'myToken']);
106
107
        $query = ['foo' => 'bar'];
108
109
        $this->mockClient()->request(
110
            'GET',
111
            $this->url('/foo'),
112
            [
113
                'auth'  => ['myToken', ''],
114
                'query' => $query,
115
            ]
116
        )->willReturn($this->fooBarResponse());
117
118
        $data = $this->client->get('/foo', $query);
119
120
        $this->assertFooBarResponse($data);
121
    }
122
123
    /**
124
     * @test
125
     */
126 View Code Duplication
    public function canMakeGetRequestsWithBasicAuthentication()
0 ignored issues
show
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...
127
    {
128
        $this->setClient(['basicAuth' => ['username', 'password']]);
129
130
        $query = ['foo' => 'bar'];
131
132
        $this->mockClient()->request(
133
            'GET',
134
            $this->url('/foo'),
135
            [
136
                'auth'  => ['username', 'password'],
137
                'query' => $query,
138
            ]
139
        )->willReturn($this->fooBarResponse());
140
141
        $data = $this->client->get('/foo', $query);
142
143
        $this->assertFooBarResponse($data);
144
    }
145
146
    /**
147
     * @test
148
     */
149 View Code Duplication
    public function canMakePostRequests()
0 ignored issues
show
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...
150
    {
151
        $postData = ['foo' => 'bar'];
152
153
        $this->mockClient()->request(
154
            'POST',
155
            $this->url('/foo'),
156
            [
157
                'json' => $postData,
158
            ]
159
        )->willReturn($this->fooBarResponse());
160
161
        $data = $this->client->post('/foo', $postData);
162
163
        $this->assertFooBarResponse($data);
164
    }
165
166
    /**
167
     * @test
168
     */
169 View Code Duplication
    public function canMakePutRequest()
0 ignored issues
show
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...
170
    {
171
        $putData = ['foo' => 'bar'];
172
173
        $this->mockClient()->request(
174
            'PUT',
175
            $this->url('/foo'),
176
            [
177
                'json' => $putData,
178
            ]
179
        )->willReturn($this->fooBarResponse());
180
181
        $data = $this->client->put('/foo', $putData);
182
183
        $this->assertFooBarResponse($data);
184
    }
185
186
    /**
187
     * @test
188
     * @expectedException \BadMethodCallException
189
     */
190
    public function deleteIsNotImplemented()
191
    {
192
        $this->client->delete('endpoint');
193
    }
194
195
    /**
196
     * @test
197
     * @expectedException \BadMethodCallException
198
     */
199
    public function patchIsNotImplemented()
200
    {
201
        $this->client->patch('endpoint');
202
    }
203
204
    /**
205
     * @test
206
     */
207
    public function convertsGuzzleResponseExceptionsIntoFriendlyClientExceptions()
208
    {
209
        $request = $this->prophesize(RequestInterface::class);
210
        $response = $this->getMockResponse(['message' => 'theMessage']);
211
        $response->getStatusCode()->willReturn(200);
212
213
        $originalException = new RequestException(
214
            'exception message',
215
            $request->reveal(),
216
            $response->reveal()
217
        );
218
219
        $this->mockClient()
220
            ->request('GET', $this->url('/foo'), ['query' => []])
221
            ->willThrow($originalException);
222
223
        try {
224
            $this->client->get('/foo');
225
            $this->fail('Should have thrown exception');
226
        } catch (\Exception $e) {
227
            $this->assertInstanceOf(Exception::class, $e);
228
            $this->assertEquals('theMessage', $e->getMessage());
229
            $this->assertSame($originalException, $e->getPrevious());
230
        }
231
    }
232
233
    /**
234
     * @test
235
     * @expectedException \Brofist\ApiClient\InvalidJsonResponseBodyException
236
     */
237
    public function throwsInvalidJsonResponse()
238
    {
239
        $this->mockClient()->request('GET', $this->url('/foo'), ['query' => []])
240
            ->willReturn($this->mockResponseBody('invalid')->reveal());
241
242
        $this->client->get('/foo');
243
    }
244
245
    /**
246
     * @test
247
     */
248 View Code Duplication
    public function passesOnHeaders()
0 ignored issues
show
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...
249
    {
250
        $options = [
251
            'headers' => ['Content-Type' => 'application/json; charset=utf-8'],
252
            'query'   => [],
253
        ];
254
        $this->mockClient()->request('GET', $this->url('/foo'), $options)
255
            ->willReturn($this->fooBarResponse());
256
257
        $data = $this->client->get('/foo', [], ['headers' => ['Content-Type' => 'application/json; charset=utf-8']]);
258
259
        $this->assertFooBarResponse($data);
260
    }
261
262
    /**
263
     * @return \Prophecy\Prophecy\ObjectProphecy
264
     */
265
    private function mockClient()
266
    {
267
        if ($this->mockClient === null) {
268
            $this->mockClient = $this->prophesize(HttpClient::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\GuzzleHttp\Client::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<GuzzleHttp\Client> of property $mockClient.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
269
        }
270
271
        return $this->mockClient;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->mockClient; of type Prophecy\Prophecy\ObjectProphecy|GuzzleHttp\Client adds the type GuzzleHttp\Client to the return on line 271 which is incompatible with the return type documented by BrofistTest\ApiClient\JsonTest::mockClient of type Prophecy\Prophecy\ObjectProphecy.
Loading history...
272
    }
273
274
    /**
275
     * @param string $path
276
     *
277
     * @return string
278
     */
279
    private function url($path = '/')
280
    {
281
        return 'https://endpoint/v1' . $path;
282
    }
283
284
    private function fooBarResponse()
285
    {
286
        return $this->mockResponseBody('{"foo":"bar"}')->reveal();
287
    }
288
289
    private function mockResponseBody($responseBody)
290
    {
291
        $response = $this->prophesize(ResponseInterface::class);
292
        $response->getBody()->willReturn($responseBody);
293
294
        return $response;
295
    }
296
297
    private function getMockResponse(array $data = [])
298
    {
299
        return $this->mockResponseBody(json_encode($data));
300
    }
301
302
    /**
303
     * @param array $data
304
     */
305
    private function assertFooBarResponse($data)
306
    {
307
        $this->assertEquals(['foo' => 'bar'], $data);
308
    }
309
310
    private function setClient(array $params = [])
311
    {
312
        $default = [
313
            'endpoint'   => 'https://endpoint/v1/',
314
            'httpClient' => $this->mockClient()->reveal(),
315
        ];
316
        $this->client = new Json(array_merge($default, $params));
317
    }
318
}
319