LoggedQueryTest.php 5.6 KB

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