QueryLoggerTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. $logger = new QueryLogger(['connection' => '']);
  65. $stringable = new class implements \Stringable
  66. {
  67. public function __toString(): string
  68. {
  69. return 'FooBar';
  70. }
  71. };
  72. $logger->log(LogLevel::DEBUG, $stringable, ['query' => null]);
  73. }
  74. /**
  75. * Tests that the connection name is logged with the query.
  76. */
  77. public function testLogConnection(): void
  78. {
  79. $logger = new QueryLogger(['connection' => 'test']);
  80. $query = new LoggedQuery();
  81. $query->query = 'SELECT a';
  82. Log::setConfig('queryLoggerTest', [
  83. 'className' => 'Array',
  84. 'scopes' => ['queriesLog'],
  85. ]);
  86. $logger->log(LogLevel::DEBUG, '', compact('query'));
  87. $this->assertStringContainsString('connection=test duration=', current(Log::engine('queryLoggerTest')->read()));
  88. }
  89. }