LoggingStatementTest.php 6.0 KB

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