DbalLoggerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\Tests\Logger;
  11. use Symfony\Bridge\Doctrine\Logger\DbalLogger;
  12. class DbalLoggerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider getLogFixtures
  16. */
  17. public function testLog($sql, $params, $logParams)
  18. {
  19. $logger = $this->getMock('Psr\\Log\\LoggerInterface');
  20. $dbalLogger = $this
  21. ->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger')
  22. ->setConstructorArgs(array($logger, null))
  23. ->setMethods(array('log'))
  24. ->getMock()
  25. ;
  26. $dbalLogger
  27. ->expects($this->once())
  28. ->method('log')
  29. ->with($sql, $logParams)
  30. ;
  31. $dbalLogger->startQuery($sql, $params);
  32. }
  33. public function getLogFixtures()
  34. {
  35. return array(
  36. array('SQL', null, array()),
  37. array('SQL', array(), array()),
  38. array('SQL', array('foo' => 'bar'), array('foo' => 'bar'))
  39. );
  40. }
  41. public function testLogNonUtf8()
  42. {
  43. $logger = $this->getMock('Psr\\Log\\LoggerInterface');
  44. $dbalLogger = $this
  45. ->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger')
  46. ->setConstructorArgs(array($logger, null))
  47. ->setMethods(array('log'))
  48. ->getMock()
  49. ;
  50. $dbalLogger
  51. ->expects($this->once())
  52. ->method('log')
  53. ->with('SQL', array('utf8' => 'foo', 'nonutf8' => DbalLogger::BINARY_DATA_VALUE))
  54. ;
  55. $dbalLogger->startQuery('SQL', array(
  56. 'utf8' => 'foo',
  57. 'nonutf8' => "\x7F\xFF",
  58. ));
  59. }
  60. public function testLogLongString()
  61. {
  62. $logger = $this->getMock('Symfony\\Component\\HttpKernel\\Log\\LoggerInterface');
  63. $dbalLogger = $this
  64. ->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger')
  65. ->setConstructorArgs(array($logger, null))
  66. ->setMethods(array('log'))
  67. ->getMock()
  68. ;
  69. $testString = 'abc';
  70. $shortString = str_pad('', DbalLogger::MAX_STRING_LENGTH, $testString);
  71. $longString = str_pad('', DbalLogger::MAX_STRING_LENGTH+1, $testString);
  72. $dbalLogger
  73. ->expects($this->once())
  74. ->method('log')
  75. ->with('SQL', array('short' => $shortString, 'long' => substr($longString, 0, DbalLogger::MAX_STRING_LENGTH - 6).' [...]'))
  76. ;
  77. $dbalLogger->startQuery('SQL', array(
  78. 'short' => $shortString,
  79. 'long' => $longString,
  80. ));
  81. }
  82. public function testLogUTF8LongString()
  83. {
  84. if (!function_exists('mb_detect_encoding')) {
  85. $this->markTestSkipped('Testing log shortening of utf8 charsets requires the mb_detect_encoding() function.');
  86. }
  87. $logger = $this->getMock('Symfony\\Component\\HttpKernel\\Log\\LoggerInterface');
  88. $dbalLogger = $this
  89. ->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger')
  90. ->setConstructorArgs(array($logger, null))
  91. ->setMethods(array('log'))
  92. ->getMock()
  93. ;
  94. $testStringArray = array('é', 'á', 'ű', 'ő', 'ú', 'ö', 'ü', 'ó', 'í');
  95. $testStringCount = count($testStringArray);
  96. $shortString = '';
  97. $longString = '';
  98. for ($i = 1; $i <= DbalLogger::MAX_STRING_LENGTH; $i++) {
  99. $shortString .= $testStringArray[$i % $testStringCount];
  100. $longString .= $testStringArray[$i % $testStringCount];
  101. }
  102. $longString .= $testStringArray[$i % $testStringCount];
  103. $dbalLogger
  104. ->expects($this->once())
  105. ->method('log')
  106. ->with('SQL', array('short' => $shortString, 'long' => mb_substr($longString, 0, DbalLogger::MAX_STRING_LENGTH - 6, mb_detect_encoding($longString)).' [...]'))
  107. ;
  108. $dbalLogger->startQuery('SQL', array(
  109. 'short' => $shortString,
  110. 'long' => $longString,
  111. ));
  112. }
  113. }