|
|
@@ -1,4 +1,5 @@
|
|
|
<?php
|
|
|
+
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
/**
|
|
|
@@ -14,10 +15,12 @@ declare(strict_types=1);
|
|
|
* @since 4.3.0
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
|
|
*/
|
|
|
+
|
|
|
namespace Cake\TestSuite;
|
|
|
|
|
|
use Cake\Database\Connection;
|
|
|
use Cake\Datasource\ConnectionManager;
|
|
|
+use Cake\Log\Log;
|
|
|
use Cake\TestSuite\Fixture\FixtureDataManager;
|
|
|
use Cake\TestSuite\Fixture\FixtureLoader;
|
|
|
use PHPUnit\Runner\BeforeFirstTestHook;
|
|
|
@@ -34,13 +37,30 @@ class FixtureSchemaExtension implements BeforeFirstTestHook
|
|
|
{
|
|
|
FixtureLoader::setInstance(new FixtureDataManager());
|
|
|
|
|
|
- // This isn't a great place for this, but I plan on revisiting
|
|
|
- // how DB logging works in tests soon.
|
|
|
$enableLogging = in_array('--debug', $_SERVER['argv'] ?? [], true);
|
|
|
+ $this->configureLogging($enableLogging);
|
|
|
$this->aliasConnections($enableLogging);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Configures a logger for SQL queries.
|
|
|
+ *
|
|
|
+ * @param bool $enableLogging Whether or not logging should be enabled.
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ protected function configureLogging(bool $enableLogging): void
|
|
|
+ {
|
|
|
+ if (!$enableLogging) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Log::setConfig('queries', [
|
|
|
+ 'className' => 'Console',
|
|
|
+ 'stream' => 'php://stderr',
|
|
|
+ 'scopes' => ['queriesLog'],
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* Alias non test connections to the test ones
|
|
|
* so that models reach the test database connections instead.
|
|
|
*
|
|
|
@@ -50,8 +70,9 @@ class FixtureSchemaExtension implements BeforeFirstTestHook
|
|
|
protected function aliasConnections(bool $enableLogging): void
|
|
|
{
|
|
|
$connections = ConnectionManager::configured();
|
|
|
- ConnectionManager::alias('test', 'default');
|
|
|
- $map = [];
|
|
|
+ $map = [
|
|
|
+ 'test' => 'default',
|
|
|
+ ];
|
|
|
foreach ($connections as $connection) {
|
|
|
if ($connection === 'test' || $connection === 'default') {
|
|
|
continue;
|