ConnectionTest.php 9.79 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\DBAL\ConnectionException;
6
use Doctrine\DBAL\DriverManager;
7
use Doctrine\DBAL\Types\Type;
8 9 10

class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
11
    protected function setUp()
12 13 14 15 16
    {
        $this->resetSharedConn();
        parent::setUp();
    }

17
    protected function tearDown()
18 19 20 21 22
    {
        parent::tearDown();
        $this->resetSharedConn();
    }

23 24
    public function testGetWrappedConnection()
    {
25
        $this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $this->_conn->getWrappedConnection());
26 27 28 29 30 31 32 33 34 35
    }

    public function testCommitWithRollbackOnlyThrowsException()
    {
        $this->_conn->beginTransaction();
        $this->_conn->setRollbackOnly();
        $this->setExpectedException('Doctrine\DBAL\ConnectionException');
        $this->_conn->commit();
    }

36 37 38 39 40
    public function testTransactionNestingBehavior()
    {
        try {
            $this->_conn->beginTransaction();
            $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
41

42 43 44 45 46 47
            try {
                $this->_conn->beginTransaction();
                $this->assertEquals(2, $this->_conn->getTransactionNestingLevel());
                throw new \Exception;
                $this->_conn->commit(); // never reached
            } catch (\Exception $e) {
48
                $this->_conn->rollBack();
49
                $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
50
                //no rethrow
51
            }
52
            $this->assertTrue($this->_conn->isRollbackOnly());
53

54 55
            $this->_conn->commit(); // should throw exception
            $this->fail('Transaction commit after failed nested transaction should fail.');
56 57
        } catch (ConnectionException $e) {
            $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
58
            $this->_conn->rollBack();
59 60 61
            $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
        }
    }
62 63 64

    public function testTransactionNestingBehaviorWithSavepoints()
    {
65 66 67 68 69 70 71 72 73
        if (!$this->_conn->getDatabasePlatform()->supportsSavepoints()) {
            $this->markTestSkipped('This test requires the platform to support savepoints.');
        }

        $this->_conn->setNestTransactionsWithSavepoints(true);
        try {
            $this->_conn->beginTransaction();
            $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());

74 75
            try {
                $this->_conn->beginTransaction();
76 77 78 79 80 81 82 83
                $this->assertEquals(2, $this->_conn->getTransactionNestingLevel());
                $this->_conn->beginTransaction();
                $this->assertEquals(3, $this->_conn->getTransactionNestingLevel());
                $this->_conn->commit();
                $this->assertEquals(2, $this->_conn->getTransactionNestingLevel());
                throw new \Exception;
                $this->_conn->commit(); // never reached
            } catch (\Exception $e) {
84
                $this->_conn->rollBack();
85
                $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
86 87 88 89 90 91
                //no rethrow
            }
            $this->assertFalse($this->_conn->isRollbackOnly());
            try {
                $this->_conn->setNestTransactionsWithSavepoints(false);
                $this->fail('Should not be able to disable savepoints in usage for nested transactions inside an open transaction.');
92
            } catch (ConnectionException $e) {
93
                $this->assertTrue($this->_conn->getNestTransactionsWithSavepoints());
94
            }
95 96 97
            $this->_conn->commit(); // should not throw exception
        } catch (ConnectionException $e) {
            $this->fail('Transaction commit after failed nested transaction should not fail when using savepoints.');
98
            $this->_conn->rollBack();
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
    public function testTransactionNestingBehaviorCantBeChangedInActiveTransaction()
    {
        if (!$this->_conn->getDatabasePlatform()->supportsSavepoints()) {
            $this->markTestSkipped('This test requires the platform to support savepoints.');
        }

        $this->_conn->beginTransaction();
        try {
            $this->_conn->setNestTransactionsWithSavepoints(true);
            $this->fail('An exception should have been thrown by chaning the nesting transaction behavior within an transaction.');
        } catch(ConnectionException $e) {
            $this->_conn->rollBack();
        }
    }

    public function testSetNestedTransactionsThroughSavepointsNotSupportedThrowsException()
    {
        if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
            $this->markTestSkipped('This test requires the platform not to support savepoints.');
        }

        $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");

        $this->_conn->setNestTransactionsWithSavepoints(true);
    }

    public function testCreateSavepointsNotSupportedThrowsException()
    {
        if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
            $this->markTestSkipped('This test requires the platform not to support savepoints.');
        }

        $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");

        $this->_conn->createSavepoint('foo');
    }

    public function testReleaseSavepointsNotSupportedThrowsException()
    {
        if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
            $this->markTestSkipped('This test requires the platform not to support savepoints.');
        }

        $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");

        $this->_conn->releaseSavepoint('foo');
    }

    public function testRollbackSavepointsNotSupportedThrowsException()
    {
        if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
            $this->markTestSkipped('This test requires the platform not to support savepoints.');
        }

        $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");

        $this->_conn->rollbackSavepoint('foo');
    }

161
    public function testTransactionBehaviorWithRollback()
162 163 164 165
    {
        try {
            $this->_conn->beginTransaction();
            $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
166

167
            throw new \Exception;
168

169
            $this->_conn->commit(); // never reached
170 171
        } catch (\Exception $e) {
            $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
172
            $this->_conn->rollBack();
173 174
            $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
        }
175 176 177 178
    }

    public function testTransactionBehaviour()
    {
179 180 181 182 183
        try {
            $this->_conn->beginTransaction();
            $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
            $this->_conn->commit();
        } catch (\Exception $e) {
184
            $this->_conn->rollBack();
185 186
            $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
        }
187 188

        $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
189 190 191 192
    }

    public function testTransactionalWithException()
    {
193 194
        try {
            $this->_conn->transactional(function($conn) {
till's avatar
till committed
195
                /* @var $conn \Doctrine\DBAL\Connection */
196
                $conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
197 198
                throw new \RuntimeException("Ooops!");
            });
199
            $this->fail('Expected exception');
200 201 202
        } catch (\RuntimeException $expected) {
            $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
        }
203
    }
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    public function testTransactionalWithThrowable()
    {
        if (version_compare(PHP_VERSION, '7.0', '<')) {
            $this->markTestSkipped('Only for PHP 7.0 and above.');
        }

        try {
            $this->_conn->transactional(function($conn) {
                /* @var $conn \Doctrine\DBAL\Connection */
                $conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
                throw new \Error("Ooops!");
            });
            $this->fail('Expected exception');
        } catch (\Error $expected) {
            $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
        }
    }

223 224 225
    public function testTransactional()
    {
        $this->_conn->transactional(function($conn) {
till's avatar
till committed
226
            /* @var $conn \Doctrine\DBAL\Connection */
227
            $conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
228 229
        });
    }
230

231 232 233 234 235 236 237 238
    public function testTransactionalReturnValue()
    {
        $res = $this->_conn->transactional(function($conn) {
            return 42;
        });
        $this->assertEquals(42, $res);
    }

239 240 241 242 243 244 245
    /**
     * Tests that the quote function accepts DBAL and PDO types.
     */
    public function testQuote()
    {
        $this->assertEquals($this->_conn->quote("foo", Type::STRING), $this->_conn->quote("foo", \PDO::PARAM_STR));
    }
246

247
    public function testPingDoesTriggersConnect()
248 249
    {
        $this->assertTrue($this->_conn->ping());
250
        $this->assertTrue($this->_conn->isConnected());
251
    }
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

    /**
     * @group DBAL-1025
     */
    public function testConnectWithoutExplicitDatabaseName()
    {
        if (in_array($this->_conn->getDatabasePlatform()->getName(), array('oracle', 'db2'), true)) {
            $this->markTestSkipped('Platform does not support connecting without database name.');
        }

        $params = $this->_conn->getParams();
        unset($params['dbname']);

        $connection = DriverManager::getConnection(
            $params,
            $this->_conn->getConfiguration(),
            $this->_conn->getEventManager()
        );

        $this->assertTrue($connection->connect());
272 273

        $connection->close();
274
    }
275
}