Failed Conditions
Push — master ( cccd95...989cb2 )
by Florent
03:57
created

getClientRegistrationEndpoint()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
c 0
b 0
f 0
rs 8.8571
cc 2
eloc 20
nc 2
nop 0
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 Psr\Http\Message\ServerRequestInterface;
28
use SimpleBus\Message\Bus\MessageBus;
29
30
/**
31
 * @group ClientRegistrationEndpoint
32
 */
33
class ClientRegistrationEndpointTest extends TestCase
34
{
35
    /**
36
     * @test
37
     */
38
    public function theClientRegistrationEndpointCanReceiveRegistrationRequests()
39
    {
40
        $request = $this->prophesize(ServerRequestInterface::class);
41
        $request->getMethod()->willReturn('POST');
42
        $request->getAttribute('initial_access_token')->willReturn(null);
43
        $request->getParsedBody()->willReturn([]);
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(bin2hex(random_bytes(16)))
72
            );
73
74
            $clientRepository = $this->prophesize(ClientRepository::class);
75
            $clientRepository->find(Argument::type(ClientId::class))->willReturn($client);
76
            $messageBus = $this->prophesize(MessageBus::class);
77
78
            $this->clientRegistrationEndpoint = new ClientRegistrationEndpoint(
79
                $clientIdGenerator->reveal(),
80
                $clientRepository->reveal(),
81
                $this->getResponseFactory(),
82
                $messageBus->reveal(),
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