LoggedQueryTest.php 4.6 KB

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