LoggingStatementTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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->method('rowCount')->will($this->returnValue(3));
  32. $inner->method('execute')->will($this->returnValue(true));
  33. $logger = $this->getMockBuilder('Cake\Database\Log\QueryLogger')->getMock();
  34. $logger->expects($this->once())
  35. ->method('log')
  36. ->with($this->logicalAnd(
  37. $this->isInstanceOf('Cake\Database\Log\LoggedQuery'),
  38. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  39. $this->attributeEqualTo('took', 5, 200),
  40. $this->attributeEqualTo('numRows', 3),
  41. $this->attributeEqualTo('params', [])
  42. ));
  43. $st = new LoggingStatement($inner);
  44. $st->queryString = 'SELECT bar FROM foo';
  45. $st->setLogger($logger);
  46. $st->execute();
  47. }
  48. /**
  49. * Tests that queries are logged when executed with params
  50. *
  51. * @return void
  52. */
  53. public function testExecuteWithParams()
  54. {
  55. $inner = $this->getMockBuilder('PDOStatement')->getMock();
  56. $inner->method('rowCount')->will($this->returnValue(4));
  57. $inner->method('execute')->will($this->returnValue(true));
  58. $logger = $this->getMockBuilder('Cake\Database\Log\QueryLogger')->getMock();
  59. $logger->expects($this->once())
  60. ->method('log')
  61. ->with($this->logicalAnd(
  62. $this->isInstanceOf('Cake\Database\Log\LoggedQuery'),
  63. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  64. $this->attributeEqualTo('took', 5, 200),
  65. $this->attributeEqualTo('numRows', 4),
  66. $this->attributeEqualTo('params', ['a' => 1, 'b' => 2])
  67. ));
  68. $st = new LoggingStatement($inner);
  69. $st->queryString = 'SELECT bar FROM foo';
  70. $st->setLogger($logger);
  71. $st->execute(['a' => 1, 'b' => 2]);
  72. }
  73. /**
  74. * Tests that queries are logged when executed with bound params
  75. *
  76. * @return void
  77. */
  78. public function testExecuteWithBinding()
  79. {
  80. $inner = $this->getMockBuilder('PDOStatement')->getMock();
  81. $inner->method('rowCount')->will($this->returnValue(4));
  82. $inner->method('execute')->will($this->returnValue(true));
  83. $logger = $this->getMockBuilder('Cake\Database\Log\QueryLogger')->getMock();
  84. $logger->expects($this->at(0))
  85. ->method('log')
  86. ->with($this->logicalAnd(
  87. $this->isInstanceOf('Cake\Database\Log\LoggedQuery'),
  88. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  89. $this->attributeEqualTo('took', 5, 200),
  90. $this->attributeEqualTo('numRows', 4),
  91. $this->attributeEqualTo('params', ['a' => 1, 'b' => '2013-01-01'])
  92. ));
  93. $logger->expects($this->at(1))
  94. ->method('log')
  95. ->with($this->logicalAnd(
  96. $this->isInstanceOf('Cake\Database\Log\LoggedQuery'),
  97. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  98. $this->attributeEqualTo('took', 5, 200),
  99. $this->attributeEqualTo('numRows', 4),
  100. $this->attributeEqualTo('params', ['a' => 1, 'b' => '2014-01-01'])
  101. ));
  102. $date = new \DateTime('2013-01-01');
  103. $inner->expects($this->at(0))->method('bindValue')->with('a', 1);
  104. $inner->expects($this->at(1))->method('bindValue')->with('b', $date);
  105. $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  106. $st = new LoggingStatement($inner, $driver);
  107. $st->queryString = 'SELECT bar FROM foo';
  108. $st->setLogger($logger);
  109. $st->bindValue('a', 1);
  110. $st->bindValue('b', $date, 'date');
  111. $st->execute();
  112. $st->bindValue('b', new \DateTime('2014-01-01'), 'date');
  113. $st->execute();
  114. }
  115. /**
  116. * Tests that queries are logged despite database errors
  117. *
  118. * @return void
  119. */
  120. public function testExecuteWithError()
  121. {
  122. $this->expectException(\LogicException::class);
  123. $this->expectExceptionMessage('This is bad');
  124. $exception = new \LogicException('This is bad');
  125. $inner = $this->getMockBuilder('PDOStatement')->getMock();
  126. $inner->expects($this->once())
  127. ->method('execute')
  128. ->will($this->throwException($exception));
  129. $logger = $this->getMockBuilder('Cake\Database\Log\QueryLogger')->getMock();
  130. $logger->expects($this->once())
  131. ->method('log')
  132. ->with($this->logicalAnd(
  133. $this->isInstanceOf('Cake\Database\Log\LoggedQuery'),
  134. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  135. $this->attributeEqualTo('took', 5, 200),
  136. $this->attributeEqualTo('params', []),
  137. $this->attributeEqualTo('error', $exception)
  138. ));
  139. $st = new LoggingStatement($inner);
  140. $st->queryString = 'SELECT bar FROM foo';
  141. $st->setLogger($logger);
  142. $st->execute();
  143. }
  144. /**
  145. * Tests setting and getting the logger
  146. *
  147. * @return void
  148. */
  149. public function testSetAndGetLogger()
  150. {
  151. $logger = $this->getMockBuilder('Cake\Database\Log\QueryLogger')->getMock();
  152. $st = new LoggingStatement();
  153. $this->assertNull($st->getLogger());
  154. $st->setLogger($logger);
  155. $this->assertSame($logger, $st->getLogger());
  156. }
  157. }