QueryLoggerTest.php 6.0 KB

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