diff --git a/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php b/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php index 4b2361c134a..94dda90b517 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php @@ -2,21 +2,27 @@ namespace Doctrine\Tests\DBAL\Functional; +use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Types\Type; class StatementTest extends \Doctrine\Tests\DbalFunctionalTestCase { - public function testStatementIsReusableAfterClosingCursor() + protected function setUp() { - $sm = $this->_conn->getSchemaManager(); - $table = new Table('stmt_test_reusable'); + parent::setUp(); + + $table = new Table('stmt_test'); $table->addColumn('id', 'integer'); - $sm->createTable($table); - $this->_conn->insert('stmt_test_reusable', array('id' => 1)); - $this->_conn->insert('stmt_test_reusable', array('id' => 2)); + $this->_conn->getSchemaManager()->dropAndCreateTable($table); + } - $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(); @@ -32,25 +38,10 @@ public function testStatementIsReusableAfterClosingCursor() $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() { $sm = $this->_conn->getSchemaManager(); - $table = new Table('stmt_test_longer_results'); + $table = new Table('stmt_longer_results'); $table->addColumn('param', 'string'); $table->addColumn('val', 'text'); $sm->createTable($table); @@ -59,9 +50,9 @@ public function testReuseStatementWithLongerResults() 'param' => 'param1', '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(); $this->assertArraySubset(array( array('param1', 'X'), @@ -71,7 +62,7 @@ public function testReuseStatementWithLongerResults() 'param' => 'param2', 'val' => 'A bit longer value', ); - $this->_conn->insert('stmt_test_longer_results', $row2); + $this->_conn->insert('stmt_longer_results', $row2); $stmt->execute(); $this->assertArraySubset(array( @@ -87,7 +78,7 @@ public function testFetchLongBlob() $this->iniSet('memory_limit', '4G'); $sm = $this->_conn->getSchemaManager(); - $table = new Table('stmt_test_long_blob'); + $table = new Table('stmt_long_blob'); $table->addColumn('contents', 'blob', array( 'length' => 0xFFFFFFFF, )); @@ -110,11 +101,11 @@ public function testFetchLongBlob() EOF ); - $this->_conn->insert('stmt_test_long_blob', array( + $this->_conn->insert('stmt_long_blob', array( 'contents' => $contents, ), 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(); $stream = Type::getType('blob') @@ -127,33 +118,27 @@ public function testFetchLongBlob() public function testIncompletelyFetchedStatementDoesNotBlockConnection() { - $table = new Table('stmt_test_non_fetched'); - $table->addColumn('id', 'integer'); - $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)); + $this->_conn->insert('stmt_test', array('id' => 1)); + $this->_conn->insert('stmt_test', 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->fetch(); $stmt1->execute(); // fetching only one record out of two $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)); $this->assertEquals(1, $stmt2->fetchColumn()); } public function testReuseStatementAfterClosingCursor() { - $table = new Table('stmt_test_close_cursor'); - $table->addColumn('id', 'integer'); - $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)); + $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_close_cursor WHERE id = ?'); + $stmt = $this->_conn->prepare('SELECT id FROM stmt_test WHERE id = ?'); $stmt->execute(array(1)); $id = $stmt->fetchColumn(); @@ -165,4 +150,70 @@ public function testReuseStatementAfterClosingCursor() $id = $stmt->fetchColumn(); $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(), + ), + ); + } }