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

testShouldSkipIncompatibleProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 21
rs 9.584
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\Exception\RuntimeException;
8
use Facile\OpenIDClient\Issuer\Metadata\Provider\RemoteProvider;
9
use Facile\OpenIDClient\Issuer\Metadata\Provider\RemoteProviderInterface;
10
use PHPUnit\Framework\TestCase;
11
12
class RemoteProviderTest extends TestCase
13
{
14
    public function testShouldReturnDataFromFirstProvider(): void
15
    {
16
        $uri = 'https://example.com';
17
        $provider1 = $this->prophesize(RemoteProviderInterface::class);
18
        $provider2 = $this->prophesize(RemoteProviderInterface::class);
19
20
        $provider1->isAllowedUri($uri)->willReturn(true);
21
        $provider2->isAllowedUri($uri)->willReturn(true);
22
23
        $provider1->fetch($uri)->shouldBeCalled()->willReturn([
24
            'foo1' => 'bar1',
25
        ]);
26
        $provider2->fetch($uri)->shouldNotBeCalled();
27
28
        $provider = new RemoteProvider([
29
            $provider1->reveal(),
30
            $provider2->reveal(),
31
        ]);
32
33
        $this->assertSame(['foo1' => 'bar1'], $provider->fetch($uri));
34
    }
35
36
    public function testShouldFallbackOnNextProvider(): void
37
    {
38
        $uri = 'https://example.com';
39
        $provider1 = $this->prophesize(RemoteProviderInterface::class);
40
        $provider2 = $this->prophesize(RemoteProviderInterface::class);
41
42
        $provider1->isAllowedUri($uri)->willReturn(true);
43
        $provider2->isAllowedUri($uri)->willReturn(true);
44
45
        $provider1->fetch($uri)->shouldBeCalled()->willThrow(new RuntimeException('Error'));
46
        $provider2->fetch($uri)->shouldBeCalled()->willReturn([
47
            'foo1' => 'bar1',
48
        ]);
49
50
        $provider = new RemoteProvider([
51
            $provider1->reveal(),
52
            $provider2->reveal(),
53
        ]);
54
55
        $this->assertSame(['foo1' => 'bar1'], $provider->fetch($uri));
56
    }
57
58
    public function testShouldSkipIncompatibleProvider(): void
59
    {
60
        $uri = 'https://example.com';
61
        $provider1 = $this->prophesize(RemoteProviderInterface::class);
62
        $provider2 = $this->prophesize(RemoteProviderInterface::class);
63
64
        $provider1->isAllowedUri($uri)->willReturn(false);
65
        $provider2->isAllowedUri($uri)->willReturn(true);
66
67
        $provider1->fetch($uri)->shouldNotBeCalled();
68
        $provider2->fetch($uri)->shouldBeCalled()->willReturn([
69
            'foo1' => 'bar1',
70
        ]);
71
72
        $provider = new RemoteProvider([
73
            $provider1->reveal(),
74
            $provider2->reveal(),
75
        ]);
76
77
        $this->assertSame(['foo1' => 'bar1'], $provider->fetch($uri));
78
    }
79
}
80