AuthorizationServiceTest::testFetchTokenFromCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 53
nc 1
nop 0
dl 0
loc 65
rs 9.0254
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClientTest\Service;
6
7
use Facile\OpenIDClient\AuthMethod\AuthMethodFactoryInterface;
8
use Facile\OpenIDClient\AuthMethod\AuthMethodInterface;
9
use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient;
10
use Facile\OpenIDClient\Client\Metadata\ClientMetadataInterface;
11
use Facile\OpenIDClient\Issuer\IssuerInterface;
12
use Facile\OpenIDClient\Issuer\Metadata\IssuerMetadataInterface;
13
use Facile\OpenIDClient\Service\AuthorizationService;
14
use Facile\OpenIDClient\Token\IdTokenVerifierBuilderInterface;
15
use Facile\OpenIDClient\Token\TokenSetFactoryInterface;
16
use Facile\OpenIDClient\Token\TokenSetInterface;
17
use Facile\OpenIDClient\Token\TokenVerifierBuilderInterface;
18
use Facile\OpenIDClientTest\TestCase;
19
use Psr\Http\Client\ClientInterface;
20
use Psr\Http\Message\RequestFactoryInterface;
21
use Psr\Http\Message\RequestInterface;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\StreamInterface;
24
25
class AuthorizationServiceTest extends TestCase
26
{
27
    public function testGetAuthorizationUri(): void
28
    {
29
        $tokenSetFactory = $this->prophesize(TokenSetFactoryInterface::class);
30
        $client = $this->prophesize(ClientInterface::class);
31
        $requestFactory = $this->prophesize(RequestFactoryInterface::class);
32
        $idTokenVerifierBuilder = $this->prophesize(IdTokenVerifierBuilderInterface::class);
33
        $tokenVerifierBuilder = $this->prophesize(TokenVerifierBuilderInterface::class);
34
35
        $service = new AuthorizationService(
36
            $tokenSetFactory->reveal(),
37
            $client->reveal(),
38
            $requestFactory->reveal(),
39
            $idTokenVerifierBuilder->reveal(),
40
            $tokenVerifierBuilder->reveal()
41
        );
42
43
        $openIdClient = $this->prophesize(OpenIDClient::class);
44
        $clientMetadata = $this->prophesize(ClientMetadataInterface::class);
45
        $issuer = $this->prophesize(IssuerInterface::class);
46
        $issuerMetadata = $this->prophesize(IssuerMetadataInterface::class);
47
48
        $openIdClient->getIssuer()->willReturn($issuer->reveal());
49
        $openIdClient->getMetadata()->willReturn($clientMetadata->reveal());
50
        $openIdClient->getHttpClient()->willReturn(null);
51
        $clientMetadata->getClientId()->willReturn('clientId');
52
        $clientMetadata->getResponseTypes()->willReturn(['code']);
53
        $clientMetadata->getRedirectUris()->willReturn(['redirect_uri_1']);
54
        $issuer->getMetadata()->willReturn($issuerMetadata);
55
        $issuerMetadata->getAuthorizationEndpoint()->willReturn('https://foo-endpoint');
56
57
        static::assertSame('https://foo-endpoint?client_id=clientId&scope=openid&response_type=code&redirect_uri=redirect_uri_1', $service->getAuthorizationUri($openIdClient->reveal()));
58
    }
59
60
    public function testFetchTokenFromCode(): void
61
    {
62
        $tokenSetFactory = $this->prophesize(TokenSetFactoryInterface::class);
63
        $client = $this->prophesize(ClientInterface::class);
64
        $requestFactory = $this->prophesize(RequestFactoryInterface::class);
65
        $idTokenVerifierBuilder = $this->prophesize(IdTokenVerifierBuilderInterface::class);
66
        $tokenVerifierBuilder = $this->prophesize(TokenVerifierBuilderInterface::class);
67
68
        $service = new AuthorizationService(
69
            $tokenSetFactory->reveal(),
70
            $client->reveal(),
71
            $requestFactory->reveal(),
72
            $idTokenVerifierBuilder->reveal(),
73
            $tokenVerifierBuilder->reveal()
74
        );
75
76
        $openIdClient = $this->prophesize(OpenIDClient::class);
77
        $openIdClient->getHttpClient()->willReturn(null);
78
        $metadata = $this->prophesize(ClientMetadataInterface::class);
79
        $authMethodFactory = $this->prophesize(AuthMethodFactoryInterface::class);
80
        $authMethod = $this->prophesize(AuthMethodInterface::class);
81
        $request = $this->prophesize(RequestInterface::class);
82
        $tokenRequest1 = $this->prophesize(RequestInterface::class);
83
        $tokenRequest2 = $this->prophesize(RequestInterface::class);
84
        $response = $this->prophesize(ResponseInterface::class);
85
        $stream = $this->prophesize(StreamInterface::class);
86
        $issuer = $this->prophesize(IssuerInterface::class);
87
        $issuerMetadata = $this->prophesize(IssuerMetadataInterface::class);
88
89
        $claims = [
90
            'grant_type' => 'authorization_code',
91
            'code' => 'foo-code',
92
            'redirect_uri' => 'redirect-uri',
93
        ];
94
95
        $requestFactory->createRequest('POST', 'token-endpoint')
96
            ->willReturn($request->reveal());
97
        $request->withHeader('content-type', 'application/x-www-form-urlencoded')
98
            ->willReturn($tokenRequest1->reveal());
99
        $openIdClient->getIssuer()->willReturn($issuer->reveal());
100
        $issuer->getMetadata()->willReturn($issuerMetadata->reveal());
101
        $issuerMetadata->getTokenEndpoint()->willReturn('token-endpoint');
102
        $issuerMetadata->get('token_endpoint')->willReturn('token-endpoint');
103
        $openIdClient->getMetadata()->willReturn($metadata->reveal());
104
        $openIdClient->getAuthMethodFactory()->willReturn($authMethodFactory->reveal());
105
        $metadata->getTokenEndpointAuthMethod()->willReturn('auth-method');
106
        $metadata->get('token_endpoint_auth_method')->willReturn('auth-method');
107
        $authMethodFactory->create('auth-method')->willReturn($authMethod->reveal());
108
        $authMethod->createRequest(
109
            $tokenRequest1->reveal(),
110
            $openIdClient->reveal(),
111
            $claims
112
        )
113
            ->willReturn($tokenRequest2->reveal());
114
115
        $client->sendRequest($tokenRequest2->reveal())
116
            ->willReturn($response->reveal());
117
        $response->getStatusCode()->willReturn(200);
118
        $response->getBody()->willReturn($stream->reveal());
119
        $stream->__toString()->willReturn('{"foo":"bar"}');
120
121
        $tokenSet = $this->prophesize(TokenSetInterface::class);
122
        $tokenSetFactory->fromArray(['foo' => 'bar'])->willReturn($tokenSet->reveal());
123
124
        static::assertSame($tokenSet->reveal(), $service->grant($openIdClient->reveal(), $claims));
125
    }
126
}
127