LoggingStatementTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database\Log;
  17. use Cake\Database\DriverInterface;
  18. use Cake\Database\Log\LoggedQuery;
  19. use Cake\Database\Log\LoggingStatement;
  20. use Cake\Database\Log\QueryLogger;
  21. use Cake\Database\StatementInterface;
  22. use Cake\Log\Log;
  23. use Cake\TestSuite\TestCase;
  24. use LogicException;
  25. /**
  26. * Tests LoggingStatement class
  27. */
  28. class LoggingStatementTest extends TestCase
  29. {
  30. public function setUp(): void
  31. {
  32. parent::setUp();
  33. Log::setConfig('queries', [
  34. 'className' => 'Array',
  35. 'scopes' => ['queriesLog'],
  36. ]);
  37. }
  38. public function tearDown(): void
  39. {
  40. parent::tearDown();
  41. Log::drop('queries');
  42. }
  43. /**
  44. * Tests that queries are logged when executed without params
  45. *
  46. * @return void
  47. */
  48. public function testExecuteNoParams()
  49. {
  50. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  51. $inner->method('rowCount')->will($this->returnValue(3));
  52. $inner->method('execute')->will($this->returnValue(true));
  53. $driver = $this->getMockBuilder(DriverInterface::class)->getMock();
  54. $st = new LoggingStatement($inner, $driver);
  55. $st->queryString = 'SELECT bar FROM foo';
  56. $st->setLogger(new QueryLogger());
  57. $st->execute();
  58. $messages = Log::engine('queries')->read();
  59. $this->assertCount(1, $messages);
  60. $this->assertRegExp('/^debug duration=\d rows=3 SELECT bar FROM foo$/', $messages[0]);
  61. }
  62. /**
  63. * Tests that queries are logged when executed with params
  64. *
  65. * @return void
  66. */
  67. public function testExecuteWithParams()
  68. {
  69. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  70. $inner->method('rowCount')->will($this->returnValue(4));
  71. $inner->method('execute')->will($this->returnValue(true));
  72. $driver = $this->getMockBuilder(DriverInterface::class)->getMock();
  73. $st = new LoggingStatement($inner, $driver);
  74. $st->queryString = 'SELECT bar FROM foo WHERE x=:a AND y=:b';
  75. $st->setLogger(new QueryLogger());
  76. $st->execute(['a' => 1, 'b' => 2]);
  77. $messages = Log::engine('queries')->read();
  78. $this->assertCount(1, $messages);
  79. $this->assertRegExp('/^debug duration=\d rows=4 SELECT bar FROM foo WHERE x=1 AND y=2$/', $messages[0]);
  80. }
  81. /**
  82. * Tests that queries are logged when executed with bound params
  83. *
  84. * @return void
  85. */
  86. public function testExecuteWithBinding()
  87. {
  88. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  89. $inner->method('rowCount')->will($this->returnValue(4));
  90. $inner->method('execute')->will($this->returnValue(true));
  91. $date = new \DateTime('2013-01-01');
  92. $inner->expects($this->at(0))->method('bindValue')->with('a', 1);
  93. $inner->expects($this->at(1))->method('bindValue')->with('b', $date);
  94. $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  95. $st = new LoggingStatement($inner, $driver);
  96. $st->queryString = 'SELECT bar FROM foo WHERE a=:a AND b=:b';
  97. $st->setLogger(new QueryLogger());
  98. $st->bindValue('a', 1);
  99. $st->bindValue('b', $date, 'date');
  100. $st->execute();
  101. $st->bindValue('b', new \DateTime('2014-01-01'), 'date');
  102. $st->execute();
  103. $messages = Log::engine('queries')->read();
  104. $this->assertCount(2, $messages);
  105. $this->assertRegExp("/^debug duration=\d rows=4 SELECT bar FROM foo WHERE a='1' AND b='2013-01-01'$/", $messages[0]);
  106. $this->assertRegExp("/^debug duration=\d rows=4 SELECT bar FROM foo WHERE a='1' AND b='2014-01-01'$/", $messages[1]);
  107. }
  108. /**
  109. * Tests that queries are logged despite database errors
  110. *
  111. * @return void
  112. */
  113. public function testExecuteWithError()
  114. {
  115. $exception = new LogicException('This is bad');
  116. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  117. $inner->expects($this->once())
  118. ->method('execute')
  119. ->will($this->throwException($exception));
  120. $driver = $this->getMockBuilder(DriverInterface::class)->getMock();
  121. $st = new LoggingStatement($inner, $driver);
  122. $st->queryString = 'SELECT bar FROM foo';
  123. $st->setLogger(new QueryLogger());
  124. try {
  125. $st->execute();
  126. } catch (LogicException $e) {
  127. $this->assertSame('This is bad', $e->getMessage());
  128. $this->assertSame($st->queryString, $e->queryString);
  129. }
  130. $messages = Log::engine('queries')->read();
  131. $this->assertCount(1, $messages);
  132. $this->assertRegExp("/^debug duration=\d rows=0 SELECT bar FROM foo$/", $messages[0]);
  133. }
  134. /**
  135. * Tests setting and getting the logger
  136. *
  137. * @return void
  138. */
  139. public function testSetAndGetLogger()
  140. {
  141. $logger = new QueryLogger();
  142. $st = new LoggingStatement(
  143. $this->getMockBuilder(StatementInterface::class)->getMock(),
  144. $this->getMockBuilder(DriverInterface::class)->getMock()
  145. );
  146. $st->setLogger($logger);
  147. $this->assertSame($logger, $st->getLogger());
  148. }
  149. }