QueryLoggerTest.php 3.3 KB

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