Skip to content

Commit 28fb634

Browse files
authored
Replace DebugStack used for logging (#57)
`DebugStack` has been deprecated in DBAL 3.2 and will be removed in DBAL 4.
1 parent 696f069 commit 28fb634

File tree

6 files changed

+53
-17
lines changed

6 files changed

+53
-17
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@
1818
"require": {
1919
"php": "^8.0",
2020
"doctrine/common": "^2.11|^3.0",
21-
"doctrine/dbal": "^2.12|^3.0",
21+
"doctrine/dbal": "^3.2",
2222
"doctrine/deprecations": "^1.1",
2323
"doctrine/event-manager": "^1.1|^2.0",
2424
"doctrine/orm": "^2.14|^3.0",
2525
"doctrine/persistence": "^2.0|^3.0",
26+
"psr/log": "^2.0|^3.0",
2627
"symfony/cache": "^5.0|^6.0|^7.0"
2728
},
2829
"require-dev": {
2930
"doctrine/collections": "^1.6.8|^2.2.1",
3031
"phpunit/phpunit": "^9.6.19",
31-
"symfony/phpunit-bridge": "^6.0|^7.0"
32+
"symfony/error-handler": "^5.4|^6.0|^7.0",
33+
"symfony/phpunit-bridge": ">= 7.0"
3234
},
3335
"autoload": {
3436
"psr-4": {

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd" bootstrap="vendor/autoload.php">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd" bootstrap="tests/bootstrap.php">
33
<testsuites>
44
<testsuite name="Library Test Suite">
55
<directory>tests/</directory>

src/ORMTestInfrastructure/ORMInfrastructure.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
use Doctrine\Common\EventManager;
1313
use Doctrine\DBAL\DriverManager;
14+
use Doctrine\DBAL\Logging\Middleware as LoggingMiddleware;
1415
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
1516
use Doctrine\Persistence\ObjectRepository;
16-
use Doctrine\DBAL\Logging\DebugStack;
1717
use Doctrine\ORM\EntityManager;
1818
use Doctrine\ORM\Events;
1919
use Doctrine\ORM\Mapping\ClassMetadataFactory;
@@ -197,7 +197,7 @@ private function __construct($entityClasses, ?ConnectionConfiguration $connectio
197197
}
198198
$this->entityClasses = $entityClasses;
199199
$this->connectionConfiguration = $connectionConfiguration;
200-
$this->queryLogger = new DebugStack();
200+
$this->queryLogger = new QueryLogger();
201201
$this->namingStrategy = new DefaultNamingStrategy();
202202
$this->mappingDriver = $mappingDriver;
203203
$this->resolveTargetListener = new ResolveTargetEntityListener();
@@ -242,13 +242,7 @@ public function getRepository($classNameOrEntity)
242242
*/
243243
public function getQueries()
244244
{
245-
return array_map(function (array $queryData) {
246-
return new Query(
247-
$queryData['sql'],
248-
(isset($queryData['params']) ? $queryData['params'] : array()),
249-
$queryData['executionMS']
250-
);
251-
}, $this->queryLogger->queries);
245+
return $this->queryLogger->getQueries();
252246
}
253247

254248
/**
@@ -332,7 +326,9 @@ protected function createEntityManager()
332326
{
333327
$configFactory = new ConfigurationFactory($this->mappingDriver);
334328
$config = $configFactory->createFor($this->entityClasses);
335-
$config->setSQLLogger($this->queryLogger);
329+
$middlewares = $config->getMiddlewares();
330+
$middlewares[] = new LoggingMiddleware($this->queryLogger);
331+
$config->setMiddlewares($middlewares);
336332
$config->setNamingStrategy($this->namingStrategy);
337333

338334
if ($this->connectionConfiguration instanceof ExistingConnectionConfiguration) {

src/ORMTestInfrastructure/Query.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111

1212
/**
1313
* Represents a query that has been executed.
14-
*
15-
* This class is designed to be populated by the data that is gathered by the DebugStack logger.
16-
*
17-
* @see \Doctrine\DBAL\Logging\DebugStack
1814
*/
1915
class Query
2016
{
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Webfactory\Doctrine\ORMTestInfrastructure;
4+
5+
use Psr\Log\AbstractLogger;
6+
use Psr\Log\LoggerInterface;
7+
8+
class QueryLogger extends AbstractLogger
9+
{
10+
public bool $enabled = true;
11+
private array $queries = [];
12+
13+
public function log($level, \Stringable|string $message, array $context = []): void
14+
{
15+
if (!$this->enabled) {
16+
return;
17+
}
18+
19+
if (str_starts_with($message, 'Executing')) {
20+
$this->queries[] = new Query($context['sql'], $context['params'] ?? []);
21+
} else if ('Beginning transaction' === $message) {
22+
$this->queries[] = new Query('"START TRANSACTION"', []);
23+
} else if ('Committing transaction' === $message) {
24+
$this->queries[] = new Query('"COMMIT"', []);
25+
} else if ('Rolling back transaction' === $message) {
26+
$this->queries[] = new Query('"ROLLBACK"', []);
27+
}
28+
}
29+
30+
public function getQueries()
31+
{
32+
return $this->queries;
33+
}
34+
}

tests/bootstrap.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use Symfony\Component\ErrorHandler\Debug;
4+
use Symfony\Component\ErrorHandler\DebugClassLoader;
5+
6+
require_once __DIR__.'/../vendor/autoload.php';
7+
8+
DebugClassLoader::enable();

0 commit comments

Comments
 (0)