-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
CakeTestCase.php
526 lines (490 loc) · 14.3 KB
/
CakeTestCase.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<?php
/**
* CakeTestCase file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package cake
* @subpackage cake.cake.tests.libs
* @since CakePHP(tm) v 1.2.0.4667
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT');
App::uses('CakeFixtureManager', 'TestSuite/Fixture');
App::uses('CakeTestFixture', 'TestSuite/Fixture');
/**
* CakeTestCase class
*
* @package cake
* @subpackage cake.cake.tests.lib
*/
abstract class CakeTestCase extends PHPUnit_Framework_TestCase {
/**
* The class responsible for managinf the creation, loading and removing of fixtures
*
* @var CakeFixtureManager
* @access public
*/
public $fixtureManager = null;
/**
* By default, all fixtures attached to this class will be truncated and reloaded after each test.
* Set this to false to handle manually
*
* @var array
* @access public
*/
public $autoFixtures = true;
/**
* Set this to false to avoid tables to be dropped if they already exist
*
* @var boolean
* @access public
*/
public $dropTables = true;
/**
* The fixtures to be loaded in this test case. Fixtures are referenced using a dot notation:
* - fixture_name : A fixtures that can be found in the main app folder and is named FixtureNameFixture
* - core.fixture_name : A fixtures that can be found in the cake core folder and is named FixtureNameFixture
* - plugin.plugin_name.fixture_name : A fixtures that can be found in the plugin "plugin_name" folder and is named FixtureNameFixture
*
* @var array
* @access public
*/
private $fixtures = array();
/**
* Configure values to restore at end of test.
*
* @var array
*/
protected $_configure = array();
/**
* Runs the test case and collects the results in a TestResult object.
* If no TestResult object is passed a new one will be created.
* This method is run for each test method in this class
*
* @param PHPUnit_Framework_TestResult $result
* @return PHPUnit_Framework_TestResult
* @throws InvalidArgumentException
*/
public function run(PHPUnit_Framework_TestResult $result = NULL) {
if (!empty($this->fixtureManager)) {
$this->fixtureManager->load($this);
}
$result = parent::run($result);
if (!empty($this->fixtureManager)) {
$this->fixtureManager->unload($this);
}
return $result;
}
/**
* Called when a test case method is about to start (to be overriden when needed.)
*
* @param string $method Test method about to get executed.
* @return void
*/
public function startTest($method) {
}
/**
* Called when a test case method has been executed (to be overriden when needed.)
*
* @param string $method Test method about that was executed.
* @return void
*/
public function endTest($method) {
}
/**
* Overrides SimpleTestCase::skipIf to provide a boolean return value
*
* @param boolean $shouldSkip
* @param string $message
* @return boolean
*/
public function skipIf($shouldSkip, $message = '') {
if ($shouldSkip) {
$this->markTestSkipped($message);
}
return $shouldSkip;
}
/**
* setup the test case, backup the static object values so they can be restored.
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->_configure = Configure::read();
if (class_exists('Router', false)) {
Router::reload();
}
}
/**
* teardown any static object changes and restore them.
*
* @return void
*/
public function tearDown() {
parent::tearDown();
App::build();
if (class_exists('ClassRegistry', false)) {
ClassRegistry::flush();
}
Configure::write($this->_configure);
}
/**
* Announces the start of a test.
*
* @param string $method Test method just started.
* @return void
*/
protected function assertPreConditions() {
parent::assertPreConditions();
$this->startTest($this->getName());
}
/**
* Announces the end of a test.
*
* @param string $method Test method just finished.
* @return void
*/
protected function assertPostConditions() {
parent::assertPostConditions();
$this->endTest($this->getName());
}
/**
* Chooses which fixtures to load for a given test
*
* @param string $fixture Each parameter is a model name that corresponds to a
* fixture, i.e. 'Post', 'Author', etc.
* @return void
* @access public
* @see CakeTestCase::$autoFixtures
*/
function loadFixtures() {
if (empty($this->fixtureManager)) {
throw new Exception(__('No fixture manager to load the test fixture'));
}
$args = func_get_args();
foreach ($args as $class) {
$this->fixtureManager->loadSingle($class);
}
}
/**
* Takes an array $expected and generates a regex from it to match the provided $string.
* Samples for $expected:
*
* Checks for an input tag with a name attribute (contains any non-empty value) and an id
* attribute that contains 'my-input':
* array('input' => array('name', 'id' => 'my-input'))
*
* Checks for two p elements with some text in them:
* array(
* array('p' => true),
* 'textA',
* '/p',
* array('p' => true),
* 'textB',
* '/p'
* )
*
* You can also specify a pattern expression as part of the attribute values, or the tag
* being defined, if you prepend the value with preg: and enclose it with slashes, like so:
* array(
* array('input' => array('name', 'id' => 'preg:/FieldName\d+/')),
* 'preg:/My\s+field/'
* )
*
* Important: This function is very forgiving about whitespace and also accepts any
* permutation of attribute order. It will also allow whitespaces between specified tags.
*
* @param string $string An HTML/XHTML/XML string
* @param array $expected An array, see above
* @param string $message SimpleTest failure output string
* @return boolean
*/
public function assertTags($string, $expected, $fullDebug = false) {
$regex = array();
$normalized = array();
foreach ((array) $expected as $key => $val) {
if (!is_numeric($key)) {
$normalized[] = array($key => $val);
} else {
$normalized[] = $val;
}
}
$i = 0;
foreach ($normalized as $tags) {
if (!is_array($tags)) {
$tags = (string)$tags;
}
$i++;
if (is_string($tags) && $tags{0} == '<') {
$tags = array(substr($tags, 1) => array());
} elseif (is_string($tags)) {
$tagsTrimmed = preg_replace('/\s+/m', '', $tags);
if (preg_match('/^\*?\//', $tags, $match) && $tagsTrimmed !== '//') {
$prefix = array(null, null);
if ($match[0] == '*/') {
$prefix = array('Anything, ', '.*?');
}
$regex[] = array(
sprintf('%sClose %s tag', $prefix[0], substr($tags, strlen($match[0]))),
sprintf('%s<[\s]*\/[\s]*%s[\s]*>[\n\r]*', $prefix[1], substr($tags, strlen($match[0]))),
$i,
);
continue;
}
if (!empty($tags) && preg_match('/^preg\:\/(.+)\/$/i', $tags, $matches)) {
$tags = $matches[1];
$type = 'Regex matches';
} else {
$tags = preg_quote($tags, '/');
$type = 'Text equals';
}
$regex[] = array(
sprintf('%s "%s"', $type, $tags),
$tags,
$i,
);
continue;
}
foreach ($tags as $tag => $attributes) {
$regex[] = array(
sprintf('Open %s tag', $tag),
sprintf('[\s]*<%s', preg_quote($tag, '/')),
$i,
);
if ($attributes === true) {
$attributes = array();
}
$attrs = array();
$explanations = array();
$i = 1;
foreach ($attributes as $attr => $val) {
if (is_numeric($attr) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
$attrs[] = $matches[1];
$explanations[] = sprintf('Regex "%s" matches', $matches[1]);
continue;
} else {
$quotes = '["\']';
if (is_numeric($attr)) {
$attr = $val;
$val = '.+?';
$explanations[] = sprintf('Attribute "%s" present', $attr);
} elseif (!empty($val) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
$quotes = '["\']?';
$val = $matches[1];
$explanations[] = sprintf('Attribute "%s" matches "%s"', $attr, $val);
} else {
$explanations[] = sprintf('Attribute "%s" == "%s"', $attr, $val);
$val = preg_quote($val, '/');
}
$attrs[] = '[\s]+' . preg_quote($attr, '/') . '=' . $quotes . $val . $quotes;
}
$i++;
}
if ($attrs) {
$permutations = $this->__array_permute($attrs);
$permutationTokens = array();
foreach ($permutations as $permutation) {
$permutationTokens[] = implode('', $permutation);
}
$regex[] = array(
sprintf('%s', implode(', ', $explanations)),
$permutationTokens,
$i,
);
}
$regex[] = array(
sprintf('End %s tag', $tag),
'[\s]*\/?[\s]*>[\n\r]*',
$i,
);
}
}
foreach ($regex as $i => $assertation) {
list($description, $expressions, $itemNum) = $assertation;
$matches = false;
foreach ((array)$expressions as $expression) {
if (preg_match(sprintf('/^%s/s', $expression), $string, $match)) {
$matches = true;
$string = substr($string, strlen($match[0]));
break;
}
}
if (!$matches) {
$this->assertTrue(false, sprintf('Item #%d / regex #%d failed: %s', $itemNum, $i, $description));
if ($fullDebug) {
debug($string, true);
debug($regex, true);
}
return false;
}
}
$this->assertTrue(true, '%s');
return true;
}
/**
* Generates all permutation of an array $items and returns them in a new array.
*
* @param array $items An array of items
* @return array
* @access private
*/
private function __array_permute($items, $perms = array()) {
static $permuted;
if (empty($perms)) {
$permuted = array();
}
if (empty($items)) {
$permuted[] = $perms;
} else {
$numItems = count($items) - 1;
for ($i = $numItems; $i >= 0; --$i) {
$newItems = $items;
$newPerms = $perms;
list($tmp) = array_splice($newItems, $i, 1);
array_unshift($newPerms, $tmp);
$this->__array_permute($newItems, $newPerms);
}
return $permuted;
}
}
/**
* Compatibility wrapper function for assertEquals
* @param mixed $a
* @param mixed $b
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected function assertEqual($a, $b, $message = '') {
return $this->assertEquals($a, $b, $message);
}
/**
* Compatibility wrapper function for assertNotEquals
* @param mixed $a
* @param mixed $b
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected function assertNotEqual($a, $b, $message = '') {
return $this->assertNotEquals($a, $b, $message);
}
/**
* Compatibility wrapper function for assertRegexp
* @param mixed $pattern a regular expression
* @param string $string the text to be matched
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected function assertPattern($pattern, $string, $message = '') {
return $this->assertRegExp($pattern, $string, $message);
}
/**
* Compatibility wrapper function for assertEquals
* @param mixed $expected
* @param mixed $actual
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected function assertIdentical($expected, $actual, $message = '') {
return $this->assertSame($expected, $actual, $message);
}
/**
* Compatibility wrapper function for assertNotEquals
* @param mixed $expected
* @param mixed $actual
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected function assertNotIdentical($expected, $actual, $message = '') {
return $this->assertNotSame($expected, $actual, $message);
}
/**
* Compatibility wrapper function for assertNotRegExp
* @param mixed $pattern a regular expression
* @param string $string the text to be matched
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected function assertNoPattern($pattern, $string, $message = '') {
return $this->assertNotRegExp($pattern, $string, $message);
}
protected function assertNoErrors() {
}
/**
* Compatibility wrapper function for setExpectedException
* @param mixed $expected the name of the Exception or error
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected function expectError($expected = false, $message = '') {
if (!$expected) {
$expected = 'Exception';
}
$this->setExpectedException($expected, $message);
}
/**
* Compatibility wrapper function for setExpectedException
* @param mixed $expected the name of the Exception
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected function expectException($name = 'Exception', $message = '') {
$this->setExpectedException($name, $message);
}
/**
* Compatibility wrapper function for assertSame
* @param mixed $expected
* @param mixed $actual
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected function assertReference(&$first, &$second, $message = '') {
return $this->assertSame($first, $second, $message);
}
/**
* Compatibility wrapper for assertIsA
*
* @param string $object
* @param string $type
* @param string $message
* @return void
*/
protected function assertIsA($object, $type, $message = '') {
return $this->assertType($type, $object, $message);
}
/**
* Compatibility function to test if value is between an acceptable range
* @param mixed $value
* @param mixed $expected
* @param mixed $margin the rage of acceptation
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected function assertWithinMargin($value, $expected, $margin, $message = '') {
$upper = $value + $margin;
$lower = $value - $margin;
$this->assertTrue((($expected <= $upper) && ($expected >= $lower)), $message);
}
/**
* Compatibility function for skipping.
*
* @param boolean $condition Condition to trigger skipping
* @param string $message Message for skip
* @return boolean
*/
protected function skipUnless($condition, $message = '') {
if (!$condition) {
$this->markTestSkipped($message);
}
return $condition;
}
}