LoggingStatementTest.php 5.6 KB

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