| 1 | <?php |
| 2 | |
| 3 | namespace Liuch\DmarcSrg; |
| 4 | |
| 5 | use Liuch\DmarcSrg\Exception\DatabaseExceptionFactory; |
| 6 | |
| 7 | class DatabaseExceptionFactoryTest extends \PHPUnit\Framework\TestCase |
| 8 | { |
| 9 | public function testWhenDatabaseAccessDenied(): void |
| 10 | { |
| 11 | $o = new \PDOException('', 1044); |
| 12 | $e = DatabaseExceptionFactory::fromException($o); |
| 13 | $this->checkException($e, 'Database access denied', $o); |
| 14 | |
| 15 | $o = new \PDOException('', 1045); |
| 16 | $e = DatabaseExceptionFactory::fromException($o); |
| 17 | $this->checkException($e, 'Database access denied', $o); |
| 18 | } |
| 19 | |
| 20 | public function testWhenDatabaseConnectionError(): void |
| 21 | { |
| 22 | $o = new \PDOException('', 2002); |
| 23 | $e = DatabaseExceptionFactory::fromException($o); |
| 24 | $this->checkException($e, 'Database connection error', $o); |
| 25 | |
| 26 | $o = new \PDOException('', 2006); |
| 27 | $e = DatabaseExceptionFactory::fromException($o); |
| 28 | $this->checkException($e, 'Database connection error', $o); |
| 29 | } |
| 30 | |
| 31 | public function testUnknownException(): void |
| 32 | { |
| 33 | $o = new \Exception('', 1044); |
| 34 | $e = DatabaseExceptionFactory::fromException($o); |
| 35 | $this->checkException($e, 'Database error', $o); |
| 36 | |
| 37 | $o = new \Exception('Some error'); |
| 38 | $e = DatabaseExceptionFactory::fromException($o); |
| 39 | $this->checkException($e, 'Database error', $o); |
| 40 | } |
| 41 | |
| 42 | private function checkException($e, $m, $o): void |
| 43 | { |
| 44 | $this->assertSame('Liuch\DmarcSrg\Exception\DatabaseException', get_class($e)); |
| 45 | $this->assertSame(-1, $e->getCode()); |
| 46 | $this->assertSame($m, $e->getMessage()); |
| 47 | $this->assertSame($o, $e->getPrevious()); |
| 48 | } |
| 49 | } |