Files

82 lines
2.8 KiB
PHP
Raw Permalink Normal View History

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;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\Attributes\Test;
2026-10-08 11:20:48 +02:00
use PHPUnit\Framework\Attributes\TestWith;
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
{
private MockObject&JwtProviderInterface $provider;
2020-04-12 14:19:51 +02:00
protected function setUp(): void
2020-04-12 14:19:51 +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
{
$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
{
$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
}
#[Test, AllowMockObjectsWithoutExpectations]
public function getRouteDefReturnsExpectedData(): void
{
self::assertEquals(
[
'name' => MercureInfoAction::class,
'middleware' => [MercureInfoAction::class],
'path' => '/mercure-info',
'allowed_methods' => ['GET'],
],
MercureInfoAction::getRouteDef(),
);
}
2020-04-12 14:19:51 +02:00
}