2020-04-12 14:19:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-04-12 18:01:13 +02:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action;
|
2020-04-12 14:19:51 +02:00
|
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
|
use Laminas\Diactoros\Response\JsonResponse;
|
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
2025-12-15 14:17:36 +01:00
|
|
|
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
|
2023-02-09 20:42:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2026-10-08 11:20:48 +02:00
|
|
|
use PHPUnit\Framework\Attributes\TestWith;
|
2022-10-23 22:04:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2020-04-12 14:19:51 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use Shlinkio\Shlink\Common\Mercure\JwtProviderInterface;
|
2026-10-08 11:20:48 +02:00
|
|
|
use Shlinkio\Shlink\Common\Mercure\MercureOptions;
|
|
|
|
|
use Shlinkio\Shlink\Common\Mercure\MercureVersion;
|
2020-04-12 14:19:51 +02:00
|
|
|
use Shlinkio\Shlink\Rest\Action\MercureInfoAction;
|
|
|
|
|
use Shlinkio\Shlink\Rest\Exception\MercureException;
|
|
|
|
|
|
|
|
|
|
class MercureInfoActionTest extends TestCase
|
|
|
|
|
{
|
2026-05-22 08:42:47 +02:00
|
|
|
private MockObject&JwtProviderInterface $provider;
|
2020-04-12 14:19:51 +02:00
|
|
|
|
2022-09-11 12:02:49 +02:00
|
|
|
protected function setUp(): void
|
2020-04-12 14:19:51 +02:00
|
|
|
{
|
2022-10-23 22:04:00 +02:00
|
|
|
$this->provider = $this->createMock(JwtProviderInterface::class);
|
2020-04-12 14:19:51 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-08 11:20:48 +02:00
|
|
|
#[Test]
|
|
|
|
|
public function throwsExceptionWhenConfigDoesNotHavePublicHost(): void
|
2020-04-12 14:19:51 +02:00
|
|
|
{
|
2022-10-23 22:04:00 +02:00
|
|
|
$this->provider->expects($this->never())->method('buildSubscriptionToken');
|
2020-04-12 14:19:51 +02:00
|
|
|
|
2026-10-08 11:20:48 +02:00
|
|
|
$action = new MercureInfoAction($this->provider, new MercureOptions());
|
2020-04-12 14:19:51 +02:00
|
|
|
|
|
|
|
|
$this->expectException(MercureException::class);
|
|
|
|
|
|
|
|
|
|
$action->handle(ServerRequestFactory::fromGlobals());
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 11:20:48 +02:00
|
|
|
#[Test]
|
2026-10-11 10:09:08 +02:00
|
|
|
#[TestWith([MercureVersion::LEGACY])]
|
|
|
|
|
#[TestWith([MercureVersion::STABLE])]
|
2026-10-08 11:20:48 +02:00
|
|
|
public function returnsExpectedInfoWhenEverythingIsOk(MercureVersion $version): void
|
2020-04-12 14:19:51 +02:00
|
|
|
{
|
2022-10-23 22:04:00 +02:00
|
|
|
$this->provider->expects($this->once())->method('buildSubscriptionToken')->willReturn('abc.123');
|
2020-04-12 14:19:51 +02:00
|
|
|
|
2026-10-08 11:20:48 +02:00
|
|
|
$action = new MercureInfoAction($this->provider, new MercureOptions(
|
|
|
|
|
publicHubUrl: 'http://foobar.com',
|
|
|
|
|
version: $version,
|
|
|
|
|
));
|
2020-04-12 14:19:51 +02:00
|
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
|
|
|
|
$resp = $action->handle(ServerRequestFactory::fromGlobals());
|
|
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
|
2020-10-04 00:35:14 +02: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(
|
2026-10-08 11:20:48 +02:00
|
|
|
Chronos::now()->addDays(1)->startOfDay(),
|
2020-04-12 14:19:51 +02:00
|
|
|
Chronos::parse($payload['jwtExpiration'])->startOfDay(),
|
|
|
|
|
);
|
2026-10-08 11:20:48 +02:00
|
|
|
self::assertEquals($version->value, $payload['version']);
|
2020-04-12 14:19:51 +02:00
|
|
|
}
|
2025-11-18 08:56:09 +01:00
|
|
|
|
2025-12-15 14:17:36 +01:00
|
|
|
#[Test, AllowMockObjectsWithoutExpectations]
|
2025-11-18 08:56:09 +01:00
|
|
|
public function getRouteDefReturnsExpectedData(): void
|
|
|
|
|
{
|
2026-05-22 08:42:47 +02:00
|
|
|
self::assertEquals(
|
|
|
|
|
[
|
|
|
|
|
'name' => MercureInfoAction::class,
|
|
|
|
|
'middleware' => [MercureInfoAction::class],
|
|
|
|
|
'path' => '/mercure-info',
|
|
|
|
|
'allowed_methods' => ['GET'],
|
|
|
|
|
],
|
|
|
|
|
MercureInfoAction::getRouteDef(),
|
|
|
|
|
);
|
2025-11-18 08:56:09 +01:00
|
|
|
}
|
2020-04-12 14:19:51 +02:00
|
|
|
}
|