Passed
Push — master ( 808df2...02b674 )
by Michiel
11:42
created

FileSizeTaskTest::unitAttributeProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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