|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Phing\Task\System; |
|
4
|
|
|
|
|
5
|
|
|
use Phing\Project; |
|
6
|
|
|
use Phing\Support\BuildFileTest; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Tests FileSizeTask |
|
10
|
|
|
* |
|
11
|
|
|
* @author Jawira Portugal <[email protected]> |
|
12
|
|
|
* @package phing.tasks.system |
|
13
|
|
|
* @license LGPL |
|
14
|
|
|
* @license https://github.com/phingofficial/phing/blob/master/LICENSE |
|
15
|
|
|
*/ |
|
16
|
|
|
class FileSizeTaskTest extends BuildFileTest |
|
17
|
|
|
{ |
|
18
|
|
|
public function setUp(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$this->configureProject(PHING_TEST_BASE . '/etc/tasks/ext/FileSizeTaskTest.xml'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function tearDown(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$this->executeTarget('clean'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testSimpleCase() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->getProject()->setProperty('dummy.size', '12345B'); |
|
31
|
|
|
$this->executeTarget(__FUNCTION__); |
|
32
|
|
|
$this->assertInLogs('12345B'); |
|
33
|
|
|
$this->assertPropertyEquals('filesize', 12345); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testPropertyNameAttribute() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->getProject()->setProperty('dummy.size', '27027K'); |
|
39
|
|
|
$this->executeTarget(__FUNCTION__); |
|
40
|
|
|
$this->assertInLogs('27675648B'); |
|
41
|
|
|
$this->assertPropertyEquals('my-filesize', 27675648); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @dataProvider unitAttributeProvider |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testUnitAttribute($dummySize, $filesizeUnit, $logVerbose, $logInfo, $expectedSize) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->getProject()->setProperty('dummy.size', $dummySize); |
|
50
|
|
|
$this->getProject()->setProperty('filesize.unit', $filesizeUnit); |
|
51
|
|
|
$this->executeTarget(__FUNCTION__); |
|
52
|
|
|
$this->assertInLogs($logVerbose, Project::MSG_VERBOSE); |
|
53
|
|
|
$this->assertInLogs($logInfo, Project::MSG_INFO); |
|
54
|
|
|
$this->assertPropertyEquals('filesize', $expectedSize); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function unitAttributeProvider() |
|
58
|
|
|
{ |
|
59
|
|
|
return [ |
|
60
|
|
|
['1K', 'b', '1024B', '1024B', 1024], |
|
61
|
|
|
['13K', 'B', '13312B', '13312B', 13312], |
|
62
|
|
|
['13K', 'k', '13312B', '13K', 13], |
|
63
|
|
|
['517K', 'M', '529408B', '0.5048828125M', 0.5048828125], |
|
64
|
|
|
['500K', 'm', '512000B', '0.48828125m', 0.48828125], |
|
65
|
|
|
['10M', 'g', '10485760B', '0.009765625g', 0.009765625], |
|
66
|
|
|
['20M', 'G', '20971520B', '0.01953125G', 0.01953125], |
|
67
|
|
|
['20m', 't', '20971520B', '1.9073486328125E-5t', '1.9073486328125E-5'], |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testExceptionFileNotSet() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->expectBuildExceptionContaining(__FUNCTION__, 'File attribute was not set', 'Input file not specified'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testExceptionInvalidFile() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->expectBuildExceptionContaining(__FUNCTION__, 'File is set, but non-existent', 'Input file does not exist or is not readable: invalid-file'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testExceptionInvalidUnit() |
|
82
|
|
|
{ |
|
83
|
|
|
$this->getProject()->setProperty('dummy.size', '1K'); |
|
84
|
|
|
$this->expectBuildExceptionContaining(__FUNCTION__, 'The unit is not a valid one', "Invalid unit 'foo'"); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testExceptionEmptyUnit() |
|
88
|
|
|
{ |
|
89
|
|
|
$this->getProject()->setProperty('dummy.size', '1K'); |
|
90
|
|
|
$this->expectBuildExceptionContaining(__FUNCTION__, 'The unit attribute is empty', "Invalid unit ''"); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testExceptionEmptyProperty() |
|
94
|
|
|
{ |
|
95
|
|
|
$this->getProject()->setProperty('dummy.size', '1K'); |
|
96
|
|
|
$this->expectBuildExceptionContaining(__FUNCTION__, 'Empty string (or "0") is passed to propertyName attribute', 'Property name cannot be empty'); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|