Skip to content

Commit

Permalink
Added working Ibm Db2 unit & integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphschindler authored and Ralph Schindler committed Jan 22, 2013
1 parent 823e727 commit 8c5e765
Show file tree
Hide file tree
Showing 12 changed files with 876 additions and 4 deletions.
21 changes: 20 additions & 1 deletion library/Zend/Db/Adapter/Driver/IbmDb2/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,24 @@ public function setDriver(IbmDb2 $driver)
return $this;
}

/**
* @param array $connectionParameters
* @return Connection
*/
public function setConnectionParameters(array $connectionParameters)
{
$this->connectionParameters = $connectionParameters;
return $this;
}

/**
* @return array
*/
public function getConnectionParameters()
{
return $this->connectionParameters;
}

public function setResource($resource)
{
if (!is_resource($resource) || get_resource_type($resource) !== 'DB2 Connection') {
Expand All @@ -71,7 +83,12 @@ public function setResource($resource)
*/
public function getCurrentSchema()
{
// TODO: Implement getCurrentSchema() method.
if (!$this->isConnected()) {
$this->connect();
}

$info = db2_server_info($this->resource);
return (isset($info->DB_NAME) ? $info->DB_NAME : '');
}

/**
Expand Down Expand Up @@ -127,6 +144,7 @@ public function connect()
__METHOD__
));
}
return $this;
}

/**
Expand All @@ -148,6 +166,7 @@ public function disconnect()
{
if ($this->resource) {
db2_close($this->resource);
$this->resource = null;
}
return $this;
}
Expand Down
4 changes: 4 additions & 0 deletions library/Zend/Db/Adapter/Driver/IbmDb2/IbmDb2.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public function createStatement($sqlOrResource = null)
} else {
if (is_string($sqlOrResource)) {
$statement->setSql($sqlOrResource);
} elseif ($sqlOrResource !== null) {
throw new Exception\InvalidArgumentException(
__FUNCTION__ . ' only accepts an SQL string or a ibm_db2 resource'
);
}
if (!$this->connection->isConnected()) {
$this->connection->connect();
Expand Down
19 changes: 16 additions & 3 deletions library/Zend/Db/Adapter/Driver/IbmDb2/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ class Statement implements StatementInterface
*/
protected $resource = null;



/**
* @param $resource
* @return Statement
*/
public function initialize($resource)
{
$this->db2 = $resource;
return $this;
}

/**
* @param IbmDb2 $driver
* @return Statement
*/
public function setDriver(IbmDb2 $driver)
{
$this->driver = $driver;
Expand Down Expand Up @@ -91,6 +98,10 @@ public function getParameterContainer()
return $this->parameterContainer;
}

/**
* @param $resource
* @throws \Zend\Db\Adapter\Exception\InvalidArgumentException
*/
public function setResource($resource)
{
if (get_resource_type($resource) !== 'DB2 Statement') {
Expand All @@ -112,7 +123,8 @@ public function getResource()
/**
* Prepare sql
*
* @param string $sql
* @param string|null $sql
* @return Statement
*/
public function prepare($sql = null)
{
Expand All @@ -131,6 +143,7 @@ public function prepare($sql = null)
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace ZendTest\Db\Adapter\Driver\IbmDb2;

abstract class AbstractIntegrationTest extends \PHPUnit_Framework_TestCase
{
protected $variables = array(
'database' => 'ZEND_DB_ADAPTER_DRIVER_IBMDB2_DATABASE',
'username' => 'ZEND_DB_ADAPTER_DRIVER_IBMDB2_USERNAME',
'password' => 'ZEND_DB_ADAPTER_DRIVER_IBMDB2_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('ibm_db2')) {
$this->fail('The phpunit group integration-ibm_db2 was enabled, but the extension is not loaded.');
}
}
}
149 changes: 149 additions & 0 deletions tests/ZendTest/Db/Adapter/Driver/IbmDb2/ConnectionIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
namespace ZendTest\Db\Adapter\Driver\IbmDb2;

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

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

/**
* @covers Zend\Db\Adapter\Driver\IbmDb2\Connection::setResource
*/
public function testSetResource()
{
$resource = db2_connect(
$this->variables['database'],
$this->variables['username'],
$this->variables['password']
);
$connection = new Connection(array());
$this->assertSame($connection, $connection->setResource($resource));

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

/**
* @covers Zend\Db\Adapter\Driver\IbmDb2\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\IbmDb2\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\IbmDb2\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\IbmDb2\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\IbmDb2\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\IbmDb2\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\IbmDb2\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\IbmDb2\Connection::execute
*/
public function testExecute()
{
$sqlsrv = new IbmDb2($this->variables);
$connection = $sqlsrv->getConnection();

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

/**
* @covers Zend\Db\Adapter\Driver\IbmDb2\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/IbmDb2/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace ZendTest\Db\Adapter\Driver\IbmDb2;

use Zend\Db\Adapter\Driver\IbmDb2\IbmDb2;
use Zend\Db\Adapter\Driver\IbmDb2\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\IbmDb2\Connection::setDriver
*/
public function testSetDriver()
{
$this->assertEquals($this->connection, $this->connection->setDriver(new IbmDb2(array())));
}

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

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

}
Loading

0 comments on commit 8c5e765

Please sign in to comment.