shlink/module/Rest/test/Exception/MissingAuthenticationExceptionTest.php

72 lines
2.2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Exception;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Rest\Exception\MissingAuthenticationException;
use function implode;
use function sprintf;
class MissingAuthenticationExceptionTest extends TestCase
{
/**
* @test
2021-12-15 00:21:53 +03:00
* @dataProvider provideExpectedHeaders
*/
2021-01-21 21:26:19 +03:00
public function exceptionIsProperlyCreatedFromExpectedHeaders(array $expectedHeaders): void
{
$expectedMessage = sprintf(
'Expected one of the following authentication headers, ["%s"], but none were provided',
2021-01-21 21:26:19 +03:00
implode('", "', $expectedHeaders),
);
2021-01-21 21:26:19 +03:00
$e = MissingAuthenticationException::forHeaders($expectedHeaders);
2021-01-21 21:26:19 +03:00
$this->assertCommonExceptionShape($e);
2020-10-04 01:35:14 +03:00
self::assertEquals($expectedMessage, $e->getMessage());
self::assertEquals($expectedMessage, $e->getDetail());
2021-12-15 00:21:53 +03:00
self::assertEquals(['expectedHeaders' => $expectedHeaders], $e->getAdditionalData());
}
2023-02-09 11:32:38 +03:00
public static function provideExpectedHeaders(): iterable
{
yield [['foo', 'bar']];
yield [['something']];
yield [[]];
yield [['foo', 'bar', 'baz']];
}
2021-01-21 21:26:19 +03:00
/**
* @test
* @dataProvider provideExpectedParam
*/
public function exceptionIsProperlyCreatedFromExpectedQueryParam(string $param): void
{
$expectedMessage = sprintf('Expected authentication to be provided in "%s" query param', $param);
$e = MissingAuthenticationException::forQueryParam($param);
$this->assertCommonExceptionShape($e);
self::assertEquals($expectedMessage, $e->getMessage());
self::assertEquals($expectedMessage, $e->getDetail());
self::assertEquals(['param' => $param], $e->getAdditionalData());
}
2023-02-09 11:32:38 +03:00
public static function provideExpectedParam(): iterable
2021-01-21 21:26:19 +03:00
{
yield ['foo'];
yield ['bar'];
yield ['something'];
}
private function assertCommonExceptionShape(MissingAuthenticationException $e): void
{
self::assertEquals('Invalid authorization', $e->getTitle());
self::assertEquals('https://shlink.io/api/error/missing-authentication', $e->getType());
2021-01-21 21:26:19 +03:00
self::assertEquals(401, $e->getStatus());
}
}