Completed
Push — master ( 39986e...13eb1c )
by Christian
02:43
created

SelfTestCliCanFork   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 70
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B doTest() 0 35 5
A testCliRuntime() 0 12 2
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 the php-cli can fork sub processes.
28
 */
29
class SelfTestCliCanFork extends AbstractSelfTest
30
{
31
    /**
32
     * The interpreter to use.
33
     *
34
     * @var string
35
     */
36
    private $interpreter;
37
38
    /**
39
     * Check that we have a correct CLI executable of PHP.
40
     *
41
     * @return void
42
     */
43
    public function doTest()
44
    {
45
        $this->setMessage('Check if the PHP CLI executable can fork processes.');
46
47
        if (null === ($this->interpreter = $this->getAutoConfig()->getPhpCliBinary())) {
48
            $this->markSkipped('No PHP interpreter detected, can not test - assuming forking is not supported.');
49
            $this->getAutoConfig()->setForkingAvailable(false);
50
            return;
51
        }
52
53
        $output = $this->testCliRuntime('var_export(function_exists("pcntl_fork"));');
54
        if ('true' !== $output) {
55
            $this->markFailed('Function pcntl_fork not available (Output: ' . $output . ').');
56
            $this->getAutoConfig()->setForkingAvailable(false);
57
            return;
58
        }
59
60
        $output = $this->testCliRuntime('var_export(pcntl_fork());');
61
        if (is_numeric($output)) {
62
            if ('-1' === $output) {
63
                $this->markFailed('Function pcntl_fork() returned -1. Forking is not supported.');
64
                $this->getAutoConfig()->setForkingAvailable(false);
65
                return;
66
            }
67
            $this->markSuccess('Forked as pid ' . $output);
68
            $this->getAutoConfig()->setForkingAvailable(true);
69
            return;
70
        }
71
72
        $this->getAutoConfig()->setForkingAvailable(false);
73
74
        $this->markWarning(
75
            'Could not determine if forking is possible, assuming not supported (Output : ' . $output .').'
76
        );
77
    }
78
79
    /**
80
     * Runs the passed test script through the php cli and returns the output.
81
     *
82
     * @param string $testScript The test script to run.
83
     *
84
     * @return null|string
85
     */
86
    private function testCliRuntime($testScript)
87
    {
88
        $process = new Process(
89
            sprintf('%s %s', escapeshellcmd($this->interpreter), escapeshellarg('-r ' . $testScript))
90
        );
91
92
        if (0 !== $process->run()) {
93
            return null;
94
        }
95
96
        return $process->getOutput();
97
    }
98
}
99