forked from anthonybishopric/php-inferno
/
Pathway_Through_Darkness.php
234 lines (206 loc) · 4.17 KB
/
Pathway_Through_Darkness.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
require_once 'assertions.php';
class Pathway_Through_Darkness
{
private $circles = array();
public function __construct(array $classes)
{
foreach ($classes as $class)
{
require_once "trials/$class.php";
$circle = new Circle($class);
$rfl_class = new ReflectionClass($class);
foreach ($rfl_class->getMethods() as $method)
{
if ($method->isPublic())
{
$circle->add_trial($this->build_trial($rfl_class, $method));
}
}
$this->circles[] = $circle;
}
}
public function descend_match($circle_pattern, $trial_pattern)
{
foreach ($this->circles as $circle)
{
if (strpos($circle->name(), $circle_pattern) !== false)
{
foreach ($circle->trials() as $trial)
{
if (strpos($trial->name(), $trial_pattern) !== false)
{
$this->run_trial($trial);
}
}
}
}
echo "Thence we came forth to rebehold the stars.\n";
}
public function descend()
{
foreach ($this->circles as $circle)
{
foreach ($circle->trials() as $trial)
{
$this->run_trial($trial);
}
}
echo "Thence we came forth to rebehold the stars.\n";
}
private function run_trial(Trial $trial)
{
try
{
$trial->run();
}
catch (Exception $e)
{
$this->print_total();
$this->print_message($trial, $e);
exit(1);
}
}
private function build_trial(ReflectionClass $rfl_class, ReflectionMethod $method)
{
return new Trial(
sprintf("In the circle of %s, %s", $rfl_class->name, $method->name),
function() use ($rfl_class, $method)
{
$instance = $rfl_class->newInstance();
$name = $method->name;
$this->call_with_aspects($method, function() use ($instance, $name)
{
$instance->$name();
});
}
);
}
private function call_with_aspects(ReflectionMethod $method, callable $fn)
{
$comment = $method->getDocComment();
$matches = [];
preg_match_all('/@([a-zA-Z\_]+)/', $comment, $matches);
if (count($matches) > 1 && in_array('suppress_warnings', $matches[1]))
{
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE ^ E_STRICT);
}
$fn();
error_reporting(E_ALL);
}
private function print_total()
{
$total = 0;
$completed = 0;
foreach ($this->circles as $circle)
{
$total+= count($circle);
$completed += $circle->count_of_completed();
echo $circle->completion_string() . "\n";
}
echo "\n\033[33m$completed / $total trials conquered\033[0m\n";
}
private function print_message(Trial $trial, Exception $e)
{
$frame = $e->getTrace()[0];
$file = $frame['file'];
$line_number = $frame['line'];
$failed_test_with_message = $trial->name . ":\n" . $file. ":" .$line_number . "\n" . $e->getMessage();
echo "\033[36m" . $failed_test_with_message . "\033[0m\n";
}
}
class Circle implements Countable
{
private $name = null;
private $trials = array();
public function __construct($name)
{
$this->name = $name;
}
public function name()
{
return $this->name;
}
public function add_trial(Trial $trial)
{
$this->trials[] = $trial;
}
public function trials()
{
return $this->trials;
}
public function count()
{
return count($this->trials);
}
public function complete()
{
return $this->count() == $this->count_of_completed();
}
public function count_of_completed()
{
$completed = 0;
foreach ($this->trials as $trial)
{
$completed += $trial->complete() ? 1 : 0;
}
return $completed;
}
public function completion_string()
{
$str = '';
foreach ($this->trials as $trial)
{
$str .= $trial->status_char();
}
return $this->name . "\n$str";
}
}
class Trial
{
public function __construct($name, callable $fn)
{
$this->name = $name;
$this->fn = $fn;
$this->completed = false;
$this->failed = false;
}
public function run()
{
try
{
$fn = $this->fn;
$fn();
$this->completed = true;
}
catch(Exception $e)
{
$this->failed = true;
throw $e;
}
}
public function complete()
{
return $this->completed;
}
public function name()
{
return $this->name;
}
public function status_char()
{
if ($this->completed)
{
return "\033[32m✞\033[0m";
}
else if($this->failed)
{
return "\033[31m♆\033[0m";
}
else
{
return ".";
}
}
}
define('__', 'FILL ME IN');