Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?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
		$stringClean = "some\nstring\nwith\ndifferent\nline endings!";
331
 
332
		$this->assertStringStartsWith("some\nstring", $stringDirty);
333
		$this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
334
		$this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);
335
 
336
		$this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
337
		$this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
338
	}
339
 
340
/**
341
 * test assertTextStartsNotWith()
342
 *
343
 * @return void
344
 */
345
	public function testAssertTextStartsNotWith() {
346
		$stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
347
		$stringClean = "some\nstring\nwith\ndifferent\nline endings!";
348
 
349
		$this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
350
	}
351
 
352
/**
353
 * test assertTextEndsWith()
354
 *
355
 * @return void
356
 */
357
	public function testAssertTextEndsWith() {
358
		$stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
359
		$stringClean = "some\nstring\nwith\ndifferent\nline endings!";
360
 
361
		$this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
362
		$this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
363
	}
364
 
365
/**
366
 * test assertTextEndsNotWith()
367
 *
368
 * @return void
369
 */
370
	public function testAssertTextEndsNotWith() {
371
		$stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
372
		$stringClean = "some\nstring\nwith\ndifferent\nline endings!";
373
 
374
		$this->assertStringEndsNotWith("different\nline endings", $stringDirty);
375
		$this->assertTextEndsNotWith("different\rline endings", $stringDirty);
376
	}
377
 
378
/**
379
 * test assertTextContains()
380
 *
381
 * @return void
382
 */
383
	public function testAssertTextContains() {
384
		$stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
385
		$stringClean = "some\nstring\nwith\ndifferent\nline endings!";
386
 
387
		$this->assertContains("different", $stringDirty);
388
		$this->assertNotContains("different\rline", $stringDirty);
389
 
390
		$this->assertTextContains("different\rline", $stringDirty);
391
	}
392
 
393
/**
394
 * test assertTextNotContains()
395
 *
396
 * @return void
397
 */
398
	public function testAssertTextNotContains() {
399
		$stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
400
		$stringClean = "some\nstring\nwith\ndifferent\nline endings!";
401
 
402
		$this->assertTextNotContains("different\rlines", $stringDirty);
403
	}
404
 
405
/**
406
 * test getMockForModel()
407
 *
408
 * @return void
409
 */
410
	public function testGetMockForModel() {
411
		App::build(array(
412
			'Model' => array(
413
				CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS
414
			)
415
		), App::RESET);
416
		$Post = $this->getMockForModel('Post');
417
		$this->assertEquals('test', $Post->useDbConfig);
418
		$this->assertInstanceOf('Post', $Post);
419
		$this->assertNull($Post->save(array()));
420
		$this->assertNull($Post->find('all'));
421
		$this->assertEquals('posts', $Post->useTable);
422
 
423
		$Post = $this->getMockForModel('Post', array('save'));
424
 
425
		$this->assertNull($Post->save(array()));
426
		$this->assertInternalType('array', $Post->find('all'));
427
	}
428
 
429
/**
430
 * Test getMockForModel on secondary datasources.
431
 *
432
 * @return void
433
 */
434
	public function testGetMockForModelSecondaryDatasource() {
435
		App::build(array(
436
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
437
			'Model/Datasource/Database' => array(
438
				CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS . 'Database' . DS
439
			)
440
		), App::RESET);
441
		CakePlugin::load('TestPlugin');
442
		ConnectionManager::create('test_secondary', array(
443
			'datasource' => 'Database/TestLocalDriver'
444
		));
445
		$post = $this->getMockForModel('SecondaryPost', array('save'));
446
		$this->assertEquals('test_secondary', $post->useDbConfig);
447
		ConnectionManager::drop('test_secondary');
448
	}
449
 
450
/**
451
 * test getMockForModel() with plugin models
452
 *
453
 * @return void
454
 */
455
	public function testGetMockForModelWithPlugin() {
456
		App::build(array(
457
			'Plugin' => array(
458
				CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
459
			)
460
		), App::RESET);
461
		CakePlugin::load('TestPlugin');
462
		$this->getMockForModel('TestPlugin.TestPluginAppModel');
463
		$TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComment');
464
 
465
		$result = ClassRegistry::init('TestPlugin.TestPluginComment');
466
		$this->assertInstanceOf('TestPluginComment', $result);
467
		$this->assertEquals('test', $result->useDbConfig);
468
 
469
		$TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComment', array('save'));
470
 
471
		$this->assertInstanceOf('TestPluginComment', $TestPluginComment);
472
		$TestPluginComment->expects($this->at(0))
473
			->method('save')
474
			->will($this->returnValue(true));
475
		$TestPluginComment->expects($this->at(1))
476
			->method('save')
477
			->will($this->returnValue(false));
478
		$this->assertTrue($TestPluginComment->save(array()));
479
		$this->assertFalse($TestPluginComment->save(array()));
480
	}
481
 
482
/**
483
 * testGetMockForModelModel
484
 *
485
 * @return void
486
 */
487
	public function testGetMockForModelModel() {
488
		$Mock = $this->getMockForModel('Model', array('save', 'setDataSource'), array('name' => 'Comment'));
489
 
490
		$result = ClassRegistry::init('Comment');
491
		$this->assertInstanceOf('Model', $result);
492
 
493
		$Mock->expects($this->at(0))
494
			->method('save')
495
			->will($this->returnValue(true));
496
		$Mock->expects($this->at(1))
497
			->method('save')
498
			->will($this->returnValue(false));
499
 
500
		$this->assertTrue($Mock->save(array()));
501
		$this->assertFalse($Mock->save(array()));
502
	}
503
 
504
/**
505
 * testGetMockForModelDoesNotExist
506
 *
507
 * @expectedException MissingModelException
508
 * @expectedExceptionMessage Model IDoNotExist could not be found
509
 * @return void
510
 */
511
	public function testGetMockForModelDoesNotExist() {
512
		$this->getMockForModel('IDoNotExist');
513
	}
514
}