Completed
Push — master ( 5ff4ab...0cf65a )
by Alexander
16s queued 10s
created

AssetConverterTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 66
c 4
b 0
f 0
dl 0
loc 132
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A tearDown() 0 5 1
A testForceConvert() 0 25 1
A testCheckOutdatedCallback() 0 30 1
A testConvert() 0 17 1
A testConvertOutdated() 0 24 1
1
<?php
2
3
namespace Yiisoft\Assets\Tests;
4
5
use Yiisoft\Assets\AssetConverter;
6
7
/**
8
 * AssetConverterTest.
9
 */
10
final class AssetConverterTest extends TestCase
11
{
12
    /**
13
     * @var string temporary files path
14
     */
15
    private $tmpPath;
16
17
    protected function setUp(): void
18
    {
19
        parent::setUp();
20
21
        $this->tmpPath = $this->aliases->get('@converter');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->aliases->get('@converter') can also be of type boolean. However, the property $tmpPath is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
22
    }
23
24
    protected function tearDown(): void
25
    {
26
        parent::tearDown();
27
28
        $this->removeAssets('@converter');
29
    }
30
31
    public function testConvert(): void
32
    {
33
        file_put_contents($this->tmpPath . '/test.php', <<<EOF
34
<?php
35
36
echo "Hello World!\n";
37
echo "Hello Yii!";
38
EOF
39
        );
40
41
        $converter = new AssetConverter($this->aliases, $this->logger);
42
43
        $converter->setCommand('php', ['txt', 'php {from} > {to}']);
44
45
        $this->assertEquals('test.txt', $converter->convert('test.php', $this->tmpPath));
46
        $this->assertFileExists($this->tmpPath . '/test.txt', 'Failed asserting that asset output file exists.');
47
        $this->assertStringEqualsFile($this->tmpPath . '/test.txt', "Hello World!\nHello Yii!");
48
    }
49
50
    /**
51
     * @depends testConvert
52
     */
53
    public function testConvertOutdated(): void
54
    {
55
        $srcFilename = $this->tmpPath . '/test.php';
56
        file_put_contents($srcFilename, <<<'EOF'
57
<?php
58
59
echo microtime();
60
EOF
61
        );
62
63
        $converter = new AssetConverter($this->aliases, $this->logger);
64
65
        $converter->setCommand('php', ['txt', 'php {from} > {to}']);
66
67
        $converter->convert('test.php', $this->tmpPath);
68
        $initialConvertTime = file_get_contents($this->tmpPath . '/test.txt');
69
70
        usleep(1);
71
        $converter->convert('test.php', $this->tmpPath);
72
        $this->assertStringEqualsFile($this->tmpPath . '/test.txt', $initialConvertTime);
73
74
        touch($srcFilename, time() + 1000);
75
        $converter->convert('test.php', $this->tmpPath);
76
        $this->assertNotEquals($initialConvertTime, file_get_contents($this->tmpPath . '/test.txt'));
77
    }
78
79
    /**
80
     * @depends testConvertOutdated
81
     */
82
    public function testForceConvert(): void
83
    {
84
        file_put_contents($this->tmpPath . '/test.php', <<<'EOF'
85
<?php
86
87
echo microtime();
88
EOF
89
        );
90
91
        $converter = new AssetConverter($this->aliases, $this->logger);
92
93
        $converter->setCommand('php', ['txt', 'php {from} > {to}']);
94
95
        $converter->convert('test.php', $this->tmpPath);
96
        $initialConvertTime = file_get_contents($this->tmpPath . '/test.txt');
97
98
        usleep(1);
99
        $converter->convert('test.php', $this->tmpPath);
100
101
        $this->assertStringEqualsFile($this->tmpPath . '/test.txt', $initialConvertTime);
102
103
        $converter->setForceConvert(true);
104
        $converter->convert('test.php', $this->tmpPath);
105
106
        $this->assertNotEquals($initialConvertTime, file_get_contents($this->tmpPath . '/test.txt'));
107
    }
108
109
    /**
110
     * @depends testConvertOutdated
111
     */
112
    public function testCheckOutdatedCallback(): void
113
    {
114
        $srcFilename = $this->tmpPath . '/test.php';
115
        file_put_contents($srcFilename, <<<'EOF'
116
<?php
117
118
echo microtime();
119
EOF
120
        );
121
122
        $converter = new AssetConverter($this->aliases, $this->logger);
123
124
        $converter->setCommand('php', ['txt', 'php {from} > {to}']);
125
126
        $converter->convert('test.php', $this->tmpPath);
127
        $initialConvertTime = file_get_contents($this->tmpPath . '/test.txt');
128
129
        $converter->setIsOutdatedCallback(function () {
130
            return false;
131
        });
132
133
        $converter->convert('test.php', $this->tmpPath);
134
        $this->assertStringEqualsFile($this->tmpPath . '/test.txt', $initialConvertTime);
135
136
        $converter->setIsOutdatedCallback(function () {
137
            return true;
138
        });
139
140
        $converter->convert('test.php', $this->tmpPath);
141
        $this->assertNotEquals($initialConvertTime, file_get_contents($this->tmpPath . '/test.txt'));
142
    }
143
}
144