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'); |
|
|
|
|
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
|
|
|
|
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 theid
property of an instance of theAccount
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.