QueryLoggerTest.php 5.1 KB

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