Browse Source

Make SQL logging in tests much simpler to operate

Instead of having to edit the phpunit.xml and/or application configuration
to enable logging, we can use the `--debug` flag that PHPUnit has to
also enable debug mode for our fixtures and database package. This
feature will only work with the new fixture extension.
Mark Story 4 years ago
parent
commit
cf37c3a833
2 changed files with 25 additions and 9 deletions
  1. 25 4
      src/TestSuite/FixtureSchemaExtension.php
  2. 0 5
      tests/bootstrap.php

+ 25 - 4
src/TestSuite/FixtureSchemaExtension.php

@@ -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;

+ 0 - 5
tests/bootstrap.php

@@ -110,11 +110,6 @@ Configure::write('Session', [
 Configure::write('Debugger.exportFormatter', TextFormatter::class);
 
 Log::setConfig([
-    // 'queries' => [
-    //     'className' => 'Console',
-    //     'stream' => 'php://stderr',
-    //     'scopes' => ['queriesLog'],
-    // ],
     'debug' => [
         'engine' => 'Cake\Log\Engine\FileLog',
         'levels' => ['notice', 'info', 'debug'],