1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MyTester; |
5
|
|
|
|
6
|
|
|
use MyTester\Annotations\Reader; |
7
|
|
|
use Nette\Utils\ArrayHash; |
8
|
|
|
use Nette\Utils\Arrays; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* SkipChecker |
12
|
|
|
* |
13
|
|
|
* @author Jakub Konečný |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
final class SkipChecker { |
17
|
|
|
use \Nette\SmartObject; |
18
|
|
|
|
19
|
|
|
public const ANNOTATION_NAME = "skip"; |
20
|
|
|
|
21
|
|
|
private Reader $annotationsReader; |
22
|
|
|
/** @var callable[] */ |
23
|
|
|
private array $checkers = []; |
24
|
|
|
|
25
|
|
|
public function __construct(Reader $annotationsReader) { |
26
|
|
|
$this->annotationsReader = $annotationsReader; |
27
|
|
|
$this->addDefaultCheckers(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function addDefaultCheckers(): void { |
31
|
|
|
$this->addChecker("php", [$this, "checkPhpVersion"]); |
32
|
|
|
$this->addChecker("extension", [$this, "checkLoadedExtension"]); |
33
|
|
|
$this->addChecker("sapi", [$this, "checkPhpSapi"]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function addChecker(string $name, callable $callback): void { |
37
|
|
|
$this->checkers[$name] = $callback; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function getSkipValue(string $class, string $method) { |
44
|
|
|
return $this->annotationsReader->getAnnotation(static::ANNOTATION_NAME, $class, $method); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Check whether to skip a test method |
49
|
|
|
* |
50
|
|
|
* @return bool|string |
51
|
|
|
*/ |
52
|
|
|
public function shouldSkip(string $class, string $method) { |
53
|
|
|
$value = $this->getSkipValue($class, $method); |
54
|
|
|
if($value === null) { |
55
|
|
|
return false; |
56
|
|
|
} elseif(is_scalar($value)) { |
57
|
|
|
return (bool) $value; |
58
|
|
|
} elseif($value instanceof ArrayHash) { |
59
|
|
|
foreach($value as $k => $v) { |
60
|
|
|
$checker = Arrays::get($this->checkers, $k, null); |
61
|
|
|
if($checker === null) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
return $checker($v); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param mixed $value |
72
|
|
|
*/ |
73
|
|
|
public function checkPhpVersion($value): ?string { |
74
|
|
|
if(version_compare(PHP_VERSION, (string) $value, "<")) { |
75
|
|
|
return "PHP version is lesser than $value"; |
76
|
|
|
} |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param mixed $value |
82
|
|
|
*/ |
83
|
|
|
public function checkLoadedExtension($value): ?string { |
84
|
|
|
if(!extension_loaded($value)) { |
85
|
|
|
return "extension $value is not loaded"; |
86
|
|
|
} |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $value |
92
|
|
|
*/ |
93
|
|
|
public function checkPhpSapi($value): ?string { |
94
|
|
|
if(PHP_SAPI != $value) { |
95
|
|
|
return "the sapi is not $value"; |
96
|
|
|
} |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
?> |