File: FileSizeTaskTest.php

package info (click to toggle)
phing 3.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,368 kB
  • sloc: php: 59,821; xml: 9,767; sql: 78; makefile: 39; sh: 14
file content (108 lines) | stat: -rw-r--r-- 3,760 bytes parent folder | download | duplicates (2)
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
<?php

namespace Phing\Test\Task\System;

use Phing\Project;
use Phing\Test\Support\BuildFileTest;

/**
 * Tests FileSizeTask.
 *
 * @author  Jawira Portugal <dev@tugal.be>
 * @license LGPL
 * @license https://github.com/phingofficial/phing/blob/master/LICENSE
 *
 * @internal
 */
class FileSizeTaskTest extends BuildFileTest
{
    public function setUp(): void
    {
        $this->configureProject(PHING_TEST_BASE . '/etc/tasks/ext/FileSizeTaskTest.xml');
    }

    public function tearDown(): void
    {
        $this->executeTarget('clean');
    }

    public function testSimpleCase(): void
    {
        $this->getProject()->setProperty('dummy.size', '12345B');
        $this->executeTarget(__FUNCTION__);
        $this->assertInLogs('12345B');
        $this->assertPropertyEquals('filesize', 12345);
    }

    public function testPropertyNameAttribute(): void
    {
        $this->getProject()->setProperty('dummy.size', '27027K');
        $this->executeTarget(__FUNCTION__);
        $this->assertInLogs('27675648B');
        $this->assertPropertyEquals('my-filesize', 27675648);
    }

    /**
     * @dataProvider unitAttributeProvider
     * @group 32bit-incompatible
     *
     * @param mixed $dummySize
     * @param mixed $filesizeUnit
     * @param mixed $logVerbose
     * @param mixed $logInfo
     * @param mixed $expectedSize
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('unitAttributeProvider')]
    #[\PHPUnit\Framework\Attributes\Group('32bit-incompatible')]
    public function testUnitAttribute($dummySize, $filesizeUnit, $logVerbose, $logInfo, $expectedSize): void
    {
        $this->getProject()->setProperty('dummy.size', $dummySize);
        $this->getProject()->setProperty('filesize.unit', $filesizeUnit);
        $this->executeTarget(__FUNCTION__);
        $this->assertInLogs($logVerbose, Project::MSG_VERBOSE);
        $this->assertInLogs($logInfo, Project::MSG_INFO);
        $this->assertPropertyEquals('filesize', $expectedSize);
    }

    public static function unitAttributeProvider(): array
    {
        return [
            ['1K', 'b', '1024B', '1024B', 1024],
            ['13K', 'B', '13312B', '13312B', 13312],
            ['13K', 'k', '13312B', '13K', 13],
            ['517K', 'M', '529408B', '0.5048828125M', 0.5048828125],
            ['500K', 'm', '512000B', '0.48828125m', 0.48828125],
            ['10M', 'g', '10485760B', '0.009765625g', 0.009765625],
            ['20M', 'G', '20971520B', '0.01953125G', 0.01953125],
            ['20m', 't', '20971520B', '1.9073486328125E-5t', '1.9073486328125E-5'],
        ];
    }

    public function testExceptionFileNotSet(): void
    {
        $this->expectBuildExceptionContaining(__FUNCTION__, 'File attribute was not set', 'Input file not specified');
    }

    public function testExceptionInvalidFile(): void
    {
        $this->expectBuildExceptionContaining(__FUNCTION__, 'File is set, but non-existent', 'Input file does not exist or is not readable: invalid-file');
    }

    public function testExceptionInvalidUnit(): void
    {
        $this->getProject()->setProperty('dummy.size', '1K');
        $this->expectBuildExceptionContaining(__FUNCTION__, 'The unit is not a valid one', "Invalid unit 'foo'");
    }

    public function testExceptionEmptyUnit(): void
    {
        $this->getProject()->setProperty('dummy.size', '1K');
        $this->expectBuildExceptionContaining(__FUNCTION__, 'The unit attribute is empty', "Invalid unit ''");
    }

    public function testExceptionEmptyProperty(): void
    {
        $this->getProject()->setProperty('dummy.size', '1K');
        $this->expectBuildExceptionContaining(__FUNCTION__, 'Empty string (or "0") is passed to propertyName attribute', 'Property name cannot be empty');
    }
}