LoggedQueryTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\Driver\Sqlserver;
  18. use Cake\Database\Log\LoggedQuery;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\Text;
  22. use Exception;
  23. /**
  24. * Tests LoggedQuery class
  25. */
  26. class LoggedQueryTest extends TestCase
  27. {
  28. protected $driver;
  29. protected $true = 'TRUE';
  30. protected $false = 'FALSE';
  31. public function setUp(): void
  32. {
  33. $this->driver = ConnectionManager::get('test')->getDriver();
  34. if ($this->driver instanceof Sqlserver) {
  35. $this->true = '1';
  36. $this->false = '0';
  37. }
  38. }
  39. /**
  40. * Tests that LoggedQuery can be converted to string
  41. */
  42. public function testStringConversion(): void
  43. {
  44. $logged = new LoggedQuery();
  45. $logged->setContext(['query' => 'SELECT foo FROM bar']);
  46. $this->assertSame('SELECT foo FROM bar', (string)$logged);
  47. }
  48. /**
  49. * Tests that query placeholders are replaced when logged
  50. */
  51. public function testStringInterpolation(): void
  52. {
  53. $query = new LoggedQuery();
  54. $query->setContext([
  55. 'driver' => $this->driver,
  56. 'query' => 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3 AND d = :p4 AND e = :p5 AND f = :p6',
  57. 'params' => ['p1' => 'string', 'p3' => null, 'p2' => 3, 'p4' => true, 'p5' => false, 'p6' => 0],
  58. ]);
  59. $expected = "SELECT a FROM b where a = 'string' AND b = 3 AND c = NULL AND d = $this->true AND e = $this->false AND f = 0";
  60. $this->assertSame($expected, (string)$query);
  61. }
  62. /**
  63. * Tests that positional placeholders are replaced when logging a query
  64. */
  65. public function testStringInterpolationNotNamed(): void
  66. {
  67. $query = new LoggedQuery();
  68. $query->setContext([
  69. 'driver' => $this->driver,
  70. 'query' => 'SELECT a FROM b where a = ? AND b = ? AND c = ? AND d = ? AND e = ? AND f = ?',
  71. 'params' => ['string', '3', null, true, false, 0],
  72. ]);
  73. $expected = "SELECT a FROM b where a = 'string' AND b = '3' AND c = NULL AND d = $this->true AND e = $this->false AND f = 0";
  74. $this->assertSame($expected, (string)$query);
  75. }
  76. /**
  77. * Tests that repeated placeholders are correctly replaced
  78. */
  79. public function testStringInterpolationDuplicate(): void
  80. {
  81. $query = new LoggedQuery();
  82. $query->setContext([
  83. 'query' => 'SELECT a FROM b where a = :p1 AND b = :p1 AND c = :p2 AND d = :p2',
  84. 'params' => ['p1' => 'string', 'p2' => 3],
  85. ]);
  86. $expected = "SELECT a FROM b where a = 'string' AND b = 'string' AND c = 3 AND d = 3";
  87. $this->assertSame($expected, (string)$query);
  88. }
  89. /**
  90. * Tests that named placeholders
  91. */
  92. public function testStringInterpolationNamed(): void
  93. {
  94. $query = new LoggedQuery();
  95. $query->setContext([
  96. 'query' => 'SELECT a FROM b where a = :p1 AND b = :p11 AND c = :p20 AND d = :p2',
  97. 'params' => ['p11' => 'test', 'p1' => 'string', 'p2' => 3, 'p20' => 5],
  98. ]);
  99. $expected = "SELECT a FROM b where a = 'string' AND b = 'test' AND c = 5 AND d = 3";
  100. $this->assertSame($expected, (string)$query);
  101. }
  102. /**
  103. * Tests that placeholders are replaced with correctly escaped strings
  104. */
  105. public function testStringInterpolationSpecialChars(): void
  106. {
  107. $query = new LoggedQuery();
  108. $query->setContext([
  109. 'query' => 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3 AND d = :p4',
  110. 'params' => ['p1' => '$2y$10$dUAIj', 'p2' => '$0.23', 'p3' => 'a\\0b\\1c\\d', 'p4' => "a'b"],
  111. ]);
  112. $expected = "SELECT a FROM b where a = '\$2y\$10\$dUAIj' AND b = '\$0.23' AND c = 'a\\\\0b\\\\1c\\\\d' AND d = 'a''b'";
  113. $this->assertSame($expected, (string)$query);
  114. }
  115. /**
  116. * Tests that query placeholders are replaced when logged
  117. */
  118. public function testBinaryInterpolation(): void
  119. {
  120. $query = new LoggedQuery();
  121. $uuid = str_replace('-', '', Text::uuid());
  122. $query->setContext([
  123. 'query' => 'SELECT a FROM b where a = :p1',
  124. 'params' => ['p1' => hex2bin($uuid)],
  125. ]);
  126. $expected = "SELECT a FROM b where a = '{$uuid}'";
  127. $this->assertSame($expected, (string)$query);
  128. }
  129. /**
  130. * Tests that unknown possible binary data is not replaced to hex.
  131. */
  132. public function testBinaryInterpolationIgnored(): void
  133. {
  134. $query = new LoggedQuery();
  135. $query->setContext([
  136. 'query' => 'SELECT a FROM b where a = :p1',
  137. 'params' => ['p1' => "a\tz"],
  138. ]);
  139. $expected = "SELECT a FROM b where a = 'a\tz'";
  140. $this->assertSame($expected, (string)$query);
  141. }
  142. public function testGetContext(): void
  143. {
  144. $query = new LoggedQuery();
  145. $query->setContext([
  146. 'query' => 'SELECT a FROM b where a = :p1',
  147. 'numRows' => 10,
  148. 'took' => 15,
  149. ]);
  150. $expected = [
  151. 'numRows' => 10,
  152. 'took' => 15.0,
  153. ];
  154. $this->assertSame($expected, $query->getContext());
  155. }
  156. public function testJsonSerialize(): void
  157. {
  158. $error = new Exception('You fail!');
  159. $query = new LoggedQuery();
  160. $query->setContext([
  161. 'query' => 'SELECT a FROM b where a = :p1',
  162. 'params' => ['p1' => '$2y$10$dUAIj'],
  163. 'numRows' => 4,
  164. 'error' => $error,
  165. ]);
  166. $expected = json_encode([
  167. 'query' => 'SELECT a FROM b where a = :p1',
  168. 'numRows' => 4,
  169. 'params' => ['p1' => '$2y$10$dUAIj'],
  170. 'took' => 0,
  171. 'error' => [
  172. 'class' => get_class($error),
  173. 'message' => $error->getMessage(),
  174. 'code' => $error->getCode(),
  175. ],
  176. ]);
  177. $this->assertEquals($expected, json_encode($query));
  178. }
  179. }