1
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.'); |
2
|
|
|
/** |
3
|
|
|
* Unit_Test library. |
4
|
|
|
* |
5
|
|
|
* $Id: Unit_Test.php 4367 2009-05-27 21:23:57Z samsoir $ |
6
|
|
|
* |
7
|
|
|
* @package Unit_Test |
8
|
|
|
* @author Kohana Team |
9
|
|
|
* @copyright (c) 2007-2008 Kohana Team |
10
|
|
|
* @license http://kohanaphp.com/license.html |
11
|
|
|
*/ |
12
|
|
|
class Unit_Test_Core |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
// The path(s) to recursively scan for tests |
16
|
|
|
protected $paths = array(); |
17
|
|
|
|
18
|
|
|
// The results of all tests from every test class |
19
|
|
|
protected $results = array(); |
20
|
|
|
|
21
|
|
|
// Statistics for every test class |
22
|
|
|
protected $stats = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Sets the test path(s), runs the tests inside and stores the results. |
26
|
|
|
* |
27
|
|
|
* @param string(s) test path(s) |
28
|
|
|
* @return void |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
// Merge possible default test path(s) from config with the rest |
33
|
|
|
$paths = array_merge(func_get_args(), Kohana::config('unit_test.paths', false, false)); |
34
|
|
|
|
35
|
|
|
// Normalize all test paths |
36
|
|
|
foreach ($paths as $path) { |
37
|
|
|
$path = str_replace('\\', '/', realpath((string) $path)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Take out duplicate test paths after normalization |
41
|
|
|
$this->paths = array_unique($paths); |
42
|
|
|
|
43
|
|
|
// Loop over each given test path |
44
|
|
|
foreach ($this->paths as $path) { |
45
|
|
|
// Validate test path |
46
|
|
|
if (! is_dir($path)) { |
47
|
|
|
throw new Kohana_Exception('unit_test.invalid_test_path', $path); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Recursively iterate over each file in the test path |
51
|
|
|
foreach ( |
52
|
|
|
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME)) |
53
|
|
|
as $path => $file |
54
|
|
|
) { |
55
|
|
|
// Normalize path |
56
|
|
|
$path = str_replace('\\', '/', $path); |
57
|
|
|
|
58
|
|
|
// Skip files without "_Test" suffix |
59
|
|
|
if (! $file->isFile() or substr($path, -9) !== '_Test'.EXT) { |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// The class name should be the same as the file name |
64
|
|
|
$class = substr($path, strrpos($path, '/') + 1, -(strlen(EXT))); |
65
|
|
|
|
66
|
|
|
// Skip hidden files |
67
|
|
|
if ($class[0] === '.') { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Check for duplicate test class name |
72
|
|
|
if (class_exists($class, false)) { |
73
|
|
|
throw new Kohana_Exception('unit_test.duplicate_test_class', $class, $path); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Include the test class |
77
|
|
|
include_once $path; |
78
|
|
|
|
79
|
|
|
// Check whether the test class has been found and loaded |
80
|
|
|
if (! class_exists($class, false)) { |
81
|
|
|
throw new Kohana_Exception('unit_test.test_class_not_found', $class, $path); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Reverse-engineer Test class |
85
|
|
|
$reflector = new ReflectionClass($class); |
86
|
|
|
|
87
|
|
|
// Test classes must extend Unit_Test_Case |
88
|
|
|
if (! $reflector->isSubclassOf(new ReflectionClass('Unit_Test_Case'))) { |
89
|
|
|
throw new Kohana_Exception('unit_test.test_class_extends', $class); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Skip disabled Tests |
93
|
|
|
if ($reflector->getConstant('DISABLED') === true) { |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Initialize setup and teardown method triggers |
98
|
|
|
$setup = $teardown = false; |
99
|
|
|
|
100
|
|
|
// Look for valid setup and teardown methods |
101
|
|
|
foreach (array('setup', 'teardown') as $method_name) { |
102
|
|
|
if ($reflector->hasMethod($method_name)) { |
103
|
|
|
$method = new ReflectionMethod($class, $method_name); |
104
|
|
|
$$method_name = ($method->isPublic() and ! $method->isStatic() and $method->getNumberOfRequiredParameters() === 0); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Initialize test class results and stats |
109
|
|
|
$this->results[$class] = array(); |
110
|
|
|
$this->stats[$class] = array( |
111
|
|
|
'passed' => 0, |
112
|
|
|
'failed' => 0, |
113
|
|
|
'errors' => 0, |
114
|
|
|
'total' => 0, |
115
|
|
|
'score' => 0, |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
// Loop through all the class methods |
119
|
|
|
foreach ($reflector->getMethods() as $method) { |
120
|
|
|
// Skip invalid test methods |
121
|
|
|
if (! $method->isPublic() or $method->isStatic() or $method->getNumberOfRequiredParameters() !== 0) { |
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Test methods should be suffixed with "_test" |
126
|
|
|
if (substr($method_name = $method->getName(), -5) !== '_test') { |
|
|
|
|
127
|
|
|
continue; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Instantiate Test class |
131
|
|
|
$object = new $class; |
132
|
|
|
|
133
|
|
|
try { |
134
|
|
|
// Run setup method |
135
|
|
|
if ($setup === true) { |
136
|
|
|
$object->setup(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Run the actual test |
140
|
|
|
$object->$method_name(); |
141
|
|
|
|
142
|
|
|
// Run teardown method |
143
|
|
|
if ($teardown === true) { |
144
|
|
|
$object->teardown(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->stats[$class]['total']++; |
148
|
|
|
|
149
|
|
|
// Test passed |
150
|
|
|
$this->results[$class][$method_name] = true; |
151
|
|
|
$this->stats[$class]['passed']++; |
152
|
|
|
} catch (Kohana_Unit_Test_Exception $e) { |
153
|
|
|
$this->stats[$class]['total']++; |
154
|
|
|
// Test failed |
155
|
|
|
$this->results[$class][$method_name] = $e; |
156
|
|
|
$this->stats[$class]['failed']++; |
157
|
|
|
} catch (Exception $e) { |
158
|
|
|
$this->stats[$class]['total']++; |
159
|
|
|
|
160
|
|
|
// Test error |
161
|
|
|
$this->results[$class][$method_name] = $e; |
162
|
|
|
$this->stats[$class]['errors']++; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// Calculate score |
166
|
|
|
$this->stats[$class]['score'] = $this->stats[$class]['passed'] * 100 / $this->stats[$class]['total']; |
167
|
|
|
|
168
|
|
|
// Cleanup |
169
|
|
|
unset($object); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Generates nice test results. |
177
|
|
|
* |
178
|
|
|
* @param boolean hide passed tests from the report |
179
|
|
|
* @return string rendered test results html |
180
|
|
|
*/ |
181
|
|
|
public function report($hide_passed = null) |
182
|
|
|
{ |
183
|
|
|
// No tests found |
184
|
|
|
if (empty($this->results)) { |
185
|
|
|
return Kohana::lang('unit_test.no_tests_found'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// Hide passed tests from the report? |
189
|
|
|
$hide_passed = (bool) (($hide_passed !== null) ? $hide_passed : Kohana::config('unit_test.hide_passed', false, false)); |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
if (PHP_SAPI == 'cli') { |
193
|
|
|
$report = View::factory('kohana_unit_test_cli'); |
194
|
|
|
} else { |
195
|
|
|
$report = View::factory('kohana_unit_test'); |
196
|
|
|
} |
197
|
|
|
// Render unit_test report |
198
|
|
|
return $report->set('results', $this->results) |
199
|
|
|
->set('stats', $this->stats) |
200
|
|
|
->set('hide_passed', $hide_passed) |
201
|
|
|
->render(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Magically convert this object to a string. |
206
|
|
|
* |
207
|
|
|
* @return string test report |
208
|
|
|
*/ |
209
|
|
|
public function __toString() |
210
|
|
|
{ |
211
|
|
|
return $this->report(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Magically gets a Unit_Test property. |
216
|
|
|
* |
217
|
|
|
* @param string property name |
218
|
|
|
* @return mixed variable value if the property is found |
219
|
|
|
* @return void if the property is not found |
220
|
|
|
*/ |
221
|
|
|
public function __get($key) |
222
|
|
|
{ |
223
|
|
|
if (isset($this->$key)) { |
224
|
|
|
return $this->$key; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} // End Unit_Test_Core |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
abstract class Unit_Test_Case |
231
|
|
|
{ |
232
|
|
|
/** |
233
|
|
|
* @param boolean $value |
234
|
|
|
* @param integer $debug |
|
|
|
|
235
|
|
|
*/ |
236
|
|
View Code Duplication |
public function assert_true($value, $debug = null) |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
if ($value != true) { |
|
|
|
|
239
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_true', gettype($value), var_export($value, true)), $debug); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
View Code Duplication |
public function assert_true_strict($value, $debug = null) |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
if ($value !== true) { |
248
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_true_strict', gettype($value), var_export($value, true)), $debug); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param boolean $value |
256
|
|
|
*/ |
257
|
|
View Code Duplication |
public function assert_false($value, $debug = null) |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
if ($value != false) { |
|
|
|
|
260
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_false', gettype($value), var_export($value, true)), $debug); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
View Code Duplication |
public function assert_false_strict($value, $debug = null) |
|
|
|
|
267
|
|
|
{ |
268
|
|
|
if ($value !== false) { |
269
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_false_strict', gettype($value), var_export($value, true)), $debug); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param string $expected |
277
|
|
|
* @param integer $actual |
278
|
|
|
*/ |
279
|
|
View Code Duplication |
public function assert_equal($expected, $actual, $debug = null) |
|
|
|
|
280
|
|
|
{ |
281
|
|
|
if ($expected != $actual) { |
282
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_equal', gettype($expected), var_export($expected, true), gettype($actual), var_export($actual, true)), $debug); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param string $expected |
290
|
|
|
* @param integer $actual |
291
|
|
|
*/ |
292
|
|
View Code Duplication |
public function assert_not_equal($expected, $actual, $debug = null) |
|
|
|
|
293
|
|
|
{ |
294
|
|
|
if ($expected == $actual) { |
295
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_equal', gettype($expected), var_export($expected, true), gettype($actual), var_export($actual, true)), $debug); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param string $expected |
303
|
|
|
* @param string $actual |
304
|
|
|
*/ |
305
|
|
View Code Duplication |
public function assert_same($expected, $actual, $debug = null) |
|
|
|
|
306
|
|
|
{ |
307
|
|
|
if ($expected !== $actual) { |
308
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_same', gettype($expected), var_export($expected, true), gettype($actual), var_export($actual, true)), $debug); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return $this; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param string $expected |
316
|
|
|
* @param integer $actual |
317
|
|
|
*/ |
318
|
|
View Code Duplication |
public function assert_not_same($expected, $actual, $debug = null) |
|
|
|
|
319
|
|
|
{ |
320
|
|
|
if ($expected === $actual) { |
|
|
|
|
321
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_same', gettype($expected), var_export($expected, true), gettype($actual), var_export($actual, true)), $debug); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return $this; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @param boolean $value |
329
|
|
|
*/ |
330
|
|
View Code Duplication |
public function assert_boolean($value, $debug = null) |
|
|
|
|
331
|
|
|
{ |
332
|
|
|
if (! is_bool($value)) { |
333
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_boolean', gettype($value), var_export($value, true)), $debug); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return $this; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @param string $value |
341
|
|
|
*/ |
342
|
|
View Code Duplication |
public function assert_not_boolean($value, $debug = null) |
|
|
|
|
343
|
|
|
{ |
344
|
|
|
if (is_bool($value)) { |
345
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_boolean', gettype($value), var_export($value, true)), $debug); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
return $this; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param integer $value |
353
|
|
|
*/ |
354
|
|
View Code Duplication |
public function assert_integer($value, $debug = null) |
|
|
|
|
355
|
|
|
{ |
356
|
|
|
if (! is_int($value)) { |
357
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_integer', gettype($value), var_export($value, true)), $debug); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return $this; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @param string $value |
365
|
|
|
*/ |
366
|
|
View Code Duplication |
public function assert_not_integer($value, $debug = null) |
|
|
|
|
367
|
|
|
{ |
368
|
|
|
if (is_int($value)) { |
369
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_integer', gettype($value), var_export($value, true)), $debug); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
return $this; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @param double $value |
377
|
|
|
*/ |
378
|
|
View Code Duplication |
public function assert_float($value, $debug = null) |
|
|
|
|
379
|
|
|
{ |
380
|
|
|
if (! is_float($value)) { |
381
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_float', gettype($value), var_export($value, true)), $debug); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return $this; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @param integer $value |
389
|
|
|
*/ |
390
|
|
View Code Duplication |
public function assert_not_float($value, $debug = null) |
|
|
|
|
391
|
|
|
{ |
392
|
|
|
if (is_float($value)) { |
393
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_float', gettype($value), var_export($value, true)), $debug); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
return $this; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @param integer[] $value |
401
|
|
|
*/ |
402
|
|
View Code Duplication |
public function assert_array($value, $debug = null) |
|
|
|
|
403
|
|
|
{ |
404
|
|
|
if (! is_array($value)) { |
405
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_array', gettype($value), var_export($value, true)), $debug); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
return $this; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @param string $key |
413
|
|
|
*/ |
414
|
|
View Code Duplication |
public function assert_array_key($key, $array, $debug = null) |
|
|
|
|
415
|
|
|
{ |
416
|
|
|
if (! array_key_exists($key, $array)) { |
417
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_array_key', gettype($key), var_export($key, true)), $debug); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
return $this; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @param string $value |
425
|
|
|
* @param string[] $array |
426
|
|
|
*/ |
427
|
|
View Code Duplication |
public function assert_in_array($value, $array, $debug = null) |
|
|
|
|
428
|
|
|
{ |
429
|
|
|
if (! in_array($value, $array)) { |
430
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_in_array', gettype($value), var_export($value, true)), $debug); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
return $this; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* @param string $value |
438
|
|
|
*/ |
439
|
|
View Code Duplication |
public function assert_not_array($value, $debug = null) |
|
|
|
|
440
|
|
|
{ |
441
|
|
|
if (is_array($value)) { |
442
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_array', gettype($value), var_export($value, true)), $debug); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
return $this; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* @param stdClass $value |
450
|
|
|
*/ |
451
|
|
View Code Duplication |
public function assert_object($value, $debug = null) |
|
|
|
|
452
|
|
|
{ |
453
|
|
|
if (! is_object($value)) { |
454
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_object', gettype($value), var_export($value, true)), $debug); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
return $this; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* @param string $value |
462
|
|
|
*/ |
463
|
|
View Code Duplication |
public function assert_not_object($value, $debug = null) |
|
|
|
|
464
|
|
|
{ |
465
|
|
|
if (is_object($value)) { |
466
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_object', gettype($value), var_export($value, true)), $debug); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
return $this; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
View Code Duplication |
public function assert_null($value, $debug = null) |
|
|
|
|
473
|
|
|
{ |
474
|
|
|
if ($value !== null) { |
475
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_null', gettype($value), var_export($value, true)), $debug); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
return $this; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* @param integer $value |
483
|
|
|
*/ |
484
|
|
View Code Duplication |
public function assert_not_null($value, $debug = null) |
|
|
|
|
485
|
|
|
{ |
486
|
|
|
if ($value === null) { |
487
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_null', gettype($value), var_export($value, true)), $debug); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
return $this; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* @param string $value |
495
|
|
|
*/ |
496
|
|
View Code Duplication |
public function assert_empty($value, $debug = null) |
|
|
|
|
497
|
|
|
{ |
498
|
|
|
if (! empty($value)) { |
499
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_empty', gettype($value), var_export($value, true)), $debug); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
return $this; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* @param string $value |
507
|
|
|
*/ |
508
|
|
View Code Duplication |
public function assert_not_empty($value, $debug = null) |
|
|
|
|
509
|
|
|
{ |
510
|
|
|
if (empty($value)) { |
511
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_empty', gettype($value), var_export($value, true)), $debug); |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
return $this; |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* @param string $value |
519
|
|
|
* @param string $regex |
520
|
|
|
*/ |
521
|
|
View Code Duplication |
public function assert_pattern($value, $regex, $debug = null) |
|
|
|
|
522
|
|
|
{ |
523
|
|
|
if (! is_string($value) or ! is_string($regex) or ! preg_match($regex, $value)) { |
524
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_pattern', var_export($value, true), var_export($regex, true)), $debug); |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
return $this; |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* @param string $value |
532
|
|
|
* @param string $regex |
533
|
|
|
*/ |
534
|
|
View Code Duplication |
public function assert_not_pattern($value, $regex, $debug = null) |
|
|
|
|
535
|
|
|
{ |
536
|
|
|
if (! is_string($value) or ! is_string($regex) or preg_match($regex, $value)) { |
537
|
|
|
throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_pattern', var_export($value, true), var_export($regex, true)), $debug); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
return $this; |
541
|
|
|
} |
542
|
|
|
} // End Unit_Test_Case |
543
|
|
|
|
544
|
|
|
|
545
|
|
|
class Kohana_Unit_Test_Exception extends Exception |
546
|
|
|
{ |
547
|
|
|
protected $debug = null; |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* Sets exception message and debug info. |
551
|
|
|
* |
552
|
|
|
* @param string message |
553
|
|
|
* @param mixed debug info |
554
|
|
|
* @param string $message |
555
|
|
|
* @return void |
|
|
|
|
556
|
|
|
*/ |
557
|
|
|
public function __construct($message, $debug = null) |
558
|
|
|
{ |
559
|
|
|
// Failure message |
560
|
|
|
parent::__construct((string) $message); |
561
|
|
|
|
562
|
|
|
// Extra user-defined debug info |
563
|
|
|
$this->debug = $debug; |
564
|
|
|
|
565
|
|
|
// Overwrite failure location |
566
|
|
|
$trace = $this->getTrace(); |
567
|
|
|
$this->file = $trace[0]['file']; |
568
|
|
|
$this->line = $trace[0]['line']; |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Returns the user-defined debug info |
573
|
|
|
* |
574
|
|
|
* @return mixed debug property |
575
|
|
|
*/ |
576
|
|
|
public function getDebug() |
577
|
|
|
{ |
578
|
|
|
return $this->debug; |
579
|
|
|
} |
580
|
|
|
} // End Kohana_Unit_Test_Exception |
581
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.