AbstractSelfTest::doTest()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
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;
22
23
use Tenside\Core\Config\TensideJsonConfig;
24
25
/**
26
 * This class is the abstract base for performing checks that the current environment is suitable for running tenside.
27
 */
28
abstract class AbstractSelfTest
29
{
30
    /**
31
     * The detailed message of the test result.
32
     *
33
     * @var string
34
     */
35
    private $message;
36
37
    /**
38
     * The optional message that might shed some light on any problem that occurred.
39
     *
40
     * @var string
41
     */
42
    private $explain;
43
44
    /**
45
     * The test result state.
46
     *
47
     * @var string
48
     *
49
     * @see TestResult::STATE_SUCCESS, TestResult::STATE_FAIL, TestResult::STATE_SKIPPED.
50
     */
51
    private $state;
52
53
    /**
54
     * The auto config the test shall write to.
55
     *
56
     * @var TensideJsonConfig
57
     */
58
    private $autoConfig;
59
60
    /**
61
     * Run the test and return the result.
62
     *
63
     * @param TensideJsonConfig $autoConfig The auto config to write config values to.
64
     *
65
     * @return SelfTestResult
66
     *
67
     * @throws \RuntimeException When anything went wrong.
68
     */
69
    public function perform(TensideJsonConfig $autoConfig)
70
    {
71
        $this->autoConfig = $autoConfig;
72
73
        $this->prepare();
74
        try {
75
            $this->doTest();
76
        } catch (\Exception $exception) {
77
            $this->finalize();
78
79
            throw new \RuntimeException('Failed to execute test.', 0, $exception);
80
        }
81
82
        $this->finalize();
83
84
        return new SelfTestResult($this->message, $this->state, get_class($this), $this->explain);
85
    }
86
87
    /**
88
     * Retrieve autoConfig
89
     *
90
     * @return TensideJsonConfig
91
     */
92
    protected function getAutoConfig()
93
    {
94
        return $this->autoConfig;
95
    }
96
97
    /**
98
     * Any initialization code.
99
     *
100
     * @return void
101
     */
102
    protected function prepare()
103
    {
104
        // No-op in this class.
105
    }
106
107
    /**
108
     * Any initialization code.
109
     *
110
     * @return void
111
     */
112
    protected function finalize()
113
    {
114
        // No-op in this class.
115
    }
116
117
    /**
118
     * Run the test and return the result.
119
     *
120
     * @return void
121
     */
122
    abstract protected function doTest();
123
124
    /**
125
     * Mark this test as successful.
126
     *
127
     * @param string|null $explain An optional additional explanation.
128
     *
129
     * @return void
130
     */
131
    protected function markSuccess($explain = null)
132
    {
133
        $this->state = SelfTestResult::STATE_SUCCESS;
134
        if (null !== $explain) {
135
            $this->setExplain($explain);
136
        }
137
    }
138
139
    /**
140
     * Mark this test as failing.
141
     *
142
     * @param string|null $explain An optional additional explanation.
143
     *
144
     * @return void
145
     */
146
    protected function markFailed($explain = null)
147
    {
148
        $this->state = SelfTestResult::STATE_FAIL;
149
        if (null !== $explain) {
150
            $this->setExplain($explain);
151
        }
152
    }
153
154
    /**
155
     * Mark this test as skipped.
156
     *
157
     * @param string|null $explain An optional additional explanation.
158
     *
159
     * @return void
160
     */
161
    protected function markSkipped($explain = null)
162
    {
163
        $this->state = SelfTestResult::STATE_SKIPPED;
164
        if (null !== $explain) {
165
            $this->setExplain($explain);
166
        }
167
    }
168
169
    /**
170
     * Mark this test as warning.
171
     *
172
     * @param string|null $explain An optional additional explanation.
173
     *
174
     * @return void
175
     */
176
    protected function markWarning($explain = null)
177
    {
178
        $this->state = SelfTestResult::STATE_WARN;
179
        if (null !== $explain) {
180
            $this->setExplain($explain);
181
        }
182
    }
183
184
    /**
185
     * Set the optional explanation.
186
     *
187
     * @param string $explain An optional additional explanation.
188
     *
189
     * @return void
190
     */
191
    protected function setExplain($explain)
192
    {
193
        $this->explain = $explain;
194
    }
195
196
    /**
197
     * Set the description of the test.
198
     *
199
     * @param string $message The description message of this test.
200
     *
201
     * @return void
202
     */
203
    protected function setMessage($message)
204
    {
205
        $this->message = $message;
206
    }
207
}
208