2024-06-08 23:58:45 +00:00
|
|
|
<?php
|
2024-07-21 01:47:21 +00:00
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
namespace Test\Unit;
|
|
|
|
|
|
|
|
use BitBadger\PDODocument\DocumentException;
|
|
|
|
use Exception;
|
2024-06-21 13:46:41 +00:00
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
2024-06-08 23:58:45 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the DocumentException class
|
|
|
|
*/
|
2024-06-21 13:46:41 +00:00
|
|
|
#[TestDox('Document Exception (Unit tests)')]
|
2024-06-08 23:58:45 +00:00
|
|
|
class DocumentExceptionTest extends TestCase
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
public function testConstructorSucceedsWithCodeAndPriorException(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$priorEx = new Exception('Uh oh');
|
|
|
|
$ex = new DocumentException('Test Exception', 17, $priorEx);
|
|
|
|
$this->assertNotNull($ex, 'The exception should not have been null');
|
|
|
|
$this->assertEquals('Test Exception', $ex->getMessage(), 'Message not filled properly');
|
|
|
|
$this->assertEquals(17, $ex->getCode(), 'Code not filled properly');
|
|
|
|
$this->assertSame($priorEx, $ex->getPrevious(), 'Prior exception not filled properly');
|
|
|
|
}
|
|
|
|
|
2024-07-21 01:47:21 +00:00
|
|
|
public function testConstructorSucceedsWithoutCodeAndPriorException(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$ex = new DocumentException('Oops');
|
|
|
|
$this->assertNotNull($ex, 'The exception should not have been null');
|
|
|
|
$this->assertEquals('Oops', $ex->getMessage(), 'Message not filled properly');
|
|
|
|
$this->assertEquals(0, $ex->getCode(), 'Code not filled properly');
|
|
|
|
$this->assertNull($ex->getPrevious(), 'Prior exception should have been null');
|
|
|
|
}
|
|
|
|
|
2024-09-22 21:44:07 +00:00
|
|
|
#[TestDox('toString() succeeds without code')]
|
2024-07-21 01:47:21 +00:00
|
|
|
public function testToStringSucceedsWithoutCode(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$ex = new DocumentException('Test failure');
|
|
|
|
$this->assertEquals("BitBadger\PDODocument\DocumentException: Test failure\n", "$ex",
|
|
|
|
'toString not generated correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-22 21:44:07 +00:00
|
|
|
#[TestDox('toString() succeeds with code')]
|
2024-07-21 01:47:21 +00:00
|
|
|
public function testToStringSucceedsWithCode(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$ex = new DocumentException('Oof', -6);
|
|
|
|
$this->assertEquals("BitBadger\PDODocument\DocumentException: [-6] Oof\n", "$ex",
|
|
|
|
'toString not generated correctly');
|
|
|
|
}
|
|
|
|
}
|