FileHandlerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Migration\Logger;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. class FileHandlerTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var FileHandler
  12. */
  13. protected $fileHandler;
  14. /**
  15. * @var \Magento\Framework\Filesystem\Driver\File|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $file;
  18. /**
  19. * @var \Migration\Config|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $config;
  22. /**
  23. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $filesystem;
  26. /**
  27. * @var string
  28. */
  29. protected $message = 'Text message';
  30. /**
  31. * @var int
  32. */
  33. protected $recordLevel = 1;
  34. /**
  35. * @var int
  36. */
  37. protected $handlerLevel = 1;
  38. /**
  39. * @return void
  40. */
  41. protected function setUp()
  42. {
  43. $this->file = $this->getMockBuilder(\Magento\Framework\Filesystem\Driver\File::class)
  44. ->disableOriginalConstructor()
  45. ->setMethods(['filePutContents', 'getRealPath', 'createDirectory'])
  46. ->getMock();
  47. $this->config = $this->getMockBuilder(\Migration\Config::class)
  48. ->disableOriginalConstructor()
  49. ->setMethods(['getOption'])
  50. ->getMock();
  51. $directoryRead = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $directoryRead->expects($this->any())->method('getAbsolutePath')->willReturn('/path/to/var');
  55. $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  56. ->disableOriginalConstructor()
  57. ->setMethods(['getDirectoryRead'])
  58. ->getMock();
  59. $this->filesystem->expects($this->any())->method('getDirectoryRead')->with(DirectoryList::VAR_DIR)
  60. ->willReturn($directoryRead);
  61. $this->fileHandler = new FileHandler($this->file, $this->config, $this->filesystem);
  62. }
  63. /**
  64. * @return void
  65. */
  66. public function testHandleSuccess()
  67. {
  68. $extra = ['mode' => 'application mode'];
  69. $context = [];
  70. $record = [
  71. 'message' => $this->message,
  72. 'level' => $this->recordLevel,
  73. 'extra' => $extra,
  74. 'context' => $context
  75. ];
  76. $this->fileHandler->setLevel($this->handlerLevel);
  77. $file = 'file/path/file.log';
  78. $this->file->expects($this->any())->method('filePutContents')->willReturn(1);
  79. $this->file->expects($this->any())->method('getRealPath')->willReturnMap(
  80. [
  81. [$file, false],
  82. ['file/path', false],
  83. ['file', '/existing_path/file']
  84. ]
  85. );
  86. $this->file->expects($this->once())->method('createDirectory')->willReturn(true);
  87. $this->config->expects($this->any())->method('getOption')->with('log_file')->willReturn($file);
  88. $result = $this->fileHandler->handle($record);
  89. $this->assertFalse($result);
  90. }
  91. /**
  92. * @return void
  93. */
  94. public function testHandleSuccessWithoutBubble()
  95. {
  96. $extra = ['mode' => 'application mode'];
  97. $context = [];
  98. $record = [
  99. 'message' => $this->message,
  100. 'level' => $this->recordLevel,
  101. 'extra' => $extra,
  102. 'context' => $context
  103. ];
  104. $this->fileHandler->setLevel($this->handlerLevel);
  105. $this->fileHandler->setBubble(false);
  106. $file = 'file/path/file.log';
  107. $this->file->expects($this->any())->method('filePutContents')->willReturn(1);
  108. $this->file->expects($this->any())->method('getRealPath')->willReturnMap(
  109. [
  110. [$file, false],
  111. ['file/path', false],
  112. ['file', '/existing_path/file']
  113. ]
  114. );
  115. $this->file->expects($this->once())->method('createDirectory')->willReturn(true);
  116. $this->config->expects($this->any())->method('getOption')->with('log_file')->willReturn($file);
  117. $result = $this->fileHandler->handle($record);
  118. $this->assertTrue($result);
  119. }
  120. /**
  121. * @return void
  122. */
  123. public function testHandleError()
  124. {
  125. $extra = ['mode' => 'application mode'];
  126. $context = [];
  127. $record = [
  128. 'message' => $this->message,
  129. 'level' => $this->recordLevel,
  130. 'extra' => $extra,
  131. 'context' => $context
  132. ];
  133. $this->fileHandler->setLevel($this->handlerLevel);
  134. $result = $this->fileHandler->handle($record);
  135. $this->assertFalse($result);
  136. }
  137. }