Passed
Push — master ( e499e9...54948a )
by Bukashk0zzz
01:19 queued 13s
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
 * @covers \AtlassianConnectBundle\Service\GuzzleJWTMiddleware
18
 */
19
final class GuzzleJWTMiddlewareTest extends TestCase
20
{
21
    /**
22
     * Test if authorization header is set when using auth tokens
23
     */
24
    public function testAuthTokenMiddleware(): void
25
    {
26
        $middleware = GuzzleJWTMiddleware::authTokenMiddleware('atlassian-connect', 'secret');
27
28
        $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

28
        $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...
29
            $this->assertTrue($request->hasHeader('Authorization'));
30
            $this->assertTrue($request->hasHeader('existing-header'));
31
            $this->assertSame('GET', $request->getMethod());
32
            $this->assertEquals(new Uri('https://atlassian.io/api/test'), $request->getUri());
33
        });
34
35
        $request = new Request('GET', 'https://atlassian.io/api/test', [
36
            'existing-header' => 'existing-value',
37
        ]);
38
39
        $invokable($request, []);
40
    }
41
42
    /**
43
     * Test if authorization and accept headers are set with user auth middleware
44
     */
45
    public function testAuthUserTokenMiddleware(): void
46
    {
47
        $mock = new MockHandler([
48
            new Response(200, [], \json_encode(['access_token' => 'token'])),
49
        ]);
50
        $client = new Client(['handler' => HandlerStack::create($mock)]);
51
52
        $middleware = GuzzleJWTMiddleware::authUserTokenMiddleware(
53
            $client,
54
            'oathClientId',
55
            'secret',
56
            'https://atlassian.io',
57
            'username'
58
        );
59
60
        $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

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