Completed
Push — master ( 622c37...dca855 )
by Christian
04:12
created

AbstractSelfTest::markSuccess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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