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