Commit 46684ea5 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Added a bunch of functional tests for the public read, write and transactional...

Added a bunch of functional tests for the public read, write and transactional public DBAL API, passing all the current drivers

Added a Write Test
parent dbb5795c
...@@ -753,7 +753,7 @@ class Connection implements DriverConnection ...@@ -753,7 +753,7 @@ class Connection implements DriverConnection
public function commit() public function commit()
{ {
if ($this->_transactionNestingLevel == 0) { if ($this->_transactionNestingLevel == 0) {
throw ConnectionException::commitFailedNoActiveTransaction(); throw ConnectionException::noActiveTransaction();
} }
if ($this->_isRollbackOnly) { if ($this->_isRollbackOnly) {
throw ConnectionException::commitFailedRollbackOnly(); throw ConnectionException::commitFailedRollbackOnly();
...@@ -779,7 +779,7 @@ class Connection implements DriverConnection ...@@ -779,7 +779,7 @@ class Connection implements DriverConnection
public function rollback() public function rollback()
{ {
if ($this->_transactionNestingLevel == 0) { if ($this->_transactionNestingLevel == 0) {
throw ConnectionException::rollbackFailedNoActiveTransaction(); throw ConnectionException::noActiveTransaction();
} }
$this->connect(); $this->connect();
......
...@@ -11,6 +11,11 @@ use Doctrine\DBAL\Events; ...@@ -11,6 +11,11 @@ use Doctrine\DBAL\Events;
class ConnectionTest extends \Doctrine\Tests\DbalTestCase class ConnectionTest extends \Doctrine\Tests\DbalTestCase
{ {
/**
* @var Doctrine\DBAL\Connection
*/
protected $_conn = null;
public function setUp() public function setUp()
{ {
$params = array( $params = array(
...@@ -23,6 +28,47 @@ class ConnectionTest extends \Doctrine\Tests\DbalTestCase ...@@ -23,6 +28,47 @@ class ConnectionTest extends \Doctrine\Tests\DbalTestCase
$this->_conn = \Doctrine\DBAL\DriverManager::getConnection($params); $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() public function testGetHost()
{ {
$this->assertEquals('localhost', $this->_conn->getHost()); $this->assertEquals('localhost', $this->_conn->getHost());
......
...@@ -28,6 +28,7 @@ class AllTests ...@@ -28,6 +28,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\Db2SchemaManagerTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\Db2SchemaManagerTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\ConnectionTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Functional\ConnectionTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\DataAccessTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Functional\DataAccessTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\WriteTest');
return $suite; return $suite;
} }
......
...@@ -8,6 +8,24 @@ require_once __DIR__ . '/../../TestInit.php'; ...@@ -8,6 +8,24 @@ require_once __DIR__ . '/../../TestInit.php';
class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase 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() public function testTransactionNestingBehavior()
{ {
...@@ -36,7 +54,7 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -36,7 +54,7 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
} }
} }
public function testTransactionBehavior() public function testTransactionBehaviorWithRollback()
{ {
try { try {
$this->_conn->beginTransaction(); $this->_conn->beginTransaction();
...@@ -50,7 +68,10 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -50,7 +68,10 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->_conn->rollback(); $this->_conn->rollback();
$this->assertEquals(0, $this->_conn->getTransactionNestingLevel()); $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
} }
}
public function testTransactionBehaviour()
{
try { try {
$this->_conn->beginTransaction(); $this->_conn->beginTransaction();
$this->assertEquals(1, $this->_conn->getTransactionNestingLevel()); $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
...@@ -61,6 +82,10 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -61,6 +82,10 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
} }
$this->assertEquals(0, $this->_conn->getTransactionNestingLevel()); $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
}
public function testTransactionalWithException()
{
try { try {
$this->_conn->transactional(function($conn) { $this->_conn->transactional(function($conn) {
$conn->executeQuery("select 1"); $conn->executeQuery("select 1");
...@@ -71,4 +96,10 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -71,4 +96,10 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
} }
} }
public function testTransactional()
{
$this->_conn->transactional(function($conn) {
$conn->executeQuery("select 1");
});
}
} }
\ No newline at end of file
...@@ -25,6 +25,101 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -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() public function testFetchAll()
{ {
$sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; $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 ...@@ -60,4 +155,16 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->assertEquals('foo', $row[1]); $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
<?php
namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\Types\Type;
require_once __DIR__ . '/../../TestInit.php';
class WriteTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
public function setUp()
{
parent::setUp();
try {
/* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
$table = new \Doctrine\DBAL\Schema\Table("write_table");
$table->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
...@@ -12,6 +12,12 @@ class DbalFunctionalTestCase extends DbalTestCase ...@@ -12,6 +12,12 @@ class DbalFunctionalTestCase extends DbalTestCase
*/ */
protected $_conn; protected $_conn;
protected function resetSharedConn()
{
$this->sharedFixture['conn'] = null;
self::$_sharedConn = null;
}
protected function setUp() protected function setUp()
{ {
if (isset($this->sharedFixture['conn'])) { if (isset($this->sharedFixture['conn'])) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment