ExceptionTest.php 9.34 KB
Newer Older
1 2 3 4
<?php
namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\DBAL\DBALException;
5
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
6 7 8

class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
9 10 11 12
    public function setUp()
    {
        parent::setUp();

13
        if ( !($this->_conn->getDriver() instanceof ExceptionConverterDriver)) {
14 15 16 17
            $this->markTestSkipped('Driver does not support special exception handling.');
        }
    }

18 19 20 21 22 23 24 25 26 27 28 29
    public function testDuplicateKeyException()
    {
        $table = new \Doctrine\DBAL\Schema\Table("duplicatekey_table");
        $table->addColumn('id', 'integer', array());
        $table->setPrimaryKey(array('id'));

        foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
            $this->_conn->executeQuery($sql);
        }

        $this->_conn->insert("duplicatekey_table", array('id' => 1));

30
        $this->setExpectedException('\Doctrine\DBAL\Exception\DuplicateKeyException', null, DBALException::ERROR_DUPLICATE_KEY);
31 32
        $this->_conn->insert("duplicatekey_table", array('id' => 1));
    }
33 34 35 36 37 38 39 40

    public function testUnknownTableException()
    {
        $sql = "SELECT * FROM unknown_table";

        $this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_UNKNOWN_TABLE);
        $this->_conn->executeQuery($sql);
    }
41

42
    public function testTableAlreadyExistsException()
43
    {
44
        $table = new \Doctrine\DBAL\Schema\Table("alreadyexist_table");
45 46 47 48 49 50 51 52 53 54 55
        $table->addColumn('id', 'integer', array());
        $table->setPrimaryKey(array('id'));

        $this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_TABLE_ALREADY_EXISTS);
        foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
            $this->_conn->executeQuery($sql);
        }
        foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
            $this->_conn->executeQuery($sql);
        }
    }
56 57 58 59 60 61 62 63

    public function testForeignKeyContraintException()
    {
        if ( ! $this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
            $this->markTestSkipped("Only fails on platforms with foreign key constraints.");
        }

        $schema = new \Doctrine\DBAL\Schema\Schema();
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        $table = $schema->createTable("constraint_error_table");
        $table->addColumn('id', 'integer', array());
        $table->setPrimaryKey(array('id'));

        $owningTable = $schema->createTable("owning_table");
        $owningTable->addColumn('id', 'integer', array());
        $owningTable->addColumn('constraint_id', 'integer', array());
        $owningTable->setPrimaryKey(array('id'));
        $owningTable->addForeignKeyConstraint($table, array('constraint_id'), array('id'));

        foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) {
            $this->_conn->executeQuery($sql);
        }

        $this->_conn->insert("constraint_error_table", array('id' => 1));
        $this->_conn->insert("owning_table", array('id' => 1, 'constraint_id' => 1));

82
        $this->setExpectedException('\Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException', null, DBALException::ERROR_FOREIGN_KEY_CONSTRAINT);
83
        $this->_conn->delete('constraint_error_table', array('id' => 1));
84
    }
85

86 87 88 89 90 91 92 93 94 95 96 97 98
    public function testNotNullException()
    {
        $schema = new \Doctrine\DBAL\Schema\Schema();

        $table = $schema->createTable("notnull_table");
        $table->addColumn('id', 'integer', array());
        $table->addColumn('value', 'integer', array('notnull' => true));
        $table->setPrimaryKey(array('id'));

        foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) {
            $this->_conn->executeQuery($sql);
        }

99
        $this->setExpectedException('\Doctrine\DBAL\Exception\NotNullableException', null, DBALException::ERROR_NOT_NULL);
100
        $this->_conn->insert("notnull_table", array('id' => 1, 'value' => null));
101
    }
102

103 104 105 106
    public function testBadFieldNameException()
    {
        $schema = new \Doctrine\DBAL\Schema\Schema();

107 108
        $table = $schema->createTable("bad_fieldname_table");
        $table->addColumn('id', 'integer', array());
109 110 111 112 113 114

        foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) {
            $this->_conn->executeQuery($sql);
        }

        $this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_BAD_FIELD_NAME);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        $this->_conn->insert("bad_fieldname_table", array('name' => 5));
    }

    public function testNonUniqueFieldNameException()
    {
        $schema = new \Doctrine\DBAL\Schema\Schema();

        $table = $schema->createTable("ambiguous_list_table");
        $table->addColumn('id', 'integer');

        $table2 = $schema->createTable("ambiguous_list_table_2");
        $table2->addColumn('id', 'integer');

        foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) {
            $this->_conn->executeQuery($sql);
        }

        $sql = 'SELECT id FROM ambiguous_list_table, ambiguous_list_table_2';
        $this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_NON_UNIQUE_FIELD_NAME);
        $this->_conn->executeQuery($sql);
135 136 137 138 139 140 141 142 143 144 145 146 147
    }

    public function testNotUniqueException()
    {
        $schema = new \Doctrine\DBAL\Schema\Schema();

        $table = $schema->createTable("unique_field_table");
        $table->addColumn('id', 'integer');
        $table->addUniqueIndex(array('id'));

        foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) {
            $this->_conn->executeQuery($sql);
        }
148

149
        $this->_conn->insert("unique_field_table", array('id' => 5));
150
        $this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_DUPLICATE_KEY);
151
        $this->_conn->insert("unique_field_table", array('id' => 5));
152 153
    }

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    public function testSyntaxErrorException()
    {
        $table = new \Doctrine\DBAL\Schema\Table("syntax_error_table");
        $table->addColumn('id', 'integer', array());
        $table->setPrimaryKey(array('id'));

        foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
            $this->_conn->executeQuery($sql);
        }

        $sql = 'SELECT id FRO syntax_error_table';
        $this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_SYNTAX);
        $this->_conn->executeQuery($sql);
    }

169 170 171 172 173 174 175 176 177 178 179 180
    /**
     * @dataProvider getSqLiteOpenConnection
     */
    public function testConnectionExceptionSqLite($mode, $exceptionCode)
    {
        if ($this->_conn->getDatabasePlatform()->getName() != 'sqlite') {
            $this->markTestSkipped("Only fails this way on sqlite");
        }

        $filename = sprintf('%s/%s', sys_get_temp_dir(), 'doctrine_failed_connection.db');

        if (file_exists($filename)) {
181 182
            unlink($filename);
        }
183 184 185 186 187

        touch($filename);
        chmod($filename, $mode);

        $params = array(
188 189 190
            'driver' => 'pdo_sqlite',
            'path'   => $filename,
        );
191 192 193 194 195 196 197 198
        $conn = \Doctrine\DBAL\DriverManager::getConnection($params);

        $schema = new \Doctrine\DBAL\Schema\Schema();
        $table = $schema->createTable("no_connection");
        $table->addColumn('id', 'integer');

        $this->setExpectedException('\Doctrine\DBAL\DBALException', null, $exceptionCode);
        foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) {
199 200
            $conn->executeQuery($sql);
        }
201 202
    }

203 204 205 206 207 208
    public function getSqLiteOpenConnection()
    {
        return array(
            array(0000, DBALException::ERROR_UNABLE_TO_OPEN),
            array(0444, DBALException::ERROR_WRITE_READONLY),
        );
209 210
    }

211 212 213 214 215 216 217 218 219
    /**
     * @dataProvider getConnectionParams
     */
    public function testConnectionException($params, $exceptionCode)
    {
        if ($this->_conn->getDatabasePlatform()->getName() == 'sqlite') {
            $this->markTestSkipped("Only skipped if platform is not sqlite");
        }

220 221 222 223
        if ($this->_conn->getDatabasePlatform()->getName() == 'drizzle') {
            $this->markTestSkipped("Drizzle does not always support authentication");
        }

224
        if ($this->_conn->getDatabasePlatform()->getName() == 'postgresql' && isset($params['password'])) {
Benjamin Eberlei's avatar
Benjamin Eberlei committed
225 226 227
            $this->markTestSkipped("Does not work on Travis");
        }

228 229 230 231 232 233 234 235 236
        $defaultParams = $this->_conn->getParams();
        $params = array_merge($defaultParams, $params);

        $conn = \Doctrine\DBAL\DriverManager::getConnection($params);

        $schema = new \Doctrine\DBAL\Schema\Schema();
        $table = $schema->createTable("no_connection");
        $table->addColumn('id', 'integer');

237 238 239 240 241 242 243
        try {
            foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) {
                $conn->executeQuery($sql);
            }
            $this->fail("Did not trigger DBALException with code " . $exceptionCode);
        } catch (\Doctrine\DBAL\DBALException $e) {
            $this->assertEquals($exceptionCode, $e->getCode(), "Got exception " . $e->getMessage());
244
        }
245 246 247 248
    }

    public function getConnectionParams()
    {
249 250 251 252 253
        return array(
            array(array('user' => 'not_existing'), DBALException::ERROR_ACCESS_DENIED),
            array(array('password' => 'really_not'), DBALException::ERROR_ACCESS_DENIED),
            array(array('host' => 'localnope'), DBALException::ERROR_ACCESS_DENIED),
        );
254
    }
255
}
256