New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement our own error handler for db2_prepare #273

Merged
merged 10 commits into from Nov 23, 2017

Unit test Adapter\Driver\IbmDb2\Statement::prepare()

Mock the relevant db2_xxx functions by placing them into the
Zend\Db\Adapter\Driver\IbmDb2 namespace so that they are preferred
over the actual ones in the idm_db2 extension.
  • Loading branch information...
akrabat committed Oct 20, 2017
commit e493cdaeac40354677f032cbb19e1b0ae57badb1
@@ -12,6 +12,9 @@
use Zend\Db\Adapter\Driver\IbmDb2\Statement;
use Zend\Db\Adapter\Driver\IbmDb2\IbmDb2;
use Zend\Db\Adapter\ParameterContainer;
use Zend\Db\Adapter\Exception\RuntimeException;
include __DIR__ . '/TestAsset/Db2Functions.php';
class StatementTest extends \PHPUnit_Framework_TestCase
{
@@ -102,26 +105,69 @@ public function testGetSql()
/**
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::prepare
* @todo Implement testPrepare().
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::isPrepared
*/
public function testPrepare()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$sql = "SELECT 'foo' FROM SYSIBM.SYSDUMMY1";
$this->statement->prepare($sql);
$this->assertTrue($this->statement->isPrepared());
}
/**
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::prepare
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::isPrepared
* @todo Implement testIsPrepared().
*/
public function testIsPrepared()
public function testPreparingTwiceErrors()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
$sql = "SELECT 'foo' FROM SYSIBM.SYSDUMMY1";
$this->statement->prepare($sql);
$this->assertTrue($this->statement->isPrepared());
$this->setExpectedException(
RuntimeException::class,
'This statement has been prepared already'
);
$this->statement->prepare($sql);
}
/**
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::prepare
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::setSql
*/
public function testPrepareThrowsRuntimeExceptionOnInvalidSql()
{
$sql = "INVALID SQL";
$this->statement->setSql($sql);
$this->setExpectedException(
RuntimeException::class,
'SQL is invalid. Error message'
);
$this->statement->prepare();
}
/**
* If error_reporting() is turned off, then the error handler will not
* be called, but a RuntimeException will still be generated as the
* resource is false
*
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::prepare
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::setSql
*/
public function testPrepareThrowsRuntimeExceptionOnInvalidSqlWithErrorReportingDisabled()
{
$currentErrorReporting = error_reporting(0);
$sql = "INVALID SQL";
$this->statement->setSql($sql);
$this->setExpectedException(
RuntimeException::class,
'Error message'
);
$this->statement->prepare();
error_reporting($currentErrorReporting);
}
/**
@@ -0,0 +1,57 @@
<?php
namespace Zend\Db\Adapter\Driver\IbmDb2;
/**
* Mock db2_prepare by placing in same namespace as Statement
*
* Return false if $sql is "invalid sql", otherwise return true
*
* @param resource $adapter
* @param string $sql
* @param array $options
* @return resource|false
*/
function db2_prepare($connection, $sql, $options = [])
{
if ($sql == 'INVALID SQL') {
// db2_prepare issues a warning with invalid SQL
trigger_error("SQL is invalid", E_USER_WARNING);
return false;
}
return true;
}
/**
* Mock db2_stmt_errormsg
*
* If you pass a string to $stmt, it will be returned to you
*
* @param mixed $stmt
* @return string
*/
function db2_stmt_errormsg($stmt = null)
{
if (is_string($stmt)) {
return $stmt;
}
return 'Error message';
}
/**
* Mock db2_stmt_error
*
* If you pass a string to $stmt, it will be returned to you
*
* @param mixed $stmt
* @return string
*/
function db2_stmt_error($stmt = null)
{
if (is_string($stmt)) {
return $stmt;
}
return '1';
}
ProTip! Use n and p to navigate between commits in a pull request.