1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the LGPL. For more information please see |
18
|
|
|
* <http://phing.info>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Phing\Tasks\System; |
22
|
|
|
|
23
|
|
|
use Exception; |
24
|
|
|
use Phing\Exception\BuildException; |
25
|
|
|
use Phing\Support\BuildFileTest; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Tests the Touch Task |
29
|
|
|
* |
30
|
|
|
* @author Michiel Rook <[email protected]> |
31
|
|
|
* @package phing.tasks.system |
32
|
|
|
*/ |
33
|
|
|
class TouchTaskTest extends BuildFileTest |
34
|
|
|
{ |
35
|
|
|
public function setUp(): void |
36
|
|
|
{ |
37
|
|
|
$this->configureProject( |
38
|
|
|
PHING_TEST_BASE |
39
|
|
|
. "/etc/tasks/system/TouchTaskTest.xml" |
40
|
|
|
); |
41
|
|
|
$this->executeTarget("setup"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function tearDown(): void |
45
|
|
|
{ |
46
|
|
|
$this->executeTarget("clean"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testSimpleTouch() |
50
|
|
|
{ |
51
|
|
|
$this->executeTarget(__FUNCTION__); |
52
|
|
|
$this->assertFileExists( |
53
|
|
|
PHING_TEST_BASE |
54
|
|
|
. "/etc/tasks/system/tmp/simple-file" |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testMkdirs() |
59
|
|
|
{ |
60
|
|
|
$this->executeTarget(__FUNCTION__); |
61
|
|
|
$this->assertFileExists( |
62
|
|
|
PHING_TEST_BASE |
63
|
|
|
. "/etc/tasks/system/tmp/this/is/a/test/file" |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testMkdirsFails() |
68
|
|
|
{ |
69
|
|
|
$this->expectException(BuildException::class); |
70
|
|
|
$this->expectExceptionMessage('Error creating new file'); |
71
|
|
|
|
72
|
|
|
$this->executeTarget(__FUNCTION__); |
73
|
|
|
|
74
|
|
|
$this->assertFileDoesNotExist( |
75
|
|
|
PHING_TEST_BASE |
76
|
|
|
. "/etc/tasks/system/tmp/this/is/a/test/file" |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testFilelist() |
81
|
|
|
{ |
82
|
|
|
$this->executeTarget(__FUNCTION__); |
83
|
|
|
$this->assertFileExists( |
84
|
|
|
PHING_TEST_BASE |
85
|
|
|
. "/etc/tasks/system/tmp/simple-file" |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testFileset() |
90
|
|
|
{ |
91
|
|
|
$this->executeTarget(__FUNCTION__); |
92
|
|
|
$this->assertFileExists( |
93
|
|
|
PHING_TEST_BASE |
94
|
|
|
. "/etc/tasks/system/tmp/simple-file" |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testMappedFileset() |
99
|
|
|
{ |
100
|
|
|
$this->executeTarget(__FUNCTION__); |
101
|
|
|
$tmpDir = $this->getProject()->getProperty('tmp.dir'); |
102
|
|
|
$this->assertFileExists($tmpDir . '/touchtest'); |
103
|
|
|
$this->assertFileExists($tmpDir . '/touchtestfoo'); |
104
|
|
|
$this->assertFileExists($tmpDir . '/touchtestbar'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* test the mapped file list |
109
|
|
|
*/ |
110
|
|
|
public function testMappedFilelist() |
111
|
|
|
{ |
112
|
|
|
$this->executeTarget(__FUNCTION__); |
113
|
|
|
$tmpDir = $this->getProject()->getProperty('tmp.dir'); |
114
|
|
|
$this->assertFileExists($tmpDir . '/touchtest'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* test millis attribute |
119
|
|
|
*/ |
120
|
|
|
public function testMillis() |
121
|
|
|
{ |
122
|
|
|
// Don't run the test on 32-bit systems |
123
|
|
|
if (PHP_INT_SIZE > 4) { |
124
|
|
|
$this->executeTarget(__FUNCTION__); |
125
|
|
|
$testFile = $this->getProject()->getProperty('tmp.dir') . '/millis-file'; |
126
|
|
|
$this->assertFileExists($testFile); |
127
|
|
|
|
128
|
|
|
$this->assertEquals('December 31 1999 23:59:59', date("F d Y H:i:s", filemtime($testFile))); |
129
|
|
|
} else { |
130
|
|
|
$this->markTestSkipped('Test cannot run on 32-bit systems, epoch millis would have a max of ~25 days'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* test seconds attribute |
136
|
|
|
*/ |
137
|
|
|
public function testSeconds() |
138
|
|
|
{ |
139
|
|
|
$this->executeTarget(__FUNCTION__); |
140
|
|
|
$testFile = $this->getProject()->getProperty('tmp.dir') . '/seconds-file'; |
141
|
|
|
$this->assertFileExists($testFile); |
142
|
|
|
|
143
|
|
|
$this->assertEquals('December 31 1999 23:59:59', date("F d Y H:i:s", filemtime($testFile))); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* test datetime attribute |
148
|
|
|
*/ |
149
|
|
|
public function testDatetime() |
150
|
|
|
{ |
151
|
|
|
$this->executeTarget(__FUNCTION__); |
152
|
|
|
$testFile = $this->getProject()->getProperty('tmp.dir') . '/datetime-file'; |
153
|
|
|
$this->assertFileExists($testFile); |
154
|
|
|
|
155
|
|
|
$this->assertEquals('December 31 1999 23:59:59', date("F d Y H:i:s", filemtime($testFile))); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* test datetime with improper datetime |
160
|
|
|
*/ |
161
|
|
|
public function testNotDateTime() |
162
|
|
|
{ |
163
|
|
|
$this->expectBuildException(__FUNCTION__, 'when datetime has invalid value'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function testNoFile() |
167
|
|
|
{ |
168
|
|
|
$this->expectBuildException(__FUNCTION__, 'when no file specified'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testFileIsDirectory() |
172
|
|
|
{ |
173
|
|
|
$this->expectBuildException(__FUNCTION__, 'when file specified is a directory'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testDatetimePreEpoch() |
177
|
|
|
{ |
178
|
|
|
$this->expectBuildException(__FUNCTION__, 'when datetime is prior to January 1, 1970'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testReadOnlyFile() |
182
|
|
|
{ |
183
|
|
|
$readOnlyFile = $this->getProject()->getProperty('tmp.dir') . '/readonly-file'; |
184
|
|
|
if (file_exists($readOnlyFile)) { |
185
|
|
|
chmod($readOnlyFile, 0666); // ensure file is writable |
186
|
|
|
} |
187
|
|
|
$writeCnt = file_put_contents($readOnlyFile, 'TouchTaskTest file'); |
188
|
|
|
if (false !== $writeCnt) { |
189
|
|
|
$this->getProject()->setProperty('readonly.file', $readOnlyFile); |
190
|
|
|
|
191
|
|
|
chmod($readOnlyFile, 0444); |
192
|
|
|
|
193
|
|
|
try { |
194
|
|
|
$this->executeTarget(__FUNCTION__); |
195
|
|
|
$this->fail('Should not be able to "touch" a read-only file'); |
196
|
|
|
} catch (Exception $e) { |
197
|
|
|
// A BuildException is expected to be thrown |
198
|
|
|
$this->assertInstanceOf(BuildException::class, $e); |
199
|
|
|
} finally { |
200
|
|
|
chmod($readOnlyFile, 0666); |
201
|
|
|
unlink($readOnlyFile); |
202
|
|
|
} |
203
|
|
|
} else { |
204
|
|
|
$this->fail('Unable to create test file: ' . $readOnlyFile); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testMillisNegative() |
209
|
|
|
{ |
210
|
|
|
$this->expectBuildException(__FUNCTION__, 'when millis is negative'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testSecondsNegative() |
214
|
|
|
{ |
215
|
|
|
$this->expectBuildException(__FUNCTION__, 'when seconds is negative'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testMillisSubSecond() |
219
|
|
|
{ |
220
|
|
|
$this->expectBuildException(__FUNCTION__, 'when millis is less than a second'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function testDefaultToNow() |
224
|
|
|
{ |
225
|
|
|
$nowTime = time(); |
226
|
|
|
|
227
|
|
|
$this->executeTarget(__FUNCTION__); |
228
|
|
|
$testFile = $this->getProject()->getProperty('tmp.dir') . '/default-now-file'; |
229
|
|
|
$this->assertFileExists($testFile); |
230
|
|
|
|
231
|
|
|
/* |
232
|
|
|
* Assert that the timestamp is within 1 second of the time the test |
233
|
|
|
* started. Ideally it's exactly the same but we'll allow for minimal |
234
|
|
|
* drift to account for a lag between when we noted the time and when |
235
|
|
|
* the file was touched. |
236
|
|
|
*/ |
237
|
|
|
$this->assertEqualsWithDelta(filemtime($testFile), $nowTime, 1, 'File timestamp not within 1 second of now'); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|