Passed
Push — master ( 02e769...327609 )
by Siad
05:24
created

TouchTaskTest::testFileIsDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
/**
22
 * Tests the Touch Task
23
 *
24
 * @author  Michiel Rook <[email protected]>
25
 * @package phing.tasks.system
26
 */
27
class TouchTaskTest extends BuildFileTest
28
{
29
    public function setUp(): void
30
    {
31
        $this->configureProject(
32
            PHING_TEST_BASE
33
            . "/etc/tasks/system/TouchTaskTest.xml"
34
        );
35
        $this->executeTarget("setup");
36
    }
37
38
    public function tearDown(): void
39
    {
40
        $this->executeTarget("clean");
41
    }
42
43
    public function testSimpleTouch()
44
    {
45
        $this->executeTarget(__FUNCTION__);
46
        $this->assertFileExists(
47
            PHING_TEST_BASE
48
            . "/etc/tasks/system/tmp/simple-file"
49
        );
50
    }
51
52
    public function testMkdirs()
53
    {
54
        $this->executeTarget(__FUNCTION__);
55
        $this->assertFileExists(
56
            PHING_TEST_BASE
57
            . "/etc/tasks/system/tmp/this/is/a/test/file"
58
        );
59
    }
60
61
    public function testMkdirsFails()
62
    {
63
        $this->expectException(BuildException::class);
64
        $this->expectExceptionMessage('Error creating new file');
65
66
        $this->executeTarget(__FUNCTION__);
67
68
        $this->assertFileDoesNotExist(
69
            PHING_TEST_BASE
70
            . "/etc/tasks/system/tmp/this/is/a/test/file"
71
        );
72
    }
73
74
    public function testFilelist()
75
    {
76
        $this->executeTarget(__FUNCTION__);
77
        $this->assertFileExists(
78
            PHING_TEST_BASE
79
            . "/etc/tasks/system/tmp/simple-file"
80
        );
81
    }
82
83
    public function testFileset()
84
    {
85
        $this->executeTarget(__FUNCTION__);
86
        $this->assertFileExists(
87
            PHING_TEST_BASE
88
            . "/etc/tasks/system/tmp/simple-file"
89
        );
90
    }
91
92
    public function testMappedFileset()
93
    {
94
        $this->executeTarget(__FUNCTION__);
95
        $tmpDir = $this->getProject()->getProperty('tmp.dir');
96
        $this->assertFileExists($tmpDir . '/touchtest');
97
        $this->assertFileExists($tmpDir . '/touchtestfoo');
98
        $this->assertFileExists($tmpDir . '/touchtestbar');
99
    }
100
101
    /**
102
     * test the mapped file list
103
     */
104
    public function testMappedFilelist()
105
    {
106
        $this->executeTarget(__FUNCTION__);
107
        $tmpDir = $this->getProject()->getProperty('tmp.dir');
108
        $this->assertFileExists($tmpDir . '/touchtest');
109
    }
110
111
    /**
112
     * test millis attribute
113
     */
114
    public function testMillis()
115
    {
116
        // Don't run the test on 32-bit systems
117
        if (PHP_INT_SIZE > 4) {
118
            $this->executeTarget(__FUNCTION__);
119
            $testFile = $this->getProject()->getProperty('tmp.dir') . '/millis-file';
120
            $this->assertFileExists($testFile);
121
122
            $this->assertEquals('December 31 1999 23:59:59', date("F d Y H:i:s", filemtime($testFile)));
123
        } else {
124
            $this->markTestSkipped('Test cannot run on 32-bit systems, epoch millis would have a max of ~25 days');
125
        }
126
    }
127
128
    /**
129
     * test seconds attribute
130
     */
131
    public function testSeconds()
132
    {
133
        $this->executeTarget(__FUNCTION__);
134
        $testFile = $this->getProject()->getProperty('tmp.dir') . '/seconds-file';
135
        $this->assertFileExists($testFile);
136
137
        $this->assertEquals('December 31 1999 23:59:59', date("F d Y H:i:s", filemtime($testFile)));
138
    }
139
140
    /**
141
     * test datetime attribute
142
     */
143
    public function testDatetime()
144
    {
145
        $this->executeTarget(__FUNCTION__);
146
        $testFile = $this->getProject()->getProperty('tmp.dir') . '/datetime-file';
147
        $this->assertFileExists($testFile);
148
149
        $this->assertEquals('December 31 1999 23:59:59', date("F d Y H:i:s", filemtime($testFile)));
150
    }
151
152
    /**
153
     * test datetime with improper datetime
154
     */
155
    public function testNotDateTime()
156
    {
157
        $this->expectBuildException(__FUNCTION__, 'when datetime has invalid value');
158
    }
159
160
    public function testNoFile()
161
    {
162
        $this->expectBuildException(__FUNCTION__, 'when no file specified');
163
    }
164
165
    public function testFileIsDirectory()
166
    {
167
        $this->expectBuildException(__FUNCTION__, 'when file specified is a directory');
168
    }
169
170
    public function testDatetimePreEpoch()
171
    {
172
        $this->expectBuildException(__FUNCTION__, 'when datetime is prior to January 1, 1970');
173
    }
174
175
    public function testReadOnlyFile()
176
    {
177
        $readOnlyFile = $this->getProject()->getProperty('tmp.dir') . '/readonly-file';
178
        if (file_exists($readOnlyFile)) {
179
            chmod($readOnlyFile, 0666); // ensure file is writable
180
        }
181
        $writeCnt = file_put_contents($readOnlyFile, 'TouchTaskTest file');
182
        if (false !== $writeCnt) {
183
            $this->getProject()->setProperty('readonly.file', $readOnlyFile);
184
185
            chmod($readOnlyFile, 0444);
186
187
            try {
188
                $this->executeTarget(__FUNCTION__);
189
                $this->fail('Should not be able to "touch" a read-only file');
190
            } catch (Exception $e) {
191
                // A BuildException is expected to be thrown
192
                $this->assertInstanceOf(BuildException::class, $e);
193
            } finally {
194
                chmod($readOnlyFile, 0666);
195
                unlink($readOnlyFile);
196
            }
197
        } else {
198
            $this->fail('Unable to create test file: ' . $readOnlyFile);
199
        }
200
    }
201
202
    public function testMillisNegative()
203
    {
204
        $this->expectBuildException(__FUNCTION__, 'when millis is negative');
205
    }
206
207
    public function testSecondsNegative()
208
    {
209
        $this->expectBuildException(__FUNCTION__, 'when seconds is negative');
210
    }
211
212
    public function testMillisSubSecond()
213
    {
214
        $this->expectBuildException(__FUNCTION__, 'when millis is less than a second');
215
    }
216
217
    public function testDefaultToNow()
218
    {
219
        $nowTime = time();
220
221
        $this->executeTarget(__FUNCTION__);
222
        $testFile = $this->getProject()->getProperty('tmp.dir') . '/default-now-file';
223
        $this->assertFileExists($testFile);
224
225
        /*
226
         * Assert that the timestamp is within 1 second of the time the test
227
         * started. Ideally it's exactly the same but we'll allow for minimal
228
         * drift to account for a lag between when we noted the time and when
229
         * the file was touched.
230
         */
231
        $this->assertEqualsWithDelta(filemtime($testFile), $nowTime, 1, 'File timestamp not within 1 second of now');
232
    }
233
}
234