Browse Source

Merge pull request #16989 from cakephp/4.x-string-bug

Fix up bug detected by Psalm
Mark Story 3 years ago
parent
commit
dfebc4c725
2 changed files with 21 additions and 2 deletions
  1. 1 1
      src/Database/Log/QueryLogger.php
  2. 20 1
      tests/TestCase/Database/Log/QueryLoggerTest.php

+ 1 - 1
src/Database/Log/QueryLogger.php

@@ -52,6 +52,6 @@ class QueryLogger extends BaseLog
             $context = $context['query']->getContext() + $context;
             $message = 'connection={connection} duration={took} rows={numRows} ' . $message;
         }
-        Log::write('debug', $message, $context);
+        Log::write('debug', (string)$message, $context);
     }
 }

+ 20 - 1
tests/TestCase/Database/Log/QueryLoggerTest.php

@@ -56,13 +56,32 @@ class QueryLoggerTest extends TestCase
             'className' => 'Array',
             'scopes' => ['foo'],
         ]);
-        $logger->log(LogLevel::DEBUG, (string)$query, compact('query'));
+        $logger->log(LogLevel::DEBUG, $query, compact('query'));
 
         $this->assertCount(1, Log::engine('queryLoggerTest')->read());
         $this->assertCount(0, Log::engine('queryLoggerTest2')->read());
     }
 
     /**
+     * Tests that passed Stringable also work.
+     */
+    public function testLogFunctionStringable(): void
+    {
+        $this->skipIf(version_compare(PHP_VERSION, '8.0', '<'), 'Stringable exists since 8.0');
+
+        $logger = new QueryLogger(['connection' => '']);
+        $stringable = new class implements \Stringable
+        {
+            public function __toString(): string
+            {
+                return 'FooBar';
+            }
+        };
+
+        $logger->log(LogLevel::DEBUG, $stringable, ['query' => null]);
+    }
+
+    /**
      * Tests that the connection name is logged with the query.
      */
     public function testLogConnection(): void