DbalLoggerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Doctrine\Logger\DbalLogger;
  13. class DbalLoggerTest extends TestCase
  14. {
  15. /**
  16. * @dataProvider getLogFixtures
  17. */
  18. public function testLog($sql, $params, $logParams)
  19. {
  20. $logger = $this->getMockBuilder('Psr\\Log\\LoggerInterface')->getMock();
  21. $dbalLogger = $this
  22. ->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger')
  23. ->setConstructorArgs(array($logger, null))
  24. ->setMethods(array('log'))
  25. ->getMock()
  26. ;
  27. $dbalLogger
  28. ->expects($this->once())
  29. ->method('log')
  30. ->with($sql, $logParams)
  31. ;
  32. $dbalLogger->startQuery($sql, $params);
  33. }
  34. public function getLogFixtures()
  35. {
  36. return array(
  37. array('SQL', null, array()),
  38. array('SQL', array(), array()),
  39. array('SQL', array('foo' => 'bar'), array('foo' => 'bar')),
  40. array('SQL', array('foo' => "\x7F\xFF"), array('foo' => DbalLogger::BINARY_DATA_VALUE)),
  41. array('SQL', array('foo' => "bar\x7F\xFF"), array('foo' => DbalLogger::BINARY_DATA_VALUE)),
  42. array('SQL', array('foo' => ''), array('foo' => '')),
  43. );
  44. }
  45. public function testLogNonUtf8()
  46. {
  47. $logger = $this->getMockBuilder('Psr\\Log\\LoggerInterface')->getMock();
  48. $dbalLogger = $this
  49. ->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger')
  50. ->setConstructorArgs(array($logger, null))
  51. ->setMethods(array('log'))
  52. ->getMock()
  53. ;
  54. $dbalLogger
  55. ->expects($this->once())
  56. ->method('log')
  57. ->with('SQL', array('utf8' => 'foo', 'nonutf8' => DbalLogger::BINARY_DATA_VALUE))
  58. ;
  59. $dbalLogger->startQuery('SQL', array(
  60. 'utf8' => 'foo',
  61. 'nonutf8' => "\x7F\xFF",
  62. ));
  63. }
  64. public function testLogNonUtf8Array()
  65. {
  66. $logger = $this->getMockBuilder('Psr\\Log\\LoggerInterface')->getMock();
  67. $dbalLogger = $this
  68. ->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger')
  69. ->setConstructorArgs(array($logger, null))
  70. ->setMethods(array('log'))
  71. ->getMock()
  72. ;
  73. $dbalLogger
  74. ->expects($this->once())
  75. ->method('log')
  76. ->with('SQL', array(
  77. 'utf8' => 'foo',
  78. array(
  79. 'nonutf8' => DbalLogger::BINARY_DATA_VALUE,
  80. ),
  81. )
  82. )
  83. ;
  84. $dbalLogger->startQuery('SQL', array(
  85. 'utf8' => 'foo',
  86. array(
  87. 'nonutf8' => "\x7F\xFF",
  88. ),
  89. ));
  90. }
  91. public function testLogLongString()
  92. {
  93. $logger = $this->getMockBuilder('Psr\\Log\\LoggerInterface')->getMock();
  94. $dbalLogger = $this
  95. ->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger')
  96. ->setConstructorArgs(array($logger, null))
  97. ->setMethods(array('log'))
  98. ->getMock()
  99. ;
  100. $testString = 'abc';
  101. $shortString = str_pad('', DbalLogger::MAX_STRING_LENGTH, $testString);
  102. $longString = str_pad('', DbalLogger::MAX_STRING_LENGTH + 1, $testString);
  103. $dbalLogger
  104. ->expects($this->once())
  105. ->method('log')
  106. ->with('SQL', array('short' => $shortString, 'long' => substr($longString, 0, DbalLogger::MAX_STRING_LENGTH - 6).' [...]'))
  107. ;
  108. $dbalLogger->startQuery('SQL', array(
  109. 'short' => $shortString,
  110. 'long' => $longString,
  111. ));
  112. }
  113. public function testLogUTF8LongString()
  114. {
  115. $logger = $this->getMockBuilder('Psr\\Log\\LoggerInterface')->getMock();
  116. $dbalLogger = $this
  117. ->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger')
  118. ->setConstructorArgs(array($logger, null))
  119. ->setMethods(array('log'))
  120. ->getMock()
  121. ;
  122. $testStringArray = array('é', 'á', 'ű', 'ő', 'ú', 'ö', 'ü', 'ó', 'í');
  123. $testStringCount = \count($testStringArray);
  124. $shortString = '';
  125. $longString = '';
  126. for ($i = 1; $i <= DbalLogger::MAX_STRING_LENGTH; ++$i) {
  127. $shortString .= $testStringArray[$i % $testStringCount];
  128. $longString .= $testStringArray[$i % $testStringCount];
  129. }
  130. $longString .= $testStringArray[$i % $testStringCount];
  131. $dbalLogger
  132. ->expects($this->once())
  133. ->method('log')
  134. ->with('SQL', array('short' => $shortString, 'long' => mb_substr($longString, 0, DbalLogger::MAX_STRING_LENGTH - 6, 'UTF-8').' [...]'))
  135. ;
  136. $dbalLogger->startQuery('SQL', array(
  137. 'short' => $shortString,
  138. 'long' => $longString,
  139. ));
  140. }
  141. }