-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileIterator.php
executable file
·185 lines (165 loc) · 5.25 KB
/
FileIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
namespace MaplePHP\Unitary;
use Closure;
use RuntimeException;
use MaplePHP\Blunder\Handlers\CliHandler;
use MaplePHP\Blunder\Run;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
class FileIterator
{
public const PATTERN = 'unitary-*.php';
private array $args;
public function __construct(array $args = [])
{
$this->args = $args;
}
/**
* Will Execute all unitary test files.
* @param string $directory
* @return void
* @throws RuntimeException
*/
public function executeAll(string $directory): void
{
$files = $this->findFiles($directory);
if (empty($files)) {
throw new RuntimeException("No files found matching the pattern \"" . (string)(static::PATTERN ?? "") . "\" in directory \"$directory\" ");
} else {
foreach ($files as $file) {
extract($this->args, EXTR_PREFIX_SAME, "wddx");
Unit::resetUnit();
Unit::setHeaders([
"args" => $this->args,
"file" => $file,
"checksum" => md5((string)$file)
]);
$call = $this->requireUnitFile((string)$file);
if (!is_null($call)) {
$call();
}
if(!Unit::hasUnit()) {
throw new RuntimeException("The Unitary Unit class has not been initiated inside \"$file\".");
}
}
Unit::completed();
}
}
/**
* Will Scan and find all unitary test files
* @param string $dir
* @return array
*/
private function findFiles(string $dir): array
{
$files = [];
$realDir = realpath($dir);
if($realDir === false) {
throw new RuntimeException("Directory \"$dir\" does not exist. Try using a absolut path!");
}
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
/** @var string $pattern */
$pattern = static::PATTERN;
foreach ($iterator as $file) {
if (($file instanceof SplFileInfo) && fnmatch($pattern, $file->getFilename()) &&
(isset($this->args['path']) || !str_contains($file->getPathname(), DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR))) {
if(!$this->findExcluded($this->exclude(), $dir, $file->getPathname())) {
$files[] = $file->getPathname();
}
}
}
return $files;
}
/**
* Get exclude parameter
* @return array
*/
public function exclude(): array
{
$excl = [];
if(isset($this->args['exclude']) && is_string($this->args['exclude'])) {
$exclude = explode(',', $this->args['exclude']);
foreach ($exclude as $file) {
$file = str_replace(['"', "'"], "", $file);
$new = trim($file);
$lastChar = substr($new, -1);
if($lastChar === DIRECTORY_SEPARATOR) {
$new .= "*";
}
$excl[] = trim($new);
}
}
return $excl;
}
/**
* Validate a exclude path
* @param array $exclArr
* @param string $relativeDir
* @param string $file
* @return bool
*/
public function findExcluded(array $exclArr, string $relativeDir, string $file): bool
{
$file = $this->getNaturalPath($file);
foreach ($exclArr as $excl) {
$relativeExclPath = $this->getNaturalPath($relativeDir . DIRECTORY_SEPARATOR . (string)$excl);
if(fnmatch($relativeExclPath, $file)) {
return true;
}
}
return false;
}
/**
* Get path as natural path
* @param string $path
* @return string
*/
public function getNaturalPath(string $path): string
{
return str_replace("\\", "/", $path);
}
/**
* Require file without inheriting any class information
* @param string $file
* @return Closure|null
*/
private function requireUnitFile(string $file): ?Closure
{
$clone = clone $this;
$call = function () use ($file, $clone): void {
$cli = new CliHandler();
if(isset(self::$headers['args']['trace'])) {
$cli->enableTraceLines(true);
}
$run = new Run($cli);
$run->load();
//ob_start();
if (!is_file($file)) {
throw new RuntimeException("File \"$file\" do not exists.");
}
require_once($file);
$clone->getUnit()->execute();
/*
$outputBuffer = ob_get_clean();
if (strlen($outputBuffer) && Unit::hasUnit()) {
$clone->getUnit()->buildNotice("Note:", $outputBuffer, 80);
}
*/
};
return $call->bindTo(null);
}
/**
* @return Unit
* @throws RuntimeException|\Exception
*/
protected function getUnit(): Unit
{
$unit = Unit::getUnit();
if (is_null($unit)) {
throw new RuntimeException("The Unit instance has not been initiated.");
}
return $unit;
}
}