DBALExceptionTest.php 1.83 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\DBAL;

use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Exception\DriverException;
7 8 9
use Doctrine\DBAL\Driver\DriverException as InnerDriverException;
use Doctrine\Tests\DbalTestCase;
use Doctrine\DBAL\Driver;
10

11
class DBALExceptionTest extends DbalTestCase
12 13 14
{
    public function testDriverExceptionDuringQueryAcceptsBinaryData()
    {
15 16
        /* @var $driver Driver */
        $driver = $this->createMock(Driver::class);
17
        $e = DBALException::driverExceptionDuringQuery($driver, new \Exception, '', array('ABC', chr(128)));
18 19
        $this->assertContains('with params ["ABC", "\x80"]', $e->getMessage());
    }
20 21 22

    public function testAvoidOverWrappingOnDriverException()
    {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        /* @var $driver Driver */
        $driver = $this->createMock(Driver::class);
        $inner = new class extends \Exception implements InnerDriverException
        {
            /**
             * {@inheritDoc}
             */
            public function getErrorCode()
            {
            }

            /**
             * {@inheritDoc}
             */
            public function getSQLState()
            {
            }
        };
        $ex = new DriverException('', $inner);
42 43 44
        $e = DBALException::driverExceptionDuringQuery($driver, $ex, '');
        $this->assertSame($ex, $e);
    }
45 46 47 48 49 50

    public function testDriverRequiredWithUrl()
    {
        $url = 'mysql://localhost';
        $exception = DBALException::driverRequired($url);

51
        $this->assertInstanceOf(DBALException::class, $exception);
52 53 54
        $this->assertSame(
            sprintf(
                "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
55
                'is given to DriverManager::getConnection(). Given URL: %s',
56 57 58 59 60
                $url
            ),
            $exception->getMessage()
        );
    }
61
}