Completed
Push — master ( 8e56f4...9f919b )
by Christian
03:16
created

AbstractSelfTestCli::hasInterpreter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/**
4
 * This file is part of tenside/core.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core
18
 * @filesource
19
 */
20
21
namespace Tenside\Core\SelfTest\Cli;
22
23
use Symfony\Component\Process\Process;
24
use Tenside\Core\SelfTest\AbstractSelfTest;
25
26
/**
27
 * This class tests that a valid php-cli binary is available.
28
 */
29
abstract class AbstractSelfTestCli extends AbstractSelfTest
30
{
31
    /**
32
     * The interpreter to use.
33
     *
34
     * @var string
35
     */
36
    private $interpreter;
37
38
    /**
39
     * Check that the interpreter is available.
40
     *
41
     * @return bool
42
     */
43
    protected function hasInterpreter()
44
    {
45
        if (null === ($this->interpreter = $this->getAutoConfig()->getPhpCliBinary())) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(null === ($this...)->getPhpCliBinary()));.
Loading history...
46
            return false;
47
        }
48
49
        return true;
50
    }
51
52
    /**
53
     * Runs the passed test script through the php cli and returns the output.
54
     *
55
     * @param string $testScript The test script to run.
56
     *
57
     * @param string $arguments  Optional cli arguments to use.
58
     *
59
     * @return null|string
60
     */
61
    protected function testCliRuntime($testScript, $arguments = '')
62
    {
63
        if ($arguments) {
64
            $arguments = escapeshellarg($arguments);
65
        }
66
67
        $process = new Process(
68
            sprintf(
69
                '%s %s %s',
70
                escapeshellcmd($this->interpreter),
71
                $arguments,
72
                escapeshellarg('-r ' . $testScript)
73
            )
74
        );
75
76
        if (0 !== $process->run()) {
77
            return null;
78
        }
79
80
        return $process->getOutput();
81
    }
82
}
83