|
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\ClientRegistrationEndpoint\Tests; |
|
15
|
|
|
|
|
16
|
|
|
use Http\Message\MessageFactory\DiactorosMessageFactory; |
|
17
|
|
|
use Http\Message\ResponseFactory; |
|
18
|
|
|
use OAuth2Framework\Component\ClientRegistrationEndpoint\ClientRegistrationEndpoint; |
|
19
|
|
|
use OAuth2Framework\Component\ClientRule\RuleManager; |
|
20
|
|
|
use OAuth2Framework\Component\Core\Client\Client; |
|
21
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
|
22
|
|
|
use OAuth2Framework\Component\Core\Client\ClientIdGenerator; |
|
23
|
|
|
use OAuth2Framework\Component\Core\Client\ClientRepository; |
|
24
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
|
25
|
|
|
use PHPUnit\Framework\TestCase; |
|
26
|
|
|
use Prophecy\Argument; |
|
27
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
28
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
29
|
|
|
use Psr\Http\Message\StreamInterface; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @group ClientRegistrationEndpoint |
|
33
|
|
|
*/ |
|
34
|
|
|
final class ClientRegistrationEndpointTest extends TestCase |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @test |
|
38
|
|
|
*/ |
|
39
|
|
|
public function theClientRegistrationEndpointCanReceiveRegistrationRequests() |
|
40
|
|
|
{ |
|
41
|
|
|
$request = $this->buildRequest([]); |
|
42
|
|
|
$request->getMethod()->willReturn('POST'); |
|
43
|
|
|
$request->getAttribute('initial_access_token')->willReturn(null); |
|
44
|
|
|
|
|
45
|
|
|
$response = $this->getClientRegistrationEndpoint()->process($request->reveal()); |
|
46
|
|
|
|
|
47
|
|
|
self::assertEquals(201, $response->getStatusCode()); |
|
48
|
|
|
$response->getBody()->rewind(); |
|
49
|
|
|
self::assertEquals('{"client_id":"CLIENT_ID"}', $response->getBody()->getContents()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var null|ClientRegistrationEndpoint |
|
54
|
|
|
*/ |
|
55
|
|
|
private $clientRegistrationEndpoint = null; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return ClientRegistrationEndpoint |
|
59
|
|
|
*/ |
|
60
|
|
|
private function getClientRegistrationEndpoint(): ClientRegistrationEndpoint |
|
61
|
|
|
{ |
|
62
|
|
|
if (null === $this->clientRegistrationEndpoint) { |
|
63
|
|
|
$client = Client::createEmpty(); |
|
64
|
|
|
$client = $client->create( |
|
65
|
|
|
ClientId::create('CLIENT_ID'), |
|
66
|
|
|
DataBag::create([]), |
|
67
|
|
|
null |
|
68
|
|
|
); |
|
69
|
|
|
$clientIdGenerator = $this->prophesize(ClientIdGenerator::class); |
|
70
|
|
|
$clientIdGenerator->createClientId()->willReturn( |
|
71
|
|
|
ClientId::create('CLIENT_ID') |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
$clientRepository = $this->prophesize(ClientRepository::class); |
|
75
|
|
|
$clientRepository->find(Argument::type(ClientId::class))->willReturn($client); |
|
76
|
|
|
$clientRepository->save(Argument::type(Client::class))->will(function (array $args) { |
|
|
|
|
|
|
77
|
|
|
}); |
|
78
|
|
|
|
|
79
|
|
|
$this->clientRegistrationEndpoint = new ClientRegistrationEndpoint( |
|
80
|
|
|
$clientIdGenerator->reveal(), |
|
81
|
|
|
$clientRepository->reveal(), |
|
82
|
|
|
$this->getResponseFactory(), |
|
83
|
|
|
new RuleManager() |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->clientRegistrationEndpoint; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @var ResponseFactory|null |
|
92
|
|
|
*/ |
|
93
|
|
|
private $responseFactory = null; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return ResponseFactory |
|
97
|
|
|
*/ |
|
98
|
|
|
private function getResponseFactory(): ResponseFactory |
|
99
|
|
|
{ |
|
100
|
|
|
if (null === $this->responseFactory) { |
|
101
|
|
|
$this->responseFactory = new DiactorosMessageFactory(); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->responseFactory; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function buildRequest(array $data): ObjectProphecy |
|
108
|
|
|
{ |
|
109
|
|
|
$body = $this->prophesize(StreamInterface::class); |
|
110
|
|
|
$body->getContents()->willReturn(json_encode($data)); |
|
111
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
|
112
|
|
|
$request->hasHeader('Content-Type')->willReturn(true); |
|
113
|
|
|
$request->getHeader('Content-Type')->willReturn(['application/json']); |
|
114
|
|
|
$request->getBody()->willReturn($body->reveal()); |
|
115
|
|
|
$request->getParsedBody()->willReturn([]); |
|
116
|
|
|
|
|
117
|
|
|
return $request; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.