QueryLoggerTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Log;
  16. use Cake\Database\Log\LoggedQuery;
  17. use Cake\Database\Log\QueryLogger;
  18. use Cake\Log\Log;
  19. /**
  20. * Tests QueryLogger class
  21. *
  22. */
  23. class QueryLoggerTest extends \Cake\TestSuite\TestCase {
  24. /**
  25. * Set up
  26. *
  27. * @return void
  28. */
  29. public function setUp() {
  30. parent::setUp();
  31. Log::reset();
  32. }
  33. /**
  34. * Tear down
  35. *
  36. * @return void
  37. */
  38. public function tearDown() {
  39. parent::tearDown();
  40. Log::reset();
  41. }
  42. /**
  43. * Tests that query placeholders are replaced when logged
  44. *
  45. * @return void
  46. */
  47. public function testStingInterpolation() {
  48. $logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
  49. $query = new LoggedQuery;
  50. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3';
  51. $query->params = ['p1' => 'string', 'p2' => 3, 'p3' => null];
  52. $logger->expects($this->once())->method('_log')->with($query);
  53. $logger->log($query);
  54. $expected = "SELECT a FROM b where a = 'string' AND b = 3 AND c = NULL";
  55. $this->assertEquals($expected, (string)$query);
  56. }
  57. /**
  58. * Tests that positional placeholders are replaced when logging a query
  59. *
  60. * @return void
  61. */
  62. public function testStingInterpolation2() {
  63. $logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
  64. $query = new LoggedQuery;
  65. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?';
  66. $query->params = ['string', '3', null];
  67. $logger->expects($this->once())->method('_log')->with($query);
  68. $logger->log($query);
  69. $expected = "SELECT a FROM b where a = 'string' AND b = '3' AND c = NULL";
  70. $this->assertEquals($expected, (string)$query);
  71. }
  72. /**
  73. * Tests that the logged query object is passed to the built-in logger using
  74. * the correct scope
  75. *
  76. * @return void
  77. */
  78. public function testLogFunction() {
  79. $logger = new QueryLogger;
  80. $query = new LoggedQuery;
  81. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?';
  82. $query->params = ['string', '3', null];
  83. $engine = $this->getMock('\Cake\Log\Engine\BaseLog', ['write'], ['scopes' => ['queriesLog']]);
  84. Log::engine('queryLoggerTest', $engine);
  85. $engine2 = $this->getMock('\Cake\Log\Engine\BaseLog', ['write'], ['scopes' => ['foo']]);
  86. Log::engine('queryLoggerTest2', $engine2);
  87. $engine2->expects($this->never())->method('write');
  88. $logger->log($query);
  89. }
  90. }