1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?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);
}
}