PDOExceptionTest.php 1.69 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests\DBAL\Driver;

use Doctrine\DBAL\Driver\PDOException;
use Doctrine\Tests\DbalTestCase;
7
use PHPUnit\Framework\MockObject\MockObject;
8
use function extension_loaded;
9 10 11

class PDOExceptionTest extends DbalTestCase
{
Sergei Morozov's avatar
Sergei Morozov committed
12
    public const ERROR_CODE = 666;
13

Sergei Morozov's avatar
Sergei Morozov committed
14
    public const MESSAGE = 'PDO Exception';
15

Sergei Morozov's avatar
Sergei Morozov committed
16
    public const SQLSTATE = 28000;
17 18 19 20

    /**
     * The PDO exception wrapper under test.
     *
Sergei Morozov's avatar
Sergei Morozov committed
21
     * @var PDOException
22 23 24 25 26 27
     */
    private $exception;

    /**
     * The wrapped PDO exception mock.
     *
28
     * @var \PDOException|MockObject
29
     */
30
    private $wrappedException;
31

32
    protected function setUp() : void
33
    {
Sergei Morozov's avatar
Sergei Morozov committed
34
        if (! extension_loaded('PDO')) {
35 36 37
            $this->markTestSkipped('PDO is not installed.');
        }

38 39
        parent::setUp();

40
        $this->wrappedException = new \PDOException(self::MESSAGE, self::SQLSTATE);
41

Sergei Morozov's avatar
Sergei Morozov committed
42
        $this->wrappedException->errorInfo = [self::SQLSTATE, self::ERROR_CODE];
43

44
        $this->exception = new PDOException($this->wrappedException);
45 46
    }

47
    public function testReturnsCode() : void
48
    {
49
        self::assertSame(self::SQLSTATE, $this->exception->getCode());
50 51
    }

52
    public function testReturnsErrorCode() : void
53
    {
54
        self::assertSame(self::ERROR_CODE, $this->exception->getErrorCode());
55 56
    }

57
    public function testReturnsMessage() : void
58
    {
59
        self::assertSame(self::MESSAGE, $this->exception->getMessage());
60 61
    }

62
    public function testReturnsSQLState() : void
63
    {
64
        self::assertSame(self::SQLSTATE, $this->exception->getSQLState());
65
    }
66

67
    public function testOriginalExceptionIsInChain() : void
68
    {
69
        self::assertSame($this->wrappedException, $this->exception->getPrevious());
70
    }
71
}