Skip to content

Commit bba537e

Browse files
committed
Improve the tests for the TracingStatementForV*::bindParam() method
1 parent 590d4cb commit bba537e

File tree

5 files changed

+80
-27
lines changed

5 files changed

+80
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

5-
- Fix PDOStatement deprecation notice on `bindParam()` (#586)
5+
- Fix deprecation notice thrown when instrumenting the `PDOStatement::bindParam()` method and passing `$length = null` (#586)
66

77
## 4.2.6 (2022-01-10)
88

src/Tracing/Doctrine/DBAL/TracingStatementForV2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool
108108
*/
109109
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
110110
{
111-
return $this->decoratedStatement->bindParam($param, $variable, $type, $length);
111+
return $this->decoratedStatement->bindParam($param, $variable, $type, ...\array_slice(\func_get_args(), 3));
112112
}
113113

114114
/**

src/Tracing/Doctrine/DBAL/TracingStatementForV3.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool
2727
*/
2828
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
2929
{
30-
return $this->decoratedStatement->bindParam($param, $variable, $type, ...array_slice(func_get_args(), 3));
30+
return $this->decoratedStatement->bindParam($param, $variable, $type, ...\array_slice(\func_get_args(), 3));
3131
}
3232

3333
/**

tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,21 @@ public function testBindParam(): void
123123

124124
$this->decoratedStatement->expects($this->once())
125125
->method('bindParam')
126-
->with('foo', $variable, ParameterType::INTEGER)
126+
->with('foo', $variable, ParameterType::INTEGER, 10)
127127
->willReturn(true);
128128

129+
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER, 10));
130+
}
131+
132+
public function testBindParamForwardsLengthParamOnlyWhenExplicitlySet(): void
133+
{
134+
$variable = 'bar';
135+
$decoratedStatement = $this->createPartialMock(TracingStatementForV2Stub::class, array_diff(get_class_methods(TracingStatementForV2Stub::class), ['bindParam']));
136+
137+
$this->statement = new TracingStatementForV2($this->hub, $decoratedStatement, 'SELECT 1', ['db.system' => 'sqlite']);
138+
129139
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER));
140+
$this->assertSame(3, $decoratedStatement->bindParamCallArgsCount);
130141
}
131142

132143
public function testErrorCode(): void
@@ -195,3 +206,35 @@ public function testRowCount(): void
195206
$this->assertSame(10, $this->statement->rowCount());
196207
}
197208
}
209+
210+
if (!interface_exists(Statement::class)) {
211+
abstract class TracingStatementForV2Stub
212+
{
213+
/**
214+
* @var int
215+
*/
216+
public $bindParamCallArgsCount = 0;
217+
}
218+
} else {
219+
/**
220+
* @phpstan-implements \IteratorAggregate<mixed, mixed>
221+
*/
222+
abstract class TracingStatementForV2Stub implements \IteratorAggregate, Statement
223+
{
224+
/**
225+
* @var int
226+
*/
227+
public $bindParamCallArgsCount = 0;
228+
229+
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
230+
{
231+
// Since PHPUnit forcefully calls the mocked methods with all
232+
// parameters, regardless of whether they were originally passed
233+
// in an explicit manner, we can't use a mock to assert the number
234+
// of args used in the call to the function
235+
$this->bindParamCallArgsCount = \func_num_args();
236+
237+
return true;
238+
}
239+
}
240+
}

tests/Tracing/Doctrine/DBAL/TracingStatementForV3Test.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,47 @@ public function testBindParam(): void
6161

6262
$this->decoratedStatement->expects($this->once())
6363
->method('bindParam')
64-
->with('foo', $variable, ParameterType::INTEGER)
64+
->with('foo', $variable, ParameterType::INTEGER, 10)
6565
->willReturn(true);
6666

67-
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER));
67+
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER, 10));
6868
}
6969

70-
public function testBindParamWithoutLength(): void
70+
public function testBindParamForwardsLengthParamOnlyWhenExplicitlySet(): void
7171
{
7272
$variable = 'bar';
73-
74-
$this->decoratedStatement = $this->createPartialMock(TestStatement::class, ['bindValue', 'execute']);
75-
$this->statement = new TracingStatementForV3($this->hub, $this->decoratedStatement, 'SELECT 1', ['db.system' => 'sqlite']);
73+
$decoratedStatement = new class() implements Statement {
74+
/**
75+
* @var int
76+
*/
77+
public $bindParamCallArgsCount = 0;
78+
79+
public function bindValue($param, $value, $type = ParameterType::STRING): bool
80+
{
81+
throw new \BadMethodCallException();
82+
}
83+
84+
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
85+
{
86+
// Since PHPUnit forcefully calls the mocked methods with all
87+
// parameters, regardless of whether they were originally passed
88+
// in an explicit manner, we can't use a mock to assert the number
89+
// of args used in the call to the function
90+
$this->bindParamCallArgsCount = \func_num_args();
91+
92+
return true;
93+
}
94+
95+
public function execute($params = null): Result
96+
{
97+
throw new \BadMethodCallException();
98+
}
99+
};
100+
101+
$this->statement = new TracingStatementForV3($this->hub, $decoratedStatement, 'SELECT 1', ['db.system' => 'sqlite']);
76102

77103
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER));
78-
$this->assertInstanceOf(TestStatement::class, $this->decoratedStatement);
79-
$this->assertCount(3, $this->decoratedStatement->args);
104+
$this->assertSame(3, $decoratedStatement->bindParamCallArgsCount);
80105
}
81106

82107
public function testExecute(): void
@@ -121,18 +146,3 @@ public function testExecuteDoesNothingIfNoSpanIsSetOnHub(): void
121146
$this->assertSame($driverResult, $this->statement->execute(['foo' => 'bar']));
122147
}
123148
}
124-
125-
abstract class TestStatement implements Statement {
126-
127-
/**
128-
* @var mixed[]|null
129-
*/
130-
public $args = null;
131-
132-
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null)
133-
{
134-
$this->args = func_get_args();
135-
136-
return true;
137-
}
138-
}

0 commit comments

Comments
 (0)