Completed
Pull Request — final (#530)
by Georges
04:25 queued 02:05
created

TestHelper::getExitCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
15
namespace phpFastCache\Helper;
16
17
use phpFastCache\Api;
18
19
/**
20
 * Class TestHelper
21
 * @package phpFastCache\Helper
22
 */
23
class TestHelper
24
{
25
    /**
26
     * @var int
27
     */
28
    protected $exitCode = 0;
29
30
    /**
31
     * TestHelper constructor.
32
     * @param $testName
33
     */
34
    public function __construct($testName)
35
    {
36
        $this->printText('[PhpFastCache API v' . Api::getVersion() . ']', true);
37
        $this->printText("[Begin Test: '{$testName}']");
38
        $this->printText('---');
39
    }
40
41
    /**
42
     * @return int
43
     */
44
    public function getExitCode()
45
    {
46
        return $this->exitCode;
47
    }
48
49
    /**
50
     * @return $this
51
     */
52
    public function resetExitCode()
53
    {
54
        $this->exitCode = 0;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @param string $string
61
     * @return $this
62
     */
63
    public function printSkipText($string)
64
    {
65
        $this->printText("[SKIP] {$string}");
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param string $string
72
     * @return $this
73
     */
74
    public function printPassText($string)
75
    {
76
        $this->printText("[PASS] {$string}");
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param string $string
83
     * @return $this
84
     */
85
    public function printFailText($string)
86
    {
87
        $this->printText("[FAIL] {$string}");
88
        $this->exitCode = 1;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param int $count
95
     * @return $this
96
     */
97
    public function printNewLine($count = 1)
98
    {
99
        for ($i = 0; $i < $count; $i++) {
100
            print PHP_EOL;
101
        }
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param $string
108
     * @param bool $strtoupper
109
     * @return $this
110
     */
111
    public function printText($string, $strtoupper = false)
112
    {
113
        if (!$strtoupper) {
114
            print trim($string) . PHP_EOL;
115
        } else {
116
            print strtoupper(trim($string) . PHP_EOL);
117
        }
118
119
        return $this;
120
    }
121
122
    /**
123
     * @param string $cmd
124
     */
125
    public function runAsyncProcess($cmd)
126
    {
127
        if (substr(php_uname(), 0, 7) === 'Windows') {
128
            pclose(popen('start /B ' . $cmd, 'r'));
129
        } else {
130
            exec($cmd . ' > /dev/null &');
131
        }
132
    }
133
134
    /**
135
     * @param string $file
136
     * @param string $ext
137
     */
138
    public function runSubProcess($file, $ext = '.php')
139
    {
140
        $this->runAsyncProcess('php ' . getcwd() . DIRECTORY_SEPARATOR . 'subprocess' . DIRECTORY_SEPARATOR . $file . '.subprocess' . $ext);
141
    }
142
143
    /**
144
     * @return void
145
     */
146
    public function terminateTest()
147
    {
148
        exit($this->exitCode);
0 ignored issues
show
Coding Style Compatibility introduced by
The method terminateTest() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
149
    }
150
151
    /**
152
     * @param $obj
153
     * @param $prop
154
     * @return mixed
155
     * @throws \ReflectionException
156
     */
157
    public function accessInaccessibleMember($obj, $prop) {
158
        $reflection = new \ReflectionClass($obj);
159
        $property = $reflection->getProperty($prop);
160
        $property->setAccessible(true);
161
        return $property->getValue($obj);
162
    }
163
164
}