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

ExecTaskTest::testCheckreturnTrue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
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 Exec Task
23
 *
24
 * @author  Michiel Rook <[email protected]>
25
 * @package phing.tasks.system
26
 */
27
class ExecTaskTest extends BuildFileTest
28
{
29
    /**
30
     * Whether test is being run on windows
31
     * @var bool
32
     */
33
    protected $windows;
34
35
    public function setUp(): void
36
    {
37
        $this->configureProject(
38
            PHING_TEST_BASE . '/etc/tasks/system/ExecTest.xml'
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
        );
40
        $this->windows = strtoupper(substr(PHP_OS, 0, 3)) == 'WIN';
41
    }
42
43
    protected function getTargetByName($name)
44
    {
45
        foreach ($this->project->getTargets() as $target) {
46
            if ($target->getName() == $name) {
47
                return $target;
48
            }
49
        }
50
        throw new Exception(sprintf('Target "%s" not found', $name));
51
    }
52
53
    protected function getTaskFromTarget($target, $taskname, $pos = 0)
54
    {
55
        $rchildren = new ReflectionProperty(get_class($target), 'children');
56
        $rchildren->setAccessible(true);
57
        $n = -1;
58
        foreach ($rchildren->getValue($target) as $child) {
59
            if ($child instanceof Task && ++$n == $pos) {
60
                return $child;
61
            }
62
        }
63
        throw new Exception(
64
            sprintf('%s #%d not found in task', $taskname, $pos)
65
        );
66
    }
67
68
    protected function getConfiguredTask($target, $task, $pos = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $pos is not used and could be removed. ( Ignorable by Annotation )

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

68
    protected function getConfiguredTask($target, $task, /** @scrutinizer ignore-unused */ $pos = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70
        $target = $this->getTargetByName($target);
71
        $task = $this->getTaskFromTarget($target, $task);
72
        $task->maybeConfigure();
73
74
        return $task;
75
    }
76
77
    protected function assertAttributeIsSetTo($property, $value, $propertyName = null)
78
    {
79
        $task = $this->getConfiguredTask(
80
            'testPropertySet' . ucfirst($property),
81
            'ExecTask'
82
        );
83
84
        if ($propertyName === null) {
85
            $propertyName = $property;
86
        }
87
88
        if ($task instanceof UnknownElement) {
89
            $task = $task->getRuntimeConfigurableWrapper()->getProxy();
90
        }
91
92
        $rprop = new ReflectionProperty('ExecTask', $propertyName);
93
        $rprop->setAccessible(true);
94
        $this->assertEquals($value, $rprop->getValue($task));
95
    }
96
97
    public function testPropertySetCommandline()
98
    {
99
        $this->assertAttributeIsSetTo('commandline', new Commandline("echo 'foo'"));
100
    }
101
102
    public function testPropertySetDir()
103
    {
104
        $this->assertAttributeIsSetTo(
105
            'dir',
106
            new PhingFile(
107
                realpath(__DIR__ . '/../../../../etc/tasks/system')
108
            )
109
        );
110
    }
111
112
    public function testPropertySetOs()
113
    {
114
        $this->assertAttributeIsSetTo('os', "linux");
115
    }
116
117
    public function testPropertySetEscape()
118
    {
119
        $this->assertAttributeIsSetTo('escape', true);
120
    }
121
122
    public function testPropertySetLogoutput()
123
    {
124
        $this->assertAttributeIsSetTo('logoutput', true, 'logOutput');
125
    }
126
127
    public function testPropertySetPassthru()
128
    {
129
        $this->assertAttributeIsSetTo('passthru', true);
130
    }
131
132
    public function testPropertySetSpawn()
133
    {
134
        $this->assertAttributeIsSetTo('spawn', true);
135
    }
136
137
    public function testPropertySetReturnProperty()
138
    {
139
        $this->assertAttributeIsSetTo('returnProperty', 'retval');
140
    }
141
142
    public function testPropertySetOutputProperty()
143
    {
144
        $this->assertAttributeIsSetTo('outputProperty', 'outval');
145
    }
146
147
    public function testPropertySetCheckReturn()
148
    {
149
        $this->assertAttributeIsSetTo('checkreturn', true);
150
    }
151
152
    public function testPropertySetOutput()
153
    {
154
        $this->assertAttributeIsSetTo(
155
            'output',
156
            new PhingFile(
157
                realpath(__DIR__ . '/../../../../etc/tasks/system')
158
                . '/outputfilename'
159
            )
160
        );
161
    }
162
163
    public function testPropertySetError()
164
    {
165
        $this->assertAttributeIsSetTo(
166
            'error',
167
            new PhingFile(
168
                realpath(__DIR__ . '/../../../../etc/tasks/system')
169
                . '/errorfilename'
170
            )
171
        );
172
    }
173
174
    public function testPropertySetLevelError()
175
    {
176
        $this->assertAttributeIsSetTo('levelError', Project::MSG_ERR, 'logLevel');
177
    }
178
179
    public function testPropertySetLevelWarning()
180
    {
181
        $this->assertAttributeIsSetTo('levelWarning', Project::MSG_WARN, 'logLevel');
182
    }
183
184
    public function testPropertySetLevelInfo()
185
    {
186
        $this->assertAttributeIsSetTo('levelInfo', Project::MSG_INFO, 'logLevel');
187
    }
188
189
    public function testPropertySetLevelVerbose()
190
    {
191
        $this->assertAttributeIsSetTo('levelVerbose', Project::MSG_VERBOSE, 'logLevel');
192
    }
193
194
    public function testPropertySetLevelDebug()
195
    {
196
        $this->assertAttributeIsSetTo('levelDebug', Project::MSG_DEBUG, 'logLevel');
197
    }
198
199
    /**
200
     * @expectedException BuildException
201
     * @expectedExceptionMessage Unknown log level "unknown"
202
     */
203
    public function testPropertySetLevelUnknown()
204
    {
205
        $this->getConfiguredTask('testPropertySetLevelUnknown', 'ExecTask');
206
    }
207
208
    public function testDoNotExecuteOnWrongOs()
209
    {
210
        $this->executeTarget(__FUNCTION__);
211
        $this->assertInLogs('not found in the specified list of valid OSes: unknownos');
212
        $this->assertNotContains(
213
            'this should not be executed',
214
            $this->getOutput()
215
        );
216
    }
217
218
    public function testDoNotExecuteOnWrongOsFamily()
219
    {
220
        $this->expectBuildException(__FUNCTION__, "Don't know how to detect os family 'unknownos'");
221
    }
222
223
    public function testExecuteOnCorrectOs()
224
    {
225
        $this->executeTarget(__FUNCTION__);
226
        $this->assertInLogs('this should be executed');
227
    }
228
229
    public function testExecuteOnCorrectOsFamily()
230
    {
231
        $this->executeTarget(__FUNCTION__);
232
        $this->assertInLogs('this should be executed');
233
    }
234
235
    public function testFailOnNonExistingDir()
236
    {
237
        try {
238
            $this->executeTarget(__FUNCTION__);
239
            $this->fail('Expected BuildException was not thrown');
240
        } catch (BuildException $e) {
241
            $this->assertContains(
242
                str_replace('/', DIRECTORY_SEPARATOR, "'/this/dir/does/not/exist' does not exist"),
243
                $e->getMessage()
244
            );
245
        }
246
    }
247
248
    public function testChangeToDir()
249
    {
250
        if ($this->windows) {
251
            $this->markTestSkipped("Windows does not have 'ls'");
252
        }
253
        $this->executeTarget(__FUNCTION__);
254
        $this->assertInLogs('ExecTaskTest.php');
255
    }
256
257
    public function testCheckreturnTrue()
258
    {
259
        if (FileSystem::getFileSystem()->which('true') === false) {
0 ignored issues
show
introduced by
The condition FileSystem::getFileSyste...which('true') === false is always false.
Loading history...
260
            $this->markTestSkipped("'true' not found.");
261
        }
262
        $this->executeTarget(__FUNCTION__);
263
        $this->assertTrue(true);
264
    }
265
266
    /**
267
     * @expectedException BuildException
268
     * @expectedExceptionMessage exec returned: 1
269
     */
270
    public function testCheckreturnFalse()
271
    {
272
        if (FileSystem::getFileSystem()->which('false') === false) {
0 ignored issues
show
introduced by
The condition FileSystem::getFileSyste...hich('false') === false is always false.
Loading history...
273
            $this->markTestSkipped("'false' not found.");
274
        }
275
        $this->executeTarget(__FUNCTION__);
276
    }
277
278
    public function testOutputProperty()
279
    {
280
        $this->executeTarget(__FUNCTION__);
281
        $this->assertInLogs('The output property\'s value is: "foo"');
282
    }
283
284
    public function testReturnProperty()
285
    {
286
        $this->executeTarget(__FUNCTION__);
287
        $this->assertInLogs('The return property\'s value is: "1"');
288
    }
289
290
    public function testEscape()
291
    {
292
        $this->executeTarget(__FUNCTION__);
293
        $this->assertInLogs($this->windows ? '"foo" "|" "cat"' : 'foo | cat');
294
    }
295
296
    public function testPassthru()
297
    {
298
        ob_start();
299
        $this->executeTarget(__FUNCTION__);
300
        $out = ob_get_clean();
301
        $this->assertEquals("foo", rtrim($out, " \r\n"));
302
        //foo should not be in logs, except for the logged command
303
        $this->assertInLogs('echo foo');
304
        $this->assertNotContains('foo', $this->logBuffer);
305
    }
306
307
    public function testOutput()
308
    {
309
        $file = tempnam(sys_get_temp_dir(), 'phing-exectest-');
310
        $this->project->setProperty('execTmpFile', $file);
311
        $this->executeTarget(__FUNCTION__);
312
        $this->assertContains('outfoo', file_get_contents($file));
313
        unlink($file);
314
    }
315
316
    public function testError()
317
    {
318
        $file = tempnam(sys_get_temp_dir(), 'phing-exectest-');
319
        $this->project->setProperty('execTmpFile', $file);
320
        $this->executeTarget(__FUNCTION__);
321
        $this->assertContains('errfoo', file_get_contents($file));
322
        unlink($file);
323
    }
324
325
    public function testSpawn()
326
    {
327
        $start = time();
328
        $this->executeTarget(__FUNCTION__);
329
        $end = time();
330
        $this->assertLessThan(
331
            4,
332
            $end - $start,
333
            'Time between start and end should be lower than 4 seconds'
334
            . ' - otherwise it looks as spawning did not work'
335
        );
336
    }
337
338
    public function testNestedArg()
339
    {
340
        $this->executeTarget(__FUNCTION__);
341
        $this->assertInLogs($this->windows ? 'nested-arg "b  ar"' : 'nested-arg b  ar');
342
    }
343
344
    /**
345
     * @expectedException BuildException
346
     * @expectedExceptionMessage ExecTask: Please provide "executable"
347
     */
348
    public function testMissingExecutableAndCommand()
349
    {
350
        $this->executeTarget(__FUNCTION__);
351
    }
352
353
    /**
354
     * Inspired by {@link http://www.phing.info/trac/ticket/833}
355
     */
356
    public function testEscapedArg()
357
    {
358
        $this->executeTarget(__FUNCTION__);
359
        $this->assertPropertyEquals('outval', $this->windows ? 'abc$b3 SB' : 'abc$b3!SB');
360
    }
361
362
    public function testEscapedArgWithoutWhitespace()
363
    {
364
        $arg = 'foo|bar';
0 ignored issues
show
Unused Code introduced by
The assignment to $arg is dead and can be removed.
Loading history...
365
        $this->executeTarget(__FUNCTION__);
366
        $this->assertInLogs($this->windows ? '"echo" "foo|bar" 2>&1' : '\'echo\' \'foo|bar\' 2>&1');
367
        $this->assertNotInLogs($this->windows ? 'echo " foo|bar " 2>&1' : 'echo \' foo|bar \' 2>&1');
368
    }
369
370
    public function testNestedEnv()
371
    {
372
        $this->executeTarget(__FUNCTION__);
373
        $this->assertStringStartsWith('phploc', $this->getProject()->getProperty('envtest'));
374
    }
375
}
376