MercureInfoActionTest::provideNoHostConfigs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Rest\Action;
6
7
use Cake\Chronos\Chronos;
8
use Laminas\Diactoros\Response\JsonResponse;
9
use Laminas\Diactoros\ServerRequestFactory;
10
use PHPUnit\Framework\TestCase;
11
use Prophecy\Argument;
12
use Prophecy\PhpUnit\ProphecyTrait;
13
use Prophecy\Prophecy\ObjectProphecy;
14
use RuntimeException;
15
use Shlinkio\Shlink\Common\Mercure\JwtProviderInterface;
16
use Shlinkio\Shlink\Rest\Action\MercureInfoAction;
17
use Shlinkio\Shlink\Rest\Exception\MercureException;
18
19
class MercureInfoActionTest extends TestCase
20
{
21
    use ProphecyTrait;
22
23
    private ObjectProphecy $provider;
24
25
    public function setUp(): void
26
    {
27
        $this->provider = $this->prophesize(JwtProviderInterface::class);
28
    }
29
30
    /**
31
     * @test
32
     * @dataProvider provideNoHostConfigs
33
     */
34
    public function throwsExceptionWhenConfigDoesNotHavePublicHost(array $mercureConfig): void
35
    {
36
        $buildToken = $this->provider->buildSubscriptionToken(Argument::any())->willReturn('abc.123');
37
38
        $action = new MercureInfoAction($this->provider->reveal(), $mercureConfig);
39
40
        $this->expectException(MercureException::class);
41
        $buildToken->shouldNotBeCalled();
42
43
        $action->handle(ServerRequestFactory::fromGlobals());
44
    }
45
46
    public function provideNoHostConfigs(): iterable
47
    {
48
        yield 'host not defined' => [[]];
49
        yield 'host is null' => [['public_hub_url' => null]];
50
    }
51
52
    /**
53
     * @test
54
     * @dataProvider provideValidConfigs
55
     */
56
    public function throwsExceptionWhenBuildingTokenFails(array $mercureConfig): void
57
    {
58
        $buildToken = $this->provider->buildSubscriptionToken(Argument::any())->willThrow(
59
            new RuntimeException('Error'),
60
        );
61
62
        $action = new MercureInfoAction($this->provider->reveal(), $mercureConfig);
63
64
        $this->expectException(MercureException::class);
65
        $buildToken->shouldBeCalledOnce();
66
67
        $action->handle(ServerRequestFactory::fromGlobals());
68
    }
69
70
    public function provideValidConfigs(): iterable
71
    {
72
        yield 'days not defined' => [['public_hub_url' => 'http://foobar.com']];
73
        yield 'days defined' => [['public_hub_url' => 'http://foobar.com', 'jwt_days_duration' => 20]];
74
    }
75
76
    /**
77
     * @test
78
     * @dataProvider provideDays
79
     */
80
    public function returnsExpectedInfoWhenEverythingIsOk(?int $days): void
81
    {
82
        $buildToken = $this->provider->buildSubscriptionToken(Argument::any())->willReturn('abc.123');
83
84
        $action = new MercureInfoAction($this->provider->reveal(), [
85
            'public_hub_url' => 'http://foobar.com',
86
            'jwt_days_duration' => $days,
87
        ]);
88
89
        /** @var JsonResponse $resp */
90
        $resp = $action->handle(ServerRequestFactory::fromGlobals());
91
        $payload = $resp->getPayload();
92
93
        self::assertArrayHasKey('mercureHubUrl', $payload);
94
        self::assertEquals('http://foobar.com/.well-known/mercure', $payload['mercureHubUrl']);
95
        self::assertArrayHasKey('token', $payload);
96
        self::assertArrayHasKey('jwtExpiration', $payload);
97
        self::assertEquals(
98
            Chronos::now()->addDays($days ?? 1)->startOfDay(),
99
            Chronos::parse($payload['jwtExpiration'])->startOfDay(),
100
        );
101
        $buildToken->shouldHaveBeenCalledOnce();
102
    }
103
104
    public function provideDays(): iterable
105
    {
106
        yield 'days not defined' => [null];
107
        yield 'days defined' => [10];
108
    }
109
}
110