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

namespace Doctrine\Tests\DBAL\Functional;

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

require_once __DIR__ . '/../../TestInit.php';

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

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

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

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

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

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

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

    public function testTransactionNestingBehaviorWithSavepoints()
    {
66 67 68 69 70 71 72 73 74
        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());

75 76
            try {
                $this->_conn->beginTransaction();
77 78 79 80 81 82 83 84 85
                $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) {
                $this->_conn->rollback();
86
                $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
87 88 89 90 91 92
                //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.');
93
            } catch (ConnectionException $e) {
94
                $this->assertTrue($this->_conn->getNestTransactionsWithSavepoints());
95
            }
96 97 98 99
            $this->_conn->commit(); // should not throw exception
        } catch (ConnectionException $e) {
            $this->fail('Transaction commit after failed nested transaction should not fail when using savepoints.');
            $this->_conn->rollback();
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
    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');
    }

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

168
            throw new \Exception;
169

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

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

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

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

    public function testTransactional()
    {
        $this->_conn->transactional(function($conn) {
till's avatar
till committed
208
            /* @var $conn \Doctrine\DBAL\Connection */
209
            $conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
210 211
        });
    }
212 213 214 215 216 217 218 219

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

221
    public function testPingDoesTriggersConnect()
222 223
    {
        $this->assertTrue($this->_conn->ping());
224
        $this->assertTrue($this->_conn->isConnected());
225
    }
226
}