Failed Conditions
Push — master ( 547d98...1bf1d9 )
by Florent
08:07
created

ClientConfigurationEndpointTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 12
dl 0
loc 101
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B theClientConfigurationEndpointCanReceiveGetRequestsAndRetrieveClientInformation() 0 24 1
B theClientConfigurationEndpointCanReceivePutRequestsAndUpdateTheClient() 0 26 1
A getClientConfigurationEndpoint() 0 13 2
A getResponseFactory() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\ClientConfigurationEndpoint\Tests;
15
16
use Http\Message\MessageFactory\DiactorosMessageFactory;
17
use Http\Message\ResponseFactory;
18
use Psr\Http\Server\RequestHandlerInterface;
19
use OAuth2Framework\Component\BearerTokenType\BearerToken;
20
use OAuth2Framework\Component\ClientConfigurationEndpoint\ClientConfigurationEndpoint;
21
use OAuth2Framework\Component\ClientRule\RuleManager;
22
use OAuth2Framework\Component\Core\Client\Client;
23
use OAuth2Framework\Component\Core\Client\ClientId;
24
use OAuth2Framework\Component\Core\Client\ClientRepository;
25
use OAuth2Framework\Component\Core\DataBag\DataBag;
26
use PHPUnit\Framework\TestCase;
27
use Prophecy\Argument;
28
use Psr\Http\Message\ServerRequestInterface;
29
30
/**
31
 * @group ClientConfigurationEndpoint
32
 */
33
final class ClientConfigurationEndpointTest extends TestCase
34
{
35
    /**
36
     * @test
37
     */
38
    public function theClientConfigurationEndpointCanReceiveGetRequestsAndRetrieveClientInformation()
39
    {
40
        $client = Client::createEmpty();
41
        $client = $client->create(
42
            ClientId::create('CLIENT_ID'),
43
            DataBag::create([
44
                'registration_access_token' => 'REGISTRATION_TOKEN',
45
            ]),
46
            null
47
        );
48
        $clientRepository = $this->prophesize(ClientRepository::class);
49
50
        $request = $this->prophesize(ServerRequestInterface::class);
51
        $request->getMethod()->willReturn('GET');
0 ignored issues
show
Bug introduced by
The method getMethod() does not exist on Prophecy\Prophecy\ObjectProphecy. Did you maybe mean getMethodProphecies()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
52
        $request->getAttribute('client')->willReturn($client);
53
        $request->getHeader('AUTHORIZATION')->willReturn(['Bearer REGISTRATION_TOKEN']);
54
55
        $handler = $this->prophesize(RequestHandlerInterface::class);
56
57
        $response = $this->getClientConfigurationEndpoint($clientRepository->reveal())->process($request->reveal(), $handler->reveal());
58
        $response->getBody()->rewind();
59
        self::assertEquals(200, $response->getStatusCode());
60
        self::assertEquals('{"registration_access_token":"REGISTRATION_TOKEN","client_id":"CLIENT_ID"}', $response->getBody()->getContents());
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function theClientConfigurationEndpointCanReceivePutRequestsAndUpdateTheClient()
67
    {
68
        $client = Client::createEmpty();
69
        $client = $client->create(
70
            ClientId::create('CLIENT_ID'),
71
            DataBag::create([
72
                'registration_access_token' => 'REGISTRATION_TOKEN',
73
            ]),
74
            null
75
        );
76
        $clientRepository = $this->prophesize(ClientRepository::class);
77
        $clientRepository->save(Argument::type(Client::class))->shouldBeCalled();
78
79
        $request = $this->prophesize(ServerRequestInterface::class);
80
        $request->getMethod()->willReturn('PUT');
0 ignored issues
show
Bug introduced by
The method getMethod() does not exist on Prophecy\Prophecy\ObjectProphecy. Did you maybe mean getMethodProphecies()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
81
        $request->getAttribute('client')->willReturn($client);
82
        $request->getParsedBody()->willReturn([]);
83
        $request->getHeader('AUTHORIZATION')->willReturn(['Bearer REGISTRATION_TOKEN']);
84
85
        $handler = $this->prophesize(RequestHandlerInterface::class);
86
87
        $response = $this->getClientConfigurationEndpoint($clientRepository->reveal())->process($request->reveal(), $handler->reveal());
88
        $response->getBody()->rewind();
89
        self::assertEquals(200, $response->getStatusCode());
90
        self::assertEquals('{"client_id":"CLIENT_ID"}', $response->getBody()->getContents());
91
    }
92
93
    /**
94
     * @var null|ClientConfigurationEndpoint
95
     */
96
    private $clientConfigurationEndpoint = null;
97
98
    /**
99
     * @param ClientRepository $clientRepository
100
     *
101
     * @return ClientConfigurationEndpoint
102
     */
103
    private function getClientConfigurationEndpoint(ClientRepository $clientRepository): ClientConfigurationEndpoint
104
    {
105
        if (null === $this->clientConfigurationEndpoint) {
106
            $this->clientConfigurationEndpoint = new ClientConfigurationEndpoint(
107
                $clientRepository,
108
                new BearerToken('Client Manager', true, false, false),
109
                $this->getResponseFactory(),
110
                new RuleManager()
111
            );
112
        }
113
114
        return $this->clientConfigurationEndpoint;
115
    }
116
117
    /**
118
     * @var ResponseFactory|null
119
     */
120
    private $responseFactory = null;
121
122
    /**
123
     * @return ResponseFactory
124
     */
125
    private function getResponseFactory(): ResponseFactory
126
    {
127
        if (null === $this->responseFactory) {
128
            $this->responseFactory = new DiactorosMessageFactory();
129
        }
130
131
        return $this->responseFactory;
132
    }
133
}
134