Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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