MediaTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\MediaStorage\Test\Unit\Helper\File;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\MediaStorage\Helper\File\Media;
  9. class MediaTest extends \PHPUnit\Framework\TestCase
  10. {
  11. const UPDATE_TIME = 'update_time';
  12. /**
  13. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  14. */
  15. protected $objectManager;
  16. /** @var \Magento\Framework\Filesystem\Directory\ReadInterface | \PHPUnit_Framework_MockObject_MockObject */
  17. protected $dirMock;
  18. /** @var Media */
  19. protected $helper;
  20. protected function setUp()
  21. {
  22. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  23. $this->dirMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $filesystemMock->expects($this->any())
  30. ->method('getDirectoryRead')
  31. ->with(DirectoryList::MEDIA)
  32. ->will($this->returnValue($this->dirMock));
  33. $dateMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $dateMock->expects($this->any())
  37. ->method('date')
  38. ->will($this->returnValue(self::UPDATE_TIME));
  39. $this->helper = $this->objectManager->getObject(
  40. \Magento\MediaStorage\Helper\File\Media::class,
  41. ['filesystem' => $filesystemMock, 'date' => $dateMock]
  42. );
  43. }
  44. /**
  45. * @param string $path
  46. * @param string $expectedDir
  47. * @param string $expectedFile
  48. * @dataProvider pathDataProvider
  49. */
  50. public function testCollectFileInfo($path, $expectedDir, $expectedFile)
  51. {
  52. $content = 'content';
  53. $mediaDirectory = 'mediaDir';
  54. $relativePath = 'relativePath';
  55. $this->dirMock->expects($this->once())
  56. ->method('getRelativePath')
  57. ->with($mediaDirectory . '/' . $path)
  58. ->will($this->returnValue($relativePath));
  59. $this->dirMock->expects($this->once())
  60. ->method('isFile')
  61. ->with($relativePath)
  62. ->will($this->returnValue(true));
  63. $this->dirMock->expects($this->once())
  64. ->method('isReadable')
  65. ->with($relativePath)
  66. ->will($this->returnValue(true));
  67. $this->dirMock->expects($this->once())
  68. ->method('readFile')
  69. ->with($relativePath)
  70. ->will($this->returnValue($content));
  71. $expected = [
  72. 'filename' => $expectedFile,
  73. 'content' => $content,
  74. 'update_time' => self::UPDATE_TIME,
  75. 'directory' => $expectedDir,
  76. ];
  77. $this->assertEquals($expected, $this->helper->collectFileInfo($mediaDirectory, $path));
  78. }
  79. /**
  80. * @return array
  81. */
  82. public function pathDataProvider()
  83. {
  84. return [
  85. 'file only' => ['filename', null, 'filename'],
  86. 'with dir' => ['dir/filename', 'dir', 'filename'],
  87. ];
  88. }
  89. /**
  90. * @expectedException \Magento\Framework\Exception\LocalizedException
  91. * @expectedExceptionMessage The "mediaDir/path" file doesn't exist. Verify the file and try again.
  92. */
  93. public function testCollectFileInfoNotFile()
  94. {
  95. $content = 'content';
  96. $mediaDirectory = 'mediaDir';
  97. $relativePath = 'relativePath';
  98. $path = 'path';
  99. $this->dirMock->expects($this->once())
  100. ->method('getRelativePath')
  101. ->with($mediaDirectory . '/' . $path)
  102. ->will($this->returnValue($relativePath));
  103. $this->dirMock->expects($this->once())
  104. ->method('isFile')
  105. ->with($relativePath)
  106. ->will($this->returnValue(false));
  107. $this->dirMock->expects($this->never())
  108. ->method('isReadable')
  109. ->with($relativePath)
  110. ->will($this->returnValue(true));
  111. $this->dirMock->expects($this->never())
  112. ->method('readFile')
  113. ->with($relativePath)
  114. ->will($this->returnValue($content));
  115. $this->helper->collectFileInfo($mediaDirectory, $path);
  116. }
  117. /**
  118. * @expectedException \Magento\Framework\Exception\LocalizedException
  119. * @expectedExceptionMessage File mediaDir/path is not readable
  120. */
  121. public function testCollectFileInfoNotReadable()
  122. {
  123. $content = 'content';
  124. $mediaDirectory = 'mediaDir';
  125. $relativePath = 'relativePath';
  126. $path = 'path';
  127. $this->dirMock->expects($this->once())
  128. ->method('getRelativePath')
  129. ->with($mediaDirectory . '/' . $path)
  130. ->will($this->returnValue($relativePath));
  131. $this->dirMock->expects($this->once())
  132. ->method('isFile')
  133. ->with($relativePath)
  134. ->will($this->returnValue(true));
  135. $this->dirMock->expects($this->once())
  136. ->method('isReadable')
  137. ->with($relativePath)
  138. ->will($this->returnValue(false));
  139. $this->dirMock->expects($this->never())
  140. ->method('readFile')
  141. ->with($relativePath)
  142. ->will($this->returnValue($content));
  143. $this->helper->collectFileInfo($mediaDirectory, $path);
  144. }
  145. }