AuthenticatedAtlassianClientTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 78
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testTenantAuthorization() 0 13 1
A getTenant() 0 14 1
A setUp() 0 4 1
A testTenantNotSetUpForOauth() 0 8 1
A testWithUserId() 0 23 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AtlassianConnectBundle\Tests\Service;
6
7
use AtlassianConnectBundle\Entity\TenantInterface;
8
use AtlassianConnectBundle\Service\AuthenticatedAtlassianClient;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Contracts\HttpClient\HttpClientInterface;
12
use Symfony\Contracts\HttpClient\ResponseInterface;
13
14
final class AuthenticatedAtlassianClientTest extends TestCase
15
{
16
    private HttpClientInterface|MockObject $inner;
17
    private AuthenticatedAtlassianClient $client;
18
19
    protected function setUp(): void
20
    {
21
        $this->inner = $this->createMock(HttpClientInterface::class);
22
        $this->client = new AuthenticatedAtlassianClient($this->inner);
23
    }
24
25
    public function testTenantAuthorization(): void
26
    {
27
        $this->inner
28
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Symfony\Contracts\HttpClient\HttpClientInterface. ( Ignorable by Annotation )

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

28
            ->/** @scrutinizer ignore-call */ 
29
              expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
            ->method('request')
30
            ->with('GET', 'https://app.atlassian.com/resource', $this->callback(function (array $options): bool {
31
                $this->assertArrayHasKey('Authorization', $options['headers']);
32
                $this->assertStringStartsWith('JWT', $options['headers']['Authorization']);
33
34
                return true;
35
            }));
36
37
        $this->client->request('GET', 'https://app.atlassian.com/resource', ['tenant' => $this->getTenant(), 'user_id' => null]);
38
    }
39
40
    public function testWithUserId(): void
41
    {
42
        $response1 = $this->createMock(ResponseInterface::class);
43
        $response1->expects($this->once())
44
            ->method('toArray')
45
            ->willReturn(['access_token' => 'token']);
46
        $tenant = $this->getTenant();
47
        $tenant->method('getOauthClientId')->willReturn('oauth');
0 ignored issues
show
Bug introduced by
The method method() does not exist on AtlassianConnectBundle\Entity\TenantInterface. ( Ignorable by Annotation )

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

47
        $tenant->/** @scrutinizer ignore-call */ 
48
                 method('getOauthClientId')->willReturn('oauth');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
49
        $this->inner
50
            ->expects($this->exactly(2))
51
            ->method('request')
52
            ->withConsecutive(
53
                ['POST', 'https://oauth-2-authorization-server.services.atlassian.com/oauth2/token', $this->anything()],
54
                ['GET', 'https://app.atlassian.com/resource', $this->callback(function (array $options): bool {
55
                    $this->assertEquals('Bearer token', $options['headers']['Authorization']);
56
57
                    return true;
58
                })]
59
            )
60
            ->willReturnOnConsecutiveCalls($response1, $this->createMock(ResponseInterface::class));
61
62
        $this->client->request('GET', 'https://app.atlassian.com/resource', ['tenant' => $tenant, 'user_id' => 'user_id']);
63
    }
64
65
    public function testTenantNotSetUpForOauth(): void
66
    {
67
        $this->expectException(\RuntimeException::class);
68
        $this->expectExceptionMessage('Tenant is not set up as oath application. Install the app with "ACT_AS_USER" scope.');
69
70
        $this->inner->expects($this->never())->method('request');
71
72
        $this->client->request('GET', 'https://app.atlassian.com/resource', ['tenant' => $this->getTenant(), 'user_id' => 'user_id']);
73
    }
74
75
    /**
76
     * @return TenantInterface|MockObject
77
     */
78
    private function getTenant(): TenantInterface
79
    {
80
        $tenant = $this->createMock(TenantInterface::class);
81
        $tenant->method('getBaseUrl')
82
            ->willReturn('https://app.atlassian.com');
83
84
        $tenant->method('getAddonKey')
85
            ->willReturn('addon-key');
86
87
        $tenant
88
            ->method('getSharedSecret')
89
            ->willReturn('shared-secret');
90
91
        return $tenant;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $tenant returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return AtlassianConnectBundle\Entity\TenantInterface.
Loading history...
92
    }
93
}
94