LoggingStatementTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Log;
  16. use Cake\Database\Log\LoggingStatement;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Tests LoggingStatement class
  20. */
  21. class LoggingStatementTest extends TestCase
  22. {
  23. /**
  24. * Tests that queries are logged when executed without params
  25. *
  26. * @return void
  27. */
  28. public function testExecuteNoParams()
  29. {
  30. $inner = $this->getMockBuilder('PDOStatement')->getMock();
  31. $inner->expects($this->once())->method('rowCount')->will($this->returnValue(3));
  32. $logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock();
  33. $logger->expects($this->once())
  34. ->method('log')
  35. ->with($this->logicalAnd(
  36. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  37. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  38. $this->attributeEqualTo('took', 5, 200),
  39. $this->attributeEqualTo('numRows', 3),
  40. $this->attributeEqualTo('params', [])
  41. ));
  42. $st = new LoggingStatement($inner);
  43. $st->queryString = 'SELECT bar FROM foo';
  44. $st->setLogger($logger);
  45. $st->execute();
  46. }
  47. /**
  48. * Tests that queries are logged when executed with params
  49. *
  50. * @return void
  51. */
  52. public function testExecuteWithParams()
  53. {
  54. $inner = $this->getMockBuilder('PDOStatement')->getMock();
  55. $inner->expects($this->once())->method('rowCount')->will($this->returnValue(4));
  56. $logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock();
  57. $logger->expects($this->once())
  58. ->method('log')
  59. ->with($this->logicalAnd(
  60. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  61. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  62. $this->attributeEqualTo('took', 5, 200),
  63. $this->attributeEqualTo('numRows', 4),
  64. $this->attributeEqualTo('params', ['a' => 1, 'b' => 2])
  65. ));
  66. $st = new LoggingStatement($inner);
  67. $st->queryString = 'SELECT bar FROM foo';
  68. $st->setLogger($logger);
  69. $st->execute(['a' => 1, 'b' => 2]);
  70. }
  71. /**
  72. * Tests that queries are logged when executed with bound params
  73. *
  74. * @return void
  75. */
  76. public function testExecuteWithBinding()
  77. {
  78. $inner = $this->getMockBuilder('PDOStatement')->getMock();
  79. $inner->expects($this->any())->method('rowCount')->will($this->returnValue(4));
  80. $logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock();
  81. $logger->expects($this->at(0))
  82. ->method('log')
  83. ->with($this->logicalAnd(
  84. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  85. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  86. $this->attributeEqualTo('took', 5, 200),
  87. $this->attributeEqualTo('numRows', 4),
  88. $this->attributeEqualTo('params', ['a' => 1, 'b' => '2013-01-01'])
  89. ));
  90. $logger->expects($this->at(1))
  91. ->method('log')
  92. ->with($this->logicalAnd(
  93. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  94. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  95. $this->attributeEqualTo('took', 5, 200),
  96. $this->attributeEqualTo('numRows', 4),
  97. $this->attributeEqualTo('params', ['a' => 1, 'b' => '2014-01-01'])
  98. ));
  99. $date = new \DateTime('2013-01-01');
  100. $inner->expects($this->at(0))->method('bindValue')->with('a', 1);
  101. $inner->expects($this->at(1))->method('bindValue')->with('b', $date);
  102. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  103. $st = new LoggingStatement($inner, $driver);
  104. $st->queryString = 'SELECT bar FROM foo';
  105. $st->setLogger($logger);
  106. $st->bindValue('a', 1);
  107. $st->bindValue('b', $date, 'date');
  108. $st->execute();
  109. $st->bindValue('b', new \DateTime('2014-01-01'), 'date');
  110. $st->execute();
  111. }
  112. /**
  113. * Tests that queries are logged despite database errors
  114. *
  115. * @return void
  116. */
  117. public function testExecuteWithError()
  118. {
  119. $this->expectException(\LogicException::class);
  120. $this->expectExceptionMessage('This is bad');
  121. $exception = new \LogicException('This is bad');
  122. $inner = $this->getMockBuilder('PDOStatement')->getMock();
  123. $inner->expects($this->once())->method('execute')
  124. ->will($this->throwException($exception));
  125. $logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock();
  126. $logger->expects($this->once())
  127. ->method('log')
  128. ->with($this->logicalAnd(
  129. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  130. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  131. $this->attributeEqualTo('took', 5, 200),
  132. $this->attributeEqualTo('params', []),
  133. $this->attributeEqualTo('error', $exception)
  134. ));
  135. $st = new LoggingStatement($inner);
  136. $st->queryString = 'SELECT bar FROM foo';
  137. $st->setLogger($logger);
  138. $st->execute();
  139. }
  140. /**
  141. * Tests setting and getting the logger
  142. *
  143. * @group deprecated
  144. * @return void
  145. */
  146. public function testLoggerCompat()
  147. {
  148. $this->deprecated(function () {
  149. $logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock();
  150. $st = new LoggingStatement();
  151. $this->assertNull($st->logger());
  152. $st->logger($logger);
  153. $this->assertSame($logger, $st->logger());
  154. });
  155. }
  156. /**
  157. * Tests setting and getting the logger
  158. *
  159. * @return void
  160. */
  161. public function testSetAndGetLogger()
  162. {
  163. $logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock();
  164. $st = new LoggingStatement();
  165. $this->assertNull($st->getLogger());
  166. $st->setLogger($logger);
  167. $this->assertSame($logger, $st->getLogger());
  168. }
  169. }