Passed
Push — master ( f80f70...56399a )
by Thomas Mauro
03:05 queued 10s
created

WebFingerProviderTest::testFetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 66
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 42
nc 1
nop 0
dl 0
loc 66
rs 9.248
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\Issuer\Metadata\Provider;
6
7
use Facile\OpenIDClient\Issuer\Metadata\Provider\DiscoveryProviderInterface;
8
use Facile\OpenIDClient\Issuer\Metadata\Provider\WebFingerProvider;
9
use function http_build_query;
10
use function json_encode;
11
use Facile\OpenIDClientTest\TestCase;
12
use Psr\Http\Client\ClientInterface;
13
use Psr\Http\Message\RequestFactoryInterface;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\StreamInterface;
17
use Psr\Http\Message\UriFactoryInterface;
18
use Psr\Http\Message\UriInterface;
19
20
class WebFingerProviderTest extends TestCase
21
{
22
    public function testFetch(): void
23
    {
24
        $client = $this->prophesize(ClientInterface::class);
25
        $requestFactory = $this->prophesize(RequestFactoryInterface::class);
26
        $uriFactory = $this->prophesize(UriFactoryInterface::class);
27
        $discoveryProvider = $this->prophesize(DiscoveryProviderInterface::class);
28
29
        $resource = '[email protected]';
30
        $provider = new WebFingerProvider(
31
            $client->reveal(),
32
            $requestFactory->reveal(),
33
            $uriFactory->reveal(),
34
            $discoveryProvider->reveal()
35
        );
36
37
        $webFingerUrl = $this->prophesize(UriInterface::class);
38
        $webFingerUrl1 = $this->prophesize(UriInterface::class);
39
40
        $uriFactory->createUri('https://example.com/.well-known/webfinger')
41
            ->willReturn($webFingerUrl1->reveal());
42
        $webFingerUrl1->withQuery(http_build_query([
43
            'resource' => 'acct:[email protected]',
44
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
45
        ]))
46
            ->willReturn($webFingerUrl->reveal());
47
        $webFingerUrl->__toString()->willReturn('https://example.com/.well-known/webfinger');
48
49
        $request = $this->prophesize(RequestInterface::class);
50
        $request1 = $this->prophesize(RequestInterface::class);
51
        $response = $this->prophesize(ResponseInterface::class);
52
        $stream = $this->prophesize(StreamInterface::class);
53
54
        $requestFactory->createRequest('GET', $webFingerUrl)
55
            ->willReturn($request1->reveal());
56
        $request1->withHeader('accept', 'application/json')
57
            ->willReturn($request->reveal());
58
59
        $client->sendRequest($request->reveal())
60
            ->willReturn($response->reveal());
61
62
        $response->getStatusCode()->willReturn(200);
63
        $response->getBody()->willReturn($stream->reveal());
64
65
        $responsePayload = [
66
            'links' => [
67
                [
68
                    'rel' => 'wrong-foo',
69
                    'href' => 'wrong-url',
70
                ],
71
                [
72
                    'rel' => 'http://openid.net/specs/connect/1.0/issuer',
73
                ],
74
                [
75
                    'rel' => 'http://openid.net/specs/connect/1.0/issuer',
76
                    'href' => 'https://openid-uri',
77
                ],
78
            ],
79
        ];
80
81
        $stream->__toString()->willReturn(json_encode($responsePayload));
82
83
        $discoveryProvider->discovery('https://openid-uri')->willReturn([
84
            'issuer' => 'https://openid-uri',
85
        ]);
86
87
        static::assertSame(['issuer' => 'https://openid-uri'], $provider->fetch($resource));
88
    }
89
}
90