LoggingStatementTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\LoggingStatement;
  19. use Cake\Database\Log\QueryLogger;
  20. use Cake\Database\StatementInterface;
  21. use Cake\Log\Log;
  22. use Cake\TestSuite\TestCase;
  23. use LogicException;
  24. /**
  25. * Tests LoggingStatement class
  26. */
  27. class LoggingStatementTest extends TestCase
  28. {
  29. public function setUp(): void
  30. {
  31. parent::setUp();
  32. Log::setConfig('queries', [
  33. 'className' => 'Array',
  34. 'scopes' => ['queriesLog'],
  35. ]);
  36. }
  37. public function tearDown(): void
  38. {
  39. parent::tearDown();
  40. Log::drop('queries');
  41. }
  42. /**
  43. * Tests that queries are logged when executed without params
  44. *
  45. * @return void
  46. */
  47. public function testExecuteNoParams()
  48. {
  49. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  50. $inner->method('rowCount')->will($this->returnValue(3));
  51. $inner->method('execute')->will($this->returnValue(true));
  52. $driver = $this->getMockBuilder(DriverInterface::class)->getMock();
  53. $st = new LoggingStatement($inner, $driver);
  54. $st->queryString = 'SELECT bar FROM foo';
  55. $st->setLogger(new QueryLogger());
  56. $st->execute();
  57. $messages = Log::engine('queries')->read();
  58. $this->assertCount(1, $messages);
  59. $this->assertRegExp('/^debug connection= duration=\d+ rows=3 SELECT bar FROM foo$/', $messages[0]);
  60. }
  61. /**
  62. * Tests that queries are logged when executed with params
  63. *
  64. * @return void
  65. */
  66. public function testExecuteWithParams()
  67. {
  68. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  69. $inner->method('rowCount')->will($this->returnValue(4));
  70. $inner->method('execute')->will($this->returnValue(true));
  71. $driver = $this->getMockBuilder(DriverInterface::class)->getMock();
  72. $st = new LoggingStatement($inner, $driver);
  73. $st->queryString = 'SELECT bar FROM foo WHERE x=:a AND y=:b';
  74. $st->setLogger(new QueryLogger());
  75. $st->execute(['a' => 1, 'b' => 2]);
  76. $messages = Log::engine('queries')->read();
  77. $this->assertCount(1, $messages);
  78. $this->assertRegExp('/^debug connection= duration=\d+ rows=4 SELECT bar FROM foo WHERE x=1 AND y=2$/', $messages[0]);
  79. }
  80. /**
  81. * Tests that queries are logged when executed with bound params
  82. *
  83. * @return void
  84. */
  85. public function testExecuteWithBinding()
  86. {
  87. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  88. $inner->method('rowCount')->will($this->returnValue(4));
  89. $inner->method('execute')->will($this->returnValue(true));
  90. $date = new \DateTime('2013-01-01');
  91. $inner->expects($this->at(0))->method('bindValue')->with('a', 1);
  92. $inner->expects($this->at(1))->method('bindValue')->with('b', $date);
  93. $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  94. $st = new LoggingStatement($inner, $driver);
  95. $st->queryString = 'SELECT bar FROM foo WHERE a=:a AND b=:b';
  96. $st->setLogger(new QueryLogger());
  97. $st->bindValue('a', 1);
  98. $st->bindValue('b', $date, 'date');
  99. $st->execute();
  100. $st->bindValue('b', new \DateTime('2014-01-01'), 'date');
  101. $st->execute();
  102. $messages = Log::engine('queries')->read();
  103. $this->assertCount(2, $messages);
  104. $this->assertRegExp("/^debug connection= duration=\d+ rows=4 SELECT bar FROM foo WHERE a='1' AND b='2013-01-01'$/", $messages[0]);
  105. $this->assertRegExp("/^debug connection= duration=\d+ rows=4 SELECT bar FROM foo WHERE a='1' AND b='2014-01-01'$/", $messages[1]);
  106. }
  107. /**
  108. * Tests that queries are logged despite database errors
  109. *
  110. * @return void
  111. */
  112. public function testExecuteWithError()
  113. {
  114. $exception = new LogicException('This is bad');
  115. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  116. $inner->expects($this->once())
  117. ->method('execute')
  118. ->will($this->throwException($exception));
  119. $driver = $this->getMockBuilder(DriverInterface::class)->getMock();
  120. $st = new LoggingStatement($inner, $driver);
  121. $st->queryString = 'SELECT bar FROM foo';
  122. $st->setLogger(new QueryLogger());
  123. try {
  124. $st->execute();
  125. } catch (LogicException $e) {
  126. $this->assertSame('This is bad', $e->getMessage());
  127. $this->assertSame($st->queryString, $e->queryString);
  128. }
  129. $messages = Log::engine('queries')->read();
  130. $this->assertCount(1, $messages);
  131. $this->assertRegExp("/^debug connection= duration=\d+ rows=0 SELECT bar FROM foo$/", $messages[0]);
  132. }
  133. /**
  134. * Tests setting and getting the logger
  135. *
  136. * @return void
  137. */
  138. public function testSetAndGetLogger()
  139. {
  140. $logger = new QueryLogger();
  141. $st = new LoggingStatement(
  142. $this->getMockBuilder(StatementInterface::class)->getMock(),
  143. $this->getMockBuilder(DriverInterface::class)->getMock()
  144. );
  145. $st->setLogger($logger);
  146. $this->assertSame($logger, $st->getLogger());
  147. }
  148. }