Completed
Push — master ( 27f0ea...ce0104 )
by Thomas Mauro
14:09
created

DiscoveryProviderTest::testDiscoveryWithBaseUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClientTest\Issuer\Metadata\Provider;
6
7
use Facile\OpenIDClient\Issuer\Metadata\Provider\DiscoveryProvider;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Prophecy\ObjectProphecy;
10
use Psr\Http\Client\ClientInterface;
11
use Psr\Http\Message\RequestFactoryInterface;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\StreamInterface;
15
use Psr\Http\Message\UriFactoryInterface;
16
use Psr\Http\Message\UriInterface;
17
18
class DiscoveryProviderTest extends TestCase
19
{
20
    /** @var ClientInterface|ObjectProphecy */
21
    private $client;
22
23
    /** @var RequestFactoryInterface|ObjectProphecy */
24
    private $requestFactory;
25
26
    /** @var UriFactoryInterface|ObjectProphecy */
27
    private $uriFactory;
28
29
    protected function setUp(): void
30
    {
31
        parent::setUp();
32
33
        $this->client = $this->prophesize(ClientInterface::class);
34
        $this->requestFactory = $this->prophesize(RequestFactoryInterface::class);
35
        $this->uriFactory = $this->prophesize(UriFactoryInterface::class);
36
    }
37
38
    /**
39
     * @param string $uri
40
     */
41
    private function prepareForDiscovery(string $uri, string $baseUri = ''): void
42
    {
43
        $client = $this->client;
44
        $requestFactory = $this->requestFactory;
45
        $uriFactory = $this->uriFactory;
46
47
        $request1 = $this->prophesize(RequestInterface::class);
48
        $request2 = $this->prophesize(RequestInterface::class);
49
        $response = $this->prophesize(ResponseInterface::class);
50
        $stream = $this->prophesize(StreamInterface::class);
51
52
        $response->getBody()->willReturn($stream->reveal());
53
        $response->getStatusCode()->willReturn(200);
54
        $stream->__toString()->willReturn('{"issuer":"https://openid-uri' . $baseUri . '"}');
55
56
        $request1->withHeader('accept', 'application/json')
57
            ->shouldBeCalled()
58
            ->willReturn($request2->reveal());
59
60
        $uri1 = $this->prophesize(UriInterface::class);
61
62
        $uri1->getPath()->willReturn($baseUri . '/.well-known/openid-configuration');
63
64
        $uri1->__toString()->willReturn('https://example.com' . $baseUri . '/.well-known/openid-configuration');
65
66
        $requestFactory->createRequest('GET', 'https://example.com' . $baseUri . '/.well-known/openid-configuration')
0 ignored issues
show
Bug introduced by
The method createRequest does only exist in Psr\Http\Message\RequestFactoryInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
            ->willReturn($request1->reveal());
68
69
        $client->sendRequest($request2->reveal())
0 ignored issues
show
Bug introduced by
The method sendRequest does only exist in Psr\Http\Client\ClientInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
            ->willReturn($response->reveal());
71
72
        $uriFactory->createUri($uri)->willReturn($uri1->reveal());
0 ignored issues
show
Bug introduced by
The method createUri does only exist in Psr\Http\Message\UriFactoryInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
73
    }
74
75
    public function testDiscovery(): void
76
    {
77
        $client = $this->client;
78
        $requestFactory = $this->requestFactory;
79
        $uriFactory = $this->uriFactory;
80
81
        $uri = 'https://example.com';
82
        $provider = new DiscoveryProvider(
83
            $client->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Psr\Http\Client\ClientInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
            $requestFactory->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Psr\Http\Message\RequestFactoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
85
            $uriFactory->reveal()
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Psr\Http\Message\UriFactoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
86
        );
87
88
        $uri2 = $this->prophesize(UriInterface::class);
89
        $uri2->__toString()->willReturn('https://example.com');
90
        $uri2->getPath()->willReturn('/');
91
92
        $uriFactory->createUri('https://example.com')
0 ignored issues
show
Bug introduced by
The method createUri does only exist in Psr\Http\Message\UriFactoryInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
93
            ->willReturn($uri2->reveal());
94
95
        $uriOpenid = $this->prophesize(UriInterface::class);
96
        $uriOpenid->__toString()->willReturn('https://example.com/.well-known/openid-configuration');
97
        $uri2->withPath('/.well-known/openid-configuration')
98
            ->willReturn($uriOpenid->reveal());
99
100
        $uriOAuth = $this->prophesize(UriInterface::class);
101
        $uriOAuth->__toString()->willReturn('https://example.com/.well-known/openid-configuration');
102
        $uri2->withPath('/.well-known/oauth-authorization-server')
103
            ->willReturn($uriOAuth->reveal());
104
105
        $this->prepareForDiscovery('https://example.com/.well-known/openid-configuration');
106
107
        static::assertSame(['issuer' => 'https://openid-uri'], $provider->discovery($uri));
108
    }
109
110
    public function testDiscoveryWithBaseUri(): void
111
    {
112
        $client = $this->client;
113
        $requestFactory = $this->requestFactory;
114
        $uriFactory = $this->uriFactory;
115
116
        $baseUri = '/realms/office';
117
        $uri = 'https://example.com' . $baseUri;
118
        $provider = new DiscoveryProvider(
119
            $client->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Psr\Http\Client\ClientInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
120
            $requestFactory->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Psr\Http\Message\RequestFactoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
121
            $uriFactory->reveal()
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Psr\Http\Message\UriFactoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
122
        );
123
124
        $uri2 = $this->prophesize(UriInterface::class);
125
        $uri2->__toString()->willReturn('https://example.com' . $baseUri);
126
        $uri2->getPath()->willReturn($baseUri);
127
128
        $uriFactory->createUri('https://example.com' . $baseUri)
0 ignored issues
show
Bug introduced by
The method createUri does only exist in Psr\Http\Message\UriFactoryInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
129
            ->willReturn($uri2->reveal());
130
131
        $uriOpenid = $this->prophesize(UriInterface::class);
132
        $uriOpenid->__toString()->willReturn('https://example.com' . $baseUri . '/.well-known/openid-configuration');
133
        $uri2->withPath($baseUri . '/.well-known/openid-configuration')
134
            ->willReturn($uriOpenid->reveal());
135
136
        $uriOAuth = $this->prophesize(UriInterface::class);
137
        $uriOAuth->__toString()->willReturn('https://example.com' . $baseUri . '/.well-known/openid-configuration');
138
        $uri2->withPath($baseUri . '/.well-known/oauth-authorization-server')
139
            ->willReturn($uriOAuth->reveal());
140
141
        $this->prepareForDiscovery('https://example.com' . $baseUri . '/.well-known/openid-configuration', $baseUri);
142
143
        static::assertSame(['issuer' => 'https://openid-uri' . $baseUri], $provider->discovery($uri));
144
    }
145
146
    public function testDiscoveryWithWellKnown(): void
147
    {
148
        $client = $this->client;
149
        $requestFactory = $this->requestFactory;
150
        $uriFactory = $this->uriFactory;
151
152
        $uri = 'https://example.com/.well-known/openid-configuration';
153
        $provider = new DiscoveryProvider(
154
            $client->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Psr\Http\Client\ClientInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
155
            $requestFactory->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Psr\Http\Message\RequestFactoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
156
            $uriFactory->reveal()
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Psr\Http\Message\UriFactoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
157
        );
158
159
        $this->prepareForDiscovery('https://example.com/.well-known/openid-configuration');
160
161
        static::assertSame(['issuer' => 'https://openid-uri'], $provider->discovery($uri));
162
    }
163
}
164