Failed Conditions
Push — master ( 393e29...5c5e5d )
by Florent
05:55
created

ClientConfigurationEndpointTest::buildRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
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 Prophecy\Prophecy\ObjectProphecy;
19
use Psr\Http\Message\StreamInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
use OAuth2Framework\Component\BearerTokenType\BearerToken;
22
use OAuth2Framework\Component\ClientConfigurationEndpoint\ClientConfigurationEndpoint;
23
use OAuth2Framework\Component\ClientRule\RuleManager;
24
use OAuth2Framework\Component\Core\Client\Client;
25
use OAuth2Framework\Component\Core\Client\ClientId;
26
use OAuth2Framework\Component\Core\Client\ClientRepository;
27
use OAuth2Framework\Component\Core\DataBag\DataBag;
28
use PHPUnit\Framework\TestCase;
29
use Prophecy\Argument;
30
use Psr\Http\Message\ServerRequestInterface;
31
32
/**
33
 * @group ClientConfigurationEndpoint
34
 */
35
final class ClientConfigurationEndpointTest extends TestCase
36
{
37
    /**
38
     * @test
39
     */
40
    public function theClientConfigurationEndpointCanReceiveGetRequestsAndRetrieveClientInformation()
41
    {
42
        $client = Client::createEmpty();
43
        $client = $client->create(
44
            ClientId::create('CLIENT_ID'),
45
            DataBag::create([
46
                'registration_access_token' => 'REGISTRATION_TOKEN',
47
            ]),
48
            null
49
        );
50
        $clientRepository = $this->prophesize(ClientRepository::class);
51
52
        $request = $this->prophesize(ServerRequestInterface::class);
53
        $request->getMethod()->willReturn('GET');
54
        $request->getAttribute('client')->willReturn($client);
55
        $request->getHeader('AUTHORIZATION')->willReturn(['Bearer REGISTRATION_TOKEN']);
56
57
        $handler = $this->prophesize(RequestHandlerInterface::class);
58
59
        $response = $this->getClientConfigurationEndpoint($clientRepository->reveal())->process($request->reveal(), $handler->reveal());
60
        $response->getBody()->rewind();
61
        self::assertEquals(200, $response->getStatusCode());
62
        self::assertEquals('{"registration_access_token":"REGISTRATION_TOKEN","client_id":"CLIENT_ID"}', $response->getBody()->getContents());
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function theClientConfigurationEndpointCanReceivePutRequestsAndUpdateTheClient()
69
    {
70
        $client = Client::createEmpty();
71
        $client = $client->create(
72
            ClientId::create('CLIENT_ID'),
73
            DataBag::create([
74
                'registration_access_token' => 'REGISTRATION_TOKEN',
75
            ]),
76
            null
77
        );
78
        $clientRepository = $this->prophesize(ClientRepository::class);
79
        $clientRepository->save(Argument::type(Client::class))->shouldBeCalled();
80
81
        $request = $this->buildRequest([]);
82
        $request->getMethod()->willReturn('PUT');
83
        $request->getAttribute('client')->willReturn($client);
84
        $request->getHeader('AUTHORIZATION')->willReturn(['Bearer REGISTRATION_TOKEN']);
85
86
        $handler = $this->prophesize(RequestHandlerInterface::class);
87
88
        $response = $this->getClientConfigurationEndpoint($clientRepository->reveal())->process($request->reveal(), $handler->reveal());
89
        $response->getBody()->rewind();
90
        self::assertEquals(200, $response->getStatusCode());
91
        self::assertEquals('{"client_id":"CLIENT_ID"}', $response->getBody()->getContents());
92
    }
93
94
    /**
95
     * @var null|ClientConfigurationEndpoint
96
     */
97
    private $clientConfigurationEndpoint = null;
98
99
    /**
100
     * @param ClientRepository $clientRepository
101
     *
102
     * @return ClientConfigurationEndpoint
103
     */
104
    private function getClientConfigurationEndpoint(ClientRepository $clientRepository): ClientConfigurationEndpoint
105
    {
106
        if (null === $this->clientConfigurationEndpoint) {
107
            $this->clientConfigurationEndpoint = new ClientConfigurationEndpoint(
108
                $clientRepository,
109
                new BearerToken('Client Manager', true, false, false),
110
                $this->getResponseFactory(),
111
                new RuleManager()
112
            );
113
        }
114
115
        return $this->clientConfigurationEndpoint;
116
    }
117
118
    /**
119
     * @var ResponseFactory|null
120
     */
121
    private $responseFactory = null;
122
123
    /**
124
     * @return ResponseFactory
125
     */
126
    private function getResponseFactory(): ResponseFactory
127
    {
128
        if (null === $this->responseFactory) {
129
            $this->responseFactory = new DiactorosMessageFactory();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Http\Message\Messag...actorosMessageFactory() of type object<Http\Message\Mess...iactorosMessageFactory> is incompatible with the declared type object<Http\Message\ResponseFactory>|null of property $responseFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
130
        }
131
132
        return $this->responseFactory;
133
    }
134
135
    private function buildRequest(array $data): ObjectProphecy
136
    {
137
        $body = $this->prophesize(StreamInterface::class);
138
        $body->getContents()->willReturn(json_encode($data));
139
        $request = $this->prophesize(ServerRequestInterface::class);
140
        $request->hasHeader('Content-Type')->willReturn(true);
141
        $request->getHeader('Content-Type')->willReturn(['application/json']);
142
        $request->getBody()->willReturn($body->reveal());
143
        $request->getParsedBody()->willReturn([]);
144
145
        return $request;
146
    }
147
}
148