Passed
Push — main ( 2c83d5...fb3f8a )
by Siad
05:21
created

FileSizeTaskTest::testExceptionEmptyUnit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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