LoggedQueryTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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->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->driver = $this->driver;
  55. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3 AND d = :p4 AND e = :p5 AND f = :p6';
  56. $query->params = ['p1' => 'string', 'p3' => null, 'p2' => 3, 'p4' => true, 'p5' => false, 'p6' => 0];
  57. $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";
  58. $this->assertSame($expected, (string)$query);
  59. }
  60. /**
  61. * Tests that positional placeholders are replaced when logging a query
  62. */
  63. public function testStringInterpolationNotNamed(): void
  64. {
  65. $query = new LoggedQuery();
  66. $query->driver = $this->driver;
  67. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ? AND d = ? AND e = ? AND f = ?';
  68. $query->params = ['string', '3', null, true, false, 0];
  69. $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";
  70. $this->assertSame($expected, (string)$query);
  71. }
  72. /**
  73. * Tests that repeated placeholders are correctly replaced
  74. */
  75. public function testStringInterpolationDuplicate(): void
  76. {
  77. $query = new LoggedQuery();
  78. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p1 AND c = :p2 AND d = :p2';
  79. $query->params = ['p1' => 'string', 'p2' => 3];
  80. $expected = "SELECT a FROM b where a = 'string' AND b = 'string' AND c = 3 AND d = 3";
  81. $this->assertSame($expected, (string)$query);
  82. }
  83. /**
  84. * Tests that named placeholders
  85. */
  86. public function testStringInterpolationNamed(): void
  87. {
  88. $query = new LoggedQuery();
  89. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p11 AND c = :p20 AND d = :p2';
  90. $query->params = ['p11' => 'test', 'p1' => 'string', 'p2' => 3, 'p20' => 5];
  91. $expected = "SELECT a FROM b where a = 'string' AND b = 'test' AND c = 5 AND d = 3";
  92. $this->assertSame($expected, (string)$query);
  93. }
  94. /**
  95. * Tests that placeholders are replaced with correctly escaped strings
  96. */
  97. public function testStringInterpolationSpecialChars(): void
  98. {
  99. $query = new LoggedQuery();
  100. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3 AND d = :p4';
  101. $query->params = ['p1' => '$2y$10$dUAIj', 'p2' => '$0.23', 'p3' => 'a\\0b\\1c\\d', 'p4' => "a'b"];
  102. $expected = "SELECT a FROM b where a = '\$2y\$10\$dUAIj' AND b = '\$0.23' AND c = 'a\\\\0b\\\\1c\\\\d' AND d = 'a''b'";
  103. $this->assertSame($expected, (string)$query);
  104. }
  105. /**
  106. * Tests that query placeholders are replaced when logged
  107. */
  108. public function testBinaryInterpolation(): void
  109. {
  110. $query = new LoggedQuery();
  111. $query->query = 'SELECT a FROM b where a = :p1';
  112. $uuid = str_replace('-', '', Text::uuid());
  113. $query->params = ['p1' => hex2bin($uuid)];
  114. $expected = "SELECT a FROM b where a = '{$uuid}'";
  115. $this->assertSame($expected, (string)$query);
  116. }
  117. /**
  118. * Tests that unknown possible binary data is not replaced to hex.
  119. */
  120. public function testBinaryInterpolationIgnored(): void
  121. {
  122. $query = new LoggedQuery();
  123. $query->query = 'SELECT a FROM b where a = :p1';
  124. $query->params = ['p1' => "a\tz"];
  125. $expected = "SELECT a FROM b where a = 'a\tz'";
  126. $this->assertSame($expected, (string)$query);
  127. }
  128. public function testGetContext(): void
  129. {
  130. $query = new LoggedQuery();
  131. $query->query = 'SELECT a FROM b where a = :p1';
  132. $query->numRows = 10;
  133. $query->took = 15;
  134. $expected = [
  135. 'numRows' => 10,
  136. 'took' => 15,
  137. ];
  138. $this->assertSame($expected, $query->getContext());
  139. }
  140. public function testJsonSerialize(): void
  141. {
  142. $query = new LoggedQuery();
  143. $query->query = 'SELECT a FROM b where a = :p1';
  144. $query->params = ['p1' => '$2y$10$dUAIj'];
  145. $query->numRows = 4;
  146. $query->error = new Exception('You fail!');
  147. $expected = json_encode([
  148. 'query' => $query->query,
  149. 'numRows' => 4,
  150. 'params' => $query->params,
  151. 'took' => 0,
  152. 'error' => [
  153. 'class' => get_class($query->error),
  154. 'message' => $query->error->getMessage(),
  155. 'code' => $query->error->getCode(),
  156. ],
  157. ]);
  158. $this->assertEquals($expected, json_encode($query));
  159. }
  160. }