getResponseFactory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\TokenIntrospectionEndpoint\Tests;
15
16
use Nyholm\Psr7\Factory\Psr17Factory;
17
use OAuth2Framework\Component\Core\AccessToken\AccessToken;
18
use OAuth2Framework\Component\Core\ResourceServer\ResourceServer;
19
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
20
use OAuth2Framework\Component\TokenIntrospectionEndpoint\TokenIntrospectionEndpoint;
21
use OAuth2Framework\Component\TokenIntrospectionEndpoint\TokenTypeHint;
22
use OAuth2Framework\Component\TokenIntrospectionEndpoint\TokenTypeHintManager;
23
use PHPUnit\Framework\TestCase;
24
use Prophecy\PhpUnit\ProphecyTrait;
25
use Prophecy\Prophecy\ObjectProphecy;
26
use Psr\Http\Message\ResponseFactoryInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
use Psr\Http\Message\StreamInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
/**
32
 * @group TokenIntrospectionEndpoint
33
 *
34
 * @internal
35
 */
36
final class TokenIntrospectionEndpointTest extends TestCase
37
{
38
    use ProphecyTrait;
39
40
    /**
41
     * @var null|TokenTypeHintManager
42
     */
43
    private $tokenTypeHintManager;
44
45
    /**
46
     * @var null|TokenIntrospectionEndpoint
47
     */
48
    private $tokenIntrospectionEndpoint;
49
50
    /**
51
     * @var null|ResponseFactoryInterface
52
     */
53
    private $responseFactory;
54
55
    /**
56
     * @var null|ResourceServer
57
     */
58
    private $resourceServer;
59
60
    /**
61
     * @test
62
     */
63
    public function aTokenTypeHintManagerCanHandleTokenTypeHints()
64
    {
65
        static::assertNotEmpty($this->getTokenTypeHintManager()->getTokenTypeHints());
66
    }
67
68
    /**
69
     * @test
70
     */
71
    public function theTokenIntrospectionEndpointReceivesAValidRequest()
72
    {
73
        $endpoint = $this->getTokenIntrospectionEndpoint();
74
75
        $request = $this->buildRequest(['token' => 'VALID_TOKEN']);
76
        $request->getAttribute('resource_server')->willReturn($this->getResourceServer());
77
78
        $handler = $this->prophesize(RequestHandlerInterface::class);
79
80
        $response = $endpoint->process($request->reveal(), $handler->reveal());
81
82
        static::assertEquals(200, $response->getStatusCode());
83
        $response->getBody()->rewind();
84
        static::assertEquals('{"active":true}', $response->getBody()->getContents());
85
    }
86
87
    /**
88
     * @test
89
     */
90
    public function theTokenIntrospectionEndpointReceivesAValidRequestWithTokenTypeHint()
91
    {
92
        $endpoint = $this->getTokenIntrospectionEndpoint();
93
94
        $request = $this->buildRequest(['token' => 'VALID_TOKEN', 'token_type_hint' => 'foo']);
95
        $request->getAttribute('resource_server')->willReturn($this->getResourceServer());
96
97
        $handler = $this->prophesize(RequestHandlerInterface::class);
98
99
        $response = $endpoint->process($request->reveal(), $handler->reveal());
100
101
        static::assertEquals(200, $response->getStatusCode());
102
        $response->getBody()->rewind();
103
        static::assertEquals('{"active":true}', $response->getBody()->getContents());
104
    }
105
106
    /**
107
     * @test
108
     */
109
    public function theTokenIntrospectionEndpointReceivesARequestWithAnUnsupportedTokenHint()
110
    {
111
        $endpoint = $this->getTokenIntrospectionEndpoint();
112
113
        $request = $this->buildRequest(['token' => 'VALID_TOKEN', 'token_type_hint' => 'bar']);
114
        $request->getAttribute('resource_server')->willReturn($this->getResourceServer());
115
116
        $handler = $this->prophesize(RequestHandlerInterface::class);
117
118
        $response = $endpoint->process($request->reveal(), $handler->reveal());
119
120
        static::assertEquals(400, $response->getStatusCode());
121
        $response->getBody()->rewind();
122
        static::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());
123
    }
124
125
    private function getTokenTypeHintManager(): TokenTypeHintManager
126
    {
127
        if (null === $this->tokenTypeHintManager) {
128
            $token = $this->prophesize(AccessToken::class);
129
            $token->getResourceServerId()->willReturn(new ResourceServerId('RESOURCE_SERVER_ID'));
130
131
            $tokenType = $this->prophesize(TokenTypeHint::class);
132
            $tokenType->find('VALID_TOKEN')->willReturn($token->reveal());
133
            $tokenType->find('BAD_TOKEN')->willReturn(null);
134
            $tokenType->hint()->willReturn('foo');
135
            $tokenType->introspect($token)->willReturn(['active' => true]);
136
137
            $this->tokenTypeHintManager = new TokenTypeHintManager();
138
            $this->tokenTypeHintManager->add($tokenType->reveal());
139
        }
140
141
        return $this->tokenTypeHintManager;
142
    }
143
144
    private function getTokenIntrospectionEndpoint(): TokenIntrospectionEndpoint
145
    {
146
        if (null === $this->tokenIntrospectionEndpoint) {
147
            $this->tokenIntrospectionEndpoint = new TokenIntrospectionEndpoint(
148
                $this->getTokenTypeHintManager(),
149
                $this->getResponseFactory()
150
            );
151
        }
152
153
        return $this->tokenIntrospectionEndpoint;
154
    }
155
156
    private function getResponseFactory(): ResponseFactoryInterface
157
    {
158
        if (null === $this->responseFactory) {
159
            $this->responseFactory = new Psr17Factory();
160
        }
161
162
        return $this->responseFactory;
163
    }
164
165
    private function getResourceServer(): ResourceServer
166
    {
167
        if (null === $this->resourceServer) {
168
            $this->resourceServer = $this->prophesize(ResourceServer::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(OAuth2...\ResourceServer::class) of type Prophecy\Prophecy\ObjectProphecy is incompatible with the declared type OAuth2Framework\Componen...ver\ResourceServer|null of property $resourceServer.

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...
169
            $this->resourceServer->getResourceServerId()->willReturn(new ResourceServerId('RESOURCE_SERVER_ID'));
170
        }
171
172
        return $this->resourceServer->reveal();
0 ignored issues
show
Bug introduced by
The method reveal() does not exist on OAuth2Framework\Componen...ceServer\ResourceServer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
        return $this->resourceServer->/** @scrutinizer ignore-call */ reveal();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
173
    }
174
175
    private function buildRequest(array $data): ObjectProphecy
176
    {
177
        $body = $this->prophesize(StreamInterface::class);
178
        $body->getContents()->willReturn(http_build_query($data));
179
        $request = $this->prophesize(ServerRequestInterface::class);
180
        $request->hasHeader('Content-Type')->willReturn(true);
181
        $request->getHeader('Content-Type')->willReturn(['application/x-www-form-urlencoded']);
182
        $request->getBody()->willReturn($body->reveal());
183
        $request->getParsedBody()->willReturn([]);
184
185
        return $request;
186
    }
187
}
188