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'); |
|
|
|
|
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'); |
|
|
|
|
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
|
|
|
|
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.