LoggingStatementTest.php 5.6 KB

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