QueryLoggerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database\Log;
  17. use Cake\Database\Log\LoggedQuery;
  18. use Cake\Database\Log\QueryLogger;
  19. use Cake\Log\Log;
  20. use Cake\TestSuite\TestCase;
  21. use Psr\Log\LogLevel;
  22. /**
  23. * Tests QueryLogger class
  24. */
  25. class QueryLoggerTest extends TestCase
  26. {
  27. /**
  28. * Tear down
  29. */
  30. public function tearDown(): void
  31. {
  32. parent::tearDown();
  33. Log::drop('queryLoggerTest');
  34. Log::drop('queryLoggerTest2');
  35. }
  36. /**
  37. * Tests that the logged query object is passed to the built-in logger using
  38. * the correct scope
  39. */
  40. public function testLogFunction(): void
  41. {
  42. $logger = new QueryLogger(['connection' => '']);
  43. $query = new LoggedQuery();
  44. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?';
  45. $query->params = ['string', '3', null];
  46. Log::setConfig('queryLoggerTest', [
  47. 'className' => 'Array',
  48. 'scopes' => ['queriesLog'],
  49. ]);
  50. Log::setConfig('queryLoggerTest2', [
  51. 'className' => 'Array',
  52. 'scopes' => ['foo'],
  53. ]);
  54. $logger->log(LogLevel::DEBUG, $query, compact('query'));
  55. $this->assertCount(1, Log::engine('queryLoggerTest')->read());
  56. $this->assertCount(0, Log::engine('queryLoggerTest2')->read());
  57. }
  58. /**
  59. * Tests that passed Stringable also work.
  60. */
  61. public function testLogFunctionStringable(): void
  62. {
  63. $this->skipIf(version_compare(PHP_VERSION, '8.0', '<'), 'Stringable exists since 8.0');
  64. Log::setConfig('queryLoggerTest', [
  65. 'className' => 'Array',
  66. 'scopes' => ['queriesLog'],
  67. ]);
  68. $logger = new QueryLogger(['connection' => '']);
  69. $stringable = new class implements \Stringable
  70. {
  71. public function __toString(): string
  72. {
  73. return 'FooBar';
  74. }
  75. };
  76. $logger->log(LogLevel::DEBUG, $stringable, ['query' => null]);
  77. $logs = Log::engine('queryLoggerTest')->read();
  78. $this->assertCount(1, $logs);
  79. $this->assertStringContainsString('FooBar', $logs[0]);
  80. }
  81. /**
  82. * Tests that the connection name is logged with the query.
  83. */
  84. public function testLogConnection(): void
  85. {
  86. $logger = new QueryLogger(['connection' => 'test']);
  87. $query = new LoggedQuery();
  88. $query->query = 'SELECT a';
  89. Log::setConfig('queryLoggerTest', [
  90. 'className' => 'Array',
  91. 'scopes' => ['queriesLog'],
  92. ]);
  93. $logger->log(LogLevel::DEBUG, '', compact('query'));
  94. $this->assertStringContainsString('connection=test role= duration=', current(Log::engine('queryLoggerTest')->read()));
  95. }
  96. }