1919use Jyxo \Beholder \Output \Output ;
2020use Jyxo \Beholder \Output \TextOutput ;
2121use Jyxo \Beholder \Result \TestSuiteResult ;
22+ use Jyxo \Timer ;
23+ use UnexpectedValueException ;
24+ use function array_keys ;
25+ use function array_multisort ;
26+ use function explode ;
27+ use function fnmatch ;
28+ use function header ;
29+ use function shuffle ;
30+ use function sprintf ;
31+ use const SORT_ASC ;
2232
2333/**
2434 * Beholder test executor.
3646 * $beholder->run();
3747 * </code>
3848 *
39- * @category Jyxo
40- * @package Jyxo\Beholder
4149 * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
4250 * @license https://github.com/jyxo/php/blob/master/license.txt
4351 * @author Jan Matoušek
4452 * @author Jaroslav Hanslík
4553 */
4654class Executor
4755{
56+
4857 /**
4958 * Plaintext output.
50- *
51- * @var string
5259 */
53- const OUTPUT_TEXT = 't ' ;
60+ public const OUTPUT_TEXT = 't ' ;
5461
5562 /**
5663 * HTML output.
57- *
58- * @var string
5964 */
60- const OUTPUT_HTML = 'h ' ;
65+ public const OUTPUT_HTML = 'h ' ;
6166
6267 /**
6368 * JSON output.
64- *
65- * @var string
6669 */
67- const OUTPUT_JSON = 'j ' ;
70+ public const OUTPUT_JSON = 'j ' ;
6871
6972 /**
7073 * No output.
71- *
72- * @var string
7374 */
74- const OUTPUT_NOTHING = 'n ' ;
75+ public const OUTPUT_NOTHING = 'n ' ;
7576
7677 /**
7778 * Output parameter.
78- *
79- * @var string
8079 */
81- const PARAM_OUTPUT = 'o ' ;
80+ public const PARAM_OUTPUT = 'o ' ;
8281
8382 /**
8483 * Parameter for including tests.
85- *
86- * @var string
8784 */
88- const PARAM_INCLUDE = 't ' ;
85+ public const PARAM_INCLUDE = 't ' ;
8986
9087 /**
9188 * Parameter for excluding tests.
92- *
93- * @var string
9489 */
95- const PARAM_EXCLUDE = 'nt ' ;
90+ public const PARAM_EXCLUDE = 'nt ' ;
9691
9792 /**
9893 * Project name.
@@ -149,48 +144,60 @@ public function __construct(string $project, array $params = [])
149144 $ this ->setParams ($ params );
150145 }
151146
152- public function setParams (array $ params )
147+ public function setParams (array $ params ): void
153148 {
154149 // Filters
155150 if (!empty ($ params [self ::PARAM_INCLUDE ])) {
156151 $ this ->includeFilter = (string ) $ params [self ::PARAM_INCLUDE ];
157152 }
153+
158154 if (!empty ($ params [self ::PARAM_EXCLUDE ])) {
159155 $ this ->excludeFilter = (string ) $ params [self ::PARAM_EXCLUDE ];
160156 }
161157
162158 // Output type
163- if (!empty ($ params [self ::PARAM_OUTPUT ])) {
164- switch ($ params [self ::PARAM_OUTPUT ]) {
165- // Nothing
166- case self ::OUTPUT_NOTHING :
167- $ this ->output = self ::OUTPUT_NOTHING ;
168- break ;
169- // Plaintext
170- case self ::OUTPUT_TEXT :
171- $ this ->output = self ::OUTPUT_TEXT ;
172- break ;
173- // JSON
174- case self ::OUTPUT_JSON :
175- $ this ->output = self ::OUTPUT_JSON ;
176- break ;
177- // HTML
178- case self ::OUTPUT_HTML :
179- default :
180- $ this ->output = self ::OUTPUT_HTML ;
181- break ;
182- }
159+ if (empty ($ params [self ::PARAM_OUTPUT ])) {
160+ return ;
161+ }
162+
163+ switch ($ params [self ::PARAM_OUTPUT ]) {
164+ // Nothing
165+ case self ::OUTPUT_NOTHING :
166+ $ this ->output = self ::OUTPUT_NOTHING ;
167+
168+ break ;
169+
170+ // Plaintext
171+
172+ case self ::OUTPUT_TEXT :
173+ $ this ->output = self ::OUTPUT_TEXT ;
174+
175+ break ;
176+
177+ // JSON
178+
179+ case self ::OUTPUT_JSON :
180+ $ this ->output = self ::OUTPUT_JSON ;
181+
182+ break ;
183+
184+ // HTML
185+
186+ case self ::OUTPUT_HTML :
187+ default :
188+ $ this ->output = self ::OUTPUT_HTML ;
189+
190+ break ;
183191 }
184192 }
185193
186194 /**
187195 * Performs chosen tests and outputs results according to the selected output type.
188196 *
189197 * @param bool $print
190- *
191- * @return \Jyxo\Beholder\Output\Output
198+ * @return Output
192199 */
193- public function run ($ print = true ): Output
200+ public function run (bool $ print = true ): Output
194201 {
195202 // Filters tests
196203 foreach (array_keys ($ this ->tests ) as $ ident ) {
@@ -206,6 +213,7 @@ public function run($print = true): Output
206213 // Performs tests and gathers results
207214 $ order = 1 ;
208215 $ allSucceeded = true ;
216+
209217 foreach ($ idents as $ ident ) {
210218 // Runs a test
211219 $ data = $ this ->runTest ($ ident );
@@ -214,15 +222,18 @@ public function run($print = true): Output
214222 $ allSucceeded = $ allSucceeded && $ data ['result ' ]->isSuccess ();
215223
216224 // Adds the text into the output
217- $ data ['order ' ] = $ order ++;
225+ $ order ++;
226+ $ data ['order ' ] = $ order ;
218227 $ this ->testsData [] = $ data ;
219228 }
220229
221230 // Sorts tests according to their identifiers
222231 $ idents = [];
232+
223233 foreach ($ this ->testsData as $ key => $ data ) {
224234 $ idents [$ key ] = $ data ['ident ' ];
225235 }
236+
226237 array_multisort ($ idents , SORT_ASC , $ this ->testsData );
227238
228239 // Outputs the header
@@ -239,19 +250,29 @@ public function run($print = true): Output
239250 // No output
240251 case self ::OUTPUT_NOTHING :
241252 $ output = new NoOutput ($ result );
253+
242254 break ;
255+
243256 // Plaintext
257+
244258 case self ::OUTPUT_TEXT :
245259 $ output = new TextOutput ($ result );
260+
246261 break ;
262+
247263 // JSON
264+
248265 case self ::OUTPUT_JSON :
249266 $ output = new JsonOutput ($ result );
267+
250268 break ;
269+
251270 // HTML
271+
252272 case self ::OUTPUT_HTML :
253273 default :
254274 $ output = new HtmlOutput ($ result );
275+
255276 break ;
256277 }
257278
@@ -277,10 +298,10 @@ public function getTestsData(): array
277298 * Adds a test.
278299 *
279300 * @param string $ident Tests identifier
280- * @param \Jyxo\Beholder\ TestCase $test Test instance
281- * @return \Jyxo\Beholder\ Executor
301+ * @param TestCase $test Test instance
302+ * @return Executor
282303 */
283- public function addTest (string $ ident , \ Jyxo \ Beholder \ TestCase $ test ): self
304+ public function addTest (string $ ident , TestCase $ test ): self
284305 {
285306 $ this ->tests [$ ident ] = $ test ;
286307
@@ -292,42 +313,46 @@ public function addTest(string $ident, \Jyxo\Beholder\TestCase $test): self
292313 *
293314 * @param string $ident Test identifier
294315 * @return array
295- * @throws \UnexpectedValueException If the test returned an unknown result value
296316 */
297317 private function runTest (string $ ident ): array
298318 {
299319 // Runs the test
300- $ timer = \ Jyxo \ Timer::start ();
320+ $ timer = Timer::start ();
301321 $ result = $ this ->tests [$ ident ]->run ();
302- if (!($ result instanceof \Jyxo \Beholder \Result)) {
303- throw new \UnexpectedValueException (sprintf ('Result %s of the test %s is not a %s instance. ' , $ result , $ ident , \Jyxo \Beholder \Result::class));
322+
323+ if (!($ result instanceof Result)) {
324+ throw new UnexpectedValueException (
325+ sprintf ('Result %s of the test %s is not a %s instance. ' , $ result , $ ident , Result::class)
326+ );
304327 }
305328
306329 // Returns result data
307330 return [
308331 'ident ' => $ ident ,
309332 'test ' => $ this ->tests [$ ident ],
310333 'result ' => $ result ,
311- 'duration ' => \ Jyxo \ Timer::stop ($ timer )
334+ 'duration ' => Timer::stop ($ timer ),
312335 ];
313336 }
314337
315338 /**
316339 * Checks if the given test will be performed according to the current filter settings.
317340 *
318341 * @param string $ident Test identifier
319- * @return boolean
342+ * @return bool
320343 */
321344 private function includeTest (string $ ident ): bool
322345 {
323346 // If the test is not among the allowed ones, return false
324347 $ include = false ;
348+
325349 foreach (explode (', ' , $ this ->includeFilter ) as $ pattern ) {
326350 if ($ this ->patternMatch ($ pattern , $ ident )) {
327351 // We cannot use "return true" because the test might be disabled later
328352 $ include = true ;
329353 }
330354 }
355+
331356 if (!$ include ) {
332357 return false ;
333358 }
@@ -348,7 +373,7 @@ private function includeTest(string $ident): bool
348373 *
349374 * @param string $pattern Pattern
350375 * @param string $string String to be matched
351- * @return boolean
376+ * @return bool
352377 */
353378 private function patternMatch (string $ pattern , string $ string ): bool
354379 {
0 commit comments