QueryLoggerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. class QueryLoggerTest extends TestCase
  24. {
  25. /**
  26. * Set up
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. Log::reset();
  34. }
  35. /**
  36. * Tear down
  37. *
  38. * @return void
  39. */
  40. public function tearDown()
  41. {
  42. parent::tearDown();
  43. Log::reset();
  44. }
  45. /**
  46. * Tests that query placeholders are replaced when logged
  47. *
  48. * @return void
  49. */
  50. public function testStringInterpolation()
  51. {
  52. $logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')
  53. ->setMethods(['_log'])
  54. ->getMock();
  55. $query = new LoggedQuery;
  56. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3 AND d = :p4 AND e = :p5 AND f = :p6';
  57. $query->params = ['p1' => 'string', 'p3' => null, 'p2' => 3, 'p4' => true, 'p5' => false, 'p6' => 0];
  58. $logger->expects($this->once())->method('_log')->with($query);
  59. $logger->log($query);
  60. $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";
  61. $this->assertEquals($expected, (string)$query);
  62. }
  63. /**
  64. * Tests that positional placeholders are replaced when logging a query
  65. *
  66. * @return void
  67. */
  68. public function testStringInterpolation2()
  69. {
  70. $logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')
  71. ->setMethods(['_log'])
  72. ->getMock();
  73. $query = new LoggedQuery;
  74. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ? AND d = ? AND e = ? AND f = ?';
  75. $query->params = ['string', '3', null, true, false, 0];
  76. $logger->expects($this->once())->method('_log')->with($query);
  77. $logger->log($query);
  78. $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";
  79. $this->assertEquals($expected, (string)$query);
  80. }
  81. /**
  82. * Tests that repeated placeholders are correctly replaced
  83. *
  84. * @return void
  85. */
  86. public function testStringInterpolation3()
  87. {
  88. $logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')
  89. ->setMethods(['_log'])
  90. ->getMock();
  91. $query = new LoggedQuery;
  92. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p1 AND c = :p2 AND d = :p2';
  93. $query->params = ['p1' => 'string', 'p2' => 3];
  94. $logger->expects($this->once())->method('_log')->with($query);
  95. $logger->log($query);
  96. $expected = "duration=0 rows=0 SELECT a FROM b where a = 'string' AND b = 'string' AND c = 3 AND d = 3";
  97. $this->assertEquals($expected, (string)$query);
  98. }
  99. /**
  100. * Tests that named placeholders
  101. *
  102. * @return void
  103. */
  104. public function testStringInterpolationNamed()
  105. {
  106. $logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')
  107. ->setMethods(['_log'])
  108. ->getMock();
  109. $query = new LoggedQuery;
  110. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p11 AND c = :p20 AND d = :p2';
  111. $query->params = ['p11' => 'test', 'p1' => 'string', 'p2' => 3, 'p20' => 5];
  112. $logger->expects($this->once())->method('_log')->with($query);
  113. $logger->log($query);
  114. $expected = "duration=0 rows=0 SELECT a FROM b where a = 'string' AND b = 'test' AND c = 5 AND d = 3";
  115. $this->assertEquals($expected, (string)$query);
  116. }
  117. /**
  118. * Tests that the logged query object is passed to the built-in logger using
  119. * the correct scope
  120. *
  121. * @return void
  122. */
  123. public function testLogFunction()
  124. {
  125. $logger = new QueryLogger;
  126. $query = new LoggedQuery;
  127. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?';
  128. $query->params = ['string', '3', null];
  129. $engine = $this->getMockBuilder('\Cake\Log\Engine\BaseLog')
  130. ->setMethods(['log'])
  131. ->setConstructorArgs(['scopes' => ['queriesLog']])
  132. ->getMock();
  133. Log::engine('queryLoggerTest');
  134. $engine2 = $this->getMockBuilder('\Cake\Log\Engine\BaseLog')
  135. ->setMethods(['log'])
  136. ->setConstructorArgs(['scopes' => ['foo']])
  137. ->getMock();
  138. Log::engine('queryLoggerTest2');
  139. $engine2->expects($this->never())->method('log');
  140. $logger->log($query);
  141. }
  142. }