FileTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB\Test\Unit\Logger;
  7. use \Magento\Framework\DB\Logger\File;
  8. class FileTest extends \PHPUnit\Framework\TestCase
  9. {
  10. const DEBUG_FILE = 'debug.file.log';
  11. /**
  12. * @var \Magento\Framework\Filesystem\File\WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  13. */
  14. private $stream;
  15. /**
  16. * @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $dir;
  19. /**
  20. * @var \Magento\Framework\DB\Logger\File
  21. */
  22. private $object;
  23. protected function setUp()
  24. {
  25. $this->stream = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\File\WriteInterface::class);
  26. $this->dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
  27. $this->dir->expects($this->any())
  28. ->method('openFile')
  29. ->with(self::DEBUG_FILE, 'a')
  30. ->will($this->returnValue($this->stream));
  31. $filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
  32. $filesystem->expects($this->any())
  33. ->method('getDirectoryWrite')
  34. ->will($this->returnValue($this->dir));
  35. $this->object = new File(
  36. $filesystem,
  37. self::DEBUG_FILE
  38. );
  39. }
  40. public function testLog()
  41. {
  42. $input = 'message';
  43. $expected = '%amessage';
  44. $this->stream->expects($this->once())
  45. ->method('write')
  46. ->with($this->matches($expected));
  47. $this->object->log($input);
  48. }
  49. /**
  50. * @param $type
  51. *
  52. * @param string $q
  53. * @param array $bind
  54. * @param \Zend_Db_Statement_Pdo|null $result
  55. * @param string $expected
  56. * @dataProvider logStatsDataProvider
  57. */
  58. public function testLogStats($type, $q, array $bind, $result, $expected)
  59. {
  60. $this->stream->expects($this->once())
  61. ->method('write')
  62. ->with($this->matches($expected));
  63. $this->object->logStats($type, $q, $bind, $result);
  64. }
  65. /**
  66. * @return array
  67. */
  68. public function logStatsDataProvider()
  69. {
  70. return [
  71. [\Magento\Framework\DB\LoggerInterface::TYPE_CONNECT, '', [], null, '%aCONNECT%a'],
  72. [
  73. \Magento\Framework\DB\LoggerInterface::TYPE_TRANSACTION,
  74. 'SELECT something',
  75. [],
  76. null,
  77. '%aTRANSACTION SELECT something%a'
  78. ],
  79. [
  80. \Magento\Framework\DB\LoggerInterface::TYPE_QUERY,
  81. 'SELECT something',
  82. [],
  83. null,
  84. '%aSQL: SELECT something%a'
  85. ],
  86. [
  87. \Magento\Framework\DB\LoggerInterface::TYPE_QUERY,
  88. 'SELECT something',
  89. ['data'],
  90. null,
  91. "%aQUERY%aSQL: SELECT something%aBIND: array (%a0 => 'data',%a)%a"
  92. ],
  93. ];
  94. }
  95. public function testLogStatsWithResult()
  96. {
  97. $result = $this->createMock(\Zend_Db_Statement_Pdo::class);
  98. $result->expects($this->once())
  99. ->method('rowCount')
  100. ->will($this->returnValue(10));
  101. $this->stream->expects($this->once())
  102. ->method('write')
  103. ->with($this->logicalNot($this->matches('%aSQL: SELECT something%aAFF: 10')));
  104. $this->object->logStats(
  105. \Magento\Framework\DB\LoggerInterface::TYPE_QUERY,
  106. 'SELECT something',
  107. [],
  108. $result
  109. );
  110. }
  111. public function testLogStatsUnknownType()
  112. {
  113. $this->stream->expects($this->once())
  114. ->method('write')
  115. ->with($this->logicalNot($this->matches('%aSELECT something%a')));
  116. $this->object->logStats('unknown', 'SELECT something');
  117. }
  118. public function testcritical()
  119. {
  120. $exception = new \Exception('error message');
  121. $expected = "%aEXCEPTION%aException%aerror message%a";
  122. $this->stream->expects($this->once())
  123. ->method('write')
  124. ->with($this->matches($expected));
  125. $this->object->critical($exception);
  126. }
  127. }