Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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