Skip to content

Commit 22b99bf

Browse files
committed
Avoid using DebugStack
1 parent b2c2f14 commit 22b99bf

File tree

6 files changed

+193
-117
lines changed

6 files changed

+193
-117
lines changed

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,8 @@ parameters:
635635
count: 1
636636
path: tests/Gedmo/Tree/RepositoryTest.php
637637

638+
-
639+
message: "#^Variable \\$stack might not be defined\\.$#"
640+
count: 3
641+
path: tests/Gedmo/Tree/TreeObjectHydratorTest.php
642+

tests/Gedmo/Tool/BaseTestCaseORM.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
use Gedmo\Timestampable\TimestampableListener;
2929
use Gedmo\Translatable\TranslatableListener;
3030
use Gedmo\Tree\TreeListener;
31-
use PHPUnit\Framework\MockObject\MockObject;
3231
use PHPUnit\Framework\TestCase;
33-
use Psr\Log\LoggerInterface;
3432

3533
/**
3634
* Base test case contains common mock objects
@@ -41,24 +39,15 @@
4139
*/
4240
abstract class BaseTestCaseORM extends TestCase
4341
{
44-
/**
45-
* @var EntityManager|null
46-
*/
47-
protected $em;
42+
protected ?EntityManager $em = null;
4843

49-
/**
50-
* @var QueryAnalyzer
51-
*/
52-
protected $queryAnalyzer;
44+
protected QueryAnalyzer $queryAnalyzer;
5345

54-
/**
55-
* @var MockObject&LoggerInterface
56-
*/
57-
protected $queryLogger;
46+
protected QueryLogger $queryLogger;
5847

5948
protected function setUp(): void
6049
{
61-
$this->queryLogger = $this->createMock(LoggerInterface::class);
50+
$this->queryLogger = new QueryLogger();
6251
}
6352

6453
/**

tests/Gedmo/Tool/QueryLogger.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Tests\Tool;
13+
14+
use Psr\Log\AbstractLogger;
15+
16+
final class QueryLogger extends AbstractLogger
17+
{
18+
/** @var array<int, array{message: string, context: mixed[]}> */
19+
public array $queries = [];
20+
21+
/**
22+
* @param mixed $level
23+
* @param string $message
24+
* @param mixed[] $context
25+
*/
26+
public function log($level, $message, array $context = []): void
27+
{
28+
$this->queries[] = [
29+
'message' => $message,
30+
'context' => $context,
31+
];
32+
}
33+
34+
public function reset(): void
35+
{
36+
$this->queries = [];
37+
}
38+
}

tests/Gedmo/Translatable/PersonalTranslationTest.php

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,35 @@ public function testShouldTranslateTheRecord(): void
7070

7171
// TODO: Remove the "if" check and "else" body when dropping support of doctrine/dbal 2.
7272
if (class_exists(Middleware::class)) {
73-
$this->queryLogger
74-
->expects(static::exactly(2))
75-
->method('debug')
76-
->withConsecutive(
77-
['Executing statement: {sql} (parameters: {params}, types: {types})', [
78-
'sql' => 'SELECT t0.id AS id_1, t0.title AS title_2 FROM Article t0 WHERE t0.id = ?',
79-
'params' => [1 => 1],
80-
'types' => [1 => ParameterType::INTEGER],
81-
]],
82-
['Executing statement: {sql} (parameters: {params}, types: {types})', [
83-
'sql' => 'SELECT t0.id AS id_1, t0.locale AS locale_2, t0.field AS field_3, t0.content AS content_4, t0.object_id AS object_id_5 FROM article_translations t0 WHERE t0.object_id = ?',
84-
'params' => [1 => 1],
85-
'types' => [1 => ParameterType::INTEGER],
86-
]]
87-
);
73+
$this->queryLogger->reset();
8874
} else {
8975
$this->startQueryLog();
9076
}
9177

9278
$article = $this->em->find(self::ARTICLE, ['id' => 1]);
9379

94-
// TODO: Remove the "if" block when dropping support of doctrine/dbal 2.
95-
if (!class_exists(Middleware::class)) {
80+
// TODO: Remove the "if" check and "else" body when dropping support of doctrine/dbal 2.
81+
if (class_exists(Middleware::class)) {
82+
static::assertCount(2, $this->queryLogger->queries);
83+
84+
static::assertSame([
85+
'message' => 'Executing statement: {sql} (parameters: {params}, types: {types})',
86+
'context' => [
87+
'sql' => 'SELECT t0.id AS id_1, t0.title AS title_2 FROM Article t0 WHERE t0.id = ?',
88+
'params' => [1 => 1],
89+
'types' => [1 => ParameterType::INTEGER],
90+
],
91+
], $this->queryLogger->queries[0]);
92+
93+
static::assertSame([
94+
'message' => 'Executing statement: {sql} (parameters: {params}, types: {types})',
95+
'context' => [
96+
'sql' => 'SELECT t0.id AS id_1, t0.locale AS locale_2, t0.field AS field_3, t0.content AS content_4, t0.object_id AS object_id_5 FROM article_translations t0 WHERE t0.object_id = ?',
97+
'params' => [1 => 1],
98+
'types' => [1 => ParameterType::INTEGER],
99+
],
100+
], $this->queryLogger->queries[1]);
101+
} else {
96102
$sqlQueriesExecuted = $this->queryAnalyzer->getExecutedQueries();
97103
static::assertCount(2, $sqlQueriesExecuted);
98104
static::assertSame('SELECT t0.id AS id_1, t0.locale AS locale_2, t0.field AS field_3, t0.content AS content_4, t0.object_id AS object_id_5 FROM article_translations t0 WHERE t0.object_id = 1', $sqlQueriesExecuted[1]);
@@ -193,24 +199,7 @@ public function testShouldFindFromIdentityMap(): void
193199

194200
// TODO: Remove the "if" check and "else" body when dropping support of doctrine/dbal 2.
195201
if (class_exists(Middleware::class)) {
196-
$this->queryLogger
197-
->expects(static::exactly(3))
198-
->method('debug')
199-
->withConsecutive(
200-
['Beginning transaction'],
201-
['Executing statement: {sql} (parameters: {params}, types: {types})', [
202-
'sql' => 'UPDATE article_translations SET content = ? WHERE id = ?',
203-
'params' => [
204-
1 => 'change lt',
205-
2 => 1,
206-
],
207-
'types' => [
208-
1 => ParameterType::STRING,
209-
2 => ParameterType::INTEGER,
210-
],
211-
]],
212-
['Committing transaction']
213-
);
202+
$this->queryLogger->reset();
214203
} else {
215204
$this->startQueryLog();
216205
}
@@ -221,8 +210,35 @@ public function testShouldFindFromIdentityMap(): void
221210
$this->em->persist($article);
222211
$this->em->flush();
223212

224-
// TODO: Remove the "if" block when dropping support of doctrine/dbal 2.
225-
if (!class_exists(Middleware::class)) {
213+
// TODO: Remove the "if" check and "else" body when dropping support of doctrine/dbal 2.
214+
if (class_exists(Middleware::class)) {
215+
static::assertCount(3, $this->queryLogger->queries);
216+
217+
static::assertSame([
218+
'message' => 'Beginning transaction',
219+
'context' => [],
220+
], $this->queryLogger->queries[0]);
221+
222+
static::assertSame([
223+
'message' => 'Executing statement: {sql} (parameters: {params}, types: {types})',
224+
'context' => [
225+
'sql' => 'UPDATE article_translations SET content = ? WHERE id = ?',
226+
'params' => [
227+
1 => 'change lt',
228+
2 => 1,
229+
],
230+
'types' => [
231+
1 => ParameterType::STRING,
232+
2 => ParameterType::INTEGER,
233+
],
234+
],
235+
], $this->queryLogger->queries[1]);
236+
237+
static::assertSame([
238+
'message' => 'Committing transaction',
239+
'context' => [],
240+
], $this->queryLogger->queries[2]);
241+
} else {
226242
$sqlQueriesExecuted = $this->queryAnalyzer->getExecutedQueries();
227243
static::assertCount(3, $sqlQueriesExecuted); // one update, transaction start - commit
228244
static::assertSame("UPDATE article_translations SET content = 'change lt' WHERE id = 1", $sqlQueriesExecuted[1]);
@@ -241,14 +257,7 @@ public function testShouldBeAbleToUseTranslationQueryHint(): void
241257

242258
// TODO: Remove the "if" check and "else" body when dropping support of doctrine/dbal 2.
243259
if (class_exists(Middleware::class)) {
244-
$this->queryLogger
245-
->expects(static::exactly(1))
246-
->method('debug')
247-
->withConsecutive(
248-
['Executing query: {sql}', [
249-
'sql' => "SELECT CAST(t1_.content AS VARCHAR(128)) AS title_0 FROM Article a0_ LEFT JOIN article_translations t1_ ON t1_.locale = 'lt' AND t1_.field = 'title' AND t1_.object_id = a0_.id",
250-
]]
251-
);
260+
$this->queryLogger->reset();
252261
} else {
253262
$this->startQueryLog();
254263
}
@@ -258,8 +267,17 @@ public function testShouldBeAbleToUseTranslationQueryHint(): void
258267
static::assertCount(1, $result);
259268
static::assertSame('lt', $result[0]['title']);
260269

261-
// TODO: Remove the "if" block when dropping support of doctrine/dbal 2.
262-
if (!class_exists(Middleware::class)) {
270+
// TODO: Remove the "if" check and "else" body when dropping support of doctrine/dbal 2.
271+
if (class_exists(Middleware::class)) {
272+
static::assertCount(1, $this->queryLogger->queries);
273+
274+
static::assertSame([
275+
'message' => 'Executing query: {sql}',
276+
'context' => [
277+
'sql' => "SELECT CAST(t1_.content AS VARCHAR(128)) AS title_0 FROM Article a0_ LEFT JOIN article_translations t1_ ON t1_.locale = 'lt' AND t1_.field = 'title' AND t1_.object_id = a0_.id",
278+
],
279+
], $this->queryLogger->queries[0]);
280+
} else {
263281
$sqlQueriesExecuted = $this->queryAnalyzer->getExecutedQueries();
264282
static::assertCount(1, $sqlQueriesExecuted);
265283
static::assertSame("SELECT CAST(t1_.content AS VARCHAR(128)) AS title_0 FROM Article a0_ LEFT JOIN article_translations t1_ ON t1_.locale = 'lt' AND t1_.field = 'title' AND t1_.object_id = a0_.id", $sqlQueriesExecuted[0]);

0 commit comments

Comments
 (0)