Skip to content

Commit 7e0e695

Browse files
Cameron Jungeste93cry
Cameron Junge
andauthored
Fix deprecation notice when passing $length = null to Statement::bindParam() with DBAL 2.x (#613)
Co-authored-by: Stefano Arlandini <[email protected]>
1 parent faa2f43 commit 7e0e695

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fix deprecation notice thrown when instrumenting the `PDOStatement::bindParam()` method and passing `$length = null` on DBAL `2.x` (#613)
6+
57
## 4.2.8 (2022-03-31)
68

79
- Fix compatibility issue with Doctrine Bundle `>= 2.6.0` (#608)

phpstan-baseline.neon

+2-2
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,12 @@ parameters:
441441
path: tests/Tracing/Doctrine/DBAL/TracingServerInfoAwareDriverConnectionTest.php
442442

443443
-
444-
message: "#^Access to an undefined property PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&Sentry\\\\SentryBundle\\\\Tests\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingStatementForV2Stub\\:\\:\\$bindParamCallArgsCount\\.$#"
444+
message: "#^Parameter \\#2 \\$decoratedStatement of class Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingStatementForV2 constructor expects Doctrine\\\\DBAL\\\\Driver\\\\Statement, PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&Sentry\\\\SentryBundle\\\\Tests\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingStatementForV2Stub given\\.$#"
445445
count: 1
446446
path: tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php
447447

448448
-
449-
message: "#^Parameter \\#2 \\$decoratedStatement of class Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingStatementForV2 constructor expects Doctrine\\\\DBAL\\\\Driver\\\\Statement, PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&Sentry\\\\SentryBundle\\\\Tests\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingStatementForV2Stub given\\.$#"
449+
message: "#^Parameter \\#4 \\$length of method Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingStatementForV2\\:\\:bindParam\\(\\) expects int\\|null, mixed given\\.$#"
450450
count: 1
451451
path: tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php
452452

src/Tracing/Doctrine/DBAL/TracingStatementForV2.php

+1-1
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, ...\array_slice(\func_get_args(), 3));
111+
return $this->decoratedStatement->bindParam($param, $variable, $type, $length ?? 0, ...\array_slice(\func_get_args(), 4));
112112
}
113113

114114
/**

tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php

+36-17
Original file line numberDiff line numberDiff line change
@@ -117,27 +117,42 @@ public function testBindValue(): void
117117
$this->assertTrue($this->statement->bindValue('foo', 'bar', ParameterType::INTEGER));
118118
}
119119

120-
public function testBindParam(): void
120+
/**
121+
* @dataProvider bindParamDataProvider
122+
*
123+
* @param mixed[] $callArgs
124+
* @param mixed[] $expectedCallArgs
125+
*/
126+
public function testBindParam(array $callArgs, array $expectedCallArgs): void
121127
{
122128
$variable = 'bar';
129+
$decoratedStatement = $this->createPartialMock(TracingStatementForV2Stub::class, array_diff(get_class_methods(TracingStatementForV2Stub::class), ['bindParam']));
123130

124-
$this->decoratedStatement->expects($this->once())
125-
->method('bindParam')
126-
->with('foo', $variable, ParameterType::INTEGER, 10)
127-
->willReturn(true);
131+
$this->statement = new TracingStatementForV2($this->hub, $decoratedStatement, 'SELECT 1', ['db.system' => 'sqlite']);
128132

129-
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER, 10));
133+
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER, ...$callArgs));
134+
$this->assertSame($expectedCallArgs, $decoratedStatement->bindParamCallArgs);
130135
}
131136

132-
public function testBindParamForwardsLengthParamOnlyWhenExplicitlySet(): void
137+
/**
138+
* @return \Generator<mixed>
139+
*/
140+
public function bindParamDataProvider(): \Generator
133141
{
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-
139-
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER));
140-
$this->assertSame(3, $decoratedStatement->bindParamCallArgsCount);
142+
yield '$length parameter not passed at all' => [
143+
[],
144+
['foo', 'bar', 1, 0],
145+
];
146+
147+
yield '$length parameter passed as `null`' => [
148+
[null],
149+
['foo', 'bar', 1, 0],
150+
];
151+
152+
yield 'additional parameters passed' => [
153+
[null, 'baz'],
154+
['foo', 'bar', 1, 0, 'baz'],
155+
];
141156
}
142157

143158
public function testErrorCode(): void
@@ -211,6 +226,10 @@ public function testRowCount(): void
211226
if (!interface_exists(Statement::class)) {
212227
abstract class TracingStatementForV2Stub
213228
{
229+
/**
230+
* @var mixed[]
231+
*/
232+
public $bindParamCallArgs = [];
214233
}
215234
} else {
216235
/**
@@ -219,17 +238,17 @@ abstract class TracingStatementForV2Stub
219238
abstract class TracingStatementForV2Stub implements \IteratorAggregate, Statement
220239
{
221240
/**
222-
* @var int
241+
* @var mixed[]
223242
*/
224-
public $bindParamCallArgsCount = 0;
243+
public $bindParamCallArgs = [];
225244

226245
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
227246
{
228247
// Since PHPUnit forcefully calls the mocked methods with all
229248
// parameters, regardless of whether they were originally passed
230249
// in an explicit manner, we can't use a mock to assert the number
231250
// of args used in the call to the function
232-
$this->bindParamCallArgsCount = \func_num_args();
251+
$this->bindParamCallArgs = \func_get_args();
233252

234253
return true;
235254
}

0 commit comments

Comments
 (0)