~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/cases/libs/cake_test_case.test.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CakeTestCaseTest file
 
4
 *
 
5
 * Test Case for CakeTestCase class
 
6
 *
 
7
 * PHP versions 4 and 5
 
8
 *
 
9
 * CakePHP : Rapid Development Framework (http://cakephp.org)
 
10
 * Copyright 2006-2010, Cake Software Foundation, Inc.
 
11
 *
 
12
 * Licensed under The MIT License
 
13
 * Redistributions of files must retain the above copyright notice.
 
14
 *
 
15
 * @copyright     Copyright 2006-2010, Cake Software Foundation, Inc.
 
16
 * @link          http://cakephp.org CakePHP Project
 
17
 * @package       cake
 
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)
 
21
 */
 
22
App::import('Core', 'CakeTestCase');
 
23
 
 
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);
 
28
}
 
29
 
 
30
Mock::generate('CakeHtmlReporter');
 
31
Mock::generate('CakeTestCase', 'CakeDispatcherMockTestCase');
 
32
 
 
33
SimpleTest::ignore('SubjectCakeTestCase');
 
34
SimpleTest::ignore('CakeDispatcherMockTestCase');
 
35
 
 
36
/**
 
37
 * SubjectCakeTestCase
 
38
 *
 
39
 * @package       cake
 
40
 * @subpackage    cake.tests.cases.libs
 
41
 */
 
42
class SubjectCakeTestCase extends CakeTestCase {
 
43
 
 
44
/**
 
45
 * Feed a Mocked Reporter to the subject case
 
46
 * prevents its pass/fails from affecting the real test
 
47
 *
 
48
 * @param string $reporter
 
49
 * @access public
 
50
 * @return void
 
51
 */
 
52
        function setReporter(&$reporter) {
 
53
                $this->_reporter = &$reporter;
 
54
        }
 
55
 
 
56
/**
 
57
 * testDummy method
 
58
 *
 
59
 * @return void
 
60
 * @access public
 
61
 */
 
62
        function testDummy() {
 
63
        }
 
64
}
 
65
 
 
66
/**
 
67
 * CakeTestCaseTest
 
68
 *
 
69
 * @package       cake
 
70
 * @subpackage    cake.tests.cases.libs
 
71
 */
 
72
class CakeTestCaseTest extends CakeTestCase {
 
73
 
 
74
/**
 
75
 * setUp
 
76
 *
 
77
 * @access public
 
78
 * @return void
 
79
 */
 
80
        function setUp() {
 
81
                $this->_debug = Configure::read('debug');
 
82
                $this->Case =& new SubjectCakeTestCase();
 
83
                $reporter =& new MockCakeHtmlReporter();
 
84
                $this->Case->setReporter($reporter);
 
85
                $this->Reporter = $reporter;
 
86
        }
 
87
 
 
88
/**
 
89
 * tearDown
 
90
 *
 
91
 * @access public
 
92
 * @return void
 
93
 */
 
94
        function tearDown() {
 
95
                Configure::write('debug', $this->_debug);
 
96
                unset($this->Case);
 
97
                unset($this->Reporter);
 
98
        }
 
99
 
 
100
/**
 
101
 * endTest
 
102
 *
 
103
 * @access public
 
104
 * @return void
 
105
 */
 
106
        function endTest() {
 
107
                App::build();
 
108
        }
 
109
 
 
110
/**
 
111
 * testAssertGoodTags
 
112
 *
 
113
 * @access public
 
114
 * @return void
 
115
 */
 
116
        function testAssertGoodTags() {
 
117
                $this->Reporter->expectAtLeastOnce('paintPass');
 
118
                $this->Reporter->expectNever('paintFail');
 
119
 
 
120
                $input = '<p>Text</p>';
 
121
                $pattern = array(
 
122
                        '<p',
 
123
                        'Text',
 
124
                        '/p',
 
125
                );
 
126
                $this->assertTrue($this->Case->assertTags($input, $pattern));
 
127
 
 
128
                $input = '<a href="/test.html" class="active">My link</a>';
 
129
                $pattern = array(
 
130
                        'a' => array('href' => '/test.html', 'class' => 'active'),
 
131
                        'My link',
 
132
                        '/a'
 
133
                );
 
134
                $this->assertTrue($this->Case->assertTags($input, $pattern));
 
135
 
 
136
                $pattern = array(
 
137
                        'a' => array('class' => 'active', 'href' => '/test.html'),
 
138
                        'My link',
 
139
                        '/a'
 
140
                );
 
141
                $this->assertTrue($this->Case->assertTags($input, $pattern), 'Attributes in wrong order. %s');
 
142
 
 
143
                $input = "<a    href=\"/test.html\"\t\n\tclass=\"active\"\tid=\"primary\">\t<span>My link</span></a>";
 
144
                $pattern = array(
 
145
                        'a' => array('id' => 'primary', 'href' => '/test.html', 'class' => 'active'),
 
146
                        '<span',
 
147
                        'My link',
 
148
                        '/span',
 
149
                        '/a'
 
150
                );
 
151
                $this->assertTrue($this->Case->assertTags($input, $pattern), 'Whitespace consumption %s');
 
152
 
 
153
                $input = '<p class="info"><a href="/test.html" class="active"><strong onClick="alert(\'hey\');">My link</strong></a></p>';
 
154
                $pattern = array(
 
155
                        'p' => array('class' => 'info'),
 
156
                        'a' => array('class' => 'active', 'href' => '/test.html' ),
 
157
                        'strong' => array('onClick' => 'alert(\'hey\');'),
 
158
                        'My link',
 
159
                        '/strong',
 
160
                        '/a',
 
161
                        '/p'
 
162
                );
 
163
                $this->assertTrue($this->Case->assertTags($input, $pattern));
 
164
        }
 
165
 
 
166
/**
 
167
 * test that assertTags knows how to handle correct quoting.
 
168
 *
 
169
 * @return void
 
170
 */
 
171
        function testAssertTagsQuotes() {
 
172
                $input = '<a href="/test.html" class="active">My link</a>';
 
173
                $pattern = array(
 
174
                        'a' => array('href' => '/test.html', 'class' => 'active'),
 
175
                        'My link',
 
176
                        '/a'
 
177
                );
 
178
                $this->assertTrue($this->Case->assertTags($input, $pattern), 'Double quoted attributes %s');
 
179
 
 
180
                $input = "<a href='/test.html' class='active'>My link</a>";
 
181
                $pattern = array(
 
182
                        'a' => array('href' => '/test.html', 'class' => 'active'),
 
183
                        'My link',
 
184
                        '/a'
 
185
                );
 
186
                $this->assertTrue($this->Case->assertTags($input, $pattern), 'Single quoted attributes %s');
 
187
                
 
188
                $input = "<a href='/test.html' class='active'>My link</a>";
 
189
                $pattern = array(
 
190
                        'a' => array('href' => 'preg:/.*\.html/', 'class' => 'active'),
 
191
                        'My link',
 
192
                        '/a'
 
193
                );
 
194
                $this->assertTrue($this->Case->assertTags($input, $pattern), 'Single quoted attributes %s');
 
195
        }
 
196
 
 
197
/**
 
198
 * testNumericValuesInExpectationForAssertTags
 
199
 *
 
200
 * @access public
 
201
 * @return void
 
202
 */
 
203
        function testNumericValuesInExpectationForAssertTags() {
 
204
                $value = 220985;
 
205
 
 
206
                $input = '<p><strong>' . $value . '</strong></p>';
 
207
                $pattern = array(
 
208
                        '<p',
 
209
                                '<strong',
 
210
                                        $value,
 
211
                                '/strong',
 
212
                        '/p'
 
213
                );
 
214
                $this->assertTrue($this->Case->assertTags($input, $pattern));
 
215
 
 
216
                $input = '<p><strong>' . $value . '</strong></p><p><strong>' . $value . '</strong></p>';
 
217
                $pattern = array(
 
218
                        '<p',
 
219
                                '<strong',
 
220
                                        $value,
 
221
                                '/strong',
 
222
                        '/p',
 
223
                        '<p',
 
224
                                '<strong',
 
225
                                        $value,
 
226
                                '/strong',
 
227
                        '/p',
 
228
                );
 
229
                $this->assertTrue($this->Case->assertTags($input, $pattern));
 
230
 
 
231
                $input = '<p><strong>' . $value . '</strong></p><p id="' . $value . '"><strong>' . $value . '</strong></p>';
 
232
                $pattern = array(
 
233
                        '<p',
 
234
                                '<strong',
 
235
                                        $value,
 
236
                                '/strong',
 
237
                        '/p',
 
238
                        'p' => array('id' => $value),
 
239
                                '<strong',
 
240
                                        $value,
 
241
                                '/strong',
 
242
                        '/p',
 
243
                );
 
244
                $this->assertTrue($this->Case->assertTags($input, $pattern));
 
245
        }
 
246
 
 
247
 /**
 
248
 * testBadAssertTags
 
249
 *
 
250
 * @access public
 
251
 * @return void
 
252
 */
 
253
        function testBadAssertTags() {
 
254
                $this->Reporter->expectAtLeastOnce('paintFail');
 
255
                $this->Reporter->expectNever('paintPass');
 
256
 
 
257
                $input = '<a href="/test.html" class="active">My link</a>';
 
258
                $pattern = array(
 
259
                        'a' => array('hRef' => '/test.html', 'clAss' => 'active'),
 
260
                        'My link',
 
261
                        '/a'
 
262
                );
 
263
                $this->assertFalse($this->Case->assertTags($input, $pattern));
 
264
 
 
265
                $input = '<a href="/test.html" class="active">My link</a>';
 
266
                $pattern = array(
 
267
                        '<a' => array('href' => '/test.html', 'class' => 'active'),
 
268
                        'My link',
 
269
                        '/a'
 
270
                );
 
271
                $this->assertFalse($this->Case->assertTags($input, $pattern));
 
272
        }
 
273
 
 
274
/**
 
275
 * testBefore
 
276
 *
 
277
 * @access public
 
278
 * @return void
 
279
 */
 
280
        function testBefore() {
 
281
                $this->Case->before('testDummy');
 
282
                $this->assertFalse(isset($this->Case->db));
 
283
 
 
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');
 
290
        }
 
291
 
 
292
/**
 
293
 * testAfter
 
294
 *
 
295
 * @access public
 
296
 * @return void
 
297
 */
 
298
        function testAfter() {
 
299
                $this->Case->after('testDummy');
 
300
                $this->assertFalse($this->Case->__truncated);
 
301
 
 
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);
 
307
        }
 
308
 
 
309
/**
 
310
 * testLoadFixtures
 
311
 *
 
312
 * @access public
 
313
 * @return void
 
314
 */
 
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!');
 
321
                $this->Case->end();
 
322
        }
 
323
 
 
324
/**
 
325
 * testGetTests Method
 
326
 *
 
327
 * @return void
 
328
 * @access public
 
329
 */
 
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'));
 
334
        }
 
335
 
 
336
/**
 
337
 * TestTestAction
 
338
 *
 
339
 * @access public
 
340
 * @return void
 
341
 */
 
342
        function testTestAction() {
 
343
                App::build(array(
 
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)
 
348
                ), true);
 
349
 
 
350
                $result = $this->Case->testAction('/tests_apps/index', array('return' => 'view'));
 
351
                $this->assertPattern('/This is the TestsAppsController index view/', $result);
 
352
 
 
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);
 
357
 
 
358
                $result = $this->Case->testAction('/tests_apps/some_method', array('return' => 'result'));
 
359
                $this->assertEqual($result, 5);
 
360
 
 
361
                $result = $this->Case->testAction('/tests_apps/set_action', array('return' => 'vars'));
 
362
                $this->assertEqual($result, array('var' => 'string'));
 
363
 
 
364
                $db =& ConnectionManager::getDataSource('test_suite');
 
365
                $fixture =& new PostFixture();
 
366
                $fixture->create($db);
 
367
 
 
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);
 
371
 
 
372
                $result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
 
373
                        'return' => 'vars',
 
374
                        'method' => 'get',
 
375
                ));
 
376
                $this->assertTrue(isset($result['params']['url']['url']));
 
377
                $this->assertEqual(array_keys($result['params']['named']), array('var1', 'var2'));
 
378
 
 
379
                $result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
 
380
                        'return' => 'vars',
 
381
                        'method' => 'get',
 
382
                ));
 
383
                $this->assertEqual($result['params']['pass'], array('gogo', 'val2'));
 
384
 
 
385
 
 
386
                $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
 
387
                        'return' => 'vars',
 
388
                        'method' => 'get',
 
389
                        'data' => array(
 
390
                                'red' => 'health',
 
391
                                'blue' => 'mana'
 
392
                        )
 
393
                ));
 
394
                $this->assertTrue(isset($result['params']['url']['red']));
 
395
                $this->assertTrue(isset($result['params']['url']['blue']));
 
396
                $this->assertTrue(isset($result['params']['url']['url']));
 
397
 
 
398
                $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
 
399
                        'return' => 'vars',
 
400
                        'method' => 'post',
 
401
                        'data' => array(
 
402
                                'name' => 'is jonas',
 
403
                                'pork' => 'and beans',
 
404
                        )
 
405
                ));
 
406
                $this->assertEqual(array_keys($result['data']), array('name', 'pork'));
 
407
                $fixture->drop($db);
 
408
 
 
409
                $db =& ConnectionManager::getDataSource('test_suite');
 
410
                $_backPrefix = $db->config['prefix'];
 
411
                $db->config['prefix'] = 'cake_testaction_test_suite_';
 
412
 
 
413
                $config = $db->config;
 
414
                $config['prefix'] = 'cake_testcase_test_';
 
415
 
 
416
                ConnectionManager::create('cake_test_case', $config);
 
417
                $db2 =& ConnectionManager::getDataSource('cake_test_case');
 
418
 
 
419
                $fixture =& new PostFixture($db2);
 
420
                $fixture->create($db2);
 
421
                $fixture->insert($db2);
 
422
 
 
423
                $result = $this->Case->testAction('/tests_apps_posts/fixtured', array(
 
424
                        'return' => 'vars',
 
425
                        'fixturize' => true,
 
426
                        'connection' => 'cake_test_case',
 
427
                ));
 
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));
 
432
 
 
433
                $fixture->drop($db2);
 
434
 
 
435
                $db =& ConnectionManager::getDataSource('test_suite');
 
436
 
 
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_';
 
441
 
 
442
                $config = $db->config;
 
443
                $config['prefix'] = 'cake_testcase_test_';
 
444
 
 
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);
 
450
 
 
451
                $this->Case->dropTables = false;
 
452
                $result = $this->Case->testAction('/tests_apps_posts/fixtured', array(
 
453
                        'return' => 'vars',
 
454
                        'fixturize' => true,
 
455
                        'connection' => 'cake_test_case',
 
456
                ));
 
457
 
 
458
                $tables = $db->listSources();
 
459
                $this->assertTrue(in_array('cake_testaction_test_suite_posts', $tables));
 
460
 
 
461
                $fixture->drop($db);
 
462
                $db =& ConnectionManager::getDataSource('test_suite');
 
463
                $db->config['prefix'] = $_backPrefix;
 
464
                $fixture->drop($db);
 
465
        }
 
466
 
 
467
/**
 
468
 * testSkipIf
 
469
 *
 
470
 * @return void
 
471
 */
 
472
        function testSkipIf() {
 
473
                $this->assertTrue($this->Case->skipIf(true));
 
474
                $this->assertFalse($this->Case->skipIf(false));
 
475
        }
 
476
 
 
477
/**
 
478
 * testTestDispatcher
 
479
 *
 
480
 * @access public
 
481
 * @return void
 
482
 */
 
483
        function testTestDispatcher() {
 
484
                App::build(array(
 
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)
 
489
                ), true);
 
490
 
 
491
                $Dispatcher =& new CakeTestDispatcher();
 
492
                $Case =& new CakeDispatcherMockTestCase();
 
493
 
 
494
                $Case->expectOnce('startController');
 
495
                $Case->expectOnce('endController');
 
496
 
 
497
                $Dispatcher->testCase($Case);
 
498
                $this->assertTrue(isset($Dispatcher->testCase));
 
499
 
 
500
                $return = $Dispatcher->dispatch('/tests_apps/index', array('autoRender' => 0, 'return' => 1, 'requested' => 1));
 
501
        }
 
502
}