統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / TestSuite / CakeTestCaseTest.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (12.672 KB)

1 635eef61 spyder1211
<?php
2
/**
3
 * CakeTestCaseTest file
4
 *
5
 * Test Case for CakeTestCase class
6
 *
7
 * CakePHP : Rapid Development Framework (http://cakephp.org)
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://cakephp.org CakePHP Project
16
 * @package       Cake.Test.Case.TestSuite
17
 * @since         CakePHP v 1.2.0.4487
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20
App::uses('CakePlugin', 'Core');
21
App::uses('Controller', 'Controller');
22
App::uses('CakeHtmlReporter', 'TestSuite/Reporter');
23
App::uses('Model', 'Model');
24
25
/**
26
 * Secondary Post stub class.
27
 */
28
class SecondaryPost extends Model {
29
30
/**
31
 * @var string
32
 */
33
        public $useTable = 'posts';
34
35
/**
36
 * @var string
37
 */
38
        public $useDbConfig = 'secondary';
39
40
}
41
42
/**
43
 * CakeTestCaseTest
44
 *
45
 * @package       Cake.Test.Case.TestSuite
46
 */
47
class CakeTestCaseTest extends CakeTestCase {
48
49
/**
50
 * fixtures property
51
 *
52
 * @var array
53
 */
54
        public $fixtures = array('core.post', 'core.author', 'core.test_plugin_comment');
55
56
/**
57
 * CakeTestCaseTest::setUpBeforeClass()
58
 *
59
 * @return void
60
 */
61
        public static function setUpBeforeClass() {
62
                require_once CAKE . 'Test' . DS . 'Fixture' . DS . 'AssertTagsTestCase.php';
63
                require_once CAKE . 'Test' . DS . 'Fixture' . DS . 'FixturizedTestCase.php';
64
        }
65
66
/**
67
 * setUp
68
 *
69
 * @return void
70
 */
71
        public function setUp() {
72
                parent::setUp();
73
                $this->Reporter = $this->getMock('CakeHtmlReporter');
74
        }
75
76
/**
77
 * tearDown
78
 *
79
 * @return void
80
 */
81
        public function tearDown() {
82
                parent::tearDown();
83
                unset($this->Result);
84
                unset($this->Reporter);
85
        }
86
87
/**
88
 * testAssertTags
89
 *
90
 * @return void
91
 */
92
        public function testAssertTagsBasic() {
93
                $test = new AssertTagsTestCase('testAssertTagsQuotes');
94
                $result = $test->run();
95
                $this->assertEquals(0, $result->errorCount());
96
                $this->assertTrue($result->wasSuccessful());
97
                $this->assertEquals(0, $result->failureCount());
98
        }
99
100
/**
101
 * test assertTags works with single and double quotes
102
 *
103
 * @return void
104
 */
105
        public function testAssertTagsQuoting() {
106
                $input = '<a href="/test.html" class="active">My link</a>';
107
                $pattern = array(
108
                        'a' => array('href' => '/test.html', 'class' => 'active'),
109
                        'My link',
110
                        '/a'
111
                );
112
                $this->assertTags($input, $pattern);
113
114
                $input = "<a href='/test.html' class='active'>My link</a>";
115
                $pattern = array(
116
                        'a' => array('href' => '/test.html', 'class' => 'active'),
117
                        'My link',
118
                        '/a'
119
                );
120
                $this->assertTags($input, $pattern);
121
122
                $input = "<a href='/test.html' class='active'>My link</a>";
123
                $pattern = array(
124
                        'a' => array('href' => 'preg:/.*\.html/', 'class' => 'active'),
125
                        'My link',
126
                        '/a'
127
                );
128
                $this->assertTags($input, $pattern);
129
130
                $input = "<span><strong>Text</strong></span>";
131
                $pattern = array(
132
                        '<span',
133
                        '<strong',
134
                        'Text',
135
                        '/strong',
136
                        '/span'
137
                );
138
                $this->assertTags($input, $pattern);
139
140
                $input = "<span class='active'><strong>Text</strong></span>";
141
                $pattern = array(
142
                        'span' => array('class'),
143
                        '<strong',
144
                        'Text',
145
                        '/strong',
146
                        '/span'
147
                );
148
                $this->assertTags($input, $pattern);
149
        }
150
151
/**
152
 * Test that assertTags runs quickly.
153
 *
154
 * @return void
155
 */
156
        public function testAssertTagsRuntimeComplexity() {
157
                $pattern = array(
158
                        'div' => array(
159
                                'attr1' => 'val1',
160
                                'attr2' => 'val2',
161
                                'attr3' => 'val3',
162
                                'attr4' => 'val4',
163
                                'attr5' => 'val5',
164
                                'attr6' => 'val6',
165
                                'attr7' => 'val7',
166
                                'attr8' => 'val8',
167
                        ),
168
                        'My div',
169
                        '/div'
170
                );
171
                $input = '<div attr8="val8" attr6="val6" attr4="val4" attr2="val2"' .
172
                        ' attr1="val1" attr3="val3" attr5="val5" attr7="val7" />' .
173
                        'My div' .
174
                        '</div>';
175
                $this->assertTags($input, $pattern);
176
        }
177
178
/**
179
 * testNumericValuesInExpectationForAssertTags
180
 *
181
 * @return void
182
 */
183
        public function testNumericValuesInExpectationForAssertTags() {
184
                $test = new AssertTagsTestCase('testNumericValuesInExpectationForAssertTags');
185
                $result = $test->run();
186
                $this->assertEquals(0, $result->errorCount());
187
                $this->assertTrue($result->wasSuccessful());
188
                $this->assertEquals(0, $result->failureCount());
189
        }
190
191
/**
192
 * testBadAssertTags
193
 *
194
 * @return void
195
 */
196
        public function testBadAssertTags() {
197
                $test = new AssertTagsTestCase('testBadAssertTags');
198
                $result = $test->run();
199
                $this->assertEquals(0, $result->errorCount());
200
                $this->assertFalse($result->wasSuccessful());
201
                $this->assertEquals(1, $result->failureCount());
202
203
                $test = new AssertTagsTestCase('testBadAssertTags2');
204
                $result = $test->run();
205
                $this->assertEquals(0, $result->errorCount());
206
                $this->assertFalse($result->wasSuccessful());
207
                $this->assertEquals(1, $result->failureCount());
208
        }
209
210
/**
211
 * testLoadFixtures
212
 *
213
 * @return void
214
 */
215
        public function testLoadFixtures() {
216
                $test = new FixturizedTestCase('testFixturePresent');
217
                $manager = $this->getMock('CakeFixtureManager');
218
                $manager->fixturize($test);
219
                $test->fixtureManager = $manager;
220
                $manager->expects($this->once())->method('load');
221
                $manager->expects($this->once())->method('unload');
222
                $result = $test->run();
223
                $this->assertEquals(0, $result->errorCount());
224
                $this->assertTrue($result->wasSuccessful());
225
                $this->assertEquals(0, $result->failureCount());
226
        }
227
228
/**
229
 * testLoadFixturesOnDemand
230
 *
231
 * @return void
232
 */
233
        public function testLoadFixturesOnDemand() {
234
                $test = new FixturizedTestCase('testFixtureLoadOnDemand');
235
                $test->autoFixtures = false;
236
                $manager = $this->getMock('CakeFixtureManager');
237
                $manager->fixturize($test);
238
                $test->fixtureManager = $manager;
239
                $manager->expects($this->once())->method('loadSingle');
240
                $result = $test->run();
241
                $this->assertEquals(0, $result->errorCount());
242
        }
243
244
/**
245
 * testLoadFixturesOnDemand
246
 *
247
 * @return void
248
 */
249
        public function testUnoadFixturesAfterFailure() {
250
                $test = new FixturizedTestCase('testFixtureLoadOnDemand');
251
                $test->autoFixtures = false;
252
                $manager = $this->getMock('CakeFixtureManager');
253
                $manager->fixturize($test);
254
                $test->fixtureManager = $manager;
255
                $manager->expects($this->once())->method('loadSingle');
256
                $result = $test->run();
257
                $this->assertEquals(0, $result->errorCount());
258
        }
259
260
/**
261
 * testThrowException
262
 *
263
 * @return void
264
 */
265
        public function testThrowException() {
266
                $test = new FixturizedTestCase('testThrowException');
267
                $test->autoFixtures = false;
268
                $manager = $this->getMock('CakeFixtureManager');
269
                $manager->fixturize($test);
270
                $test->fixtureManager = $manager;
271
                $manager->expects($this->once())->method('unload');
272
                $result = $test->run();
273
                $this->assertEquals(1, $result->errorCount());
274
        }
275
276
/**
277
 * testSkipIf
278
 *
279
 * @return void
280
 */
281
        public function testSkipIf() {
282
                $test = new FixturizedTestCase('testSkipIfTrue');
283
                $result = $test->run();
284
                $this->assertEquals(1, $result->skippedCount());
285
286
                $test = new FixturizedTestCase('testSkipIfFalse');
287
                $result = $test->run();
288
                $this->assertEquals(0, $result->skippedCount());
289
        }
290
291
/**
292
 * Test that CakeTestCase::setUp() backs up values.
293
 *
294
 * @return void
295
 */
296
        public function testSetupBackUpValues() {
297
                $this->assertArrayHasKey('debug', $this->_configure);
298
                $this->assertArrayHasKey('Plugin', $this->_pathRestore);
299
        }
300
301
/**
302
 * test assertTextNotEquals()
303
 *
304
 * @return void
305
 */
306
        public function testAssertTextNotEquals() {
307
                $one = "\r\nOne\rTwooo";
308
                $two = "\nOne\nTwo";
309
                $this->assertTextNotEquals($one, $two);
310
        }
311
312
/**
313
 * test assertTextEquals()
314
 *
315
 * @return void
316
 */
317
        public function testAssertTextEquals() {
318
                $one = "\r\nOne\rTwo";
319
                $two = "\nOne\nTwo";
320
                $this->assertTextEquals($one, $two);
321
        }
322
323
/**
324
 * test assertTextStartsWith()
325
 *
326
 * @return void
327
 */
328
        public function testAssertTextStartsWith() {
329
                $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
330
331
                $this->assertStringStartsWith("some\nstring", $stringDirty);
332
                $this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
333
                $this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);
334
335
                $this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
336
                $this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
337
        }
338
339
/**
340
 * test assertTextStartsNotWith()
341
 *
342
 * @return void
343
 */
344
        public function testAssertTextStartsNotWith() {
345
                $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
346
                $this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
347
        }
348
349
/**
350
 * test assertTextEndsWith()
351
 *
352
 * @return void
353
 */
354
        public function testAssertTextEndsWith() {
355
                $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
356
                $this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
357
                $this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
358
        }
359
360
/**
361
 * test assertTextEndsNotWith()
362
 *
363
 * @return void
364
 */
365
        public function testAssertTextEndsNotWith() {
366
                $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
367
                $this->assertStringEndsNotWith("different\nline endings", $stringDirty);
368
                $this->assertTextEndsNotWith("different\rline endings", $stringDirty);
369
        }
370
371
/**
372
 * test assertTextContains()
373
 *
374
 * @return void
375
 */
376
        public function testAssertTextContains() {
377
                $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
378
                $this->assertContains("different", $stringDirty);
379
                $this->assertNotContains("different\rline", $stringDirty);
380
                $this->assertTextContains("different\rline", $stringDirty);
381
        }
382
383
/**
384
 * test assertTextNotContains()
385
 *
386
 * @return void
387
 */
388
        public function testAssertTextNotContains() {
389
                $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
390
                $this->assertTextNotContains("different\rlines", $stringDirty);
391
        }
392
393
/**
394
 * test getMockForModel()
395
 *
396
 * @return void
397
 */
398
        public function testGetMockForModel() {
399
                App::build(array(
400
                        'Model' => array(
401
                                CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS
402
                        )
403
                ), App::RESET);
404
                $Post = $this->getMockForModel('Post');
405
                $this->assertEquals('test', $Post->useDbConfig);
406
                $this->assertInstanceOf('Post', $Post);
407
                $this->assertNull($Post->save(array()));
408
                $this->assertNull($Post->find('all'));
409
                $this->assertEquals('posts', $Post->useTable);
410
411
                $Post = $this->getMockForModel('Post', array('save'));
412
413
                $this->assertNull($Post->save(array()));
414
                $this->assertInternalType('array', $Post->find('all'));
415
        }
416
417
/**
418
 * Test getMockForModel on secondary datasources.
419
 *
420
 * @return void
421
 */
422
        public function testGetMockForModelSecondaryDatasource() {
423
                App::build(array(
424
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
425
                        'Model/Datasource/Database' => array(
426
                                CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS . 'Database' . DS
427
                        )
428
                ), App::RESET);
429
                CakePlugin::load('TestPlugin');
430
                ConnectionManager::create('test_secondary', array(
431
                        'datasource' => 'Database/TestLocalDriver'
432
                ));
433
                $post = $this->getMockForModel('SecondaryPost', array('save'));
434
                $this->assertEquals('test_secondary', $post->useDbConfig);
435
                ConnectionManager::drop('test_secondary');
436
        }
437
438
/**
439
 * test getMockForModel() with plugin models
440
 *
441
 * @return void
442
 */
443
        public function testGetMockForModelWithPlugin() {
444
                App::build(array(
445
                        'Plugin' => array(
446
                                CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
447
                        )
448
                ), App::RESET);
449
                CakePlugin::load('TestPlugin');
450
                $this->getMockForModel('TestPlugin.TestPluginAppModel');
451
                $this->getMockForModel('TestPlugin.TestPluginComment');
452
453
                $result = ClassRegistry::init('TestPlugin.TestPluginComment');
454
                $this->assertInstanceOf('TestPluginComment', $result);
455
                $this->assertEquals('test', $result->useDbConfig);
456
457
                $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComment', array('save'));
458
459
                $this->assertInstanceOf('TestPluginComment', $TestPluginComment);
460
                $TestPluginComment->expects($this->at(0))
461
                        ->method('save')
462
                        ->will($this->returnValue(true));
463
                $TestPluginComment->expects($this->at(1))
464
                        ->method('save')
465
                        ->will($this->returnValue(false));
466
                $this->assertTrue($TestPluginComment->save(array()));
467
                $this->assertFalse($TestPluginComment->save(array()));
468
        }
469
470
/**
471
 * testGetMockForModelModel
472
 *
473
 * @return void
474
 */
475
        public function testGetMockForModelModel() {
476
                $Mock = $this->getMockForModel('Model', array('save', 'setDataSource'), array('name' => 'Comment'));
477
478
                $result = ClassRegistry::init('Comment');
479
                $this->assertInstanceOf('Model', $result);
480
481
                $Mock->expects($this->at(0))
482
                        ->method('save')
483
                        ->will($this->returnValue(true));
484
                $Mock->expects($this->at(1))
485
                        ->method('save')
486
                        ->will($this->returnValue(false));
487
488
                $this->assertTrue($Mock->save(array()));
489
                $this->assertFalse($Mock->save(array()));
490
        }
491
492
/**
493
 * testGetMockForModelDoesNotExist
494
 *
495
 * @expectedException MissingModelException
496
 * @expectedExceptionMessage Model IDoNotExist could not be found
497
 * @return void
498
 */
499
        public function testGetMockForModelDoesNotExist() {
500
                $this->getMockForModel('IDoNotExist');
501
        }
502
}