Passed
Push — master ( bb1484...f097b8 )
by Bjørn
02:20
created

CustomTester::standardErrorHandling()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0073

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 24
ccs 14
cts 15
cp 0.9333
rs 9.4222
cc 5
nc 5
nop 2
crap 5.0073
1
<?php
2
3
namespace HtaccessCapabilityTester\Testers;
4
5
use \HtaccessCapabilityTester\HtaccessCapabilityTester;
6
use \HtaccessCapabilityTester\HttpRequesterInterface;
7
use \HtaccessCapabilityTester\HttpResponse;
8
use \HtaccessCapabilityTester\SimpleHttpRequester;
9
use \HtaccessCapabilityTester\TestResult;
10
use \HtaccessCapabilityTester\Testers\Helpers\ResponseInterpreter;
11
12
class CustomTester extends AbstractTester
13
{
14
    /** @var array  A definition defining the test */
15
    protected $test;
16
17
    /** @var array  For convenience, all tests */
18
    private $tests;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param  array   $test     The test (may contain subtests)
24
     *
25
     * @return void
26
     */
27 80
    public function __construct($test)
28
    {
29 80
        $this->test = $test;
30
31 80
        if (isset($test['subtests'])) {
32 38
            $this->tests = $test['subtests'];
33
34
            // Add main subdir to subdir for all subtests
35 38
            foreach ($this->tests as &$subtest) {
36 38
                if (isset($subtest['subdir'])) {
37 38
                    $subtest['subdir'] = $test['subdir'] . '/' . $subtest['subdir'];
38
                }
39
            }
40
        } else {
41 65
            $this->tests = [$test];
42
        }
43
44
        //echo '<pre>' . print_r($this->tests, true) . '</pre>';
45
        //echo json_encode($this->tests) . '<br>';
46 80
        parent::__construct();
47 80
    }
48
49
    /**
50
     * Register the test files using the "registerTestFile" method
51
     *
52
     * @return  void
53
     */
54 80
    protected function registerTestFiles()
55
    {
56
57 80
        foreach ($this->tests as $test) {
58 80
            if (isset($test['files'])) {
59 80
                foreach ($test['files'] as $file) {
60
                    // Two syntaxes are allowed:
61
                    // - Simple array (ie: ['0.txt', '0']
62
                    // - Named, ie:  ['filename' => '0.txt', 'content' => '0']
63
                    // The second makes more readable YAML definitions
64 80
                    if (isset($file['filename'])) {
65
                        $filename = $file['filename'];
66
                        $content = $file['content'];
67
                    } else {
68 80
                        list ($filename, $content) = $file;
69
                    }
70 80
                    $this->registerTestFile($test['subdir'] . '/' . $filename, $content);
71
                }
72
            }
73
        }
74 80
    }
75
76 75
    public function getSubDir()
77
    {
78 75
        return $this->test['subdir'];
79
    }
80
81
    /**
82
     *  Standard Error handling
83
     *
84
     * @param  array         $test      the subtest
85
     * @param  HttpResponse  $response
86
     *
87
     * @return TestResult|null  If no errors, null is returned, otherwise a TestResult
88
     */
89 71
    private function standardErrorHandling($test, $response)
0 ignored issues
show
Unused Code introduced by
The parameter $test 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

89
    private function standardErrorHandling(/** @scrutinizer ignore-unused */ $test, $response)

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...
90
    {
91 71
        switch ($response->statusCode) {
92 71
            case '403':
93 9
                return new TestResult(null, '403 Forbidden');
94 62
            case '404':
95
                return new TestResult(null, '404 Not Found');
96 62
            case '500':
97 10
                $hct = $this->getHtaccessCapabilityTester();
98
99
                // Run innocent request / get it from cache. This sets
100
                // $statusCodeOfLastRequest, which we need now
101 10
                $hct->innocentRequestWorks();
102 10
                if ($hct->statusCodeOfLastRequest == '500') {
103 2
                    return new TestResult(null, 'Errored with 500. Everything errors with 500.');
104
                } else {
105 8
                    return new TestResult(
106 8
                        false,
107
                        'Errored with 500. ' .
108 8
                        'Not all goes 500, so it must be a forbidden directive in the .htaccess'
109
                    );
110
                }
111
        }
112 52
        return null;
113
    }
114
115
    /**
116
     * Checks if standard error handling should be bypassed on the test.
117
     *
118
     *
119
     * @param  array         $test      the subtest
120
     * @param  HttpResponse  $response
121
     *
122
     * @return bool          true if it should be bypassed
123
     */
124 80
    private function bypassStandardErrorHandling($test, $response)
125
    {
126 80
        $bypassErrors = [];
127 80
        $byPass = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $byPass is dead and can be removed.
Loading history...
128 80
        if (isset($test['request']['bypass-standard-error-handling'])) {
129 35
            $bypassErrors = $test['request']['bypass-standard-error-handling'];
130
        }
131 80
        if (in_array($response->statusCode, $bypassErrors) || in_array('all', $bypassErrors)) {
132 30
            return true;
133
        }
134 71
        return false;
135
    }
136
137
    /**
138
     *  Run single test
139
     *
140
     * @param  array  $test  the subtest to run
141
     *
142
     * @return TestResult  Returns a test result
143
     */
144 80
    private function realRunSubTest($test)
145
    {
146 80
        $requestUrl = $this->baseUrl . '/' . $test['subdir'] . '/';
147 80
        if (isset($test['request']['url'])) {
148 46
            $requestUrl .= $test['request']['url'];
149
        } else {
150 65
            $requestUrl .= $test['request'];
151
        }
152
        //echo $requestUrl . '<br>';
153 80
        $response = $this->makeHttpRequest($requestUrl);
154
155
        // Standard error handling
156 80
        if (!($this->bypassStandardErrorHandling($test, $response))) {
157 71
            $errorResult = $this->standardErrorHandling($test, $response);
158 71
            if (!is_null($errorResult)) {
159 19
                return $errorResult;
160
            }
161
        }
162 71
        return ResponseInterpreter::interpret($response, $test['interpretation']);
163
    }
164
165
    /**
166
     *  Run
167
     *
168
     * @param  string  $baseDir  Directory on the server where the test files can be put
169
     * @param  string  $baseUrl  The base URL of the test files
170
     *
171
     * @return TestResult  Returns a test result
172
     * @throws \Exception  In case the test cannot be run due to serious issues
173
     */
174 80
    private function realRun($baseDir, $baseUrl)
175
    {
176 80
        $this->prepareForRun($baseDir, $baseUrl);
177
178 80
        $result = null;
179 80
        foreach ($this->tests as $i => $test) {
180
            /*
181
            Disabled, as I'm no longer sure if it is that useful
182
183
            if (isset($test['requirements'])) {
184
            $hct = $this->getHtaccessCapabilityTester();
185
186
            foreach ($test['requirements'] as $requirement) {
187
                $requirementResult = $hct->callMethod($requirement);
188
                if (!$requirementResult) {
189
                    // Skip test
190
                    continue 2;
191
                }
192
            }
193
            }*/
194 80
            if (isset($test['request'])) {
195 80
                $result = $this->realRunSubTest($test);
196 80
                if ($result->info != 'no-match') {
197 79
                    return $result;
198
                }
199
            }
200
        }
201 13
        if (is_null($result)) {
202
            $result = new TestResult(null, 'Nothing to test!');
203
        }
204 13
        return $result;
205
    }
206
207
    /**
208
     *  Run
209
     *
210
     * @param  string  $baseDir  Directory on the server where the test files can be put
211
     * @param  string  $baseUrl  The base URL of the test files
212
     *
213
     * @return TestResult  Returns a test result
214
     * @throws \Exception  In case the test cannot be run due to serious issues
215
     */
216 80
    public function run($baseDir, $baseUrl)
217
    {
218 80
        $testResult = $this->realRun($baseDir, $baseUrl);
219
220
        // A test might not create a request if it has an unfulfilled requirement
221 80
        if (isset($this->lastHttpResponse)) {
222 80
            $testResult->statusCodeOfLastRequest = $this->lastHttpResponse->statusCode;
223
        }
224 80
        return $testResult;
225
    }
226
}
227