LoggingStatementTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 DateTime;
  24. use TestApp\Error\Exception\MyPDOException;
  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. public function testExecuteNoParams(): void
  47. {
  48. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  49. $inner->method('rowCount')->will($this->returnValue(3));
  50. $inner->method('execute')->will($this->returnValue(true));
  51. $driver = $this->getMockBuilder(DriverInterface::class)->getMock();
  52. $st = $this->getMockBuilder(LoggingStatement::class)
  53. ->onlyMethods(['__get'])
  54. ->setConstructorArgs([$inner, $driver])
  55. ->getMock();
  56. $st->expects($this->any())
  57. ->method('__get')
  58. ->willReturn('SELECT bar FROM foo');
  59. $st->setLogger(new QueryLogger(['connection' => 'test']));
  60. $st->execute();
  61. $st->fetchAll();
  62. $messages = Log::engine('queries')->read();
  63. $this->assertCount(1, $messages);
  64. $this->assertMatchesRegularExpression('/^debug: connection=test duration=\d+ rows=3 SELECT bar FROM foo$/', $messages[0]);
  65. }
  66. /**
  67. * Tests that queries are logged when executed with params
  68. */
  69. public function testExecuteWithParams(): void
  70. {
  71. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  72. $inner->method('rowCount')->will($this->returnValue(4));
  73. $inner->method('execute')->will($this->returnValue(true));
  74. $driver = $this->getMockBuilder(DriverInterface::class)->getMock();
  75. $st = $this->getMockBuilder(LoggingStatement::class)
  76. ->onlyMethods(['__get'])
  77. ->setConstructorArgs([$inner, $driver])
  78. ->getMock();
  79. $st->expects($this->any())
  80. ->method('__get')
  81. ->willReturn('SELECT bar FROM foo WHERE x=:a AND y=:b');
  82. $st->setLogger(new QueryLogger(['connection' => 'test']));
  83. $st->execute(['a' => 1, 'b' => 2]);
  84. $st->fetchAll();
  85. $messages = Log::engine('queries')->read();
  86. $this->assertCount(1, $messages);
  87. $this->assertMatchesRegularExpression('/^debug: connection=test duration=\d+ rows=4 SELECT bar FROM foo WHERE x=1 AND y=2$/', $messages[0]);
  88. }
  89. /**
  90. * Tests that queries are logged when executed with bound params
  91. */
  92. public function testExecuteWithBinding(): void
  93. {
  94. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  95. $inner->method('rowCount')->will($this->returnValue(4));
  96. $inner->method('execute')->will($this->returnValue(true));
  97. $date = new DateTime('2013-01-01');
  98. $inner->expects($this->atLeast(2))
  99. ->method('bindValue')
  100. ->withConsecutive(['a', 1], ['b', $date]);
  101. $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  102. $st = $this->getMockBuilder(LoggingStatement::class)
  103. ->onlyMethods(['__get'])
  104. ->setConstructorArgs([$inner, $driver])
  105. ->getMock();
  106. $st->expects($this->any())
  107. ->method('__get')
  108. ->willReturn('SELECT bar FROM foo WHERE a=:a AND b=:b');
  109. $st->setLogger(new QueryLogger(['connection' => 'test']));
  110. $st->bindValue('a', 1);
  111. $st->bindValue('b', $date, 'date');
  112. $st->execute();
  113. $st->fetchAll();
  114. $st->bindValue('b', new DateTime('2014-01-01'), 'date');
  115. $st->execute();
  116. $st->fetchAll();
  117. $messages = Log::engine('queries')->read();
  118. $this->assertCount(2, $messages);
  119. $this->assertMatchesRegularExpression("/^debug: connection=test duration=\d+ rows=4 SELECT bar FROM foo WHERE a='1' AND b='2013-01-01'$/", $messages[0]);
  120. $this->assertMatchesRegularExpression("/^debug: connection=test duration=\d+ rows=4 SELECT bar FROM foo WHERE a='1' AND b='2014-01-01'$/", $messages[1]);
  121. }
  122. /**
  123. * Tests that queries are logged despite database errors
  124. */
  125. public function testExecuteWithError(): void
  126. {
  127. $this->skipIf(
  128. version_compare(PHP_VERSION, '8.2.0', '>='),
  129. 'Setting queryString on exceptions does not work on 8.2+'
  130. );
  131. $exception = new MyPDOException('This is bad');
  132. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  133. $inner->expects($this->once())
  134. ->method('execute')
  135. ->will($this->throwException($exception));
  136. $driver = $this->getMockBuilder(DriverInterface::class)->getMock();
  137. $st = $this->getMockBuilder(LoggingStatement::class)
  138. ->onlyMethods(['__get'])
  139. ->setConstructorArgs([$inner, $driver])
  140. ->getMock();
  141. $st->expects($this->any())
  142. ->method('__get')
  143. ->willReturn('SELECT bar FROM foo');
  144. $st->setLogger(new QueryLogger(['connection' => 'test']));
  145. $this->deprecated(function () use ($st) {
  146. try {
  147. $st->execute();
  148. } catch (MyPDOException $e) {
  149. $this->assertSame('This is bad', $e->getMessage());
  150. $this->assertSame($st->queryString, $e->queryString);
  151. }
  152. });
  153. $messages = Log::engine('queries')->read();
  154. $this->assertCount(1, $messages);
  155. $this->assertMatchesRegularExpression("/^debug: connection=test duration=\d+ rows=0 SELECT bar FROM foo$/", $messages[0]);
  156. }
  157. /**
  158. * Tests setting and getting the logger
  159. */
  160. public function testSetAndGetLogger(): void
  161. {
  162. $logger = new QueryLogger(['connection' => 'test']);
  163. $st = new LoggingStatement(
  164. $this->getMockBuilder(StatementInterface::class)->getMock(),
  165. $this->getMockBuilder(DriverInterface::class)->getMock()
  166. );
  167. $st->setLogger($logger);
  168. $this->assertSame($logger, $st->getLogger());
  169. }
  170. }