2019-12-01 03:14:29 -06:00
|
|
|
<?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
|
|
|
|
* @dataProvider provideExpectedTypes
|
|
|
|
*/
|
2021-01-21 12:26:19 -06:00
|
|
|
public function exceptionIsProperlyCreatedFromExpectedHeaders(array $expectedHeaders): void
|
2019-12-01 03:14:29 -06:00
|
|
|
{
|
|
|
|
$expectedMessage = sprintf(
|
|
|
|
'Expected one of the following authentication headers, ["%s"], but none were provided',
|
2021-01-21 12:26:19 -06:00
|
|
|
implode('", "', $expectedHeaders),
|
2019-12-01 03:14:29 -06:00
|
|
|
);
|
|
|
|
|
2021-01-21 12:26:19 -06:00
|
|
|
$e = MissingAuthenticationException::forHeaders($expectedHeaders);
|
2019-12-01 03:14:29 -06:00
|
|
|
|
2021-01-21 12:26:19 -06:00
|
|
|
$this->assertCommonExceptionShape($e);
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($expectedMessage, $e->getMessage());
|
|
|
|
self::assertEquals($expectedMessage, $e->getDetail());
|
2021-01-21 12:26:19 -06:00
|
|
|
self::assertEquals([
|
|
|
|
'expectedTypes' => $expectedHeaders,
|
|
|
|
'expectedHeaders' => $expectedHeaders,
|
|
|
|
], $e->getAdditionalData());
|
2019-12-01 03:14:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideExpectedTypes(): iterable
|
|
|
|
{
|
|
|
|
yield [['foo', 'bar']];
|
|
|
|
yield [['something']];
|
|
|
|
yield [[]];
|
|
|
|
yield [['foo', 'bar', 'baz']];
|
|
|
|
}
|
2021-01-21 12:26:19 -06: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());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideExpectedParam(): iterable
|
|
|
|
{
|
|
|
|
yield ['foo'];
|
|
|
|
yield ['bar'];
|
|
|
|
yield ['something'];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function assertCommonExceptionShape(MissingAuthenticationException $e): void
|
|
|
|
{
|
|
|
|
self::assertEquals('Invalid authorization', $e->getTitle());
|
|
|
|
self::assertEquals('INVALID_AUTHORIZATION', $e->getType());
|
|
|
|
self::assertEquals(401, $e->getStatus());
|
|
|
|
}
|
2019-12-01 03:14:29 -06:00
|
|
|
}
|