3
* CakeTestCaseTest file
5
* Test Case for CakeTestCase class
9
* CakePHP : Rapid Development Framework (http://cakephp.org)
10
* Copyright 2006-2010, Cake Software Foundation, Inc.
12
* Licensed under The MIT License
13
* Redistributions of files must retain the above copyright notice.
15
* @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
16
* @link http://cakephp.org CakePHP Project
18
* @subpackage cake.cake.libs.
19
* @since CakePHP v 1.2.0.4487
20
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
22
App::import('Core', 'CakeTestCase');
24
if (!class_exists('AppController')) {
25
require_once LIBS . 'controller' . DS . 'app_controller.php';
26
} elseif (!defined('APP_CONTROLLER_EXISTS')) {
27
define('APP_CONTROLLER_EXISTS', true);
30
Mock::generate('CakeHtmlReporter');
31
Mock::generate('CakeTestCase', 'CakeDispatcherMockTestCase');
33
SimpleTest::ignore('SubjectCakeTestCase');
34
SimpleTest::ignore('CakeDispatcherMockTestCase');
40
* @subpackage cake.tests.cases.libs
42
class SubjectCakeTestCase extends CakeTestCase {
45
* Feed a Mocked Reporter to the subject case
46
* prevents its pass/fails from affecting the real test
48
* @param string $reporter
52
function setReporter(&$reporter) {
53
$this->_reporter = &$reporter;
62
function testDummy() {
70
* @subpackage cake.tests.cases.libs
72
class CakeTestCaseTest extends CakeTestCase {
81
$this->_debug = Configure::read('debug');
82
$this->Case =& new SubjectCakeTestCase();
83
$reporter =& new MockCakeHtmlReporter();
84
$this->Case->setReporter($reporter);
85
$this->Reporter = $reporter;
95
Configure::write('debug', $this->_debug);
97
unset($this->Reporter);
116
function testAssertGoodTags() {
117
$this->Reporter->expectAtLeastOnce('paintPass');
118
$this->Reporter->expectNever('paintFail');
120
$input = '<p>Text</p>';
126
$this->assertTrue($this->Case->assertTags($input, $pattern));
128
$input = '<a href="/test.html" class="active">My link</a>';
130
'a' => array('href' => '/test.html', 'class' => 'active'),
134
$this->assertTrue($this->Case->assertTags($input, $pattern));
137
'a' => array('class' => 'active', 'href' => '/test.html'),
141
$this->assertTrue($this->Case->assertTags($input, $pattern), 'Attributes in wrong order. %s');
143
$input = "<a href=\"/test.html\"\t\n\tclass=\"active\"\tid=\"primary\">\t<span>My link</span></a>";
145
'a' => array('id' => 'primary', 'href' => '/test.html', 'class' => 'active'),
151
$this->assertTrue($this->Case->assertTags($input, $pattern), 'Whitespace consumption %s');
153
$input = '<p class="info"><a href="/test.html" class="active"><strong onClick="alert(\'hey\');">My link</strong></a></p>';
155
'p' => array('class' => 'info'),
156
'a' => array('class' => 'active', 'href' => '/test.html' ),
157
'strong' => array('onClick' => 'alert(\'hey\');'),
163
$this->assertTrue($this->Case->assertTags($input, $pattern));
167
* test that assertTags knows how to handle correct quoting.
171
function testAssertTagsQuotes() {
172
$input = '<a href="/test.html" class="active">My link</a>';
174
'a' => array('href' => '/test.html', 'class' => 'active'),
178
$this->assertTrue($this->Case->assertTags($input, $pattern), 'Double quoted attributes %s');
180
$input = "<a href='/test.html' class='active'>My link</a>";
182
'a' => array('href' => '/test.html', 'class' => 'active'),
186
$this->assertTrue($this->Case->assertTags($input, $pattern), 'Single quoted attributes %s');
188
$input = "<a href='/test.html' class='active'>My link</a>";
190
'a' => array('href' => 'preg:/.*\.html/', 'class' => 'active'),
194
$this->assertTrue($this->Case->assertTags($input, $pattern), 'Single quoted attributes %s');
198
* testNumericValuesInExpectationForAssertTags
203
function testNumericValuesInExpectationForAssertTags() {
206
$input = '<p><strong>' . $value . '</strong></p>';
214
$this->assertTrue($this->Case->assertTags($input, $pattern));
216
$input = '<p><strong>' . $value . '</strong></p><p><strong>' . $value . '</strong></p>';
229
$this->assertTrue($this->Case->assertTags($input, $pattern));
231
$input = '<p><strong>' . $value . '</strong></p><p id="' . $value . '"><strong>' . $value . '</strong></p>';
238
'p' => array('id' => $value),
244
$this->assertTrue($this->Case->assertTags($input, $pattern));
253
function testBadAssertTags() {
254
$this->Reporter->expectAtLeastOnce('paintFail');
255
$this->Reporter->expectNever('paintPass');
257
$input = '<a href="/test.html" class="active">My link</a>';
259
'a' => array('hRef' => '/test.html', 'clAss' => 'active'),
263
$this->assertFalse($this->Case->assertTags($input, $pattern));
265
$input = '<a href="/test.html" class="active">My link</a>';
267
'<a' => array('href' => '/test.html', 'class' => 'active'),
271
$this->assertFalse($this->Case->assertTags($input, $pattern));
280
function testBefore() {
281
$this->Case->before('testDummy');
282
$this->assertFalse(isset($this->Case->db));
284
$this->Case->fixtures = array('core.post');
285
$this->Case->before('start');
286
$this->assertTrue(isset($this->Case->db));
287
$this->assertTrue(isset($this->Case->_fixtures['core.post']));
288
$this->assertTrue(is_a($this->Case->_fixtures['core.post'], 'CakeTestFixture'));
289
$this->assertEqual($this->Case->_fixtureClassMap['Post'], 'core.post');
298
function testAfter() {
299
$this->Case->after('testDummy');
300
$this->assertFalse($this->Case->__truncated);
302
$this->Case->fixtures = array('core.post');
303
$this->Case->before('start');
304
$this->Case->start();
305
$this->Case->after('testDummy');
306
$this->assertTrue($this->Case->__truncated);
315
function testLoadFixtures() {
316
$this->Case->fixtures = array('core.post');
317
$this->Case->autoFixtures = false;
318
$this->Case->before('start');
319
$this->expectError();
320
$this->Case->loadFixtures('Wrong!');
325
* testGetTests Method
330
function testGetTests() {
331
$result = $this->Case->getTests();
332
$this->assertEqual(array_slice($result, 0, 2), array('start', 'startCase'));
333
$this->assertEqual(array_slice($result, -2), array('endCase', 'end'));
342
function testTestAction() {
344
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
345
'models' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS),
346
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS),
347
'controllers' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'controllers' . DS)
350
$result = $this->Case->testAction('/tests_apps/index', array('return' => 'view'));
351
$this->assertPattern('/This is the TestsAppsController index view/', $result);
353
$result = $this->Case->testAction('/tests_apps/index', array('return' => 'contents'));
354
$this->assertPattern('/This is the TestsAppsController index view/', $result);
355
$this->assertPattern('/<html/', $result);
356
$this->assertPattern('/<\/html>/', $result);
358
$result = $this->Case->testAction('/tests_apps/some_method', array('return' => 'result'));
359
$this->assertEqual($result, 5);
361
$result = $this->Case->testAction('/tests_apps/set_action', array('return' => 'vars'));
362
$this->assertEqual($result, array('var' => 'string'));
364
$db =& ConnectionManager::getDataSource('test_suite');
365
$fixture =& new PostFixture();
366
$fixture->create($db);
368
$result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
369
$this->assertTrue(array_key_exists('posts', $result));
370
$this->assertEqual(count($result['posts']), 1);
372
$result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
376
$this->assertTrue(isset($result['params']['url']['url']));
377
$this->assertEqual(array_keys($result['params']['named']), array('var1', 'var2'));
379
$result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
383
$this->assertEqual($result['params']['pass'], array('gogo', 'val2'));
386
$result = $this->Case->testAction('/tests_apps_posts/url_var', array(
394
$this->assertTrue(isset($result['params']['url']['red']));
395
$this->assertTrue(isset($result['params']['url']['blue']));
396
$this->assertTrue(isset($result['params']['url']['url']));
398
$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
402
'name' => 'is jonas',
403
'pork' => 'and beans',
406
$this->assertEqual(array_keys($result['data']), array('name', 'pork'));
409
$db =& ConnectionManager::getDataSource('test_suite');
410
$_backPrefix = $db->config['prefix'];
411
$db->config['prefix'] = 'cake_testaction_test_suite_';
413
$config = $db->config;
414
$config['prefix'] = 'cake_testcase_test_';
416
ConnectionManager::create('cake_test_case', $config);
417
$db2 =& ConnectionManager::getDataSource('cake_test_case');
419
$fixture =& new PostFixture($db2);
420
$fixture->create($db2);
421
$fixture->insert($db2);
423
$result = $this->Case->testAction('/tests_apps_posts/fixtured', array(
426
'connection' => 'cake_test_case',
428
$this->assertTrue(isset($result['posts']));
429
$this->assertEqual(count($result['posts']), 3);
430
$tables = $db2->listSources();
431
$this->assertFalse(in_array('cake_testaction_test_suite_posts', $tables));
433
$fixture->drop($db2);
435
$db =& ConnectionManager::getDataSource('test_suite');
437
//test that drop tables behaves as exepected with testAction
438
$db =& ConnectionManager::getDataSource('test_suite');
439
$_backPrefix = $db->config['prefix'];
440
$db->config['prefix'] = 'cake_testaction_test_suite_';
442
$config = $db->config;
443
$config['prefix'] = 'cake_testcase_test_';
445
ConnectionManager::create('cake_test_case', $config);
446
$db =& ConnectionManager::getDataSource('cake_test_case');
447
$fixture =& new PostFixture($db);
448
$fixture->create($db);
449
$fixture->insert($db);
451
$this->Case->dropTables = false;
452
$result = $this->Case->testAction('/tests_apps_posts/fixtured', array(
455
'connection' => 'cake_test_case',
458
$tables = $db->listSources();
459
$this->assertTrue(in_array('cake_testaction_test_suite_posts', $tables));
462
$db =& ConnectionManager::getDataSource('test_suite');
463
$db->config['prefix'] = $_backPrefix;
472
function testSkipIf() {
473
$this->assertTrue($this->Case->skipIf(true));
474
$this->assertFalse($this->Case->skipIf(false));
483
function testTestDispatcher() {
485
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
486
'models' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS),
487
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS),
488
'controllers' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'controllers' . DS)
491
$Dispatcher =& new CakeTestDispatcher();
492
$Case =& new CakeDispatcherMockTestCase();
494
$Case->expectOnce('startController');
495
$Case->expectOnce('endController');
497
$Dispatcher->testCase($Case);
498
$this->assertTrue(isset($Dispatcher->testCase));
500
$return = $Dispatcher->dispatch('/tests_apps/index', array('autoRender' => 0, 'return' => 1, 'requested' => 1));