smeserver-dmarc-srg/root/opt/dmarc-srg/tests/classes/Exception/DatabaseExceptionFactoryTest.php

50 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2023-06-21 15:19:40 +02:00
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Exception\DatabaseExceptionFactory;
class DatabaseExceptionFactoryTest extends \PHPUnit\Framework\TestCase
{
public function testWhenDatabaseAccessDenied(): void
{
$o = new \PDOException('', 1044);
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database access denied', $o);
$o = new \PDOException('', 1045);
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database access denied', $o);
}
public function testWhenDatabaseConnectionError(): void
{
$o = new \PDOException('', 2002);
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database connection error', $o);
$o = new \PDOException('', 2006);
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database connection error', $o);
}
public function testUnknownException(): void
{
$o = new \Exception('', 1044);
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database error', $o);
$o = new \Exception('Some error');
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database error', $o);
}
private function checkException($e, $m, $o): void
{
$this->assertSame('Liuch\DmarcSrg\Exception\DatabaseException', get_class($e));
$this->assertSame(-1, $e->getCode());
$this->assertSame($m, $e->getMessage());
$this->assertSame($o, $e->getPrevious());
}
}