Completed
Push — v7 ( f31319...501a7e )
by Georges
01:50
created

TestHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Helper;
17
18
use Phpfastcache\Api;
19
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
20
21
/**
22
 * Class TestHelper
23
 * @package phpFastCache\Helper
24
 */
25
class TestHelper
26
{
27
    /**
28
     * @var int
29
     */
30
    protected $exitCode = 0;
31
32
    /**
33
     * TestHelper constructor.
34
     * @param $testName
35
     */
36
    public function __construct($testName)
37
    {
38
        $this->printText('[PhpFastCache CORE v' . Api::getPhpFastCacheVersion() . Api::getPhpFastCacheGitHeadHash() . ']', true);
39
        $this->printText('[PhpFastCache API v' . Api::getVersion() . ']', true);
40
        $this->printText('[PHP v' . PHP_VERSION . ']', true);
41
        $this->printText("[Begin Test: '{$testName}']");
42
        $this->printText('---');
43
44
        /**
45
         * Catch all uncaught exception
46
         * to our own exception handler
47
         */
48
        set_exception_handler([$this, 'exceptionHandler']);
49
        set_error_handler([$this, 'errorHandler']);
50
    }
51
52
    /**
53
     * @return int
54
     */
55
    public function getExitCode(): int
56
    {
57
        return $this->exitCode;
58
    }
59
60
    /**
61
     * @return $this
62
     */
63
    public function resetExitCode(): self
64
    {
65
        $this->exitCode = 0;
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param string $string
72
     * @return $this
73
     */
74
    public function printSkipText($string): self
75
    {
76
        $this->printText("[SKIP] {$string}");
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param string $string
83
     * @return $this
84
     */
85
    public function printPassText($string): self
86
    {
87
        $this->printText("[PASS] {$string}");
88
89
        return $this;
90
    }
91
92
    /**
93
     * @param string $string
94
     * @return $this
95
     */
96
    public function printInfoText($string): self
97
    {
98
        $this->printText("[INFO] {$string}");
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param string $string
105
     * @return $this
106
     */
107
    public function printDebugText($string): self
108
    {
109
        $this->printText("[DEBUG] {$string}");
110
111
        return $this;
112
    }
113
114
    /**
115
     * @param string $string
116
     * @return $this
117
     */
118
    public function printFailText($string): self
119
    {
120
        $this->printText("[FAIL] {$string}");
121
        $this->exitCode = 1;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @param int $count
128
     * @return $this
129
     */
130
    public function printNewLine($count = 1): self
131
    {
132
        for ($i = 0; $i < $count; $i++) {
133
            print PHP_EOL;
134
        }
135
136
        return $this;
137
    }
138
139
    /**
140
     * @param $string
141
     * @param bool $strtoupper
142
     * @return $this
143
     */
144
    public function printText($string, $strtoupper = false): self
145
    {
146
        if (!$strtoupper) {
147
            print \trim($string) . PHP_EOL;
148
        } else {
149
            print strtoupper(\trim($string) . PHP_EOL);
150
        }
151
152
        return $this;
153
    }
154
155
    /**
156
     * @param string $cmd
157
     */
158
    public function runAsyncProcess($cmd)
159
    {
160
        if (\substr(php_uname(), 0, 7) === 'Windows') {
161
            pclose(popen('start /B ' . $cmd, 'r'));
0 ignored issues
show
Bug introduced by
It seems like popen('start /B ' . $cmd, 'r') can also be of type false; however, parameter $handle of pclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
            pclose(/** @scrutinizer ignore-type */ popen('start /B ' . $cmd, 'r'));
Loading history...
162
        } else {
163
            exec($cmd . ' > /dev/null &');
164
        }
165
    }
166
167
    /**
168
     * @param string $file
169
     * @param string $ext
170
     */
171
    public function runSubProcess($file, $ext = '.php')
172
    {
173
        $this->runAsyncProcess(($this->isHHVM() ? 'hhvm ' : 'php ') . getcwd() . DIRECTORY_SEPARATOR . 'subprocess' . DIRECTORY_SEPARATOR . $file . '.subprocess' . $ext);
174
    }
175
176
    /**
177
     * @return void
178
     */
179
    public function terminateTest()
180
    {
181
        exit($this->exitCode);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
182
    }
183
184
    /**
185
     * @return bool
186
     */
187
    public function isHHVM(): bool
188
    {
189
        return \defined('HHVM_VERSION');
190
    }
191
192
    /**
193
     * @param $obj
194
     * @param $prop
195
     * @return mixed
196
     * @throws \ReflectionException
197
     */
198
    public function accessInaccessibleMember($obj, $prop)
199
    {
200
        $reflection = new \ReflectionClass($obj);
201
        $property = $reflection->getProperty($prop);
202
        $property->setAccessible(true);
203
        return $property->getValue($obj);
204
    }
205
206
    /**
207
     * @param \Throwable $exception
208
     */
209
    public function exceptionHandler(\Throwable $exception)
210
    {
211
        if ($exception instanceof PhpfastcacheDriverCheckException) {
212
            $this->printSkipText('A driver could not be initialized due to missing requirement: ' . $exception->getMessage());
213
        } else {
214
            $this->printFailText(sprintf(
215
              'Uncaught exception "%s" in "%s" line %d with message: "%s"',
216
              \get_class($exception),
217
              $exception->getFile(),
218
              $exception->getLine(),
219
              $exception->getMessage()
220
            ));
221
        }
222
        $this->terminateTest();
223
    }
224
225
    /**
226
     * @param int $errno
227
     * @param string $errstr
228
     * @param string $errfile
229
     * @param int $errline
230
     */
231
    public function errorHandler(int $errno, string $errstr, string $errfile, int $errline)
232
    {
233
        $errorType = '';
234
235
        switch ($errno) {
236
            case E_PARSE:
237
            case E_ERROR:
238
            case E_CORE_ERROR:
239
            case E_COMPILE_ERROR:
240
            case E_USER_ERROR:
241
                $errorType = '[FATAL ERROR]';
242
                break;
243
            case E_WARNING:
244
            case E_USER_WARNING:
245
            case E_COMPILE_WARNING:
246
            case E_RECOVERABLE_ERROR:
247
                $errorType = '[WARNING]';
248
                break;
249
            case E_NOTICE:
250
            case E_USER_NOTICE:
251
                $errorType = '[NOTICE]';
252
                break;
253
            case E_STRICT:
254
                $errorType = '[STRICT]';
255
                break;
256
            case E_DEPRECATED:
257
            case E_USER_DEPRECATED:
258
                $errorType = '[DEPRECATED]';
259
                break;
260
            default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
261
                break;
262
        }
263
264
        if ($errorType === '[FATAL ERROR]') {
265
            $this->printFailText(sprintf(
266
              "A critical error has been caught: \"%s\" in %s line %d",
267
              "$errorType $errstr",
268
              $errfile,
269
              $errline
270
            ));
271
        } else {
272
            $this->printDebugText(sprintf(
273
              "A non-critical error has been caught: \"%s\" in %s line %d",
274
              "$errorType $errstr",
275
              $errfile,
276
              $errline
277
            ));
278
        }
279
    }
280
}