123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\MediaStorage\Test\Unit\Helper\File;
- use Magento\Framework\App\Filesystem\DirectoryList;
- use Magento\MediaStorage\Helper\File\Media;
- class MediaTest extends \PHPUnit\Framework\TestCase
- {
- const UPDATE_TIME = 'update_time';
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- protected $objectManager;
- /** @var \Magento\Framework\Filesystem\Directory\ReadInterface | \PHPUnit_Framework_MockObject_MockObject */
- protected $dirMock;
- /** @var Media */
- protected $helper;
- protected function setUp()
- {
- $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->dirMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
- ->disableOriginalConstructor()
- ->getMock();
- $filesystemMock->expects($this->any())
- ->method('getDirectoryRead')
- ->with(DirectoryList::MEDIA)
- ->will($this->returnValue($this->dirMock));
- $dateMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
- ->disableOriginalConstructor()
- ->getMock();
- $dateMock->expects($this->any())
- ->method('date')
- ->will($this->returnValue(self::UPDATE_TIME));
- $this->helper = $this->objectManager->getObject(
- \Magento\MediaStorage\Helper\File\Media::class,
- ['filesystem' => $filesystemMock, 'date' => $dateMock]
- );
- }
- /**
- * @param string $path
- * @param string $expectedDir
- * @param string $expectedFile
- * @dataProvider pathDataProvider
- */
- public function testCollectFileInfo($path, $expectedDir, $expectedFile)
- {
- $content = 'content';
- $mediaDirectory = 'mediaDir';
- $relativePath = 'relativePath';
- $this->dirMock->expects($this->once())
- ->method('getRelativePath')
- ->with($mediaDirectory . '/' . $path)
- ->will($this->returnValue($relativePath));
- $this->dirMock->expects($this->once())
- ->method('isFile')
- ->with($relativePath)
- ->will($this->returnValue(true));
- $this->dirMock->expects($this->once())
- ->method('isReadable')
- ->with($relativePath)
- ->will($this->returnValue(true));
- $this->dirMock->expects($this->once())
- ->method('readFile')
- ->with($relativePath)
- ->will($this->returnValue($content));
- $expected = [
- 'filename' => $expectedFile,
- 'content' => $content,
- 'update_time' => self::UPDATE_TIME,
- 'directory' => $expectedDir,
- ];
- $this->assertEquals($expected, $this->helper->collectFileInfo($mediaDirectory, $path));
- }
- /**
- * @return array
- */
- public function pathDataProvider()
- {
- return [
- 'file only' => ['filename', null, 'filename'],
- 'with dir' => ['dir/filename', 'dir', 'filename'],
- ];
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- * @expectedExceptionMessage The "mediaDir/path" file doesn't exist. Verify the file and try again.
- */
- public function testCollectFileInfoNotFile()
- {
- $content = 'content';
- $mediaDirectory = 'mediaDir';
- $relativePath = 'relativePath';
- $path = 'path';
- $this->dirMock->expects($this->once())
- ->method('getRelativePath')
- ->with($mediaDirectory . '/' . $path)
- ->will($this->returnValue($relativePath));
- $this->dirMock->expects($this->once())
- ->method('isFile')
- ->with($relativePath)
- ->will($this->returnValue(false));
- $this->dirMock->expects($this->never())
- ->method('isReadable')
- ->with($relativePath)
- ->will($this->returnValue(true));
- $this->dirMock->expects($this->never())
- ->method('readFile')
- ->with($relativePath)
- ->will($this->returnValue($content));
- $this->helper->collectFileInfo($mediaDirectory, $path);
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- * @expectedExceptionMessage File mediaDir/path is not readable
- */
- public function testCollectFileInfoNotReadable()
- {
- $content = 'content';
- $mediaDirectory = 'mediaDir';
- $relativePath = 'relativePath';
- $path = 'path';
- $this->dirMock->expects($this->once())
- ->method('getRelativePath')
- ->with($mediaDirectory . '/' . $path)
- ->will($this->returnValue($relativePath));
- $this->dirMock->expects($this->once())
- ->method('isFile')
- ->with($relativePath)
- ->will($this->returnValue(true));
- $this->dirMock->expects($this->once())
- ->method('isReadable')
- ->with($relativePath)
- ->will($this->returnValue(false));
- $this->dirMock->expects($this->never())
- ->method('readFile')
- ->with($relativePath)
- ->will($this->returnValue($content));
- $this->helper->collectFileInfo($mediaDirectory, $path);
- }
- }
|