Commit c3fb1842 authored by Sergei Morozov's avatar Sergei Morozov Committed by Marco Pivetta

[DBAL-2546] Refactored unit tests and added some more for edge cases

parent 7e93a36e
...@@ -2,21 +2,27 @@ ...@@ -2,21 +2,27 @@
namespace Doctrine\Tests\DBAL\Functional; namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
class StatementTest extends \Doctrine\Tests\DbalFunctionalTestCase class StatementTest extends \Doctrine\Tests\DbalFunctionalTestCase
{ {
public function testStatementIsReusableAfterClosingCursor() protected function setUp()
{ {
$sm = $this->_conn->getSchemaManager(); parent::setUp();
$table = new Table('stmt_test_reusable');
$table = new Table('stmt_test');
$table->addColumn('id', 'integer'); $table->addColumn('id', 'integer');
$sm->createTable($table); $this->_conn->getSchemaManager()->dropAndCreateTable($table);
$this->_conn->insert('stmt_test_reusable', array('id' => 1)); }
$this->_conn->insert('stmt_test_reusable', array('id' => 2));
$stmt = $this->_conn->prepare('SELECT id FROM stmt_test_reusable ORDER BY id'); public function testStatementIsReusableAfterClosingCursor()
{
$this->_conn->insert('stmt_test', array('id' => 1));
$this->_conn->insert('stmt_test', array('id' => 2));
$stmt = $this->_conn->prepare('SELECT id FROM stmt_test ORDER BY id');
$stmt->execute(); $stmt->execute();
...@@ -32,25 +38,10 @@ class StatementTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -32,25 +38,10 @@ class StatementTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->assertEquals(2, $id); $this->assertEquals(2, $id);
} }
public function testClosedCursorDoesNotContainResults()
{
$sm = $this->_conn->getSchemaManager();
$table = new Table('stmt_test_no_results');
$table->addColumn('id', 'integer');
$sm->createTable($table);
$this->_conn->insert('stmt_test_no_results', array('id' => 1));
$stmt = $this->_conn->prepare('SELECT id FROM stmt_test_no_results');
$stmt->execute();
$stmt->closeCursor();
$this->assertFalse($stmt->fetchColumn());
}
public function testReuseStatementWithLongerResults() public function testReuseStatementWithLongerResults()
{ {
$sm = $this->_conn->getSchemaManager(); $sm = $this->_conn->getSchemaManager();
$table = new Table('stmt_test_longer_results'); $table = new Table('stmt_longer_results');
$table->addColumn('param', 'string'); $table->addColumn('param', 'string');
$table->addColumn('val', 'text'); $table->addColumn('val', 'text');
$sm->createTable($table); $sm->createTable($table);
...@@ -59,9 +50,9 @@ class StatementTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -59,9 +50,9 @@ class StatementTest extends \Doctrine\Tests\DbalFunctionalTestCase
'param' => 'param1', 'param' => 'param1',
'val' => 'X', 'val' => 'X',
); );
$this->_conn->insert('stmt_test_longer_results', $row1); $this->_conn->insert('stmt_longer_results', $row1);
$stmt = $this->_conn->prepare('SELECT param, val FROM stmt_test_longer_results ORDER BY param'); $stmt = $this->_conn->prepare('SELECT param, val FROM stmt_longer_results ORDER BY param');
$stmt->execute(); $stmt->execute();
$this->assertArraySubset(array( $this->assertArraySubset(array(
array('param1', 'X'), array('param1', 'X'),
...@@ -71,7 +62,7 @@ class StatementTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -71,7 +62,7 @@ class StatementTest extends \Doctrine\Tests\DbalFunctionalTestCase
'param' => 'param2', 'param' => 'param2',
'val' => 'A bit longer value', 'val' => 'A bit longer value',
); );
$this->_conn->insert('stmt_test_longer_results', $row2); $this->_conn->insert('stmt_longer_results', $row2);
$stmt->execute(); $stmt->execute();
$this->assertArraySubset(array( $this->assertArraySubset(array(
...@@ -87,7 +78,7 @@ class StatementTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -87,7 +78,7 @@ class StatementTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->iniSet('memory_limit', '4G'); $this->iniSet('memory_limit', '4G');
$sm = $this->_conn->getSchemaManager(); $sm = $this->_conn->getSchemaManager();
$table = new Table('stmt_test_long_blob'); $table = new Table('stmt_long_blob');
$table->addColumn('contents', 'blob', array( $table->addColumn('contents', 'blob', array(
'length' => 0xFFFFFFFF, 'length' => 0xFFFFFFFF,
)); ));
...@@ -110,11 +101,11 @@ d+N0hqezcjblboJ3Bj8ARJilHX4FAAA= ...@@ -110,11 +101,11 @@ d+N0hqezcjblboJ3Bj8ARJilHX4FAAA=
EOF EOF
); );
$this->_conn->insert('stmt_test_long_blob', array( $this->_conn->insert('stmt_long_blob', array(
'contents' => $contents, 'contents' => $contents,
), array(\PDO::PARAM_LOB)); ), array(\PDO::PARAM_LOB));
$stmt = $this->_conn->prepare('SELECT contents FROM stmt_test_long_blob'); $stmt = $this->_conn->prepare('SELECT contents FROM stmt_long_blob');
$stmt->execute(); $stmt->execute();
$stream = Type::getType('blob') $stream = Type::getType('blob')
...@@ -127,33 +118,27 @@ EOF ...@@ -127,33 +118,27 @@ EOF
public function testIncompletelyFetchedStatementDoesNotBlockConnection() public function testIncompletelyFetchedStatementDoesNotBlockConnection()
{ {
$table = new Table('stmt_test_non_fetched'); $this->_conn->insert('stmt_test', array('id' => 1));
$table->addColumn('id', 'integer'); $this->_conn->insert('stmt_test', array('id' => 2));
$this->_conn->getSchemaManager()->createTable($table);
$this->_conn->insert('stmt_test_non_fetched', array('id' => 1));
$this->_conn->insert('stmt_test_non_fetched', array('id' => 2));
$stmt1 = $this->_conn->prepare('SELECT id FROM stmt_test_non_fetched'); $stmt1 = $this->_conn->prepare('SELECT id FROM stmt_test');
$stmt1->execute(); $stmt1->execute();
$stmt1->fetch(); $stmt1->fetch();
$stmt1->execute(); $stmt1->execute();
// fetching only one record out of two // fetching only one record out of two
$stmt1->fetch(); $stmt1->fetch();
$stmt2 = $this->_conn->prepare('SELECT id FROM stmt_test_non_fetched WHERE id = ?'); $stmt2 = $this->_conn->prepare('SELECT id FROM stmt_test WHERE id = ?');
$stmt2->execute(array(1)); $stmt2->execute(array(1));
$this->assertEquals(1, $stmt2->fetchColumn()); $this->assertEquals(1, $stmt2->fetchColumn());
} }
public function testReuseStatementAfterClosingCursor() public function testReuseStatementAfterClosingCursor()
{ {
$table = new Table('stmt_test_close_cursor'); $this->_conn->insert('stmt_test', array('id' => 1));
$table->addColumn('id', 'integer'); $this->_conn->insert('stmt_test', array('id' => 2));
$this->_conn->getSchemaManager()->createTable($table);
$this->_conn->insert('stmt_test_close_cursor', array('id' => 1));
$this->_conn->insert('stmt_test_close_cursor', array('id' => 2));
$stmt = $this->_conn->prepare('SELECT id FROM stmt_test_close_cursor WHERE id = ?'); $stmt = $this->_conn->prepare('SELECT id FROM stmt_test WHERE id = ?');
$stmt->execute(array(1)); $stmt->execute(array(1));
$id = $stmt->fetchColumn(); $id = $stmt->fetchColumn();
...@@ -165,4 +150,70 @@ EOF ...@@ -165,4 +150,70 @@ EOF
$id = $stmt->fetchColumn(); $id = $stmt->fetchColumn();
$this->assertEquals(2, $id); $this->assertEquals(2, $id);
} }
/**
* @dataProvider emptyFetchProvider
*/
public function testFetchFromNonExecutedStatement(callable $fetch, $expected)
{
$stmt = $this->_conn->prepare('SELECT id FROM stmt_test');
$this->assertSame($expected, $fetch($stmt));
}
public function testCloseCursorOnNonExecutedStatement()
{
$stmt = $this->_conn->prepare('SELECT id FROM stmt_test');
$this->assertTrue($stmt->closeCursor());
}
/**
* @dataProvider emptyFetchProvider
*/
public function testFetchFromNonExecutedStatementWithClosedCursor(callable $fetch, $expected)
{
$stmt = $this->_conn->prepare('SELECT id FROM stmt_test');
$stmt->closeCursor();
$this->assertSame($expected, $fetch($stmt));
}
/**
* @dataProvider emptyFetchProvider
*/
public function testFetchFromExecutedStatementWithClosedCursor(callable $fetch, $expected)
{
$this->_conn->insert('stmt_test', array('id' => 1));
$stmt = $this->_conn->prepare('SELECT id FROM stmt_test');
$stmt->execute();
$stmt->closeCursor();
$this->assertSame($expected, $fetch($stmt));
}
public static function emptyFetchProvider()
{
return array(
'fetch' => array(
function (Statement $stmt) {
return $stmt->fetch();
},
false,
),
'fetch-column' => array(
function (Statement $stmt) {
return $stmt->fetchColumn();
},
false,
),
'fetch-all' => array(
function (Statement $stmt) {
return $stmt->fetchAll();
},
array(),
),
);
}
} }
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