diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index a2b277ebd..3a2c1b9f9 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -753,7 +753,7 @@ class Connection implements DriverConnection public function commit() { if ($this->_transactionNestingLevel == 0) { - throw ConnectionException::commitFailedNoActiveTransaction(); + throw ConnectionException::noActiveTransaction(); } if ($this->_isRollbackOnly) { throw ConnectionException::commitFailedRollbackOnly(); @@ -779,7 +779,7 @@ class Connection implements DriverConnection public function rollback() { if ($this->_transactionNestingLevel == 0) { - throw ConnectionException::rollbackFailedNoActiveTransaction(); + throw ConnectionException::noActiveTransaction(); } $this->connect(); diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php index 6390bf374..88eb23286 100644 --- a/tests/Doctrine/Tests/DBAL/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -11,6 +11,11 @@ use Doctrine\DBAL\Events; class ConnectionTest extends \Doctrine\Tests\DbalTestCase { + /** + * @var Doctrine\DBAL\Connection + */ + protected $_conn = null; + public function setUp() { $params = array( @@ -23,6 +28,47 @@ class ConnectionTest extends \Doctrine\Tests\DbalTestCase $this->_conn = \Doctrine\DBAL\DriverManager::getConnection($params); } + public function testIsConnected() + { + $this->assertFalse($this->_conn->isConnected()); + } + + public function testNoTransactionActiveByDefault() + { + $this->assertFalse($this->_conn->isTransactionActive()); + } + + public function testCommitWithNoActiveTransaction_ThrowsException() + { + $this->setExpectedException('Doctrine\DBAL\ConnectionException'); + $this->_conn->commit(); + } + + public function testRollbackWithNoActiveTransaction_ThrowsException() + { + $this->setExpectedException('Doctrine\DBAL\ConnectionException'); + $this->_conn->rollback(); + } + + public function testSetRollbackOnlyNoActiveTransaction_ThrowsException() + { + $this->setExpectedException('Doctrine\DBAL\ConnectionException'); + $this->_conn->setRollbackOnly(); + } + + public function testIsRollbackOnlyNoActiveTransaction_ThrowsException() + { + $this->setExpectedException('Doctrine\DBAL\ConnectionException'); + $this->_conn->isRollbackOnly(); + } + + public function testGetConfiguration() + { + $config = $this->_conn->getConfiguration(); + + $this->assertType('Doctrine\DBAL\Configuration', $config); + } + public function testGetHost() { $this->assertEquals('localhost', $this->_conn->getHost()); diff --git a/tests/Doctrine/Tests/DBAL/Functional/AllTests.php b/tests/Doctrine/Tests/DBAL/Functional/AllTests.php index 88574060c..573177070 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/AllTests.php +++ b/tests/Doctrine/Tests/DBAL/Functional/AllTests.php @@ -28,6 +28,7 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\Db2SchemaManagerTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Functional\ConnectionTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Functional\DataAccessTest'); + $suite->addTestSuite('Doctrine\Tests\DBAL\Functional\WriteTest'); return $suite; } diff --git a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php index 26663fcd0..5cb6d4167 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php @@ -8,7 +8,25 @@ require_once __DIR__ . '/../../TestInit.php'; class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase { - + public function setUp() + { + $this->resetSharedConn(); + parent::setUp(); + } + + public function testGetWrappedConnection() + { + $this->assertType('Doctrine\DBAL\Driver\Connection', $this->_conn->getWrappedConnection()); + } + + public function testCommitWithRollbackOnlyThrowsException() + { + $this->_conn->beginTransaction(); + $this->_conn->setRollbackOnly(); + $this->setExpectedException('Doctrine\DBAL\ConnectionException'); + $this->_conn->commit(); + } + public function testTransactionNestingBehavior() { try { @@ -36,7 +54,7 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase } } - public function testTransactionBehavior() + public function testTransactionBehaviorWithRollback() { try { $this->_conn->beginTransaction(); @@ -50,7 +68,10 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase $this->_conn->rollback(); $this->assertEquals(0, $this->_conn->getTransactionNestingLevel()); } - + } + + public function testTransactionBehaviour() + { try { $this->_conn->beginTransaction(); $this->assertEquals(1, $this->_conn->getTransactionNestingLevel()); @@ -61,6 +82,10 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase } $this->assertEquals(0, $this->_conn->getTransactionNestingLevel()); + } + + public function testTransactionalWithException() + { try { $this->_conn->transactional(function($conn) { $conn->executeQuery("select 1"); @@ -70,5 +95,11 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase $this->assertEquals(0, $this->_conn->getTransactionNestingLevel()); } } - + + public function testTransactional() + { + $this->_conn->transactional(function($conn) { + $conn->executeQuery("select 1"); + }); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php b/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php index 7d5123d66..0e9693a14 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php @@ -25,6 +25,101 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase } } + public function testPrepareWithBindValue() + { + $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; + $stmt = $this->_conn->prepare($sql); + $this->assertType('Doctrine\DBAL\Statement', $stmt); + + $stmt->bindValue(1, 1); + $stmt->bindValue(2, 'foo'); + $stmt->execute(); + + $row = $stmt->fetch(\PDO::FETCH_ASSOC); + $row = array_change_key_case($row, \CASE_LOWER); + $this->assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row); + } + + public function testPrepareWithBindParam() + { + $paramInt = 1; + $paramStr = 'foo'; + + $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; + $stmt = $this->_conn->prepare($sql); + $this->assertType('Doctrine\DBAL\Statement', $stmt); + + $stmt->bindParam(1, $paramInt); + $stmt->bindParam(2, $paramStr); + $stmt->execute(); + + $row = $stmt->fetch(\PDO::FETCH_ASSOC); + $row = array_change_key_case($row, \CASE_LOWER); + $this->assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row); + } + + public function testPrepareWithFetchAll() + { + $paramInt = 1; + $paramStr = 'foo'; + + $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; + $stmt = $this->_conn->prepare($sql); + $this->assertType('Doctrine\DBAL\Statement', $stmt); + + $stmt->bindParam(1, $paramInt); + $stmt->bindParam(2, $paramStr); + $stmt->execute(); + + $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); + $rows[0] = array_change_key_case($rows[0], \CASE_LOWER); + $this->assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $rows[0]); + } + + public function testPrepareWithFetchColumn() + { + $paramInt = 1; + $paramStr = 'foo'; + + $sql = "SELECT test_int FROM fetch_table WHERE test_int = ? AND test_string = ?"; + $stmt = $this->_conn->prepare($sql); + $this->assertType('Doctrine\DBAL\Statement', $stmt); + + $stmt->bindParam(1, $paramInt); + $stmt->bindParam(2, $paramStr); + $stmt->execute(); + + $column = $stmt->fetchColumn(); + $this->assertEquals(1, $column); + } + + public function testPrepareWithQuoted() + { + $table = 'fetch_table'; + $paramInt = 1; + $paramStr = 'foo'; + + $sql = "SELECT test_int, test_string FROM " . $this->_conn->quoteIdentifier($table) . " ". + "WHERE test_int = " . $this->_conn->quote($paramInt) . " AND test_string = " . $this->_conn->quote($paramStr); + $stmt = $this->_conn->prepare($sql); + $this->assertType('Doctrine\DBAL\Statement', $stmt); + } + + public function testPrepareWithExecuteParams() + { + $paramInt = 1; + $paramStr = 'foo'; + + $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; + $stmt = $this->_conn->prepare($sql); + $this->assertType('Doctrine\DBAL\Statement', $stmt); + $stmt->execute(array($paramInt, $paramStr)); + + $row = $stmt->fetch(\PDO::FETCH_ASSOC); + $row = array_change_key_case($row, \CASE_LOWER); + $this->assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row); + } + public function testFetchAll() { $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; @@ -60,4 +155,16 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase $this->assertEquals('foo', $row[1]); } + public function testFetchColumn() + { + $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; + $testInt = $this->_conn->fetchColumn($sql, array(1, 'foo'), 0); + + $this->assertEquals(1, $testInt); + + $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; + $testString = $this->_conn->fetchColumn($sql, array(1, 'foo'), 1); + + $this->assertEquals('foo', $testString); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php b/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php new file mode 100644 index 000000000..3b2538fee --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php @@ -0,0 +1,122 @@ +addColumn('test_int', 'integer'); + $table->addColumn('test_string', 'string', array('notnull' => false)); + + $sm = $this->_conn->getSchemaManager(); + $sm->createTable($table); + } catch(\Exception $e) { + + } + $this->_conn->executeUpdate('DELETE FROM write_table'); + } + + public function testExecuteUpdate() + { + $sql = "INSERT INTO " . $this->_conn->quoteIdentifier('write_table') . " ( " . + $this->_conn->quoteIdentifier('test_int') . " ) VALUES ( " . $this->_conn->quote(1) . ")"; + $affected = $this->_conn->executeUpdate($sql); + + $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!"); + } + + public function testExecuteUpdateWithTypes() + { + $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; + $affected = $this->_conn->executeUpdate($sql, array(1, 'foo'), array(\PDO::PARAM_INT, \PDO::PARAM_STR)); + + $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!"); + } + + public function testPrepareRowCountReturnsAffectedRows() + { + $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; + $stmt = $this->_conn->prepare($sql); + + $stmt->bindValue(1, 1); + $stmt->bindValue(2, "foo"); + $stmt->execute(); + + $this->assertEquals(1, $stmt->rowCount()); + } + + public function testPrepareWithPdoTypes() + { + $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; + $stmt = $this->_conn->prepare($sql); + + $stmt->bindValue(1, 1, \PDO::PARAM_INT); + $stmt->bindValue(2, "foo", \PDO::PARAM_STR); + $stmt->execute(); + + $this->assertEquals(1, $stmt->rowCount()); + } + + public function testPrepareWithDbalTypes() + { + $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; + $stmt = $this->_conn->prepare($sql); + + $stmt->bindValue(1, 1, Type::getType('integer')); + $stmt->bindValue(2, "foo", Type::getType('string')); + $stmt->execute(); + + $this->assertEquals(1, $stmt->rowCount()); + } + + public function testPrepareWithDbalTypeNames() + { + $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; + $stmt = $this->_conn->prepare($sql); + + $stmt->bindValue(1, 1, 'integer'); + $stmt->bindValue(2, "foo", 'string'); + $stmt->execute(); + + $this->assertEquals(1, $stmt->rowCount()); + } + + public function insertRows() + { + $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 1))); + $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2))); + } + + public function testInsert() + { + $this->insertRows(); + } + + public function testDelete() + { + $this->insertRows(); + + $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2))); + $this->assertEquals(1, count($this->_conn->fetchAll('SELECT * FROM write_table'))); + + $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1))); + $this->assertEquals(0, count($this->_conn->fetchAll('SELECT * FROM write_table'))); + } + + public function testUpdate() + { + $this->insertRows(); + + $this->assertEquals(1, $this->_conn->update('write_table', array('test_int' => 2), array('test_int' => 1))); + $this->assertEquals(2, $this->_conn->update('write_table', array('test_int' => 3), array('test_int' => 2))); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/DbalFunctionalTestCase.php b/tests/Doctrine/Tests/DbalFunctionalTestCase.php index e5400211c..c4705a25e 100644 --- a/tests/Doctrine/Tests/DbalFunctionalTestCase.php +++ b/tests/Doctrine/Tests/DbalFunctionalTestCase.php @@ -12,6 +12,12 @@ class DbalFunctionalTestCase extends DbalTestCase */ protected $_conn; + protected function resetSharedConn() + { + $this->sharedFixture['conn'] = null; + self::$_sharedConn = null; + } + protected function setUp() { if (isset($this->sharedFixture['conn'])) {