-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch tests to the middleware logging system
- Loading branch information
Showing
43 changed files
with
417 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\DbalExtensions; | ||
|
||
use Doctrine\Common\EventManager; | ||
use Doctrine\DBAL\Configuration; | ||
use Doctrine\DBAL\Connection as BaseConnection; | ||
use Doctrine\DBAL\Driver; | ||
use Doctrine\DBAL\Logging\Middleware as LoggingMiddleware; | ||
|
||
use function class_exists; | ||
|
||
class Connection extends BaseConnection | ||
{ | ||
/** @var QueryLog */ | ||
public $queryLog; | ||
|
||
public function __construct(array $params, Driver $driver, ?Configuration $config = null, ?EventManager $eventManager = null) | ||
{ | ||
$this->queryLog = new QueryLog(); | ||
if (class_exists(LoggingMiddleware::class)) { | ||
$logging = new LoggingMiddleware(new SqlLogger($this->queryLog)); | ||
$driver = $logging->wrap($driver); | ||
} else { | ||
$config = $config ?? new Configuration(); | ||
$config->setSQLLogger(new LegacySqlLogger($this->queryLog)); | ||
} | ||
|
||
parent::__construct($params, $driver, $config, $eventManager); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\DbalExtensions; | ||
|
||
use Doctrine\DBAL\Logging\SQLLogger; | ||
|
||
final class LegacySqlLogger implements SQLLogger | ||
{ | ||
/** @var QueryLog */ | ||
private $queryLog; | ||
|
||
public function __construct(QueryLog $queryLog) | ||
{ | ||
$this->queryLog = $queryLog; | ||
} | ||
|
||
public function startQuery($sql, ?array $params = null, ?array $types = null): void | ||
{ | ||
$this->queryLog->logQuery($sql, $params, $types); | ||
} | ||
|
||
public function stopQuery(): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\DbalExtensions; | ||
|
||
final class QueryLog | ||
{ | ||
/** @var bool */ | ||
public $enabled = false; | ||
|
||
/** @var list<array{sql: string, params: array|null, types: array|null}> */ | ||
public $queries = []; | ||
|
||
public function logQuery(string $sql, ?array $params = null, ?array $types = null): void | ||
{ | ||
if (! $this->enabled) { | ||
return; | ||
} | ||
|
||
$this->queries[] = [ | ||
'sql' => $sql, | ||
'params' => $params, | ||
'types' => $types, | ||
]; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function reset(): self | ||
{ | ||
$this->enabled = false; | ||
$this->queries = []; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function enable(): self | ||
{ | ||
$this->enabled = true; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function disable(): self | ||
{ | ||
$this->enabled = false; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\DbalExtensions; | ||
|
||
use Psr\Log\AbstractLogger; | ||
|
||
final class SqlLogger extends AbstractLogger | ||
{ | ||
/** @var QueryLog */ | ||
private $queryLog; | ||
|
||
public function __construct(QueryLog $queryLog) | ||
{ | ||
$this->queryLog = $queryLog; | ||
} | ||
|
||
public function log($level, $message, array $context = []): void | ||
{ | ||
if (! isset($context['sql'])) { | ||
return; | ||
} | ||
|
||
$this->queryLog->logQuery( | ||
$context['sql'], | ||
$context['params'] ?? null, | ||
$context['types'] ?? null | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.