Passed
Pull Request — master (#71)
by Matthieu
06:12 queued 02:17
created

testTenantNotSetUpForOauth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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
    /**
17
     * @var MockObject|HttpClientInterface
18
     */
19
    private $inner;
20
    private AuthenticatedAtlassianClient $client;
21
22
    protected function setUp(): void
23
    {
24
        $this->inner = $this->createMock(HttpClientInterface::class);
25
        $this->client = new AuthenticatedAtlassianClient($this->inner);
26
    }
27
28
    public function testTenantAuthorization(): void
29
    {
30
        $this->inner
31
            ->expects($this->once())
32
            ->method('request')
33
            ->with('GET', 'https://app.atlassian.com/resource', $this->callback(function (array $options): bool {
34
                $this->assertArrayHasKey('Authorization', $options['headers']);
35
                $this->assertStringStartsWith('JWT', $options['headers']['Authorization']);
36
37
                return true;
38
            }));
39
40
        $this->client->request('GET', 'https://app.atlassian.com/resource', ['tenant' => $this->getTenant(), 'user_id' => null]);
41
    }
42
43
    public function testWithUserId(): void
44
    {
45
        $response1 = $this->createMock(ResponseInterface::class);
46
        $response1->expects($this->once())
47
            ->method('toArray')
48
            ->willReturn(['access_token' => 'token']);
49
        $tenant = $this->getTenant();
50
        $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

50
        $tenant->/** @scrutinizer ignore-call */ 
51
                 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...
51
52
        $this->inner
53
            ->expects($this->exactly(2))
54
            ->method('request')
55
            ->withConsecutive(
56
                ['POST', 'https://oauth-2-authorization-server.services.atlassian.com/oauth2/token', $this->anything()],
57
                ['GET', 'https://app.atlassian.com/resource', $this->callback(function (array $options): bool {
58
                    $this->assertEquals('Bearer token', $options['headers']['Authorization']);
59
60
                    return true;
61
                })]
62
            )
63
            ->willReturnOnConsecutiveCalls($response1, $this->createMock(ResponseInterface::class));
64
65
        $this->client->request('GET', 'https://app.atlassian.com/resource', ['tenant' => $tenant, 'user_id' => 'user_id']);
66
    }
67
68
    public function testTenantNotSetUpForOauth(): void
69
    {
70
        $this->expectException(\RuntimeException::class);
71
        $this->expectExceptionMessage('Tenant is not set up as oath application. Install the app with "ACT_AS_USER" scope.');
72
73
        $this->inner->expects($this->never())->method('request');
74
75
        $this->client->request('GET', 'https://app.atlassian.com/resource', ['tenant' => $this->getTenant(), 'user_id' => 'user_id']);
76
    }
77
78
    /**
79
     * @return TenantInterface|MockObject
80
     */
81
    private function getTenant(): TenantInterface
82
    {
83
        $tenant = $this->createMock(TenantInterface::class);
84
        $tenant->method('getBaseUrl')
85
            ->willReturn('https://app.atlassian.com');
86
87
        $tenant->method('getAddonKey')
88
            ->willReturn('addon-key');
89
90
        $tenant
91
            ->method('getSharedSecret')
92
            ->willReturn('shared-secret');
93
94
        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...
95
    }
96
}
97