1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DmFilemanTest\Helper\FileInfo; |
4
|
|
|
|
5
|
|
|
use DmFileman\Helper\FileInfo\Formatter; |
6
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
7
|
|
|
|
8
|
|
|
class FormatterTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
/** @var Formatter */ |
11
|
|
|
protected $sut; |
12
|
|
|
|
13
|
|
|
protected function setUp() |
14
|
|
|
{ |
15
|
|
|
$this->sut = new Formatter(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @covers DmFileman\Helper\FileInfo\Formatter |
20
|
|
|
*/ |
21
|
|
|
public function testFormatSizeReturnsZeroIfSplInfoIsMissing() |
22
|
|
|
{ |
23
|
|
|
$actualResult = $this->sut->formatSize(null); |
24
|
|
|
|
25
|
|
|
$this->assertEquals(0, $actualResult); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @covers DmFileman\Helper\FileInfo\Formatter |
30
|
|
|
* @requires PHP 5.6 |
31
|
|
|
*/ |
32
|
|
|
public function testFormatSizeReturnsZeroOnZeroSizedSplInfo() |
33
|
|
|
{ |
34
|
|
|
$splFileInfoMock = $this->getSplFileInfoMock(); |
35
|
|
|
|
36
|
|
|
$splFileInfoMock->expects($this->any())->method('getSize')->will($this->returnValue(0)); |
37
|
|
|
|
38
|
|
|
$actualResult = $this->sut->formatSize($splFileInfoMock); |
39
|
|
|
|
40
|
|
|
$this->assertEquals(0, $actualResult); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @covers DmFileman\Helper\FileInfo\Formatter |
45
|
|
|
* @requires PHP 5.6 |
46
|
|
|
*/ |
47
|
|
|
public function testFormatSizeReturnsFileSizeIfRawIsGiven() |
48
|
|
|
{ |
49
|
|
|
$splFileInfoMock = $this->getSplFileInfoMock(); |
50
|
|
|
|
51
|
|
|
$splFileInfoMock->expects($this->any())->method('getSize')->will($this->returnValue(100)); |
52
|
|
|
|
53
|
|
|
$actualResult = $this->sut->formatSize($splFileInfoMock, true); |
54
|
|
|
|
55
|
|
|
$this->assertEquals(100, $actualResult); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function formatSizeProvider() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
[100, '100 B'], |
65
|
|
|
[1024, '1 KB'], |
66
|
|
|
[10240, '10 KB'], |
67
|
|
|
[3145728, '3 MB'], |
68
|
|
|
[3200000, '3.05 MB'], |
69
|
|
|
[1024010, '1.000,01 KB', ',', '.'], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @dataProvider formatSizeProvider |
75
|
|
|
* @covers DmFileman\Helper\FileInfo\Formatter |
76
|
|
|
* @requires PHP 5.6 |
77
|
|
|
* |
78
|
|
|
* @param int $size |
79
|
|
|
* @param string $expectedResult |
80
|
|
|
* @param null|string $decPoint |
81
|
|
|
* @param null|string $thousandsSep |
82
|
|
|
*/ |
83
|
|
|
public function testFormatSizeReturnsHumanReadable($size, $expectedResult, $decPoint = null, $thousandsSep = null) |
84
|
|
|
{ |
85
|
|
|
if ($decPoint || $thousandsSep) { |
|
|
|
|
86
|
|
|
$this->sut = new Formatter($decPoint, $thousandsSep); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$splFileInfoMock = $this->getSplFileInfoMock(); |
90
|
|
|
|
91
|
|
|
$splFileInfoMock->expects($this->any())->method('getSize')->will($this->returnValue($size)); |
92
|
|
|
|
93
|
|
|
$actualResult = $this->sut->formatSize($splFileInfoMock); |
94
|
|
|
|
95
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @covers DmFileman\Helper\FileInfo\Formatter |
100
|
|
|
* @requires PHP 5.6 |
101
|
|
|
*/ |
102
|
|
|
public function testFormatSizeReturnsRawIfUnitIsNotSet() |
103
|
|
|
{ |
104
|
|
|
$size = pow(10, 20); |
105
|
|
|
|
106
|
|
|
$splFileInfoMock = $this->getSplFileInfoMock(); |
107
|
|
|
|
108
|
|
|
$splFileInfoMock->expects($this->any())->method('getSize')->will($this->returnValue($size)); |
109
|
|
|
|
110
|
|
|
$actualResult = $this->sut->formatSize($splFileInfoMock); |
111
|
|
|
|
112
|
|
|
$this->assertEquals($size, $actualResult); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @covers DmFileman\Helper\FileInfo\Formatter |
117
|
|
|
*/ |
118
|
|
|
public function testFormatPermissionsReturnEmptyStringOnEmptySplFileInfo() |
119
|
|
|
{ |
120
|
|
|
$actualResult = $this->sut->formatPermissions(); |
121
|
|
|
|
122
|
|
|
$this->assertEquals('', $actualResult); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @covers DmFileman\Helper\FileInfo\Formatter |
127
|
|
|
* @requires PHP 5.6 |
128
|
|
|
*/ |
129
|
|
|
public function testFormatPermissions() |
130
|
|
|
{ |
131
|
|
|
$splFileInfoMock = $this->getSplFileInfoMock(); |
132
|
|
|
|
133
|
|
|
$splFileInfoMock->expects($this->any())->method('getPerms')->will($this->returnValue(33188)); |
134
|
|
|
|
135
|
|
|
$actualResult = $this->sut->formatPermissions($splFileInfoMock); |
136
|
|
|
|
137
|
|
|
$this->assertEquals('0644', $actualResult); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @covers DmFileman\Helper\FileInfo\Formatter |
142
|
|
|
*/ |
143
|
|
|
public function testFormatOwnerReturnEmptyStringOnEmptySplFileInfo() |
144
|
|
|
{ |
145
|
|
|
$actualResult = $this->sut->formatOwner(); |
146
|
|
|
|
147
|
|
|
$this->assertEquals('', $actualResult); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @covers DmFileman\Helper\FileInfo\Formatter |
152
|
|
|
* @requires PHP 5.6 |
153
|
|
|
*/ |
154
|
|
|
public function testFormatOwner() |
155
|
|
|
{ |
156
|
|
|
$userId = posix_geteuid(); |
157
|
|
|
|
158
|
|
|
$splFileInfoMock = $this->getSplFileInfoMock(); |
159
|
|
|
|
160
|
|
|
$splFileInfoMock->expects($this->any())->method('getOwner')->will($this->returnValue($userId)); |
161
|
|
|
|
162
|
|
|
$actualResult = $this->sut->formatOwner($splFileInfoMock); |
163
|
|
|
|
164
|
|
|
$this->assertEquals(posix_getpwuid($userId)['name'], $actualResult); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @covers DmFileman\Helper\FileInfo\Formatter |
169
|
|
|
*/ |
170
|
|
|
public function testFormatGroupReturnEmptyStringOnEmptySplFileInfo() |
171
|
|
|
{ |
172
|
|
|
$actualResult = $this->sut->formatGroup(); |
173
|
|
|
|
174
|
|
|
$this->assertEquals('', $actualResult); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @covers DmFileman\Helper\FileInfo\Formatter |
179
|
|
|
* @requires PHP 5.6 |
180
|
|
|
*/ |
181
|
|
|
public function testFormatGroup() |
182
|
|
|
{ |
183
|
|
|
$groupId = posix_getegid(); |
184
|
|
|
|
185
|
|
|
$splFileInfoMock = $this->getSplFileInfoMock(); |
186
|
|
|
|
187
|
|
|
$splFileInfoMock->expects($this->any())->method('getGroup')->will($this->returnValue($groupId)); |
188
|
|
|
|
189
|
|
|
$actualResult = $this->sut->formatGroup($splFileInfoMock); |
190
|
|
|
|
191
|
|
|
$this->assertEquals(posix_getpwuid($groupId)['name'], $actualResult); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return PHPUnit_Framework_MockObject_MockObject |
196
|
|
|
*/ |
197
|
|
|
private function getSplFileInfoMock() |
198
|
|
|
{ |
199
|
|
|
return $this->getMockBuilder('SplFileInfo') |
200
|
|
|
->setMethods(['getSize', 'getPerms', 'getOwner', 'getGroup']) |
201
|
|
|
->disableOriginalConstructor() |
202
|
|
|
->getMock(); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: