QueryLoggerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /**
  22. * Tests QueryLogger class
  23. */
  24. class QueryLoggerTest extends TestCase
  25. {
  26. /**
  27. * Set up
  28. *
  29. * @return void
  30. */
  31. public function setUp(): void
  32. {
  33. parent::setUp();
  34. Log::reset();
  35. }
  36. /**
  37. * Tear down
  38. *
  39. * @return void
  40. */
  41. public function tearDown(): void
  42. {
  43. parent::tearDown();
  44. Log::reset();
  45. }
  46. /**
  47. * Tests that query placeholders are replaced when logged
  48. *
  49. * @return void
  50. */
  51. public function testStringInterpolation()
  52. {
  53. $logger = $this->getMockBuilder('Cake\Database\Log\QueryLogger')
  54. ->setMethods(['_log'])
  55. ->getMock();
  56. $query = new LoggedQuery();
  57. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3 AND d = :p4 AND e = :p5 AND f = :p6';
  58. $query->params = ['p1' => 'string', 'p3' => null, 'p2' => 3, 'p4' => true, 'p5' => false, 'p6' => 0];
  59. $logger->expects($this->once())->method('_log')->with($query);
  60. $logger->log($query);
  61. $expected = "duration=0 rows=0 SELECT a FROM b where a = 'string' AND b = 3 AND c = NULL AND d = 1 AND e = 0 AND f = 0";
  62. $this->assertEquals($expected, (string)$query);
  63. }
  64. /**
  65. * Tests that positional placeholders are replaced when logging a query
  66. *
  67. * @return void
  68. */
  69. public function testStringInterpolationNotNamed()
  70. {
  71. $logger = $this->getMockBuilder('Cake\Database\Log\QueryLogger')
  72. ->setMethods(['_log'])
  73. ->getMock();
  74. $query = new LoggedQuery();
  75. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ? AND d = ? AND e = ? AND f = ?';
  76. $query->params = ['string', '3', null, true, false, 0];
  77. $logger->expects($this->once())->method('_log')->with($query);
  78. $logger->log($query);
  79. $expected = "duration=0 rows=0 SELECT a FROM b where a = 'string' AND b = '3' AND c = NULL AND d = 1 AND e = 0 AND f = 0";
  80. $this->assertEquals($expected, (string)$query);
  81. }
  82. /**
  83. * Tests that repeated placeholders are correctly replaced
  84. *
  85. * @return void
  86. */
  87. public function testStringInterpolationDuplicate()
  88. {
  89. $logger = $this->getMockBuilder('Cake\Database\Log\QueryLogger')
  90. ->setMethods(['_log'])
  91. ->getMock();
  92. $query = new LoggedQuery();
  93. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p1 AND c = :p2 AND d = :p2';
  94. $query->params = ['p1' => 'string', 'p2' => 3];
  95. $logger->expects($this->once())->method('_log')->with($query);
  96. $logger->log($query);
  97. $expected = "duration=0 rows=0 SELECT a FROM b where a = 'string' AND b = 'string' AND c = 3 AND d = 3";
  98. $this->assertEquals($expected, (string)$query);
  99. }
  100. /**
  101. * Tests that named placeholders
  102. *
  103. * @return void
  104. */
  105. public function testStringInterpolationNamed()
  106. {
  107. $logger = $this->getMockBuilder('Cake\Database\Log\QueryLogger')
  108. ->setMethods(['_log'])
  109. ->getMock();
  110. $query = new LoggedQuery();
  111. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p11 AND c = :p20 AND d = :p2';
  112. $query->params = ['p11' => 'test', 'p1' => 'string', 'p2' => 3, 'p20' => 5];
  113. $logger->expects($this->once())->method('_log')->with($query);
  114. $logger->log($query);
  115. $expected = "duration=0 rows=0 SELECT a FROM b where a = 'string' AND b = 'test' AND c = 5 AND d = 3";
  116. $this->assertEquals($expected, (string)$query);
  117. }
  118. /**
  119. * Tests that placeholders are replaced with correctly escaped strings
  120. *
  121. * @return void
  122. */
  123. public function testStringInterpolationSpecialChars()
  124. {
  125. $logger = $this->getMockBuilder('Cake\Database\Log\QueryLogger')
  126. ->setMethods(['_log'])
  127. ->getMock();
  128. $query = new LoggedQuery();
  129. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3 AND d = :p4';
  130. $query->params = ['p1' => '$2y$10$dUAIj', 'p2' => '$0.23', 'p3' => 'a\\0b\\1c\\d', 'p4' => "a'b"];
  131. $logger->expects($this->once())->method('_log')->with($query);
  132. $logger->log($query);
  133. $expected = "duration=0 rows=0 SELECT a FROM b where a = '\$2y\$10\$dUAIj' AND b = '\$0.23' AND c = 'a\\\\0b\\\\1c\\\\d' AND d = 'a''b'";
  134. $this->assertEquals($expected, (string)$query);
  135. }
  136. /**
  137. * Tests that the logged query object is passed to the built-in logger using
  138. * the correct scope
  139. *
  140. * @return void
  141. */
  142. public function testLogFunction()
  143. {
  144. $logger = new QueryLogger();
  145. $query = new LoggedQuery();
  146. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?';
  147. $query->params = ['string', '3', null];
  148. $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  149. ->setMethods(['log'])
  150. ->setConstructorArgs(['scopes' => ['queriesLog']])
  151. ->getMock();
  152. Log::engine('queryLoggerTest');
  153. $engine2 = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  154. ->setMethods(['log'])
  155. ->setConstructorArgs(['scopes' => ['foo']])
  156. ->getMock();
  157. Log::engine('queryLoggerTest2');
  158. $engine2->expects($this->never())->method('log');
  159. $logger->log($query);
  160. }
  161. }