AbstractDriverTest.php 9.02 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\DBAL\Driver;

use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Driver\DriverException;
7 8 9
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
use Doctrine\DBAL\VersionAwarePlatformDriver;
use Doctrine\Tests\DbalTestCase;
10
use Throwable;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

abstract class AbstractDriverTest extends DbalTestCase
{
    const EXCEPTION_CONNECTION = 'Doctrine\DBAL\Exception\ConnectionException';
    const EXCEPTION_CONSTRAINT_VIOLATION = 'Doctrine\DBAL\Exception\ConstraintViolationException';
    const EXCEPTION_DATABASE_OBJECT_EXISTS = 'Doctrine\DBAL\Exception\DatabaseObjectExistsException';
    const EXCEPTION_DATABASE_OBJECT_NOT_FOUND = 'Doctrine\DBAL\Exception\DatabaseObjectNotFoundException';
    const EXCEPTION_DRIVER = 'Doctrine\DBAL\Exception\DriverException';
    const EXCEPTION_FOREIGN_KEY_CONSTRAINT_VIOLATION = 'Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException';
    const EXCEPTION_INVALID_FIELD_NAME = 'Doctrine\DBAL\Exception\InvalidFieldNameException';
    const EXCEPTION_NON_UNIQUE_FIELD_NAME = 'Doctrine\DBAL\Exception\NonUniqueFieldNameException';
    const EXCEPTION_NOT_NULL_CONSTRAINT_VIOLATION = 'Doctrine\DBAL\Exception\NotNullConstraintViolationException';
    const EXCEPTION_READ_ONLY = 'Doctrine\DBAL\Exception\ReadOnlyException';
    const EXCEPTION_SERVER = 'Doctrine\DBAL\Exception\ServerException';
    const EXCEPTION_SYNTAX_ERROR = 'Doctrine\DBAL\Exception\SyntaxErrorException';
    const EXCEPTION_TABLE_EXISTS = 'Doctrine\DBAL\Exception\TableExistsException';
    const EXCEPTION_TABLE_NOT_FOUND = 'Doctrine\DBAL\Exception\TableNotFoundException';
    const EXCEPTION_UNIQUE_CONSTRAINT_VIOLATION = 'Doctrine\DBAL\Exception\UniqueConstraintViolationException';
29 30
    const EXCEPTION_DEADLOCK = 'Doctrine\DBAL\Exception\DeadlockException';
    const EXCEPTION_LOCK_WAIT_TIMEOUT = 'Doctrine\DBAL\Exception\LockWaitTimeoutException';
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

    /**
     * The driver mock under test.
     *
     * @var \Doctrine\DBAL\Driver
     */
    protected $driver;

    protected function setUp()
    {
        parent::setUp();

        $this->driver = $this->createDriver();
    }

    public function testConvertsException()
    {
        if ( ! $this->driver instanceof ExceptionConverterDriver) {
            $this->markTestSkipped('This test is only intended for exception converter drivers.');
        }

        $data = $this->getExceptionConversions();

        if (empty($data)) {
            $this->fail(
                sprintf(
                    'No test data found for test %s. You have to return test data from %s.',
                    get_class($this) . '::' . __FUNCTION__,
                    get_class($this) . '::getExceptionConversionData'
                )
            );
        }

64 65 66 67 68 69
        $driverException = new class extends \Exception implements DriverException
        {
            public function __construct()
            {
                parent::__construct('baz');
            }
70

71 72 73 74 75 76 77
            /**
             * {@inheritDoc}
             */
            public function getErrorCode()
            {
                return 'foo';
            }
78

79 80 81 82 83 84 85 86
            /**
             * {@inheritDoc}
             */
            public function getSQLState()
            {
                return 'bar';
            }
        };
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

        $data[] = array($driverException, self::EXCEPTION_DRIVER);

        $message = 'DBAL exception message';

        foreach ($data as $item) {
            /** @var $driverException \Doctrine\DBAL\Driver\DriverException */
            list($driverException, $convertedExceptionClassName) = $item;

            $convertedException = $this->driver->convertException($message, $driverException);

            $this->assertSame($convertedExceptionClassName, get_class($convertedException));

            $this->assertSame($driverException->getErrorCode(), $convertedException->getErrorCode());
            $this->assertSame($driverException->getSQLState(), $convertedException->getSQLState());
            $this->assertSame($message, $convertedException->getMessage());
        }
    }

    public function testCreatesDatabasePlatformForVersion()
    {
        if ( ! $this->driver instanceof VersionAwarePlatformDriver) {
            $this->markTestSkipped('This test is only intended for version aware platform drivers.');
        }

        $data = $this->getDatabasePlatformsForVersions();

        if (empty($data)) {
            $this->fail(
                sprintf(
                    'No test data found for test %s. You have to return test data from %s.',
                    get_class($this) . '::' . __FUNCTION__,
                    get_class($this) . '::getDatabasePlatformsForVersions'
                )
            );
        }

        foreach ($data as $item) {
            $this->assertSame($item[1], get_class($this->driver->createDatabasePlatformForVersion($item[0])));
        }
    }

    /**
     * @expectedException \Doctrine\DBAL\DBALException
     */
    public function testThrowsExceptionOnCreatingDatabasePlatformsForInvalidVersion()
    {
        if ( ! $this->driver instanceof VersionAwarePlatformDriver) {
            $this->markTestSkipped('This test is only intended for version aware platform drivers.');
        }

        $this->driver->createDatabasePlatformForVersion('foo');
    }

    public function testReturnsDatabaseName()
    {
        $params = array(
            'user'     => 'foo',
            'password' => 'bar',
            'dbname'   => 'baz',
        );

        $connection = $this->getConnectionMock();

        $connection->expects($this->once())
            ->method('getParams')
            ->will($this->returnValue($params));

        $this->assertSame($params['dbname'], $this->driver->getDatabase($connection));
    }

    public function testReturnsDatabasePlatform()
    {
        $this->assertEquals($this->createPlatform(), $this->driver->getDatabasePlatform());
    }

    public function testReturnsSchemaManager()
    {
        $connection    = $this->getConnectionMock();
        $schemaManager = $this->driver->getSchemaManager($connection);

        $this->assertEquals($this->createSchemaManager($connection), $schemaManager);
        $this->assertAttributeSame($connection, '_conn', $schemaManager);
    }

172 173 174 175 176
    /**
     * Factory method for creating the driver instance under test.
     *
     * @return \Doctrine\DBAL\Driver
     */
177 178
    abstract protected function createDriver();

179 180 181 182 183 184 185 186
    /**
     * Factory method for creating the the platform instance return by the driver under test.
     *
     * The platform instance returned by this method must be the same as returned by
     * the driver's getDatabasePlatform() method.
     *
     * @return \Doctrine\DBAL\Platforms\AbstractPlatform
     */
187 188
    abstract protected function createPlatform();

189 190 191 192 193 194 195 196 197 198
    /**
     * Factory method for creating the the schema manager instance return by the driver under test.
     *
     * The schema manager instance returned by this method must be the same as returned by
     * the driver's getSchemaManager() method.
     *
     * @param Connection $connection The underlying connection to use.
     *
     * @return \Doctrine\DBAL\Schema\AbstractSchemaManager
     */
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    abstract protected function createSchemaManager(Connection $connection);

    protected function getConnectionMock()
    {
        return $this->getMockBuilder('Doctrine\DBAL\Connection')
            ->disableOriginalConstructor()
            ->getMock();
    }

    protected function getDatabasePlatformsForVersions()
    {
        return array();
    }

    protected function getExceptionConversionData()
    {
        return array();
    }

    private function getExceptionConversions()
    {
        $data = array();

        foreach ($this->getExceptionConversionData() as $convertedExceptionClassName => $errors) {
            foreach ($errors as $error) {
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                $driverException = new class ($error[0], $error[1], $error[2])
                    extends \Exception
                    implements DriverException
                {
                    /**
                     * @var mixed
                     */
                    private $errorCode;

                    /**
                     * @var mixed
                     */
                    private $sqlState;

                    public function __construct($errorCode, $sqlState, $message)
                    {
                        parent::__construct($message);

                        $this->errorCode = $errorCode;
                        $this->sqlState  = $sqlState;
                    }

                    /**
                     * {@inheritDoc}
                     */
                    public function getErrorCode()
                    {
                        return $this->errorCode;
                    }

                    /**
                     * {@inheritDoc}
                     */
                    public function getSQLState()
                    {
                        return $this->sqlState;
                    }
                };
262 263 264 265 266 267 268 269

                $data[] = array($driverException, $convertedExceptionClassName);
            }
        }

        return $data;
    }
}