LoggingStatementTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Test\TestCase\Database\Log;
  18. use Cake\Database\Log\LoggingStatement;
  19. use PDOStatement;
  20. /**
  21. * Tests LoggingStatement class
  22. *
  23. **/
  24. class LoggingStatementTest extends \Cake\TestSuite\TestCase {
  25. /**
  26. * Tests that queries are logged when executed without params
  27. *
  28. * @return void
  29. */
  30. public function testExecuteNoParams() {
  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, 60),
  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. $inner = $this->getMock('PDOStatement');
  55. $inner->expects($this->once())->method('rowCount')->will($this->returnValue(4));
  56. $logger = $this->getMock('\Cake\Database\Log\QueryLogger');
  57. $logger->expects($this->once())
  58. ->method('log')
  59. ->with($this->logicalAnd(
  60. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  61. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  62. $this->attributeEqualTo('took', 5, 5),
  63. $this->attributeEqualTo('numRows', 4),
  64. $this->attributeEqualTo('params', ['a' => 1, 'b' => 2])
  65. ));
  66. $st = new LoggingStatement($inner);
  67. $st->queryString = 'SELECT bar FROM foo';
  68. $st->logger($logger);
  69. $st->execute(['a' => 1, 'b' => 2]);
  70. }
  71. /**
  72. * Tests that queries are logged when executed with bound params
  73. *
  74. * @return void
  75. */
  76. public function testExecuteWithBinding() {
  77. $inner = $this->getMock('PDOStatement');
  78. $inner->expects($this->any())->method('rowCount')->will($this->returnValue(4));
  79. $logger = $this->getMock('\Cake\Database\Log\QueryLogger');
  80. $logger->expects($this->at(0))
  81. ->method('log')
  82. ->with($this->logicalAnd(
  83. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  84. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  85. $this->attributeEqualTo('took', 5, 5),
  86. $this->attributeEqualTo('numRows', 4),
  87. $this->attributeEqualTo('params', ['a' => 1, 'b' => '2013-01-01'])
  88. ));
  89. $logger->expects($this->at(1))
  90. ->method('log')
  91. ->with($this->logicalAnd(
  92. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  93. $this->attributeEqualTo('query', 'SELECT bar FROM foo'),
  94. $this->attributeEqualTo('took', 5, 5),
  95. $this->attributeEqualTo('numRows', 4),
  96. $this->attributeEqualTo('params', ['a' => 1, 'b' => '2014-01-01'])
  97. ));
  98. $date = new \DateTime('2013-01-01');
  99. $inner->expects($this->at(0))->method('bindValue')->with('a', 1);
  100. $inner->expects($this->at(1))->method('bindValue')->with('b', $date);
  101. $driver = $this->getMock('\Cake\Database\Driver');
  102. $st = new LoggingStatement($inner, $driver);
  103. $st->queryString = 'SELECT bar FROM foo';
  104. $st->logger($logger);
  105. $st->bindValue('a', 1);
  106. $st->bindValue('b', $date, 'date');
  107. $st->execute();
  108. $st->bindValue('b', new \DateTime('2014-01-01'), 'date');
  109. $st->execute();
  110. }
  111. }