Completed
Push — master ( 31e4dd...c38430 )
by Christian
07:22
created

SelfTestController::prepareTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of tenside/core-bundle.
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-bundle
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core-bundle/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core-bundle
18
 * @filesource
19
 */
20
21
namespace Tenside\CoreBundle\Controller;
22
23
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
24
use Symfony\Component\HttpFoundation\JsonResponse;
25
use Tenside\Core\SelfTest\Cli\SelfTestCliCanFork;
26
use Tenside\Core\SelfTest\Php\SelfTestEnvPopulated;
27
use Tenside\CoreBundle\Annotation\ApiDescription;
28
use Tenside\Core\SelfTest\Cli\SelfTestCanSpawnProcesses;
29
use Tenside\Core\SelfTest\Cli\SelfTestCliArguments;
30
use Tenside\Core\SelfTest\Cli\SelfTestCliRuntime;
31
use Tenside\Core\SelfTest\Generic\SelfTestCalledViaHttps;
32
use Tenside\Core\SelfTest\Generic\SelfTestFileOwnerMatches;
33
use Tenside\Core\SelfTest\Php\SelfTestAllowUrlFopenEnabled;
34
use Tenside\Core\SelfTest\Php\SelfTestSuhosin;
35
use Tenside\Core\SelfTest\SelfTest;
36
37
/**
38
 * This class provides the self test entry points.
39
 */
40
class SelfTestController extends AbstractController
41
{
42
    /**
43
     * Retrieve the results of all tests.
44
     *
45
     * @return JsonResponse
46
     *
47
     * @ApiDoc(
48
     *   section="selftest",
49
     *   statusCodes = {
50
     *     200 = "When everything worked out ok"
51
     *   },
52
     *   authentication = true,
53
     *   authenticationRoles = {
54
     *     "ROLE_MANIPULATE_REQUIREMENTS"
55
     *   }
56
     * )
57
     * @ApiDescription(
58
     *   response={
59
     *     "results" = {
60
     *       "actualType" = "collection",
61
     *       "subType" = "object",
62
     *       "description" = "The test results.",
63
     *       "children" = {
64
     *         "name" = {
65
     *           "dataType" = "string",
66
     *           "description" = "The name of the test"
67
     *         },
68
     *         "state" = {
69
     *           "dataType" = "choice",
70
     *           "description" = "The test result state.",
71
     *           "format" = "[FAIL|SKIPPED|SUCCESS|WARNING]"
72
     *         },
73
     *         "message" = {
74
     *           "dataType" = "string",
75
     *           "description" = "The detailed message of the test result."
76
     *         },
77
     *         "explain" = {
78
     *           "dataType" = "string",
79
     *           "description" = "Optional description that could hint any problems and/or explain the error further."
80
     *         }
81
     *       }
82
     *     }
83
     *   }
84
     * )
85
     */
86
    public function getAllTestsAction()
87
    {
88
        $tester = $this->prepareTests();
89
90
        $data = ['results' => []];
91
        foreach ($tester->perform() as $result) {
92
            $data['results'][] = [
93
                'name'    => $this->testClassToSlug($result->getTestClass()),
94
                'state'   => $result->getState(),
95
                'message' => $result->getMessage(),
96
                'explain' => $result->getExplain(),
97
            ];
98
        }
99
100
        return JsonResponse::create($data);
101
    }
102
103
    /**
104
     * Retrieve the automatic generated tenside configuration.
105
     *
106
     * The automatic configuration consists of several values.
107
     *
108
     * @return JsonResponse
109
     *
110
     * @ApiDoc(
111
     *   section="selftest",
112
     *   statusCodes = {
113
     *     200 = "When everything worked out ok"
114
     *   },
115
     *   authentication = true,
116
     *   authenticationRoles = {
117
     *     "ROLE_MANIPULATE_REQUIREMENTS"
118
     *   }
119
     * )
120
     * @ApiDescription(
121
     *   response={
122
     *     "php_cli" = {
123
     *       "dataType" = "string",
124
     *       "description" = "The PHP interpreter to run on command line."
125
     *     },
126
     *     "php_cli_arguments" = {
127
     *       "dataType" = "string",
128
     *       "description" = "Command line arguments to add."
129
     *     }
130
     *   }
131
     * )
132
     */
133
    public function getAutoConfigAction()
134
    {
135
        $tester = $this->prepareTests();
136
        $tester->perform();
137
138
        $config = $tester->getAutoConfig();
139
        $result = [];
140
141
        if ($phpCli = $config->getPhpCliBinary()) {
0 ignored issues
show
Bug introduced by
The method getPhpCliBinary() does not seem to exist on object<Tenside\Core\SelfTest\AutoConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
            $result['php_cli'] = $phpCli;
143
        }
144
        if ($phpArguments = $config->getPhpCliArguments()) {
0 ignored issues
show
Bug introduced by
The method getPhpCliArguments() does not seem to exist on object<Tenside\Core\SelfTest\AutoConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
            $result['php_cli_arguments'] = $phpArguments;
146
        }
147
        if ($phpEnvironment = $config->getPhpCliEnvironment()) {
0 ignored issues
show
Bug introduced by
The method getPhpCliEnvironment() does not seem to exist on object<Tenside\Core\SelfTest\AutoConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
            $result['php_cli_environment'] = $phpEnvironment;
149
        }
150
151
        $result['php_can_fork'] = $config->isForkingAvailable();
0 ignored issues
show
Bug introduced by
The method isForkingAvailable() does not seem to exist on object<Tenside\Core\SelfTest\AutoConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
152
153
        return JsonResponse::create($result);
154
    }
155
156
    /**
157
     * Create a slug from a test class.
158
     *
159
     * @param string $className The class name to convert.
160
     *
161
     * @return string
162
     */
163
    private function testClassToSlug($className)
164
    {
165
        $className = basename(str_replace('\\', '/', $className));
166
167
        if ('SelfTest' === substr($className, 0, 8)) {
168
            $className = substr($className, 8);
169
        }
170
171
        $className = strtolower(substr(preg_replace('#([A-Z])#', '-$1', $className), 1));
172
173
        return $className;
174
    }
175
176
    /**
177
     * Prepare the tests.
178
     *
179
     * @return SelfTest
180
     */
181
    private function prepareTests()
182
    {
183
        $tester = new SelfTest();
184
185
        $tester->addTest(new SelfTestCalledViaHttps());
186
        $tester->addTest(new SelfTestFileOwnerMatches());
187
        $tester->addTest(new SelfTestAllowUrlFopenEnabled());
188
        $tester->addTest(new SelfTestSuhosin());
189
        $tester->addTest(new SelfTestEnvPopulated());
190
        $tester->addTest(new SelfTestCanSpawnProcesses());
191
        $tester->addTest(new SelfTestCliRuntime());
192
        $tester->addTest(new SelfTestCliArguments());
193
        $tester->addTest(new SelfTestCliCanFork());
194
195
        return $tester;
196
    }
197
}
198