diff --git a/composer.json b/composer.json index 011dae8..82f963c 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a2a8d5d..83a6de6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - + tests/ diff --git a/src/ORMTestInfrastructure/ORMInfrastructure.php b/src/ORMTestInfrastructure/ORMInfrastructure.php index ea55c3c..77f6e79 100644 --- a/src/ORMTestInfrastructure/ORMInfrastructure.php +++ b/src/ORMTestInfrastructure/ORMInfrastructure.php @@ -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; @@ -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(); @@ -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(); } /** @@ -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) { diff --git a/src/ORMTestInfrastructure/Query.php b/src/ORMTestInfrastructure/Query.php index f9e36e4..3da999e 100644 --- a/src/ORMTestInfrastructure/Query.php +++ b/src/ORMTestInfrastructure/Query.php @@ -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 { diff --git a/src/ORMTestInfrastructure/QueryLogger.php b/src/ORMTestInfrastructure/QueryLogger.php new file mode 100644 index 0000000..44b40a0 --- /dev/null +++ b/src/ORMTestInfrastructure/QueryLogger.php @@ -0,0 +1,34 @@ +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; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..24f1e2f --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,8 @@ +