AbstractDriverTest.php 8.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?php

namespace Doctrine\Tests\DBAL\Driver;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
use Doctrine\DBAL\VersionAwarePlatformDriver;
use Doctrine\Tests\DbalTestCase;

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';
27 28
    const EXCEPTION_DEADLOCK = 'Doctrine\DBAL\Exception\DeadlockException';
    const EXCEPTION_LOCK_WAIT_TIMEOUT = 'Doctrine\DBAL\Exception\LockWaitTimeoutException';
29 30 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

    /**
     * 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'
                )
            );
        }

62
        $driverException = $this->createMock('Doctrine\DBAL\Driver\DriverException');
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 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

        $driverException->expects($this->any())
            ->method('getErrorCode')
            ->will($this->returnValue('foo'));

        $driverException->expects($this->any())
            ->method('getSQLState')
            ->will($this->returnValue('bar'));

        $driverException->expects($this->any())
            ->method('getMessage')
            ->will($this->returnValue('baz'));

        $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);
    }

160 161 162 163 164
    /**
     * Factory method for creating the driver instance under test.
     *
     * @return \Doctrine\DBAL\Driver
     */
165 166
    abstract protected function createDriver();

167 168 169 170 171 172 173 174
    /**
     * 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
     */
175 176
    abstract protected function createPlatform();

177 178 179 180 181 182 183 184 185 186
    /**
     * 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
     */
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    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) {
212
                $driverException = $this->createMock('Doctrine\DBAL\Driver\DriverException');
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

                $driverException->expects($this->any())
                    ->method('getErrorCode')
                    ->will($this->returnValue($error[0]));

                $driverException->expects($this->any())
                    ->method('getSQLState')
                    ->will($this->returnValue($error[1]));

                $driverException->expects($this->any())
                    ->method('getMessage')
                    ->will($this->returnValue($error[2]));

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

        return $data;
    }
}