Skip to content

Commit 31a3ed1

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

File tree

6 files changed

+86
-27
lines changed

6 files changed

+86
-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

phpstan-baseline.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,16 @@ parameters:
245245
count: 1
246246
path: tests/Tracing/Doctrine/DBAL/TracingServerInfoAwareDriverConnectionTest.php
247247

248+
-
249+
message: "#^Access to an undefined property PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&Sentry\\\\SentryBundle\\\\Tests\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingStatementForV2Stub\\:\\:\\$bindParamCallArgsCount\\.$#"
250+
count: 1
251+
path: tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php
252+
253+
-
254+
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\\.$#"
255+
count: 1
256+
path: tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php
257+
248258
-
249259
message: "#^Trying to mock an undefined method closeCursor\\(\\) on class Doctrine\\\\DBAL\\\\Driver\\\\Statement\\.$#"
250260
count: 1

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: 40 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,31 @@ 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+
} else {
215+
/**
216+
* @phpstan-implements \IteratorAggregate<mixed, mixed>
217+
*/
218+
abstract class TracingStatementForV2Stub implements \IteratorAggregate, Statement
219+
{
220+
/**
221+
* @var int
222+
*/
223+
public $bindParamCallArgsCount = 0;
224+
225+
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
226+
{
227+
// Since PHPUnit forcefully calls the mocked methods with all
228+
// parameters, regardless of whether they were originally passed
229+
// in an explicit manner, we can't use a mock to assert the number
230+
// of args used in the call to the function
231+
$this->bindParamCallArgsCount = \func_num_args();
232+
233+
return true;
234+
}
235+
}
236+
}

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)