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

SelfTestCliArguments::testCliRuntime()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 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\Console\Output\BufferedOutput;
24
25
/**
26
 * This class tests that a valid php-cli binary is available.
27
 */
28
class SelfTestCliArguments extends AbstractSelfTestCli
29
{
30
    /**
31
     * The output buffer to keep track of the detection.
32
     *
33
     * @var BufferedOutput
34
     */
35
    private $log;
36
37
    /**
38
     * Check that we have a correct CLI executable of PHP.
39
     *
40
     * @return void
41
     */
42
    public function doTest()
43
    {
44
        $this->setMessage('Check which arguments to pass to the PHP CLI executable.');
45
46
        if (!$this->hasInterpreter()) {
47
            $this->markFailed('No PHP interpreter detected, can not test.');
48
            return;
49
        }
50
51
        $this->log = new BufferedOutput();
52
53
        if ($this->check()) {
54
            $data = $this->log->fetch();
55
            if (empty($data)) {
56
                $data = 'No special arguments needed.';
57
            }
58
            $this->markSuccess($data);
59
            return;
60
        }
61
62
        $this->markWarning(
63
            'Could not determine command line arguments, leaving unconfigured and hope the best.'
64
        );
65
    }
66
67
    /**
68
     * Check the needed parameters.
69
     *
70
     * @return bool
71
     */
72
    private function check()
73
    {
74
        return
75
            $this->testMemoryLimit()
76
            && $this->testMaxExecutionTime();
77
    }
78
79
    /**
80
     * Test if raising the memory limit is needed.
81
     *
82
     * @return bool
83
     */
84 View Code Duplication
    private function testMemoryLimit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        $output = $this->testCliRuntime('echo ini_get(\'memory_limit\');');
87
        if ('-1' !== $output) {
88
            if ($this->testOverride('echo ini_get(\'memory_limit\');', '-d memory_limit=1G', '1G')) {
89
                $this->getAutoConfig()->addCommandLineArgument('-d memory_limit=1G');
90
                $this->log->writeln('Will override memory_limit of ' . $output . ' with 1G.');
91
            }
92
        }
93
94
        return true;
95
    }
96
97
    /**
98
     * Test if raising the memory limit is needed.
99
     *
100
     * @return bool
101
     */
102 View Code Duplication
    private function testMaxExecutionTime()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $output = $this->testCliRuntime('echo ini_get(\'max_execution_time\');');
105
        if (900 > intval($output)) {
106
            if ($this->testOverride('echo ini_get(\'max_execution_time\');', '-d max_execution_time=900', '900')) {
107
                $this->getAutoConfig()->addCommandLineArgument('-d max_execution_time=900');
108
                $this->log->writeln('Will override max_execution_time of ' . $output . ' with 900 seconds.');
109
            }
110
        }
111
112
        return true;
113
    }
114
115
    /**
116
     * Test if overriding a parameter works.
117
     *
118
     * @param string $script        The script to run.
119
     *
120
     * @param string $definition    The argument to pass.
121
     *
122
     * @param string $expectedValue The expected output value.
123
     *
124
     * @return bool
125
     */
126
    private function testOverride($script, $definition, $expectedValue)
127
    {
128
        $output = $this->testCliRuntime($script, $definition);
129
        if ($expectedValue !== $output) {
130
            $this->log->writeln('Could not override via ' . $definition);
131
            return false;
132
        }
133
134
        return true;
135
    }
136
}
137