Test Failed
Push — master ( 440ce5...0cdbc1 )
by Siad
07:01
created

RSTTaskTest::testSingleFileParameterFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
/**
21
 * Unit test for reStructuredText rendering task.
22
 *
23
 * PHP version 5
24
 *
25
 * @category   Tasks
26
 * @package    phing.tasks.ext
27
 * @author     Christian Weiske <[email protected]>
28
 * @license    LGPL v3 or later http://www.gnu.org/licenses/lgpl.html
29
 * @link       http://www.phing.info/
30
 */
31
class RSTTaskTest extends BuildFileTest
32
{
33
    public function setUp(): void
34
    {
35
        //needed for PEAR's System class
36
        error_reporting(error_reporting() & ~E_STRICT & ~E_DEPRECATED);
37
38
        chdir(PHING_TEST_BASE . '/etc/tasks/ext/rst');
0 ignored issues
show
Bug introduced by
The constant PHING_TEST_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
        $this->configureProject(
40
            PHING_TEST_BASE . '/etc/tasks/ext/rst/build.xml'
41
        );
42
    }
43
44
    public function tearDown(): void
45
    {
46
        // remove excess file if the test failed
47
        @unlink(PHING_TEST_BASE . '/etc/tasks/ext/rst/files/single.html');
1 ignored issue
show
Bug introduced by
The constant PHING_TEST_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

47
        /** @scrutinizer ignore-unhandled */ @unlink(PHING_TEST_BASE . '/etc/tasks/ext/rst/files/single.html');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
48
    }
49
50
    protected function assertPreConditions(): void
51
    {
52
        try {
53
            $this->testGetToolPathHtmlFormat();
54
        } catch (BuildException $be) {
55
            $this->markTestSkipped($be->getMessage());
56
        }
57
    }
58
59
    /**
60
     * Checks if a given file has been created and unlinks it afterwards.
61
     *
62
     * @param string $file relative file path
63
     *
64
     * @return void
65
     */
66
    protected function assertFileCreated($file)
67
    {
68
        $this->assertFileExists(
69
            PHING_TEST_BASE . '/etc/tasks/ext/rst/' . $file,
0 ignored issues
show
Bug introduced by
The constant PHING_TEST_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
70
            $file . ' has not been created'
71
        );
72
        unlink(PHING_TEST_BASE . '/etc/tasks/ext/rst/' . $file);
73
    }
74
75
76
    /**
77
     * @expectedException BuildException
78
     * @expectedExceptionMessage "rst2doesnotexist" not found. Install python-docutils.
79
     */
80
    public function testGetToolPathFail()
81
    {
82
        if (method_exists('ReflectionMethod', 'setAccessible')) {
83
            $rt = new RSTTask();
84
            $ref = new ReflectionClass($rt);
85
            $method = $ref->getMethod('getToolPath');
86
            $method->setAccessible(true);
87
            $method->invoke($rt, 'doesnotexist');
88
        } else {
89
            $this->markTestSkipped('No ReflectionMethod::setAccessible available.');
90
        }
91
    }
92
93
    /**
94
     * Get the tool path previously set with setToolpath()
95
     */
96
    public function testGetToolPathCustom()
97
    {
98
        if (method_exists('ReflectionMethod', 'setAccessible')) {
99
            $rt = new RSTTask();
100
            $rt->setToolpath('true'); //mostly /bin/true on unix
101
            $ref = new ReflectionClass($rt);
102
            $method = $ref->getMethod('getToolPath');
103
            $method->setAccessible(true);
104
            $this->assertContains('/true', $method->invoke($rt, 'foo'));
105
        } else {
106
            $this->markTestSkipped('No ReflectionMethod::setAccessible available.');
107
        }
108
    }
109
110
111
    /**
112
     * @expectedException BuildException
113
     * @expectedExceptionMessage Tool does not exist. Path:
114
     */
115
    public function testSetToolpathNotExisting()
116
    {
117
        $rt = new RSTTask();
118
        $rt->setToolpath('doesnotandwillneverexist');
119
    }
120
121
    /**
122
     * @expectedException BuildException
123
     * @expectedExceptionMessage Tool not executable. Path:
124
     */
125
    public function testSetToolpathNonExecutable()
126
    {
127
        $rt = new RSTTask();
128
        $rt->setToolpath(__FILE__);
129
    }
130
131
    public function testGetToolPathHtmlFormat()
132
    {
133
        if (method_exists('ReflectionMethod', 'setAccessible')) {
134
            $rt = new RSTTask();
135
            $ref = new ReflectionClass($rt);
136
            $method = $ref->getMethod('getToolPath');
137
            $method->setAccessible(true);
138
            $this->assertContains('rst2html', $method->invoke($rt, 'html'));
139
        } else {
140
            $this->markTestSkipped('No ReflectionMethod::setAccessible available.');
141
        }
142
    }
143
144
    public function testSingleFileParameterFile()
145
    {
146
        $this->executeTarget(__FUNCTION__);
147
        $this->assertFileCreated('files/single.html');
148
    }
149
150
    public function testSingleFileParameterFileNoExt()
151
    {
152
        $this->executeTarget(__FUNCTION__);
153
        $this->assertFileCreated('files/single-no-ext.html');
154
    }
155
156
    public function testSingleFileParameterFileFormat()
157
    {
158
        $this->executeTarget(__FUNCTION__);
159
        $this->assertFileCreated('files/single.3');
160
    }
161
162
    public function testSingleFileInvalidParameterFormat()
163
    {
164
        $this->expectBuildExceptionContaining(
165
            __FUNCTION__,
166
            'Invalid parameter',
167
            'Invalid output format "foo", allowed are'
168
        );
169
    }
170
171
    public function testSingleFileParameterFileFormatDestination()
172
    {
173
        $this->executeTarget(__FUNCTION__);
174
        $this->assertFileCreated('files/single-destination.html');
175
    }
176
177
    public function testParameterDestinationAsDirectory()
178
    {
179
        $this->executeTarget(__FUNCTION__);
180
        $this->assertFileCreated('files/subdir/files/single.html');
181
        rmdir(PHING_TEST_BASE . '/etc/tasks/ext/rst/files/subdir/files');
0 ignored issues
show
Bug introduced by
The constant PHING_TEST_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
182
        rmdir(PHING_TEST_BASE . '/etc/tasks/ext/rst/files/subdir');
183
    }
184
185
    public function testParameterDestinationDirectoryWithFileset()
186
    {
187
        $this->executeTarget(__FUNCTION__);
188
        $this->assertFileCreated('files/subdir/files/single.html');
189
        $this->assertFileCreated('files/subdir/files/two.html');
190
        rmdir(PHING_TEST_BASE . '/etc/tasks/ext/rst/files/subdir/files');
0 ignored issues
show
Bug introduced by
The constant PHING_TEST_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
191
        rmdir(PHING_TEST_BASE . '/etc/tasks/ext/rst/files/subdir');
192
    }
193
194
    public function testParameterDestinationDirectoryWithFilesetDot()
195
    {
196
        $this->executeTarget(__FUNCTION__);
197
        $this->assertFileCreated('files/subdir/files/single.html');
198
        $this->assertFileCreated('files/subdir/files/two.html');
199
        rmdir(PHING_TEST_BASE . '/etc/tasks/ext/rst/files/subdir/files');
0 ignored issues
show
Bug introduced by
The constant PHING_TEST_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
200
        rmdir(PHING_TEST_BASE . '/etc/tasks/ext/rst/files/subdir');
201
    }
202
203
    public function testParameterUptodate()
204
    {
205
        $this->executeTarget(__FUNCTION__);
206
        $file = PHING_TEST_BASE . '/etc/tasks/ext/rst/files/single.html';
0 ignored issues
show
Bug introduced by
The constant PHING_TEST_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
207
        $this->assertFileExists($file);
208
        $this->assertEquals(
209
            0,
210
            filesize($file),
211
            'File size is not 0, which it should have been when'
212
            . ' rendering was skipped'
213
        );
214
        unlink($file);
215
    }
216
217
    public function testDirectoryCreation()
218
    {
219
        $this->executeTarget(__FUNCTION__);
220
        $this->assertFileCreated('files/a/b/c/single.html');
221
        rmdir(PHING_TEST_BASE . '/etc/tasks/ext/rst/files/a/b/c');
0 ignored issues
show
Bug introduced by
The constant PHING_TEST_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
222
        rmdir(PHING_TEST_BASE . '/etc/tasks/ext/rst/files/a/b');
223
        rmdir(PHING_TEST_BASE . '/etc/tasks/ext/rst/files/a');
224
    }
225
226
    public function testBrokenFile()
227
    {
228
        $this->expectBuildExceptionContaining(
229
            __FUNCTION__,
230
            'Broken file',
231
            'Rendering rST failed'
232
        );
233
        $this->assertInLogs(
234
            'broken.rst:2: (WARNING/2)'
235
            . ' Bullet list ends without a blank line; unexpected unindent.'
236
        );
237
        $this->assertFileCreated('files/broken.html');
238
    }
239
240
    public function testMissingFiles()
241
    {
242
        $this->expectBuildExceptionContaining(
243
            __FUNCTION__,
244
            'Missing attributes/tags',
245
            '"file" attribute or "fileset" subtag required'
246
        );
247
    }
248
249
    public function testMultiple()
250
    {
251
        $this->executeTarget(__FUNCTION__);
252
        $this->assertFileCreated('files/single.html');
253
        $this->assertFileCreated('files/two.html');
254
    }
255
256
    public function testMultipleDir()
257
    {
258
        $this->executeTarget(__FUNCTION__);
259
        $this->assertFileCreated('files/single.html');
260
        $this->assertFileCreated('files/two.html');
261
    }
262
263
    public function testMultipleDirWildcard()
264
    {
265
        $this->executeTarget(__FUNCTION__);
266
        $this->assertFileCreated('files/single.html');
267
    }
268
269
270
    public function testMultipleMapper()
271
    {
272
        $this->executeTarget(__FUNCTION__);
273
        $this->assertFileCreated('files/single.my.html');
274
        $this->assertFileCreated('files/two.my.html');
275
    }
276
277
    /**
278
     * @expectedException BuildException
279
     * @expectedExceptionMessage No filename mapper found for "./files/single.rst"
280
     */
281
    public function testNotMatchingMapper()
282
    {
283
        $this->executeTarget(__FUNCTION__);
284
    }
285
286
287
    public function testFilterChain()
288
    {
289
        $this->executeTarget(__FUNCTION__);
290
        $file = PHING_TEST_BASE . '/etc/tasks/ext/rst/files/filterchain.html';
0 ignored issues
show
Bug introduced by
The constant PHING_TEST_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
291
        $this->assertFileExists($file);
292
        $cont = file_get_contents($file);
293
        $this->assertContains('This is a bar.', $cont);
294
        unlink($file);
295
    }
296
297
298
    public function testCustomParameter()
299
    {
300
        $this->executeTarget(__FUNCTION__);
301
        $this->assertFileExists('files/single.html');
302
        $file = PHING_TEST_BASE . '/etc/tasks/ext/rst/files/single.html';
0 ignored issues
show
Bug introduced by
The constant PHING_TEST_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
303
        $cont = file_get_contents($file);
304
        $this->assertContains('this is a custom css file', $cont);
305
        $this->assertContains('#FF8000', $cont);
306
        unlink($file);
307
    }
308
}
309