Skip to content

Commit 6b43422

Browse files
authored
Merge pull request #23 from rtm-ctrlz/feat-bind-param
feat(FakePdoStatement): implement `$pdoStatement->bindParam()` method
2 parents e65364a + 73c4cbf commit 6b43422

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

src/FakePdoStatementTrait.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Vimeo\MysqlEngine;
33

4+
use PDO;
5+
46
trait FakePdoStatementTrait
57
{
68
/**
@@ -70,8 +72,9 @@ public function __construct(FakePdoInterface $conn, string $sql, ?\PDO $real)
7072
* @param string|int $key
7173
* @param scalar $value
7274
* @param int $type
75+
* @return bool
7376
*/
74-
public function bindValue($key, $value, $type = \PDO::PARAM_STR) : void
77+
public function bindValue($key, $value, $type = \PDO::PARAM_STR) : bool
7578
{
7679
if (\is_string($key) && $key[0] !== ':') {
7780
$key = ':' . $key;
@@ -83,8 +86,35 @@ public function bindValue($key, $value, $type = \PDO::PARAM_STR) : void
8386
$this->boundValues[$key] = $value;
8487

8588
if ($this->realStatement) {
86-
$this->realStatement->bindValue($key, $value, $type);
89+
return $this->realStatement->bindValue($key, $value, $type);
8790
}
91+
return true;
92+
}
93+
94+
/**
95+
* @param string|int $key
96+
* @param scalar $value
97+
* @param int $type
98+
* @param int $maxLength
99+
* @param mixed $driverOptions
100+
* @return bool
101+
*/
102+
public function bindParam($key, &$value, $type = PDO::PARAM_STR, $maxLength = null, $driverOptions = null): bool
103+
{
104+
if (\is_string($key) && $key[0] !== ':') {
105+
$key = ':' . $key;
106+
} elseif (\is_int($key)) {
107+
// Parameter offsets start at 1, which is weird.
108+
--$key;
109+
}
110+
$this->boundValues[$key] = &$value;
111+
if ($this->realStatement) {
112+
/**
113+
* @psalm-suppress PossiblyNullArgument
114+
*/
115+
return $this->realStatement->bindParam($key, $value, $type, $maxLength, $driverOptions);
116+
}
117+
return true;
88118
}
89119

90120
/**

tests/ParameterBindTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vimeo\MysqlEngine\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
class ParameterBindTest extends TestCase
10+
{
11+
12+
public function dataSqlWithParameter(): array
13+
{
14+
return [
15+
'numeric' => ['SELECT ?', 1],
16+
':field' => ['SELECT :field', ':field'],
17+
'field' => ['SELECT :field', 'field'],
18+
];
19+
}
20+
21+
/**
22+
* @param string $sql
23+
* @param string|int $key
24+
* @dataProvider dataSqlWithParameter
25+
*/
26+
public function testBindValue(string $sql, $key): void
27+
{
28+
$pdo = self::getPdo('mysql:foo;dbname=test;');
29+
30+
$statement = $pdo->prepare($sql);
31+
$statement->bindValue($key, 999);
32+
$statement->execute();
33+
34+
self::assertEquals(999, $statement->fetchColumn(0));
35+
}
36+
37+
/**
38+
* @param string $sql
39+
* @param string|int $key
40+
* @dataProvider dataSqlWithParameter
41+
*/
42+
public function testBindParam(string $sql, $key): void
43+
{
44+
$pdo = self::getPdo('mysql:foo;dbname=test;');
45+
46+
$var = 1000;
47+
$statement = $pdo->prepare($sql);
48+
$statement->bindParam($key, $var);
49+
50+
$var = 10;
51+
$statement->execute();
52+
self::assertEquals(10, $statement->fetchColumn(0));
53+
}
54+
55+
private static function getPdo(string $connection_string): \PDO
56+
{
57+
if (\PHP_MAJOR_VERSION === 8) {
58+
return new \Vimeo\MysqlEngine\Php8\FakePdo($connection_string, '', '', []);
59+
}
60+
61+
return new \Vimeo\MysqlEngine\Php7\FakePdo($connection_string, '', '', []);
62+
}
63+
}

0 commit comments

Comments
 (0)