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