Passed
Pull Request — master (#49)
by Matthieu
17:00
created

GuzzleJWTMiddlewareTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testAuthTokenMiddleware() 0 16 1
A testAuthUserTokenMiddleware() 0 26 1
1
<?php declare(strict_types = 1);
2
3
namespace AtlassianConnectBundle\Tests\Service;
4
5
use AtlassianConnectBundle\Service\GuzzleJWTMiddleware;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Handler\MockHandler;
8
use GuzzleHttp\HandlerStack;
9
use GuzzleHttp\Psr7\Request;
10
use GuzzleHttp\Psr7\Response;
11
use GuzzleHttp\Psr7\Uri;
12
use PHPUnit\Framework\TestCase;
13
14
/**
15
 * Class GuzzleJWTMiddlewareTest
16
 */
17
final class GuzzleJWTMiddlewareTest extends TestCase
18
{
19
    /**
20
     * Test if authorization header is set when using auth tokens
21
     */
22
    public function testAuthTokenMiddleware(): void
23
    {
24
        $middleware = GuzzleJWTMiddleware::authTokenMiddleware('atlassian-connect', 'secret');
25
26
        $invokable = $middleware(function (Request $request, array $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

26
        $invokable = $middleware(function (Request $request, /** @scrutinizer ignore-unused */ array $options) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
27
            $this->assertTrue($request->hasHeader('Authorization'));
28
            $this->assertTrue($request->hasHeader('existing-header'));
29
            $this->assertSame('GET', $request->getMethod());
30
            $this->assertEquals(new Uri('https://atlassian.io/api/test'), $request->getUri());
31
        });
32
33
        $request = new Request('GET', 'https://atlassian.io/api/test', [
34
            'existing-header' => 'existing-value',
35
        ]);
36
37
        $invokable($request, []);
38
    }
39
40
    /**
41
     * Test if authorization and accept headers are set with user auth middleware
42
     */
43
    public function testAuthUserTokenMiddleware(): void
44
    {
45
        $mock = new MockHandler([
46
            new Response(200, [], \json_encode(['access_token' => 'token'])),
47
        ]);
48
        $client = new Client(['handler' => HandlerStack::create($mock)]);
49
50
        $middleware = GuzzleJWTMiddleware::authUserTokenMiddleware(
51
            $client,
52
            'oathClientId',
53
            'secret',
54
            'https://atlassian.io',
55
            'username'
56
        );
57
58
        $invokable = $middleware(function (Request $request, array $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
        $invokable = $middleware(function (Request $request, /** @scrutinizer ignore-unused */ array $options) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
            $this->assertSame('application/json', $request->getHeader('Accept')[0]);
60
            $this->assertSame('Bearer token', $request->getHeader('Authorization')[0]);
61
            $this->assertEquals(new Uri('https://atlassian.io/api/test'), $request->getUri());
62
        });
63
64
        $request = new Request('GET', 'https://atlassian.io/api/test', [
65
            'existing-header' => 'existing-value',
66
        ]);
67
68
        $invokable($request, []);
69
    }
70
}
71