Skip to content

Commit

Permalink
Added test for OCI8 Statement bindParams method
Browse files Browse the repository at this point in the history
  • Loading branch information
eisberg committed Nov 21, 2019
1 parent b033c86 commit 091095b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
39 changes: 32 additions & 7 deletions lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ class OCI8Statement implements IteratorAggregate, Statement
*/
private $result = false;

/**
* @param resource $dbh The connection handle.
*
* @return resource
*/
protected function prepareQuery($dbh, string $query)
{
$stmt = oci_parse($dbh, $query);
assert(is_resource($stmt));

return $stmt;
}

/**
* Creates a new OCI8Statement that uses the given connection handle and SQL statement.
*
Expand All @@ -100,10 +113,7 @@ public function __construct($dbh, string $query, OCI8Connection $conn)
{
[$query, $paramMap] = self::convertPositionalToNamedPlaceholders($query);

$stmt = oci_parse($dbh, $query);
assert(is_resource($stmt));

$this->_sth = $stmt;
$this->_sth = $this->prepareQuery($dbh, $query);
$this->_dbh = $dbh;
$this->_paramMap = $paramMap;
$this->_conn = $conn;
Expand Down Expand Up @@ -273,7 +283,7 @@ public function bindValue($param, $value, int $type = ParameterType::STRING) : v
*/
public function bindParam($param, &$variable, int $type = ParameterType::STRING, ?int $length = null) : void
{
$param = $this->_paramMap[$param] ?? $param;
$param = $this->_paramMap[$param] ?? (string) $param;

if ($type === ParameterType::LARGE_OBJECT) {
$lob = oci_new_descriptor($this->_dbh, OCI_D_LOB);
Expand All @@ -287,13 +297,28 @@ public function bindParam($param, &$variable, int $type = ParameterType::STRING,
}

$this->boundValues[$param] =& $variable;
$this->bindParamByName(
$param,
$variable,
$this->convertParameterType($type),
$length ?? -1
);
}

/**
* @param mixed $param The variable to bind to the parameter.
* @param mixed $variable The value to bind to the parameter.
*
* @throws OCI8Exception
*/
protected function bindParamByName($param, &$variable, int $type, int $length) : void
{
if (! oci_bind_by_name(
$this->_sth,
$param,
$variable,
$length ?? -1,
$this->convertParameterType($type)
$length,
$type
)) {
throw OCI8Exception::fromErrorInfo(oci_error($this->_sth));
}
Expand Down
52 changes: 52 additions & 0 deletions tests/Doctrine/Tests/DBAL/Driver/OCI8/OCI8StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,39 @@ public function testExecute(array $params) : void
$statement->execute($params);
}

/**
* Testing binding named and positioned parameters
*
* @param mixed[] $params
*
* @dataProvider bindParamsDataProvider
*/
public function testBindParam(array $params, string $query) : void
{
$conn = $this->createMock(OCI8Connection::class);
/** @var OCI8Statement|MockObject $statement */
$statement = $this->getMockBuilder(OCI8Statement::class)
->onlyMethods([ 'prepareQuery', 'bindParamByName'])
// $dbh, string $query, OCI8Connection $conn
->setConstructorArgs([null, $query, $conn])
->getMock();

$i = 0;
foreach ($params as $index => $value) {
$statement->expects($this->at($i))
->method('bindParamByName')
->with(
$this->stringStartsWith(':'),
$this->equalTo($value)
);
$i++;
}

foreach ($params as $index => $value) {
$statement->bindParam($index, $value);
}
}

/**
* @return array<int, array<int, mixed>>
*/
Expand All @@ -91,6 +124,25 @@ public static function executeDataProvider() : iterable
];
}

/**
* @return array<int, array<int, mixed>|string>
*/
public static function bindParamsDataProvider() : iterable
{
return [
// Positional placeholders
[
[1 => 'test', 2 => null, 3 => 'value'],
'select * from dual where 1 = ? and 2 = ? and 3 = ?',
],
// Named placeholders
[
[':p1' => null, ':p2' => 'test', ':p3' => 'value'],
'select * from dual where 1 = :p1 and 2 = :p2 and 3 = :p3',
],
];
}

/**
* @dataProvider nonTerminatedLiteralProvider
*/
Expand Down

0 comments on commit 091095b

Please sign in to comment.