CachedProviderDecoratorTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 52
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testShouldPersistCachedData() 0 24 1
A testShouldReuseCachedData() 0 24 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClientTest\Issuer\Metadata\Provider;
6
7
use Facile\OpenIDClient\Issuer\Metadata\Provider\CachedProviderDecorator;
8
use Facile\OpenIDClient\Issuer\Metadata\Provider\RemoteProviderInterface;
9
use Facile\OpenIDClientTest\TestCase;
10
use Prophecy\Argument;
11
use Psr\SimpleCache\CacheInterface;
12
13
class CachedProviderDecoratorTest extends TestCase
14
{
15
    public function testShouldPersistCachedData(): void
16
    {
17
        $uri = 'https://example.com';
18
        $provider1 = $this->prophesize(RemoteProviderInterface::class);
19
        $cache = $this->prophesize(CacheInterface::class);
20
21
        $metadata = [
22
            'foo1' => 'bar1',
23
        ];
24
        $provider1->fetch($uri)->shouldBeCalled()->willReturn($metadata);
25
26
        $provider = new CachedProviderDecorator(
27
            $provider1->reveal(),
28
            $cache->reveal()
29
        );
30
31
        $cache->get(Argument::type('string'))
32
            ->shouldBeCalled()
33
            ->willReturn(null);
34
35
        $cache->set(Argument::type('string'), json_encode($metadata), null)
36
            ->shouldBeCalled();
37
38
        $this->assertSame($metadata, $provider->fetch($uri));
39
    }
40
41
    public function testShouldReuseCachedData(): void
42
    {
43
        $uri = 'https://example.com';
44
        $provider1 = $this->prophesize(RemoteProviderInterface::class);
45
        $cache = $this->prophesize(CacheInterface::class);
46
47
        $metadata = [
48
            'foo1' => 'bar1',
49
        ];
50
        $provider1->fetch($uri)->shouldNotBeCalled();
51
52
        $provider = new CachedProviderDecorator(
53
            $provider1->reveal(),
54
            $cache->reveal()
55
        );
56
57
        $cache->get(Argument::type('string'))
58
            ->shouldBeCalled()
59
            ->willReturn(json_encode($metadata));
60
61
        $cache->set(Argument::cetera())
62
            ->shouldNotBeCalled();
63
64
        $this->assertSame($metadata, $provider->fetch($uri));
65
    }
66
}
67