Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 977755e

Browse files
authored
Fix wrong method existence check in TracingDriverConnection::errorCode() (getsentry#568)
1 parent b013a88 commit 977755e

File tree

4 files changed

+68
-17
lines changed

4 files changed

+68
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Add return typehints to the methods of the `SentryExtension` class to prepare for Symfony 6 (#563)
66
- Fix setting the IP address on the user context when it's not available (#565)
7+
- Fix wrong method existence check in `TracingDriverConnection::errorCode()` (#568)
78

89
## 4.2.3 (2021-09-21)
910

phpstan-baseline.neon

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,7 @@ parameters:
1616
path: src/EventListener/ErrorListener.php
1717

1818
-
19-
message: "#^Call to an undefined method Doctrine\\\\DBAL\\\\Driver\\\\Connection\\:\\:errorCode\\(\\)\\.$#"
20-
count: 1
21-
path: src/Tracing/Doctrine/DBAL/TracingDriverConnection.php
22-
23-
-
24-
message: "#^Method Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingDriverConnection\\:\\:errorCode\\(\\) has no return typehint specified\\.$#"
25-
count: 1
26-
path: src/Tracing/Doctrine/DBAL/TracingDriverConnection.php
27-
28-
-
29-
message: "#^Method Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingDriverConnection\\:\\:errorInfo\\(\\) has no return typehint specified\\.$#"
19+
message: "#^Method Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingDriverConnection\\:\\:errorInfo\\(\\) return type has no value type specified in iterable type array\\.$#"
3020
count: 1
3121
path: src/Tracing/Doctrine/DBAL/TracingDriverConnection.php
3222

@@ -170,6 +160,16 @@ parameters:
170160
count: 1
171161
path: tests/Tracing/Cache/AbstractTraceableCacheAdapterTest.php
172162

163+
-
164+
message: "#^Trying to mock an undefined method errorCode\\(\\) on class Doctrine\\\\DBAL\\\\Driver\\\\Connection\\.$#"
165+
count: 1
166+
path: tests/Tracing/Doctrine/DBAL/TracingDriverConnectionTest.php
167+
168+
-
169+
message: "#^Trying to mock an undefined method errorInfo\\(\\) on class Doctrine\\\\DBAL\\\\Driver\\\\Connection\\.$#"
170+
count: 1
171+
path: tests/Tracing/Doctrine/DBAL/TracingDriverConnectionTest.php
172+
173173
-
174174
message: "#^Trying to mock an undefined method closeCursor\\(\\) on class Doctrine\\\\DBAL\\\\Driver\\\\Statement\\.$#"
175175
count: 1

src/Tracing/Doctrine/DBAL/TracingDriverConnection.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function lastInsertId($name = null)
133133
/**
134134
* {@inheritdoc}
135135
*/
136-
public function beginTransaction()
136+
public function beginTransaction(): bool
137137
{
138138
return $this->traceFunction(self::SPAN_OP_CONN_BEGIN_TRANSACTION, 'BEGIN TRANSACTION', function (): bool {
139139
return $this->decoratedConnection->beginTransaction();
@@ -143,7 +143,7 @@ public function beginTransaction()
143143
/**
144144
* {@inheritdoc}
145145
*/
146-
public function commit()
146+
public function commit(): bool
147147
{
148148
return $this->traceFunction(self::SPAN_OP_TRANSACTION_COMMIT, 'COMMIT', function (): bool {
149149
return $this->decoratedConnection->commit();
@@ -153,7 +153,7 @@ public function commit()
153153
/**
154154
* {@inheritdoc}
155155
*/
156-
public function rollBack()
156+
public function rollBack(): bool
157157
{
158158
return $this->traceFunction(self::SPAN_OP_TRANSACTION_ROLLBACK, 'ROLLBACK', function (): bool {
159159
return $this->decoratedConnection->rollBack();
@@ -163,9 +163,9 @@ public function rollBack()
163163
/**
164164
* {@inheritdoc}
165165
*/
166-
public function errorCode()
166+
public function errorCode(): ?string
167167
{
168-
if (method_exists($this->decoratedConnection, 'errorInfo')) {
168+
if (method_exists($this->decoratedConnection, 'errorCode')) {
169169
return $this->decoratedConnection->errorCode();
170170
}
171171

@@ -175,7 +175,7 @@ public function errorCode()
175175
/**
176176
* {@inheritdoc}
177177
*/
178-
public function errorInfo()
178+
public function errorInfo(): array
179179
{
180180
if (method_exists($this->decoratedConnection, 'errorInfo')) {
181181
return $this->decoratedConnection->errorInfo();

tests/Tracing/Doctrine/DBAL/TracingDriverConnectionTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,56 @@ public function testRollBackDoesNothingIfNoSpanIsSetOnHub(): void
339339
$this->assertFalse($this->connection->rollBack());
340340
}
341341

342+
public function testErrorCode(): void
343+
{
344+
if (!self::isDoctrineDBALVersion2Installed()) {
345+
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be ^2.13.');
346+
}
347+
348+
$this->decoratedConnection->expects($this->once())
349+
->method('errorCode')
350+
->willReturn('1002');
351+
352+
$this->assertSame('1002', $this->connection->errorCode());
353+
}
354+
355+
public function testErrorCodeThrowsExceptionIfDecoratedConnectionDoesNotImplementMethod(): void
356+
{
357+
if (!self::isDoctrineDBALVersion3Installed()) {
358+
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be >= 3.0.');
359+
}
360+
361+
$this->expectException(\BadMethodCallException::class);
362+
$this->expectExceptionMessage('The Sentry\\SentryBundle\\Tracing\\Doctrine\\DBAL\\TracingDriverConnection::errorCode() method is not supported on Doctrine DBAL 3.0.');
363+
364+
$this->connection->errorCode();
365+
}
366+
367+
public function testErrorInfo(): void
368+
{
369+
if (!self::isDoctrineDBALVersion2Installed()) {
370+
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be ^2.13.');
371+
}
372+
373+
$this->decoratedConnection->expects($this->once())
374+
->method('errorInfo')
375+
->willReturn(['foobar']);
376+
377+
$this->assertSame(['foobar'], $this->connection->errorInfo());
378+
}
379+
380+
public function testErrorInfoThrowsExceptionIfDecoratedConnectionDoesNotImplementMethod(): void
381+
{
382+
if (!self::isDoctrineDBALVersion3Installed()) {
383+
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be >= 3.0.');
384+
}
385+
386+
$this->expectException(\BadMethodCallException::class);
387+
$this->expectExceptionMessage('The Sentry\\SentryBundle\\Tracing\\Doctrine\\DBAL\\TracingDriverConnection::errorInfo() method is not supported on Doctrine DBAL 3.0.');
388+
389+
$this->connection->errorInfo();
390+
}
391+
342392
public function testGetWrappedConnection(): void
343393
{
344394
$connection = new TracingDriverConnection($this->hub, $this->decoratedConnection, 'foo_platform', []);

0 commit comments

Comments
 (0)