Commit 22b2b6b6 authored by Ian Jenkins's avatar Ian Jenkins

Refactoring tests

parent 385cbb7d
...@@ -21,7 +21,6 @@ namespace Doctrine\DBAL\Driver\OCI8; ...@@ -21,7 +21,6 @@ namespace Doctrine\DBAL\Driver\OCI8;
use Doctrine\DBAL\Driver\Connection; use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Platforms\OraclePlatform;
/** /**
* OCI8 implementation of the Connection interface. * OCI8 implementation of the Connection interface.
...@@ -38,7 +37,7 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection ...@@ -38,7 +37,7 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
/** /**
* @var integer * @var integer
*/ */
protected $executeMode = OCI_COMMIT_ON_SUCCESS; protected $executeMode;
/** /**
* Creates a Connection to an Oracle Database using oci8 extension. * Creates a Connection to an Oracle Database using oci8 extension.
...@@ -58,6 +57,8 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection ...@@ -58,6 +57,8 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
define('OCI_NO_AUTO_COMMIT', 0); define('OCI_NO_AUTO_COMMIT', 0);
} }
$this->executeMode = OCI_COMMIT_ON_SUCCESS;
$this->dbh = $persistent $this->dbh = $persistent
? @oci_pconnect($username, $password, $db, $charset, $sessionMode) ? @oci_pconnect($username, $password, $db, $charset, $sessionMode)
: @oci_connect($username, $password, $db, $charset, $sessionMode); : @oci_connect($username, $password, $db, $charset, $sessionMode);
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
namespace Doctrine\Tests\DBAL\Functional; namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\IBMDB2\DB2Connection;
use Doctrine\DBAL\Driver\IBMDB2\DB2Statement;
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
use Doctrine\DBAL\Driver\Mysqli\MysqliStatement;
use Doctrine\DBAL\Driver\OCI8\OCI8Connection;
use Doctrine\DBAL\Driver\OCI8\OCI8Statement;
use Doctrine\DBAL\Driver\SQLAnywhere\SQLAnywhereConnection;
use Doctrine\DBAL\Driver\SQLAnywhere\SQLAnywhereStatement;
use Doctrine\DBAL\Driver\SQLSrv\SQLSrvConnection;
use Doctrine\DBAL\Driver\SQLSrv\SQLSrvStatement;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use IteratorIterator;
use PDO; use PDO;
class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
...@@ -868,86 +878,54 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -868,86 +878,54 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
} }
} }
public function testIteratorCallsFetchTheExpectedNumberOfTimes() public function getConnections()
{ {
$sql = 'SELECT test_int, test_string FROM fetch_table'; return [
$originalStmt = $this->_conn->prepare($sql); [MysqliConnection::class, MysqliStatement::class],
[OCI8Connection::class, OCI8Statement::class],
// Mock the connection [DB2Connection::class, DB2Statement::class],
$connectionMock = $this->getMockBuilder('Doctrine\\DBAL\\Driver\\Connection') [SQLAnywhereConnection::class, SQLAnywhereStatement::class],
->setMethods(['prepare', 'query']) [SQLSrvConnection::class, SQLSrvStatement::class],
->getMockForAbstractClass(); ];
$stmtSpy = new StatementSpy($originalStmt);
// Return our mocked statement from the query
$connectionMock->expects($this->any())
->method('prepare')
->will($this->returnValue($stmtSpy));
$connectionMock->expects($this->any())
->method('query')
->will($this->returnCallback(function () use ($stmtSpy) {
$stmtSpy->execute();
return $stmtSpy;
}));
$stmt = $connectionMock->query($sql);
$stmt->setFetchMode(\PDO::FETCH_ASSOC);
// Assert no fetch calls thus far
self::assertEquals(0, $stmtSpy->getFetchCalls());
$i = 0;
foreach ($stmt as $row) {
$i++;
}
// Assert expected number of fetches
$this->assertGreaterThan(0, $stmtSpy->getFetchCalls());
$this->assertEquals($i, $stmtSpy->getFetchCalls());
} }
}
class StatementSpy implements \IteratorAggregate
{
/** /**
* @var Statement|\Doctrine\DBAL\Statement * @dataProvider getConnections
*/ */
private $stmt; public function testGettingIteratorDoesNotCallFetch($connection, $statement)
{
$stmt = $this->createPartialMock($statement, ['fetch']);
$stmt->expects($this->never())->method('fetch');
private $fetchCalls = 0; $conn = $this->createMock($connection);
$conn->expects($this->any())->method('prepare')->willReturn($stmt);
public function __construct(Statement $stmt) $ii = new IteratorIterator($stmt);
{ $ii->getInnerIterator();
$this->stmt = $stmt;
} }
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) /**
* @dataProvider getConnections
*/
public function testIterationCallsFetchOncePerStep($connection, $statement)
{ {
return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3); $values = ['foo', '', 'bar', '0', 'baz', 0, 'qux', null, 'quz', false];
} $calls = 0;
public function execute($params = null) $stmt = $this->createPartialMock($statement, ['fetch']);
{ $stmt->expects($this->exactly(10))
return $this->stmt->execute($params); ->method('fetch')
} ->willReturnCallback(function() use ($values, &$calls) {
$calls++;
yield from $values;
});
public function getIterator() $conn = $this->createMock($connection);
{ $conn->expects($this->any())->method('prepare')->willReturn($stmt);
foreach ($this->stmt as $item) {
$this->fetchCalls++;
yield $item;
}
}
public function getFetchCalls() foreach ($stmt as $i => $_) {
{ $_->next();
return $this->fetchCalls; $this->assertEquals($i + 1, $calls);
}
} }
} }
class MyFetchClass
{
public $test_int, $test_string, $test_datetime;
}
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