shlink/module/Rest/test/Action/MercureInfoActionTest.php

85 lines
2.6 KiB
PHP
Raw Normal View History

2020-04-12 15:19:51 +03:00
<?php
declare(strict_types=1);
2020-04-12 19:01:13 +03:00
namespace ShlinkioTest\Shlink\Rest\Action;
2020-04-12 15:19:51 +03:00
use Cake\Chronos\Chronos;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\MockObject\MockObject;
2020-04-12 15:19:51 +03:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\Mercure\JwtProviderInterface;
use Shlinkio\Shlink\Rest\Action\MercureInfoAction;
use Shlinkio\Shlink\Rest\Exception\MercureException;
class MercureInfoActionTest extends TestCase
{
2022-10-24 20:53:13 +03:00
private MockObject & JwtProviderInterface $provider;
2020-04-12 15:19:51 +03:00
protected function setUp(): void
2020-04-12 15:19:51 +03:00
{
$this->provider = $this->createMock(JwtProviderInterface::class);
2020-04-12 15:19:51 +03:00
}
/**
* @test
* @dataProvider provideNoHostConfigs
*/
public function throwsExceptionWhenConfigDoesNotHavePublicHost(array $mercureConfig): void
{
$this->provider->expects($this->never())->method('buildSubscriptionToken');
2020-04-12 15:19:51 +03:00
$action = new MercureInfoAction($this->provider, $mercureConfig);
2020-04-12 15:19:51 +03:00
$this->expectException(MercureException::class);
$action->handle(ServerRequestFactory::fromGlobals());
}
2023-02-09 11:32:38 +03:00
public static function provideNoHostConfigs(): iterable
2020-04-12 15:19:51 +03:00
{
yield 'host not defined' => [[]];
yield 'host is null' => [['public_hub_url' => null]];
}
public function provideValidConfigs(): iterable
{
yield 'days not defined' => [['public_hub_url' => 'http://foobar.com']];
yield 'days defined' => [['public_hub_url' => 'http://foobar.com', 'jwt_days_duration' => 20]];
}
/**
* @test
* @dataProvider provideDays
*/
public function returnsExpectedInfoWhenEverythingIsOk(?int $days): void
{
$this->provider->expects($this->once())->method('buildSubscriptionToken')->willReturn('abc.123');
2020-04-12 15:19:51 +03:00
$action = new MercureInfoAction($this->provider, [
2020-04-12 15:19:51 +03:00
'public_hub_url' => 'http://foobar.com',
'jwt_days_duration' => $days,
]);
/** @var JsonResponse $resp */
$resp = $action->handle(ServerRequestFactory::fromGlobals());
$payload = $resp->getPayload();
2020-10-04 01:35:14 +03:00
self::assertArrayHasKey('mercureHubUrl', $payload);
self::assertEquals('http://foobar.com/.well-known/mercure', $payload['mercureHubUrl']);
self::assertArrayHasKey('token', $payload);
self::assertArrayHasKey('jwtExpiration', $payload);
self::assertEquals(
Chronos::now()->addDays($days ?? 1)->startOfDay(),
2020-04-12 15:19:51 +03:00
Chronos::parse($payload['jwtExpiration'])->startOfDay(),
);
}
2023-02-09 11:32:38 +03:00
public static function provideDays(): iterable
2020-04-12 15:19:51 +03:00
{
yield 'days not defined' => [null];
yield 'days defined' => [10];
}
}