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

namespace Doctrine\Tests\DBAL\Driver;

use Doctrine\DBAL\Driver\PDOException;
use Doctrine\Tests\DbalTestCase;
Sergei Morozov's avatar
Sergei Morozov committed
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.
     *
Sergei Morozov's avatar
Sergei Morozov committed
28
     * @var \PDOException|PHPUnit_Framework_MockObject_MockObject
29
     */
30
    private $wrappedException;
31 32 33

    protected function setUp()
    {
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 48
    }

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

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

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

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

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