2018-11-17 12:23:25 -06:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2018-11-17 12:23:25 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Exception;
|
|
|
|
|
|
|
|
use Exception;
|
2019-11-24 16:24:53 -06:00
|
|
|
use Fig\Http\Message\StatusCodeInterface;
|
2018-11-17 12:23:25 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
|
|
|
use Throwable;
|
|
|
|
|
2019-12-01 03:14:29 -06:00
|
|
|
use function sprintf;
|
|
|
|
|
2018-11-17 12:23:25 -06:00
|
|
|
class InvalidUrlExceptionTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider providePrevious
|
|
|
|
*/
|
2019-02-17 13:28:34 -06:00
|
|
|
public function properlyCreatesExceptionFromUrl(?Throwable $prev): void
|
2018-11-17 12:23:25 -06:00
|
|
|
{
|
2019-12-01 03:14:29 -06:00
|
|
|
$url = 'http://the_url.com';
|
|
|
|
$expectedMessage = sprintf('Provided URL %s is invalid. Try with a different one.', $url);
|
|
|
|
$e = InvalidUrlException::fromUrl($url, $prev);
|
2018-11-17 12:23:25 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($expectedMessage, $e->getMessage());
|
|
|
|
self::assertEquals($expectedMessage, $e->getDetail());
|
|
|
|
self::assertEquals('Invalid URL', $e->getTitle());
|
2022-08-13 10:15:04 -05:00
|
|
|
self::assertEquals('https://shlink.io/api/error/invalid-url', $e->getType());
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals(['url' => $url], $e->getAdditionalData());
|
|
|
|
self::assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getCode());
|
|
|
|
self::assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getStatus());
|
|
|
|
self::assertEquals($prev, $e->getPrevious());
|
2018-11-17 12:23:25 -06:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
public function providePrevious(): iterable
|
2018-11-17 12:23:25 -06:00
|
|
|
{
|
2019-02-17 13:28:34 -06:00
|
|
|
yield 'null previous' => [null];
|
|
|
|
yield 'instance previous' => [new Exception('Previous error', 10)];
|
2018-11-17 12:23:25 -06:00
|
|
|
}
|
|
|
|
}
|