LoggingStatementTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 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. 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 = new LoggingStatement($inner, $driver);
  53. $st->queryString = 'SELECT bar FROM foo';
  54. $st->setLogger(new QueryLogger(['connection' => 'test']));
  55. $st->execute();
  56. $st->fetchAll();
  57. $messages = Log::engine('queries')->read();
  58. $this->assertCount(1, $messages);
  59. $this->assertMatchesRegularExpression('/^debug: connection=test duration=\d+ rows=3 SELECT bar FROM foo$/', $messages[0]);
  60. }
  61. /**
  62. * Tests that queries are logged when executed with params
  63. */
  64. public function testExecuteWithParams(): void
  65. {
  66. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  67. $inner->method('rowCount')->will($this->returnValue(4));
  68. $inner->method('execute')->will($this->returnValue(true));
  69. $driver = $this->getMockBuilder(DriverInterface::class)->getMock();
  70. $st = new LoggingStatement($inner, $driver);
  71. $st->queryString = 'SELECT bar FROM foo WHERE x=:a AND y=:b';
  72. $st->setLogger(new QueryLogger(['connection' => 'test']));
  73. $st->execute(['a' => 1, 'b' => 2]);
  74. $st->fetchAll();
  75. $messages = Log::engine('queries')->read();
  76. $this->assertCount(1, $messages);
  77. $this->assertMatchesRegularExpression('/^debug: connection=test duration=\d+ rows=4 SELECT bar FROM foo WHERE x=1 AND y=2$/', $messages[0]);
  78. }
  79. /**
  80. * Tests that queries are logged when executed with bound params
  81. */
  82. public function testExecuteWithBinding(): void
  83. {
  84. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  85. $inner->method('rowCount')->will($this->returnValue(4));
  86. $inner->method('execute')->will($this->returnValue(true));
  87. $date = new DateTime('2013-01-01');
  88. $inner->expects($this->atLeast(2))
  89. ->method('bindValue')
  90. ->withConsecutive(['a', 1], ['b', $date]);
  91. $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  92. $st = new LoggingStatement($inner, $driver);
  93. $st->queryString = 'SELECT bar FROM foo WHERE a=:a AND b=:b';
  94. $st->setLogger(new QueryLogger(['connection' => 'test']));
  95. $st->bindValue('a', 1);
  96. $st->bindValue('b', $date, 'date');
  97. $st->execute();
  98. $st->fetchAll();
  99. $st->bindValue('b', new DateTime('2014-01-01'), 'date');
  100. $st->execute();
  101. $st->fetchAll();
  102. $messages = Log::engine('queries')->read();
  103. $this->assertCount(2, $messages);
  104. $this->assertMatchesRegularExpression("/^debug: connection=test duration=\d+ rows=4 SELECT bar FROM foo WHERE a='1' AND b='2013-01-01'$/", $messages[0]);
  105. $this->assertMatchesRegularExpression("/^debug: connection=test 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. public function testExecuteWithError(): void
  111. {
  112. $exception = new LogicException('This is bad');
  113. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  114. $inner->expects($this->once())
  115. ->method('execute')
  116. ->will($this->throwException($exception));
  117. $driver = $this->getMockBuilder(DriverInterface::class)->getMock();
  118. $st = new LoggingStatement($inner, $driver);
  119. $st->queryString = 'SELECT bar FROM foo';
  120. $st->setLogger(new QueryLogger(['connection' => 'test']));
  121. try {
  122. $st->execute();
  123. } catch (LogicException $e) {
  124. $this->assertSame('This is bad', $e->getMessage());
  125. $this->assertSame($st->queryString, $e->queryString);
  126. }
  127. $messages = Log::engine('queries')->read();
  128. $this->assertCount(1, $messages);
  129. $this->assertMatchesRegularExpression("/^debug: connection=test duration=\d+ rows=0 SELECT bar FROM foo$/", $messages[0]);
  130. }
  131. /**
  132. * Tests setting and getting the logger
  133. */
  134. public function testSetAndGetLogger(): void
  135. {
  136. $logger = new QueryLogger(['connection' => 'test']);
  137. $st = new LoggingStatement(
  138. $this->getMockBuilder(StatementInterface::class)->getMock(),
  139. $this->getMockBuilder(DriverInterface::class)->getMock()
  140. );
  141. $st->setLogger($logger);
  142. $this->assertSame($logger, $st->getLogger());
  143. }
  144. }