Skip to content

Commit c84d330

Browse files
committed
Merge branch '3.6.x' into 4.0.x
* 3.6.x: Bump laminas/automatic-releases to 1.24.0 Free pgsql results on destruct PHPUnit 9.6.3 Use TestLogger instead of mocks
2 parents e543ade + cb208f8 commit c84d330

File tree

5 files changed

+83
-56
lines changed

5 files changed

+83
-56
lines changed

.github/workflows/release-on-milestone-closed.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
uses: "actions/checkout@v3"
1616

1717
- name: "Release"
18-
uses: "laminas/automatic-releases@1.0.1"
18+
uses: "laminas/automatic-releases@1.24.0"
1919
with:
2020
command-name: "laminas:automatic-releases:release"
2121
env:

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@
3939
},
4040
"require-dev": {
4141
"doctrine/coding-standard": "11.1.0",
42+
"fig/log-test": "^1",
4243
"jetbrains/phpstorm-stubs": "2022.3",
4344
"phpstan/phpstan": "1.9.14",
4445
"phpstan/phpstan-phpunit": "1.3.3",
4546
"phpstan/phpstan-strict-rules": "^1.4",
46-
"phpunit/phpunit": "9.6.0",
47+
"phpunit/phpunit": "9.6.3",
4748
"psalm/plugin-phpunit": "0.18.4",
4849
"squizlabs/php_codesniffer": "3.7.1",
4950
"symfony/cache": "^5.4|^6.0",

src/Driver/PgSQL/Result.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ public function __construct(PgSqlResult $result)
3737
$this->result = $result;
3838
}
3939

40+
public function __destruct()
41+
{
42+
if (! isset($this->result)) {
43+
return;
44+
}
45+
46+
$this->free();
47+
}
48+
4049
/** {@inheritdoc} */
4150
public function fetchNumeric(): array|false
4251
{

tests/Functional/Driver/PgSQL/ResultTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44

55
namespace Doctrine\DBAL\Tests\Functional\Driver\PgSQL;
66

7+
use Doctrine\DBAL\Driver\PgSQL\Result;
78
use Doctrine\DBAL\Tests\FunctionalTestCase;
89
use Doctrine\DBAL\Tests\TestUtil;
910
use Doctrine\DBAL\Types\Types;
11+
use Error;
1012
use Generator;
13+
use PgSql\Connection as PgSqlConnection;
1114

15+
use function assert;
1216
use function chr;
17+
use function pg_query;
18+
use function pg_result_status;
19+
20+
use const PGSQL_TUPLES_OK;
1321

1422
class ResultTest extends FunctionalTestCase
1523
{
@@ -209,4 +217,21 @@ public function testTypeConversionWithDuplicateFieldNames(): void
209217
$this->connection->fetchFirstColumn('SELECT a.*, b.* FROM types_test a, types_test2 b'),
210218
);
211219
}
220+
221+
public function testResultIsFreedOnDestruct(): void
222+
{
223+
$pgsqlConnection = $this->connection->getNativeConnection();
224+
assert($pgsqlConnection instanceof PgSqlConnection);
225+
$pgsqlResult = pg_query($pgsqlConnection, 'SELECT 1');
226+
assert($pgsqlResult !== false);
227+
228+
self::assertSame(PGSQL_TUPLES_OK, pg_result_status($pgsqlResult));
229+
230+
new Result($pgsqlResult);
231+
232+
$this->expectException(Error::class);
233+
$this->expectExceptionMessage('PostgreSQL result has already been closed');
234+
235+
pg_result_status($pgsqlResult);
236+
}
212237
}

tests/Logging/MiddlewareTest.php

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
use Doctrine\DBAL\Driver\Connection;
99
use Doctrine\DBAL\Logging\Middleware;
1010
use Doctrine\DBAL\ParameterType;
11-
use PHPUnit\Framework\MockObject\MockObject;
1211
use PHPUnit\Framework\TestCase;
13-
use Psr\Log\LoggerInterface;
12+
use Psr\Log\Test\TestLogger;
1413

1514
class MiddlewareTest extends TestCase
1615
{
1716
private Driver $driver;
18-
19-
/** @var LoggerInterface&MockObject */
20-
private LoggerInterface $logger;
17+
private TestLogger $logger;
2118

2219
public function setUp(): void
2320
{
@@ -27,104 +24,99 @@ public function setUp(): void
2724
$driver->method('connect')
2825
->willReturn($connection);
2926

30-
$this->logger = $this->createMock(LoggerInterface::class);
27+
$this->logger = new TestLogger();
3128

3229
$middleware = new Middleware($this->logger);
3330
$this->driver = $middleware->wrap($driver);
3431
}
3532

3633
public function testConnectAndDisconnect(): void
3734
{
38-
$this->logger->expects(self::exactly(2))
39-
->method('info')
40-
->withConsecutive(
41-
[
42-
'Connecting with parameters {params}',
43-
[
44-
'params' => [
45-
'username' => 'admin',
46-
'password' => '<redacted>',
47-
'url' => '<redacted>',
48-
],
49-
],
50-
],
51-
['Disconnecting', []],
52-
);
53-
5435
$this->driver->connect([
5536
'username' => 'admin',
5637
'password' => 'Passw0rd!',
5738
'url' => 'mysql://user:secret@localhost/mydb',
5839
]);
40+
41+
self::assertTrue($this->logger->hasInfo([
42+
'message' => 'Connecting with parameters {params}',
43+
'context' => [
44+
'params' => [
45+
'username' => 'admin',
46+
'password' => '<redacted>',
47+
'url' => '<redacted>',
48+
],
49+
],
50+
]));
5951
}
6052

6153
public function testQuery(): void
6254
{
63-
$this->logger->expects(self::once())
64-
->method('debug')
65-
->with('Executing query: {sql}', ['sql' => 'SELECT 1']);
66-
6755
$connection = $this->driver->connect([]);
6856
$connection->query('SELECT 1');
57+
58+
self::assertTrue($this->logger->hasDebug([
59+
'message' => 'Executing query: {sql}',
60+
'context' => ['sql' => 'SELECT 1'],
61+
]));
6962
}
7063

7164
public function testExec(): void
7265
{
73-
$this->logger->expects(self::once())
74-
->method('debug')
75-
->with('Executing statement: {sql}', ['sql' => 'DROP DATABASE doctrine']);
76-
7766
$connection = $this->driver->connect([]);
7867
$connection->exec('DROP DATABASE doctrine');
68+
69+
self::assertTrue($this->logger->hasDebug([
70+
'message' => 'Executing statement: {sql}',
71+
'context' => ['sql' => 'DROP DATABASE doctrine'],
72+
]));
7973
}
8074

8175
public function testBeginCommitRollback(): void
8276
{
83-
$this->logger->expects(self::exactly(3))
84-
->method('debug')
85-
->withConsecutive(
86-
['Beginning transaction'],
87-
['Committing transaction'],
88-
['Rolling back transaction'],
89-
);
90-
9177
$connection = $this->driver->connect([]);
9278
$connection->beginTransaction();
9379
$connection->commit();
9480
$connection->rollBack();
81+
82+
self::assertTrue($this->logger->hasDebug('Beginning transaction'));
83+
self::assertTrue($this->logger->hasDebug('Committing transaction'));
84+
self::assertTrue($this->logger->hasDebug('Rolling back transaction'));
9585
}
9686

9787
public function testExecuteStatementWithParameters(): void
9888
{
99-
$this->logger->expects(self::once())
100-
->method('debug')
101-
->with('Executing statement: {sql} (parameters: {params}, types: {types})', [
102-
'sql' => 'SELECT ?, ?',
103-
'params' => [1 => 42],
104-
'types' => [1 => ParameterType::INTEGER],
105-
]);
106-
10789
$connection = $this->driver->connect([]);
10890
$statement = $connection->prepare('SELECT ?, ?');
10991
$statement->bindValue(1, 42, ParameterType::INTEGER);
11092

11193
$statement->execute();
94+
95+
self::assertTrue($this->logger->hasDebug([
96+
'message' => 'Executing statement: {sql} (parameters: {params}, types: {types})',
97+
'context' => [
98+
'sql' => 'SELECT ?, ?',
99+
'params' => [1 => 42],
100+
'types' => [1 => ParameterType::INTEGER],
101+
],
102+
]));
112103
}
113104

114105
public function testExecuteStatementWithNamedParameters(): void
115106
{
116-
$this->logger->expects(self::once())
117-
->method('debug')
118-
->with('Executing statement: {sql} (parameters: {params}, types: {types})', [
119-
'sql' => 'SELECT :value',
120-
'params' => ['value' => 'Test'],
121-
'types' => ['value' => ParameterType::STRING],
122-
]);
123-
124107
$connection = $this->driver->connect([]);
125108
$statement = $connection->prepare('SELECT :value');
126109
$statement->bindValue('value', 'Test', ParameterType::STRING);
127110

128111
$statement->execute();
112+
113+
self::assertTrue($this->logger->hasDebug([
114+
'message' => 'Executing statement: {sql} (parameters: {params}, types: {types})',
115+
'context' => [
116+
'sql' => 'SELECT :value',
117+
'params' => ['value' => 'Test'],
118+
'types' => ['value' => ParameterType::STRING],
119+
],
120+
]));
129121
}
130122
}

0 commit comments

Comments
 (0)