packages/ztd-query-pdo-adapter/tests/Unit/ZtdPdoExceptionTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit;
6
7use PDOException;
8use PHPUnit\Framework\Attributes\CoversClass;
9use PHPUnit\Framework\TestCase;
10use RuntimeException;
11use ZtdQuery\Adapter\Pdo\ZtdPdoException;
12
13#[CoversClass(ZtdPdoException::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\ZtdPdoStatement::class)]
15#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\ZtdPdo::class)]
16#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Driver\PdoConnection::class)]
17#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Driver\PdoStatement::class)]
18#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\StatementExecution::class)]
19#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\Bindings::class)]
20#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\BufferedRow::class)]
21#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\DriverSessionFactory::class)]
22#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\ParameterKind::class)]
23#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\ParameterBinder::class)]
24#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\PreparedQuery::class)]
25#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\ConnectionExecution::class)]
26final class ZtdPdoExceptionTest extends TestCase
27{
28 public function testItIsCaughtByCodeThatCatchesPdosOwnFailures(): void
29 {
30 $exception = new ZtdPdoException('test');
31
32 self::assertContains(PDOException::class, class_parents($exception));
33 }
34
35 public function testMessageIsSet(): void
36 {
37 $exception = new ZtdPdoException('something went wrong');
38
39 self::assertSame('something went wrong', $exception->getMessage());
40 }
41
42 public function testPreviousExceptionIsPreserved(): void
43 {
44 $previous = new RuntimeException('cause');
45 $exception = new ZtdPdoException('wrapped', 0, $previous);
46
47 self::assertSame($previous, $exception->getPrevious());
48 }
49
50 public function testDefaultCodeIsZero(): void
51 {
52 $exception = new ZtdPdoException('test');
53
54 self::assertSame(0, $exception->getCode());
55 }
56
57 public function testExplicitCodeIsPreserved(): void
58 {
59 $exception = new ZtdPdoException('test', 42);
60
61 self::assertSame(42, $exception->getCode());
62 }
63}
64