2018-11-17 21:23:25 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2018-11-17 21:23:25 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Exception;
|
|
|
|
|
|
|
|
use Exception;
|
2019-11-25 01:24:53 +03:00
|
|
|
use Fig\Http\Message\StatusCodeInterface;
|
2018-11-17 21:23:25 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
|
|
|
use Throwable;
|
|
|
|
|
2019-12-01 12:14:29 +03:00
|
|
|
use function sprintf;
|
|
|
|
|
2018-11-17 21:23:25 +03:00
|
|
|
class InvalidUrlExceptionTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider providePrevious
|
|
|
|
*/
|
2019-02-17 22:28:34 +03:00
|
|
|
public function properlyCreatesExceptionFromUrl(?Throwable $prev): void
|
2018-11-17 21:23:25 +03:00
|
|
|
{
|
2019-12-01 12:14:29 +03: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 21:23:25 +03:00
|
|
|
|
2019-12-01 12:14:29 +03:00
|
|
|
$this->assertEquals($expectedMessage, $e->getMessage());
|
|
|
|
$this->assertEquals($expectedMessage, $e->getDetail());
|
|
|
|
$this->assertEquals('Invalid URL', $e->getTitle());
|
|
|
|
$this->assertEquals('INVALID_URL', $e->getType());
|
|
|
|
$this->assertEquals(['url' => $url], $e->getAdditionalData());
|
2019-11-25 01:24:53 +03:00
|
|
|
$this->assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getCode());
|
2019-12-01 12:14:29 +03:00
|
|
|
$this->assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getStatus());
|
2018-11-17 21:23:25 +03:00
|
|
|
$this->assertEquals($prev, $e->getPrevious());
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
public function providePrevious(): iterable
|
2018-11-17 21:23:25 +03:00
|
|
|
{
|
2019-02-17 22:28:34 +03:00
|
|
|
yield 'null previous' => [null];
|
|
|
|
yield 'instance previous' => [new Exception('Previous error', 10)];
|
2018-11-17 21:23:25 +03:00
|
|
|
}
|
|
|
|
}
|