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

TokenRevocationPostEndpointTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A aTokenTypeHintManagerCanHandleTokenTypeHints() 0 4 1
A theTokenRevocationEndpointReceivesAValidPostRequest() 0 15 1
A theTokenRevocationEndpointReceivesAValidPostRequestWithTokenTypeHint() 0 15 1
A theTokenRevocationEndpointReceivesARequestWithAnUnsupportedTokenHint() 0 15 1
A getTokenTypeHintManager() 0 18 2
A getTokenRevocationPostEndpoint() 0 11 2
A getResponseFactory() 0 8 2
A getClient() 0 13 2
A buildRequest() 0 12 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\TokenRevocationEndpoint\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\Core\Client\Client;
22
use OAuth2Framework\Component\Core\Client\ClientId;
23
use OAuth2Framework\Component\Core\DataBag\DataBag;
24
use OAuth2Framework\Component\Core\Token\Token;
25
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
26
use OAuth2Framework\Component\TokenRevocationEndpoint\TokenRevocationPostEndpoint;
27
use OAuth2Framework\Component\TokenRevocationEndpoint\TokenTypeHint;
28
use OAuth2Framework\Component\TokenRevocationEndpoint\TokenTypeHintManager;
29
use PHPUnit\Framework\TestCase;
30
use Psr\Http\Message\ServerRequestInterface;
31
32
/**
33
 * @group TokenRevocationEndpoint
34
 */
35
final class TokenRevocationPostEndpointTest extends TestCase
36
{
37
    /**
38
     * @test
39
     */
40
    public function aTokenTypeHintManagerCanHandleTokenTypeHints()
41
    {
42
        self::assertNotEmpty($this->getTokenTypeHintManager()->getTokenTypeHints());
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function theTokenRevocationEndpointReceivesAValidPostRequest()
49
    {
50
        $endpoint = $this->getTokenRevocationPostEndpoint();
51
52
        $request = $this->buildRequest(['token' => 'VALID_TOKEN']);
53
        $request->getAttribute('client')->willReturn($this->getClient());
54
55
        $handler = $this->prophesize(RequestHandlerInterface::class);
56
57
        $response = $endpoint->process($request->reveal(), $handler->reveal());
58
59
        self::assertEquals(200, $response->getStatusCode());
60
        $response->getBody()->rewind();
61
        self::assertEquals('', $response->getBody()->getContents());
62
    }
63
64
    /**
65
     * @test
66
     */
67
    public function theTokenRevocationEndpointReceivesAValidPostRequestWithTokenTypeHint()
68
    {
69
        $endpoint = $this->getTokenRevocationPostEndpoint();
70
71
        $request = $this->buildRequest(['token' => 'VALID_TOKEN', 'token_type_hint' => 'foo']);
72
        $request->getAttribute('client')->willReturn($this->getClient());
73
74
        $handler = $this->prophesize(RequestHandlerInterface::class);
75
76
        $response = $endpoint->process($request->reveal(), $handler->reveal());
77
78
        self::assertEquals(200, $response->getStatusCode());
79
        $response->getBody()->rewind();
80
        self::assertEquals('', $response->getBody()->getContents());
81
    }
82
83
    /**
84
     * @test
85
     */
86
    public function theTokenRevocationEndpointReceivesARequestWithAnUnsupportedTokenHint()
87
    {
88
        $endpoint = $this->getTokenRevocationPostEndpoint();
89
90
        $request = $this->buildRequest(['token' => 'VALID_TOKEN', 'token_type_hint' => 'bar']);
91
        $request->getAttribute('client')->willReturn($this->getClient());
92
93
        $handler = $this->prophesize(RequestHandlerInterface::class);
94
95
        $response = $endpoint->process($request->reveal(), $handler->reveal());
96
97
        self::assertEquals(400, $response->getStatusCode());
98
        $response->getBody()->rewind();
99
        self::assertEquals('{"error":"unsupported_token_type","error_description":"The token type hint \"bar\" is not supported. Please use one of the following values: foo."}', $response->getBody()->getContents());
100
    }
101
102
    /**
103
     * @var TokenTypeHintManager|null
104
     */
105
    private $tokenTypeHintManager = null;
106
107
    /**
108
     * @return TokenTypeHintManager
109
     */
110
    private function getTokenTypeHintManager(): TokenTypeHintManager
111
    {
112
        if (null === $this->tokenTypeHintManager) {
113
            $token = $this->prophesize(Token::class);
114
            $token->getClientId()->willReturn(ClientId::create('CLIENT_ID'));
115
116
            $tokenType = $this->prophesize(TokenTypeHint::class);
117
            $tokenType->find('VALID_TOKEN')->willReturn($token->reveal());
118
            $tokenType->find('BAD_TOKEN')->willReturn(null);
119
            $tokenType->hint()->willReturn('foo');
120
            $tokenType->revoke($token)->willReturn(null);
121
122
            $this->tokenTypeHintManager = new TokenTypeHintManager();
123
            $this->tokenTypeHintManager->add($tokenType->reveal());
124
        }
125
126
        return $this->tokenTypeHintManager;
127
    }
128
129
    /**
130
     * @var TokenRevocationPostEndpoint|null
131
     */
132
    private $tokenRevocationEndpoint = null;
133
134
    /**
135
     * @return TokenRevocationPostEndpoint
136
     */
137
    private function getTokenRevocationPostEndpoint(): TokenRevocationPostEndpoint
138
    {
139
        if (null === $this->tokenRevocationEndpoint) {
140
            $this->tokenRevocationEndpoint = new TokenRevocationPostEndpoint(
141
                $this->getTokenTypeHintManager(),
142
                $this->getResponseFactory()
143
            );
144
        }
145
146
        return $this->tokenRevocationEndpoint;
147
    }
148
149
    /**
150
     * @var ResponseFactory|null
151
     */
152
    private $responseFactory = null;
153
154
    /**
155
     * @return ResponseFactory
156
     */
157
    private function getResponseFactory(): ResponseFactory
158
    {
159
        if (null === $this->responseFactory) {
160
            $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...
161
        }
162
163
        return $this->responseFactory;
164
    }
165
166
    /**
167
     * @var Client|null
168
     */
169
    private $client = null;
170
171
    /**
172
     * @return Client
173
     */
174
    private function getClient(): Client
175
    {
176
        if (null === $this->client) {
177
            $this->client = Client::createEmpty();
178
            $this->client = $this->client->create(
179
                ClientId::create('CLIENT_ID'),
180
                DataBag::create([]),
181
                UserAccountId::create('USER_ACCOUNT')
182
            );
183
        }
184
185
        return $this->client;
186
    }
187
188
    private function buildRequest(array $data): ObjectProphecy
189
    {
190
        $body = $this->prophesize(StreamInterface::class);
191
        $body->getContents()->willReturn(http_build_query($data));
192
        $request = $this->prophesize(ServerRequestInterface::class);
193
        $request->hasHeader('Content-Type')->willReturn(true);
194
        $request->getHeader('Content-Type')->willReturn(['application/x-www-form-urlencoded']);
195
        $request->getBody()->willReturn($body->reveal());
196
        $request->getParsedBody()->willReturn([]);
197
198
        return $request;
199
    }
200
}
201