Skip to content

Commit

Permalink
Added working integration tests for Zend\Db Oci8 driver
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphschindler committed Jan 21, 2013
1 parent 1adee24 commit 173c73f
Show file tree
Hide file tree
Showing 13 changed files with 853 additions and 1 deletion.
7 changes: 6 additions & 1 deletion library/Zend/Db/Adapter/Driver/Oci8/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ public function getCurrentSchema()
$this->connect();
}

// @todo
$query = "SELECT sys_context('USERENV', 'DB_NAME') as \"database_name\" FROM DUAL";
$stmt = oci_parse($this->resource, $query);
oci_execute($stmt);
$dbNameArray = oci_fetch_array($stmt, OCI_ASSOC);
return $dbNameArray['database_name'];
}

/**
Expand Down Expand Up @@ -177,6 +181,7 @@ public function connect()
new Exception\ErrorException($e['message'], $e['code'])
);
}
return $this;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions library/Zend/Db/Adapter/Driver/Oci8/Oci8.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function registerStatementPrototype(Statement $statementPrototype)
{
$this->statementPrototype = $statementPrototype;
$this->statementPrototype->setDriver($this); // needs access to driver to createResult()
return $this;
}

/**
Expand All @@ -102,6 +103,7 @@ public function getStatementPrototype()
public function registerResultPrototype(Result $resultPrototype)
{
$this->resultPrototype = $resultPrototype;
return $this;
}

/**
Expand Down Expand Up @@ -157,6 +159,10 @@ public function createStatement($sqlOrResource = null)
} else {
if (is_string($sqlOrResource)) {
$statement->setSql($sqlOrResource);
} elseif ($sqlOrResource !== null) {
throw new Exception\InvalidArgumentException(
'Oci8 only accepts an SQL string or a oci8 resource in ' . __FUNCTION__
);
}
if (!$this->connection->isConnected()) {
$this->connection->connect();
Expand Down
10 changes: 10 additions & 0 deletions library/Zend/Db/Adapter/Driver/Oci8/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ public function buffer()
return null;
}

/**
* Is the result buffered?
*
* @return bool
*/
public function isBuffered()
{
return false;
}

/**
* Return the resource
* @return mixed
Expand Down
2 changes: 2 additions & 0 deletions library/Zend/Db/Adapter/Driver/Oci8/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function setSql($sql)
public function setParameterContainer(ParameterContainer $parameterContainer)
{
$this->parameterContainer = $parameterContainer;
return $this;
}

/**
Expand Down Expand Up @@ -187,6 +188,7 @@ public function prepare($sql = null)
}

$this->isPrepared = true;
return $this;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/ZendTest/Db/Adapter/Driver/Oci8/AbstractIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace ZendTest\Db\Adapter\Driver\Oci8;

abstract class AbstractIntegrationTest extends \PHPUnit_Framework_TestCase
{
protected $variables = array(
'hostname' => 'ZEND_DB_ADAPTER_DRIVER_OCI8_HOSTNAME',
'username' => 'ZEND_DB_ADAPTER_DRIVER_OCI8_USERNAME',
'password' => 'ZEND_DB_ADAPTER_DRIVER_OCI8_PASSWORD',
);

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
foreach ($this->variables as $name => $value) {
if (!isset($GLOBALS[$value])) {
$this->fail('Missing required variable ' . $value . ' from phpunit.xml for this integration test');
}
$this->variables[$name] = $GLOBALS[$value];
}

if (!extension_loaded('oci8')) {
$this->fail('The phpunit group integration-oci8 was enabled, but the extension is not loaded.');
}
}
}
151 changes: 151 additions & 0 deletions tests/ZendTest/Db/Adapter/Driver/Oci8/ConnectionIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
namespace ZendTest\Db\Adapter\Driver\Oci8;

use Zend\Db\Adapter\Driver\Oci8\Oci8;
use Zend\Db\Adapter\Driver\Oci8\Connection;

/**
* @group integration
* @group integration-sqlserver
*/
class ConnectionIntegrationTest extends AbstractIntegrationTest
{
/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::getCurrentSchema
*/
public function testGetCurrentSchema()
{
$connection = new Connection($this->variables);
$this->assertInternalType('string', $connection->getCurrentSchema());
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::setResource
*/
public function testSetResource()
{
$this->markTestIncomplete('edit this');
$resource = sqlsrv_connect(
$this->variables['hostname'], array(
'UID' => $this->variables['username'],
'PWD' => $this->variables['password']
)
);
$connection = new Connection(array());
$this->assertSame($connection, $connection->setResource($resource));

$connection->disconnect();
unset($connection);
unset($resource);
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::getResource
*/
public function testGetResource()
{
$connection = new Connection($this->variables);
$connection->connect();
$this->assertInternalType('resource', $connection->getResource());

$connection->disconnect();
unset($connection);
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::connect
*/
public function testConnect()
{
$connection = new Connection($this->variables);
$this->assertSame($connection, $connection->connect());
$this->assertTrue($connection->isConnected());

$connection->disconnect();
unset($connection);
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::isConnected
*/
public function testIsConnected()
{
$connection = new Connection($this->variables);
$this->assertFalse($connection->isConnected());
$this->assertSame($connection, $connection->connect());
$this->assertTrue($connection->isConnected());

$connection->disconnect();
unset($connection);
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::disconnect
*/
public function testDisconnect()
{
$connection = new Connection($this->variables);
$connection->connect();
$this->assertTrue($connection->isConnected());
$connection->disconnect();
$this->assertFalse($connection->isConnected());
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::beginTransaction
* @todo Implement testBeginTransaction().
*/
public function testBeginTransaction()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::commit
* @todo Implement testCommit().
*/
public function testCommit()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::rollback
* @todo Implement testRollback().
*/
public function testRollback()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::execute
*/
public function testExecute()
{
$oci8 = new Oci8($this->variables);
$connection = $oci8->getConnection();

$result = $connection->execute('SELECT \'foo\' FROM DUAL');
$this->assertInstanceOf('Zend\Db\Adapter\Driver\Oci8\Result', $result);
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::getLastGeneratedValue
*/
public function testGetLastGeneratedValue()
{
$this->markTestIncomplete('Need to create a temporary sequence.');
$connection = new Connection($this->variables);
$connection->getLastGeneratedValue();
}
}
56 changes: 56 additions & 0 deletions tests/ZendTest/Db/Adapter/Driver/Oci8/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace ZendTest\Db\Adapter\Driver\Oci8;

use Zend\Db\Adapter\Driver\Oci8\Oci8;
use Zend\Db\Adapter\Driver\Oci8\Connection;

class ConnectionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Connection
*/
protected $connection;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->connection = new Connection(array());
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::setDriver
*/
public function testSetDriver()
{
$this->assertEquals($this->connection, $this->connection->setDriver(new Oci8(array())));
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::setConnectionParameters
*/
public function testSetConnectionParameters()
{
$this->assertEquals($this->connection, $this->connection->setConnectionParameters(array()));
}

/**
* @covers Zend\Db\Adapter\Driver\Oci8\Connection::getConnectionParameters
*/
public function testGetConnectionParameters()
{
$this->connection->setConnectionParameters(array('foo' => 'bar'));
$this->assertEquals(array('foo' => 'bar'), $this->connection->getConnectionParameters());
}

}
38 changes: 38 additions & 0 deletions tests/ZendTest/Db/Adapter/Driver/Oci8/Oci8IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace ZendTest\Db\Adapter\Driver\Oci8;

use Zend\Db\Adapter\Driver\Oci8\Oci8;

/**
* @group integration
* @group integration-oci8
*/
class Oci8IntegrationTest extends AbstractIntegrationTest
{
/**
* @group integration-oci8
* @covers Zend\Db\Adapter\Driver\Oci8\Oci8::checkEnvironment
*/
public function testCheckEnvironment()
{
$sqlserver = new Oci8(array());
$this->assertNull($sqlserver->checkEnvironment());
}

public function testCreateStatement()
{
$driver = new Oci8(array());
$resource = oci_connect($this->variables['username'], $this->variables['password']);

$driver->getConnection()->setResource($resource);

$stmt = $driver->createStatement('SELECT * FROM DUAL');
$this->assertInstanceOf('Zend\Db\Adapter\Driver\Oci8\Statement', $stmt);
$stmt = $driver->createStatement();
$this->assertInstanceOf('Zend\Db\Adapter\Driver\Oci8\Statement', $stmt);

$this->setExpectedException('Zend\Db\Adapter\Exception\InvalidArgumentException', 'only accepts an SQL string or a oci8 resource');
$driver->createStatement(new \stdClass);
}

}
Loading

0 comments on commit 173c73f

Please sign in to comment.