QueryLoggerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. */
  24. class QueryLoggerTest extends TestCase
  25. {
  26. /**
  27. * Set up
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. Log::reset();
  35. }
  36. /**
  37. * Tear down
  38. *
  39. * @return void
  40. */
  41. public function tearDown()
  42. {
  43. parent::tearDown();
  44. Log::reset();
  45. }
  46. /**
  47. * Tests that query placeholders are replaced when logged
  48. *
  49. * @return void
  50. */
  51. public function testStringInterpolation()
  52. {
  53. $logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
  54. $query = new LoggedQuery;
  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. $logger->expects($this->once())->method('_log')->with($query);
  58. $logger->log($query);
  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 positional placeholders are replaced when logging a query
  64. *
  65. * @return void
  66. */
  67. public function testStringInterpolation2()
  68. {
  69. $logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
  70. $query = new LoggedQuery;
  71. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ? AND d = ? AND e = ? AND f = ?';
  72. $query->params = ['string', '3', null, true, false, 0];
  73. $logger->expects($this->once())->method('_log')->with($query);
  74. $logger->log($query);
  75. $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";
  76. $this->assertEquals($expected, (string)$query);
  77. }
  78. /**
  79. * Tests that repeated placeholders are correctly replaced
  80. *
  81. * @return void
  82. */
  83. public function testStringInterpolation3()
  84. {
  85. $logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
  86. $query = new LoggedQuery;
  87. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p1 AND c = :p2 AND d = :p2';
  88. $query->params = ['p1' => 'string', 'p2' => 3];
  89. $logger->expects($this->once())->method('_log')->with($query);
  90. $logger->log($query);
  91. $expected = "duration=0 rows=0 SELECT a FROM b where a = 'string' AND b = 'string' AND c = 3 AND d = 3";
  92. $this->assertEquals($expected, (string)$query);
  93. }
  94. /**
  95. * Tests that named placeholders
  96. *
  97. * @return void
  98. */
  99. public function testStringInterpolationNamed()
  100. {
  101. $logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
  102. $query = new LoggedQuery;
  103. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p11 AND c = :p20 AND d = :p2';
  104. $query->params = ['p11' => 'test', 'p1' => 'string', 'p2' => 3, 'p20' => 5];
  105. $logger->expects($this->once())->method('_log')->with($query);
  106. $logger->log($query);
  107. $expected = "duration=0 rows=0 SELECT a FROM b where a = 'string' AND b = 'test' AND c = 5 AND d = 3";
  108. $this->assertEquals($expected, (string)$query);
  109. }
  110. /**
  111. * Tests that the logged query object is passed to the built-in logger using
  112. * the correct scope
  113. *
  114. * @return void
  115. */
  116. public function testLogFunction()
  117. {
  118. $logger = new QueryLogger;
  119. $query = new LoggedQuery;
  120. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?';
  121. $query->params = ['string', '3', null];
  122. $engine = $this->getMock('\Cake\Log\Engine\BaseLog', ['log'], ['scopes' => ['queriesLog']]);
  123. Log::engine('queryLoggerTest');
  124. $engine2 = $this->getMock('\Cake\Log\Engine\BaseLog', ['log'], ['scopes' => ['foo']]);
  125. Log::engine('queryLoggerTest2');
  126. $engine2->expects($this->never())->method('log');
  127. $logger->log($query);
  128. }
  129. }