Skip to content

Fix wrong method existence check in TracingDriverConnection::errorCode() #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

## 4.2.3 (2021-09-21)

Expand Down
22 changes: 11 additions & 11 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@ parameters:
path: src/EventListener/ErrorListener.php

-
message: "#^Call to an undefined method Doctrine\\\\DBAL\\\\Driver\\\\Connection\\:\\:errorCode\\(\\)\\.$#"
count: 1
path: src/Tracing/Doctrine/DBAL/TracingDriverConnection.php

-
message: "#^Method Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingDriverConnection\\:\\:errorCode\\(\\) has no return typehint specified\\.$#"
count: 1
path: src/Tracing/Doctrine/DBAL/TracingDriverConnection.php

-
message: "#^Method Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingDriverConnection\\:\\:errorInfo\\(\\) has no return typehint specified\\.$#"
message: "#^Method Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingDriverConnection\\:\\:errorInfo\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Tracing/Doctrine/DBAL/TracingDriverConnection.php

Expand Down Expand Up @@ -170,6 +160,16 @@ parameters:
count: 1
path: tests/Tracing/Cache/AbstractTraceableCacheAdapterTest.php

-
message: "#^Trying to mock an undefined method errorCode\\(\\) on class Doctrine\\\\DBAL\\\\Driver\\\\Connection\\.$#"
count: 1
path: tests/Tracing/Doctrine/DBAL/TracingDriverConnectionTest.php

-
message: "#^Trying to mock an undefined method errorInfo\\(\\) on class Doctrine\\\\DBAL\\\\Driver\\\\Connection\\.$#"
count: 1
path: tests/Tracing/Doctrine/DBAL/TracingDriverConnectionTest.php

-
message: "#^Trying to mock an undefined method closeCursor\\(\\) on class Doctrine\\\\DBAL\\\\Driver\\\\Statement\\.$#"
count: 1
Expand Down
12 changes: 6 additions & 6 deletions src/Tracing/Doctrine/DBAL/TracingDriverConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function lastInsertId($name = null)
/**
* {@inheritdoc}
*/
public function beginTransaction()
public function beginTransaction(): bool
{
return $this->traceFunction(self::SPAN_OP_CONN_BEGIN_TRANSACTION, 'BEGIN TRANSACTION', function (): bool {
return $this->decoratedConnection->beginTransaction();
Expand All @@ -143,7 +143,7 @@ public function beginTransaction()
/**
* {@inheritdoc}
*/
public function commit()
public function commit(): bool
{
return $this->traceFunction(self::SPAN_OP_TRANSACTION_COMMIT, 'COMMIT', function (): bool {
return $this->decoratedConnection->commit();
Expand All @@ -153,7 +153,7 @@ public function commit()
/**
* {@inheritdoc}
*/
public function rollBack()
public function rollBack(): bool
{
return $this->traceFunction(self::SPAN_OP_TRANSACTION_ROLLBACK, 'ROLLBACK', function (): bool {
return $this->decoratedConnection->rollBack();
Expand All @@ -163,9 +163,9 @@ public function rollBack()
/**
* {@inheritdoc}
*/
public function errorCode()
public function errorCode(): ?string
{
if (method_exists($this->decoratedConnection, 'errorInfo')) {
if (method_exists($this->decoratedConnection, 'errorCode')) {
return $this->decoratedConnection->errorCode();
}

Expand All @@ -175,7 +175,7 @@ public function errorCode()
/**
* {@inheritdoc}
*/
public function errorInfo()
public function errorInfo(): array
{
if (method_exists($this->decoratedConnection, 'errorInfo')) {
return $this->decoratedConnection->errorInfo();
Expand Down
50 changes: 50 additions & 0 deletions tests/Tracing/Doctrine/DBAL/TracingDriverConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,56 @@ public function testRollBackDoesNothingIfNoSpanIsSetOnHub(): void
$this->assertFalse($this->connection->rollBack());
}

public function testErrorCode(): void
{
if (!self::isDoctrineDBALVersion2Installed()) {
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be ^2.13.');
}

$this->decoratedConnection->expects($this->once())
->method('errorCode')
->willReturn('1002');

$this->assertSame('1002', $this->connection->errorCode());
}

public function testErrorCodeThrowsExceptionIfDecoratedConnectionDoesNotImplementMethod(): void
{
if (!self::isDoctrineDBALVersion3Installed()) {
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be >= 3.0.');
}

$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('The Sentry\\SentryBundle\\Tracing\\Doctrine\\DBAL\\TracingDriverConnection::errorCode() method is not supported on Doctrine DBAL 3.0.');

$this->connection->errorCode();
}

public function testErrorInfo(): void
{
if (!self::isDoctrineDBALVersion2Installed()) {
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be ^2.13.');
}

$this->decoratedConnection->expects($this->once())
->method('errorInfo')
->willReturn(['foobar']);

$this->assertSame(['foobar'], $this->connection->errorInfo());
}

public function testErrorInfoThrowsExceptionIfDecoratedConnectionDoesNotImplementMethod(): void
{
if (!self::isDoctrineDBALVersion3Installed()) {
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be >= 3.0.');
}

$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('The Sentry\\SentryBundle\\Tracing\\Doctrine\\DBAL\\TracingDriverConnection::errorInfo() method is not supported on Doctrine DBAL 3.0.');

$this->connection->errorInfo();
}

public function testGetWrappedConnection(): void
{
$connection = new TracingDriverConnection($this->hub, $this->decoratedConnection, 'foo_platform', []);
Expand Down