pdo-document/tests/Unit/DocumentExceptionTest.php

41 lines
1.3 KiB
PHP
Raw Normal View History

2024-10-05 19:43:41 +00:00
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\PDODocument\DocumentException;
pest()->group('unit');
describe('Constructor', function () {
test('fills code and prior exception if provided', function () {
$priorEx = new Exception('Uh oh');
expect(new DocumentException('Test Exception', 17, $priorEx))
->not->toBeNull()
->getMessage()->toBe('Test Exception')
->getCode()->toBe(17)
->getPrevious()->toBe($priorEx);
});
test('uses expected code and prior exception if not provided', function () {
expect(new DocumentException('Oops'))
->not->toBeNull()
->getMessage()->toBe('Oops')
->getCode()->toBe(0)
->getPrevious()->toBeNull();
});
});
describe('->__toString()', function () {
test('excludes code if 0', function () {
$ex = new DocumentException('Test failure');
expect("$ex")->toBe("BitBadger\PDODocument\DocumentException: Test failure\n");
});
test('includes code if non-zero', function () {
$ex = new DocumentException('Oof', -6);
expect("$ex")->toBe("BitBadger\PDODocument\DocumentException: [-6] Oof\n");
});
});