Skip to content

Commit

Permalink
Replace DebugStack used for logging (#57)
Browse files Browse the repository at this point in the history
`DebugStack` has been deprecated in DBAL 3.2 and will be removed in DBAL
4.
  • Loading branch information
mpdude authored Sep 5, 2024
1 parent 696f069 commit 28fb634
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 17 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@
"require": {
"php": "^8.0",
"doctrine/common": "^2.11|^3.0",
"doctrine/dbal": "^2.12|^3.0",
"doctrine/dbal": "^3.2",
"doctrine/deprecations": "^1.1",
"doctrine/event-manager": "^1.1|^2.0",
"doctrine/orm": "^2.14|^3.0",
"doctrine/persistence": "^2.0|^3.0",
"psr/log": "^2.0|^3.0",
"symfony/cache": "^5.0|^6.0|^7.0"
},
"require-dev": {
"doctrine/collections": "^1.6.8|^2.2.1",
"phpunit/phpunit": "^9.6.19",
"symfony/phpunit-bridge": "^6.0|^7.0"
"symfony/error-handler": "^5.4|^6.0|^7.0",
"symfony/phpunit-bridge": ">= 7.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd" bootstrap="vendor/autoload.php">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd" bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="Library Test Suite">
<directory>tests/</directory>
Expand Down
16 changes: 6 additions & 10 deletions src/ORMTestInfrastructure/ORMInfrastructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

use Doctrine\Common\EventManager;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Logging\Middleware as LoggingMiddleware;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\ObjectRepository;
use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
Expand Down Expand Up @@ -197,7 +197,7 @@ private function __construct($entityClasses, ?ConnectionConfiguration $connectio
}
$this->entityClasses = $entityClasses;
$this->connectionConfiguration = $connectionConfiguration;
$this->queryLogger = new DebugStack();
$this->queryLogger = new QueryLogger();
$this->namingStrategy = new DefaultNamingStrategy();
$this->mappingDriver = $mappingDriver;
$this->resolveTargetListener = new ResolveTargetEntityListener();
Expand Down Expand Up @@ -242,13 +242,7 @@ public function getRepository($classNameOrEntity)
*/
public function getQueries()
{
return array_map(function (array $queryData) {
return new Query(
$queryData['sql'],
(isset($queryData['params']) ? $queryData['params'] : array()),
$queryData['executionMS']
);
}, $this->queryLogger->queries);
return $this->queryLogger->getQueries();
}

/**
Expand Down Expand Up @@ -332,7 +326,9 @@ protected function createEntityManager()
{
$configFactory = new ConfigurationFactory($this->mappingDriver);
$config = $configFactory->createFor($this->entityClasses);
$config->setSQLLogger($this->queryLogger);
$middlewares = $config->getMiddlewares();
$middlewares[] = new LoggingMiddleware($this->queryLogger);
$config->setMiddlewares($middlewares);
$config->setNamingStrategy($this->namingStrategy);

if ($this->connectionConfiguration instanceof ExistingConnectionConfiguration) {
Expand Down
4 changes: 0 additions & 4 deletions src/ORMTestInfrastructure/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@

/**
* Represents a query that has been executed.
*
* This class is designed to be populated by the data that is gathered by the DebugStack logger.
*
* @see \Doctrine\DBAL\Logging\DebugStack
*/
class Query
{
Expand Down
34 changes: 34 additions & 0 deletions src/ORMTestInfrastructure/QueryLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Webfactory\Doctrine\ORMTestInfrastructure;

use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;

class QueryLogger extends AbstractLogger
{
public bool $enabled = true;
private array $queries = [];

public function log($level, \Stringable|string $message, array $context = []): void
{
if (!$this->enabled) {
return;
}

if (str_starts_with($message, 'Executing')) {
$this->queries[] = new Query($context['sql'], $context['params'] ?? []);
} else if ('Beginning transaction' === $message) {
$this->queries[] = new Query('"START TRANSACTION"', []);
} else if ('Committing transaction' === $message) {
$this->queries[] = new Query('"COMMIT"', []);
} else if ('Rolling back transaction' === $message) {
$this->queries[] = new Query('"ROLLBACK"', []);
}
}

public function getQueries()
{
return $this->queries;
}
}
8 changes: 8 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\ErrorHandler\DebugClassLoader;

require_once __DIR__.'/../vendor/autoload.php';

DebugClassLoader::enable();

0 comments on commit 28fb634

Please sign in to comment.