-
Notifications
You must be signed in to change notification settings - Fork 56
/
spec.php
324 lines (272 loc) · 11 KB
/
spec.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* @name SimpleSpec
* PHP SimpleTest extension for Behavior driven development(BDD)
*
* why not PHPSpec? well its a good project but SimpleTest has better coverage for testing
* and i want better grammer
*
* Features:
* - Reuse SimpleTest framework
* - simplly one include from away
* - Underscore for readibility - all examples uses underscore to seperate descriptions.
* - Natural language grammer
* - before_all, after_all is not supported, i don't want to modify SimpleTest
* - conventional before/after named prepare/cleanup respectively
*
* @example
* class Describe_his_mum extends SimpleSpec {
* function prepare() {
* $this->mum = new Mum(array('mood'=>'angry'));
* }
*
* function should_be_very_angry_when_i_break_my_lunch_box() {
* expects($this->mum->mood)->should_be('angry');
* }
*
* function should_be_very_happy_when_i_punch_her_in_the_face() {
* punch($this->mum);
* expects($this->mum->mood)->should_be('happy');
* }
*
* function cleanup() {
* unset($this->mum); // kill da mum
* }
* }
* @author - Taylor Luk aka 'speedmax'
* @license Free for all
*/
class SimpleSpec extends UnitTestCase {
public $target;
private $negate;
private $matcher;
function __construct($label = false) {
if (! $label) {
$label = str_replace(array('Describe_', '_'), array('', ' '), get_class($this));
}
$this->matcher = new SpecMatcher($this);
parent::__construct($label);
}
function _isTest($method) {
if (strtolower(substr($method, 0, 2)) == 'it' || strtolower(substr($method, 0, 6)) == 'should') {
return ! SimpleTestCompatibility::isA($this, strtolower($method));
}
return false;
}
function prepare() {
}
function cleanup() {
}
function setUp() {
$this->prepare();
}
function tearDown() {
$this->cleanup();
}
function __call($name, $args) {
$matcher = null;
$this->matcher->negate(false);
array_unshift($args, $this->target);
if (preg_match('/should_not_(.*)/', $name, $match)) {
$matcher = $match[1];
$this->matcher->negate(true);
}
elseif (preg_match('/should_(.*)/', $name, $match)) {
$matcher = $match[1];
}
if (!method_exists($this->matcher, $matcher)) {
throw new Exception("matcher doesn't exist");
} else {
call_user_func_array(array($this->matcher, $matcher), $args);
}
}
function offsetGet($object) {
return $this->expect($object);
}
function offsetSet($key, $value) {}
function offsetExists($key) {}
function offsetUnset($key) {}
function expect($object) {
$this->target = $object;
return $this;
}
function value_of($object) {
return $this->expect($object);
}
function assert(&$expectation, $compare, $message = '%s') {
$result = $expectation->test($compare);
if ($this->matcher->negate) {
$result = !$result;
}
if ($result) {
return $this->pass(sprintf($message,$expectation->overlayMessage($compare, $this->_reporter->getDumper())));
} else {
return $this->fail(sprintf($message,$expectation->overlayMessage($compare, $this->_reporter->getDumper())));
}
}
/**
* Uses a stack trace to find the line of an assertion.
* @return string Line number of first assert*
* method embedded in format string.
* @access public
*/
function getAssertionLine() {
$trace = new SimpleStackTrace(array('should', 'it_should', 'assert', 'expect', 'pass', 'fail', 'skip'));
return $trace->traceMethod();
}
}
function expects($subject) {
$trace = debug_backtrace();
$object = $trace[1]['object'];
return $object->expect($subject);
}
class Have_Matcher {
function __construct($subject, $count, $runtime) {
$this->subject = $subject;
$this->count = $count;
$this->runtime = $this->runtime;
}
function __get($key) {
$object = $runtime->target;
if (is_array($object) && isset($object[$this->subject]))
$subject = $object[$this->subject];
elseif (is_object($object) && isset($object->{$this->subject}))
$subject = $object->{$this->subject};
return $this->runtime->be(count($subject), $this->count);
}
}
class SpecMatcher {
private $tester;
public $negate;
function __construct($runtime) {
$this->runtime = $runtime;
}
function negate($bool = false) {
$this->negate = $bool;
}
function be($first, $second, $message = '%s') {
return $this->runtime->assert(new EqualExpectation($first), $second, $message);
}
function be_equal($first, $second, $message = '%s') {
return $this->be($first, $second, $message);
}
function be_empty($subject, $message = '%s') {
$dumper = new SimpleDumper();
return $this->be_true(empty($subject) == true, "[{$dumper->describeValue($subject)}] should be empty");
}
function be_true($result, $message = '%s') {
return $this->runtime->assert(new TrueExpectation(), $result, $message);
}
function be_false($result, $message = '%s') {
return $this->runtime->assert(new FalseExpectation(), $result, $message);
}
function be_null($value, $message = '%s') {
$dumper = new SimpleDumper();
$message = sprintf($message, '[' . $dumper->describeValue($value) . '] should be null');
return $this->runtime->assert(new TrueExpectation(), ! isset($value), $message);
}
function be_a($object, $type, $message = '%s') {
if (strtolower($type) == 'object')
$type = 'stdClass';
return $this->runtime->assert(new IsAExpectation($type),$object, $message);
}
function be_an($object, $type, $message = '%s') {
return $this->be_a($object, $type, $message);
}
function be_within_margin($first, $second, $margin, $message = '%s') {
return $this->runtime->assert(
new WithinMarginExpectation($first, $margin),
$second,
$message);
}
function be_outside_margin($first, $second, $margin, $message = '%s') {
return $this->runtime->assert(
new assertOutsideMargin($first, $margin),
$second,
$message);
}
function be_identical($first, $second, $message = '%s') {
return $this->runtime->assert(
new IdenticalExpectation($first),
$second,
$message);
}
function be_reference_of(&$first, &$second, $message = '%s') {
$dumper = new SimpleDumper();
$message = sprintf(
$message,
'[' . $dumper->describeValue($first) .
'] and [' . $dumper->describeValue($second) .
'] should reference the same object');
return $this->runtime->assert(new TrueExpectation(), SimpleTestCompatibility::isReference($first, $second), $message);
}
function be_clone_of(&$first, &$second, $message = '%s') {
$dumper = new SimpleDumper();
$message = sprintf(
$message,
'[' . $dumper->describeValue($first) .
'] and [' . $dumper->describeValue($second) .
'] should not be the same object');
$identical = new IdenticalExpectation($first);
return $this->runtime->assert(new TrueExpectation(),
$identical->test($second) && ! SimpleTestCompatibility::isReference($first, $second),
$message);
}
function be_copy_of(&$first, &$second, $message = '%s') {
$dumper = new SimpleDumper();
$message = sprintf(
$message,
"[" . $dumper->describeValue($first) .
"] and [" . $dumper->describeValue($second) .
"] should not be the same object");
return $this->runtime->assert(new FaseExpectation(),
SimpleTestCompatibility::isReference($first, $second),
$message);
}
function contain($subject, $target, $message = '%s') {
$dumper = new SimpleDumper();
$message = "[ {$dumper->describeValue($subject)}] should contain [{$dumper->describeValue($target)}]";
if (is_array($subject) && is_array($target)) {
return $this->be_true(array_intersect($target, $subject) == $target, $message);
} elseif (is_array($subject)) {
return $this->be_true(in_array($target, $subject), $message);
} elseif (is_string($subject)) {
return $this->be_true(strpos($target, $subject) !== false, $message);
}
}
function have($target, $count, $key, $messages = '%s') {
$dumper = new SimpleDumper();
$subject = null;
if (is_array($target) && isset($target[$key]))
$subject = $target[$key];
elseif (is_object($target) && isset($target->$key))
$subject = $target->$key;
$result = count($subject);
$messages = "Expecting count for [$key] should be [$count], got [$result]";
return $this->be($result, $count, $messages);
}
function match($subject, $pattern, $message = '%s') {
$regex = "/^[\/{#](.*)[\/}#][imsxeADSUXJu]*/sm";
if (preg_match($regex, $subject)) {
list($subject, $pattern) = array($pattern, $subject);
}
return $this->runtime->assert(
new PatternExpectation($pattern),
$subject,
$message);
}
function expect_error($message = '%s') {
$context = &SimpleTest::getContext();
$queue = &$context->get('SimpleErrorQueue');
$queue->expectError($this->runtime->_coerceExpectation($this->negate), $message);
}
function expect_exception($message = '%s') {
$context = &SimpleTest::getContext();
$queue = &$context->get('SimpleExceptionTrap');
// :HACK: Directly substituting in seems to cause a segfault with
// Zend Optimizer on some systems
$line = $this->runtime->getAssertionLine();
$queue->expectException($this->negate, $message . $line);
}
}
?>