LoggingStatementTest.php 4.8 KB

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