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

ClientRegistrationEndpointTest::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\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) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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();
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...
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