2019-02-27 00:31:07 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2019-02-27 00:31:07 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Exception;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use LogicException;
|
2023-02-09 22:42:18 +03:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2019-02-27 00:31:07 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\RuntimeException;
|
2022-09-18 19:44:01 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\UnlocatableIpType;
|
2019-02-27 00:31:07 +03:00
|
|
|
use Throwable;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2019-02-27 00:31:07 +03:00
|
|
|
class IpCannotBeLocatedExceptionTest extends TestCase
|
|
|
|
{
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2019-02-27 00:31:07 +03:00
|
|
|
public function forEmptyAddressInitializesException(): void
|
|
|
|
{
|
|
|
|
$e = IpCannotBeLocatedException::forEmptyAddress();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertTrue($e->isNonLocatableAddress());
|
|
|
|
self::assertEquals('Ignored visit with no IP address', $e->getMessage());
|
|
|
|
self::assertEquals(0, $e->getCode());
|
|
|
|
self::assertNull($e->getPrevious());
|
2022-09-18 19:44:01 +03:00
|
|
|
self::assertEquals(UnlocatableIpType::EMPTY_ADDRESS, $e->type);
|
2019-02-27 00:31:07 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2019-02-27 00:31:07 +03:00
|
|
|
public function forLocalhostInitializesException(): void
|
|
|
|
{
|
|
|
|
$e = IpCannotBeLocatedException::forLocalhost();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertTrue($e->isNonLocatableAddress());
|
|
|
|
self::assertEquals('Ignored localhost address', $e->getMessage());
|
|
|
|
self::assertEquals(0, $e->getCode());
|
|
|
|
self::assertNull($e->getPrevious());
|
2022-09-18 19:44:01 +03:00
|
|
|
self::assertEquals(UnlocatableIpType::LOCALHOST, $e->type);
|
2019-02-27 00:31:07 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test, DataProvider('provideErrors')]
|
2019-02-27 00:31:07 +03:00
|
|
|
public function forErrorInitializesException(Throwable $prev): void
|
|
|
|
{
|
|
|
|
$e = IpCannotBeLocatedException::forError($prev);
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertFalse($e->isNonLocatableAddress());
|
|
|
|
self::assertEquals('An error occurred while locating IP', $e->getMessage());
|
|
|
|
self::assertEquals($prev->getCode(), $e->getCode());
|
|
|
|
self::assertSame($prev, $e->getPrevious());
|
2022-09-18 19:44:01 +03:00
|
|
|
self::assertEquals(UnlocatableIpType::ERROR, $e->type);
|
2019-02-27 00:31:07 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 11:32:38 +03:00
|
|
|
public static function provideErrors(): iterable
|
2019-02-27 00:31:07 +03:00
|
|
|
{
|
|
|
|
yield 'Simple exception with positive code' => [new Exception('Some message', 100)];
|
|
|
|
yield 'Runtime exception with negative code' => [new RuntimeException('Something went wrong', -50)];
|
|
|
|
yield 'Logic exception with default code' => [new LogicException('Conditions unmet')];
|
|
|
|
}
|
|
|
|
}
|