LoggingStatementTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\LoggingStatement;
  17. use PDOStatement;
  18. /**
  19. * Tests LoggingStatement class
  20. *
  21. **/
  22. class LoggingStatementTest extends \Cake\TestSuite\TestCase {
  23. /**
  24. * Tests that queries are logged when executed without params
  25. *
  26. * @return void
  27. */
  28. public function testExecuteNoParams() {
  29. $inner = $this->getMock('PDOStatement');
  30. $inner->expects($this->once())->method('rowCount')->will($this->returnValue(3));
  31. $logger = $this->getMock('\Cake\Database\Log\QueryLogger');
  32. $logger->expects($this->once())
  33. ->method('log')
  34. ->with($this->logicalAnd(
  35. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  36. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  37. $this->attributeEqualTo('took', 5, 200),
  38. $this->attributeEqualTo('numRows', 3),
  39. $this->attributeEqualTo('params', [])
  40. ));
  41. $st = new LoggingStatement($inner);
  42. $st->queryString = 'SELECT bar FROM foo';
  43. $st->logger($logger);
  44. $st->execute();
  45. }
  46. /**
  47. * Tests that queries are logged when executed with params
  48. *
  49. * @return void
  50. */
  51. public function testExecuteWithParams() {
  52. $inner = $this->getMock('PDOStatement');
  53. $inner->expects($this->once())->method('rowCount')->will($this->returnValue(4));
  54. $logger = $this->getMock('\Cake\Database\Log\QueryLogger');
  55. $logger->expects($this->once())
  56. ->method('log')
  57. ->with($this->logicalAnd(
  58. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  59. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  60. $this->attributeEqualTo('took', 5, 200),
  61. $this->attributeEqualTo('numRows', 4),
  62. $this->attributeEqualTo('params', ['a' => 1, 'b' => 2])
  63. ));
  64. $st = new LoggingStatement($inner);
  65. $st->queryString = 'SELECT bar FROM foo';
  66. $st->logger($logger);
  67. $st->execute(['a' => 1, 'b' => 2]);
  68. }
  69. /**
  70. * Tests that queries are logged when executed with bound params
  71. *
  72. * @return void
  73. */
  74. public function testExecuteWithBinding() {
  75. $inner = $this->getMock('PDOStatement');
  76. $inner->expects($this->any())->method('rowCount')->will($this->returnValue(4));
  77. $logger = $this->getMock('\Cake\Database\Log\QueryLogger');
  78. $logger->expects($this->at(0))
  79. ->method('log')
  80. ->with($this->logicalAnd(
  81. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  82. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  83. $this->attributeEqualTo('took', 5, 200),
  84. $this->attributeEqualTo('numRows', 4),
  85. $this->attributeEqualTo('params', ['a' => 1, 'b' => '2013-01-01'])
  86. ));
  87. $logger->expects($this->at(1))
  88. ->method('log')
  89. ->with($this->logicalAnd(
  90. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  91. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  92. $this->attributeEqualTo('took', 5, 200),
  93. $this->attributeEqualTo('numRows', 4),
  94. $this->attributeEqualTo('params', ['a' => 1, 'b' => '2014-01-01'])
  95. ));
  96. $date = new \DateTime('2013-01-01');
  97. $inner->expects($this->at(0))->method('bindValue')->with('a', 1);
  98. $inner->expects($this->at(1))->method('bindValue')->with('b', $date);
  99. $driver = $this->getMock('\Cake\Database\Driver');
  100. $st = new LoggingStatement($inner, $driver);
  101. $st->queryString = 'SELECT bar FROM foo';
  102. $st->logger($logger);
  103. $st->bindValue('a', 1);
  104. $st->bindValue('b', $date, 'date');
  105. $st->execute();
  106. $st->bindValue('b', new \DateTime('2014-01-01'), 'date');
  107. $st->execute();
  108. }
  109. /**
  110. * Tests that queries are logged despite database errors
  111. *
  112. * @expectedException \LogicException
  113. * @expectedExceptionMessage This is bad
  114. * @return void
  115. */
  116. public function testExecuteWithError() {
  117. $exception = new \LogicException('This is bad');
  118. $inner = $this->getMock('PDOStatement');
  119. $inner->expects($this->once())->method('execute')
  120. ->will($this->throwException($exception));
  121. $logger = $this->getMock('\Cake\Database\Log\QueryLogger');
  122. $logger->expects($this->once())
  123. ->method('log')
  124. ->with($this->logicalAnd(
  125. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  126. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  127. $this->attributeEqualTo('took', 5, 200),
  128. $this->attributeEqualTo('params', []),
  129. $this->attributeEqualTo('error', $exception)
  130. ));
  131. $st = new LoggingStatement($inner);
  132. $st->queryString = 'SELECT bar FROM foo';
  133. $st->logger($logger);
  134. $st->execute();
  135. }
  136. }