QueryLoggerTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests QueryLogger class
  22. *
  23. */
  24. class QueryLoggerTest extends TestCase {
  25. /**
  26. * Set up
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. Log::reset();
  33. }
  34. /**
  35. * Tear down
  36. *
  37. * @return void
  38. */
  39. public function tearDown() {
  40. parent::tearDown();
  41. Log::reset();
  42. }
  43. /**
  44. * Tests that query placeholders are replaced when logged
  45. *
  46. * @return void
  47. */
  48. public function testStingInterpolation() {
  49. $logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
  50. $query = new LoggedQuery;
  51. $query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3';
  52. $query->params = ['p1' => 'string', 'p3' => null, 'p2' => 3];
  53. $logger->expects($this->once())->method('_log')->with($query);
  54. $logger->log($query);
  55. $expected = "SELECT a FROM b where a = 'string' AND b = 3 AND c = NULL";
  56. $this->assertEquals($expected, (string)$query);
  57. }
  58. /**
  59. * Tests that positional placeholders are replaced when logging a query
  60. *
  61. * @return void
  62. */
  63. public function testStingInterpolation2() {
  64. $logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
  65. $query = new LoggedQuery;
  66. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?';
  67. $query->params = ['string', '3', null];
  68. $logger->expects($this->once())->method('_log')->with($query);
  69. $logger->log($query);
  70. $expected = "SELECT a FROM b where a = 'string' AND b = '3' AND c = NULL";
  71. $this->assertEquals($expected, (string)$query);
  72. }
  73. /**
  74. * Tests that the logged query object is passed to the built-in logger using
  75. * the correct scope
  76. *
  77. * @return void
  78. */
  79. public function testLogFunction() {
  80. $logger = new QueryLogger;
  81. $query = new LoggedQuery;
  82. $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?';
  83. $query->params = ['string', '3', null];
  84. $engine = $this->getMock('\Cake\Log\Engine\BaseLog', ['log'], ['scopes' => ['queriesLog']]);
  85. Log::engine('queryLoggerTest', $engine);
  86. $engine2 = $this->getMock('\Cake\Log\Engine\BaseLog', ['log'], ['scopes' => ['foo']]);
  87. Log::engine('queryLoggerTest2', $engine2);
  88. $engine2->expects($this->never())->method('log');
  89. $logger->log($query);
  90. }
  91. }