Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * FixtureTask Test case
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.Test.Case.Console.Command.Task
15
 * @since         CakePHP(tm) v 1.3
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('ShellDispatcher', 'Console');
20
App::uses('Shell', 'Console');
21
App::uses('ConsoleOutput', 'Console');
22
App::uses('ConsoleInput', 'Console');
23
App::uses('ModelTask', 'Console/Command/Task');
24
App::uses('FixtureTask', 'Console/Command/Task');
25
App::uses('TemplateTask', 'Console/Command/Task');
26
App::uses('DbConfigTask', 'Console/Command/Task');
27
 
28
/**
29
 * FixtureTaskTest class
30
 *
31
 * @package       Cake.Test.Case.Console.Command.Task
32
 */
33
class FixtureTaskTest extends CakeTestCase {
34
 
35
/**
36
 * fixtures
37
 *
38
 * @var array
39
 */
40
	public $fixtures = array('core.article', 'core.comment', 'core.datatype', 'core.binary_test', 'core.user');
41
 
42
/**
43
 * Whether backup global state for each test method or not
44
 *
45
 * @var boolean
46
 */
47
	public $backupGlobals = false;
48
 
49
/**
50
 * setUp method
51
 *
52
 * @return void
53
 */
54
	public function setUp() {
55
		parent::setUp();
56
		$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
57
		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
58
 
59
		$this->Task = $this->getMock('FixtureTask',
60
			array('in', 'err', 'createFile', '_stop', 'clear'),
61
			array($out, $out, $in)
62
		);
63
		$this->Task->Model = $this->getMock('ModelTask',
64
			array('in', 'out', 'err', 'createFile', 'getName', 'getTable', 'listAll'),
65
			array($out, $out, $in)
66
		);
67
		$this->Task->Template = new TemplateTask($out, $out, $in);
68
		$this->Task->DbConfig = $this->getMock('DbConfigTask', array(), array($out, $out, $in));
69
		$this->Task->Template->initialize();
70
	}
71
 
72
/**
73
 * tearDown method
74
 *
75
 * @return void
76
 */
77
	public function tearDown() {
78
		parent::tearDown();
79
		unset($this->Task);
80
	}
81
 
82
/**
83
 * test that initialize sets the path
84
 *
85
 * @return void
86
 */
87
	public function testConstruct() {
88
		$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
89
		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
90
 
91
		$Task = new FixtureTask($out, $out, $in);
92
		$this->assertEquals(APP . 'Test' . DS . 'Fixture' . DS, $Task->path);
93
	}
94
 
95
/**
96
 * test import option array generation
97
 *
98
 * @return void
99
 */
100
	public function testImportOptionsSchemaRecords() {
101
		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
102
		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
103
 
104
		$result = $this->Task->importOptions('Article');
105
		$expected = array('schema' => 'Article', 'records' => true);
106
		$this->assertEquals($expected, $result);
107
	}
108
 
109
/**
110
 * test importOptions choosing nothing.
111
 *
112
 * @return void
113
 */
114
	public function testImportOptionsNothing() {
115
		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
116
		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
117
		$this->Task->expects($this->at(2))->method('in')->will($this->returnValue('n'));
118
 
119
		$result = $this->Task->importOptions('Article');
120
		$expected = array();
121
		$this->assertEquals($expected, $result);
122
	}
123
 
124
/**
125
 * test importOptions with overwriting command line options.
126
 *
127
 * @return void
128
 */
129
	public function testImportOptionsWithCommandLineOptions() {
130
		$this->Task->params = array('schema' => true, 'records' => true);
131
 
132
		$result = $this->Task->importOptions('Article');
133
		$expected = array('schema' => 'Article', 'records' => true);
134
		$this->assertEquals($expected, $result);
135
	}
136
 
137
/**
138
 * test importOptions with schema.
139
 *
140
 * @return void
141
 */
142
	public function testImportOptionsWithSchema() {
143
		$this->Task->params = array('schema' => true);
144
		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
145
		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
146
 
147
		$result = $this->Task->importOptions('Article');
148
		$expected = array('schema' => 'Article');
149
		$this->assertEquals($expected, $result);
150
	}
151
 
152
/**
153
 * test importOptions with records.
154
 *
155
 * @return void
156
 */
157
	public function testImportOptionsWithRecords() {
158
		$this->Task->params = array('records' => true);
159
		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
160
 
161
		$result = $this->Task->importOptions('Article');
162
		$expected = array('records' => true);
163
		$this->assertEquals($expected, $result);
164
	}
165
 
166
/**
167
 * test importOptions choosing from Table.
168
 *
169
 * @return void
170
 */
171
	public function testImportOptionsTable() {
172
		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
173
		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
174
		$this->Task->expects($this->at(2))->method('in')->will($this->returnValue('y'));
175
		$result = $this->Task->importOptions('Article');
176
		$expected = array('fromTable' => true);
177
		$this->assertEquals($expected, $result);
178
	}
179
 
180
/**
181
 * test generating a fixture with database conditions.
182
 *
183
 * @return void
184
 */
185
	public function testImportRecordsFromDatabaseWithConditionsPoo() {
186
		$this->Task->interactive = true;
187
		$this->Task->expects($this->at(0))->method('in')
188
			->will($this->returnValue('WHERE 1=1'));
189
 
190
		$this->Task->connection = 'test';
191
		$this->Task->path = '/my/path/';
192
 
193
		$result = $this->Task->bake('Article', false, array(
194
			'fromTable' => true, 'schema' => 'Article', 'records' => false
195
		));
196
 
197
		$this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
198
		$this->assertContains('public $records', $result);
199
		$this->assertContains('public $import', $result);
200
		$this->assertContains("'title' => 'First Article'", $result, 'Missing import data %s');
201
		$this->assertContains('Second Article', $result, 'Missing import data %s');
202
		$this->assertContains('Third Article', $result, 'Missing import data %s');
203
	}
204
 
205
/**
206
 * test that connection gets set to the import options when a different connection is used.
207
 *
208
 * @return void
209
 */
210
	public function testImportOptionsAlternateConnection() {
211
		$this->Task->connection = 'test';
212
		$result = $this->Task->bake('Article', false, array('schema' => 'Article'));
213
		$this->assertContains("'connection' => 'test'", $result);
214
	}
215
 
216
/**
217
 * Ensure that fixture data doesn't get overly escaped.
218
 *
219
 * @return void
220
 */
221
	public function testImportRecordsNoEscaping() {
222
		$db = ConnectionManager::getDataSource('test');
223
		if ($db instanceof Sqlserver) {
224
			$this->markTestSkipped('This test does not run on SQLServer');
225
		}
226
 
227
		$Article = ClassRegistry::init('Article');
228
		$Article->updateAll(array('body' => "'Body \"value\"'"));
229
 
230
		$this->Task->interactive = true;
231
		$this->Task->expects($this->at(0))
232
			->method('in')
233
			->will($this->returnValue('WHERE 1=1 LIMIT 10'));
234
 
235
		$this->Task->connection = 'test';
236
		$this->Task->path = '/my/path/';
237
		$result = $this->Task->bake('Article', false, array(
238
			'fromTable' => true,
239
			'schema' => 'Article',
240
			'records' => false
241
		));
242
		$this->assertContains("'body' => 'Body \"value\"'", $result, 'Data has bad escaping');
243
	}
244
 
245
/**
246
 * test that execute passes runs bake depending with named model.
247
 *
248
 *
249
 * @return void
250
 */
251
	public function testExecuteWithNamedModel() {
252
		$this->Task->connection = 'test';
253
		$this->Task->path = '/my/path/';
254
		$this->Task->args = array('article');
255
		$filename = '/my/path/ArticleFixture.php';
256
 
257
		$this->Task->expects($this->at(0))->method('createFile')
258
			->with($filename, $this->stringContains('class ArticleFixture'));
259
 
260
		$this->Task->execute();
261
	}
262
 
263
/**
264
 * test that execute runs all() when args[0] = all
265
 *
266
 * @return void
267
 */
268
	public function testExecuteIntoAll() {
269
		$this->Task->connection = 'test';
270
		$this->Task->path = '/my/path/';
271
		$this->Task->args = array('all');
272
		$this->Task->Model->expects($this->any())
273
			->method('listAll')
274
			->will($this->returnValue(array('articles', 'comments')));
275
 
276
		$filename = '/my/path/ArticleFixture.php';
277
		$this->Task->expects($this->at(0))
278
			->method('createFile')
279
			->with($filename, $this->stringContains('class ArticleFixture'));
280
 
281
		$filename = '/my/path/CommentFixture.php';
282
		$this->Task->expects($this->at(1))
283
			->method('createFile')
284
			->with($filename, $this->stringContains('class CommentFixture'));
285
 
286
		$this->Task->execute();
287
	}
288
 
289
/**
290
 * test using all() with -count and -records
291
 *
292
 * @return void
293
 */
294
	public function testAllWithCountAndRecordsFlags() {
295
		$this->Task->connection = 'test';
296
		$this->Task->path = '/my/path/';
297
		$this->Task->args = array('all');
298
		$this->Task->params = array('count' => 10, 'records' => true);
299
 
300
		$this->Task->Model->expects($this->any())->method('listAll')
301
			->will($this->returnValue(array('Articles', 'comments')));
302
 
303
		$filename = '/my/path/ArticleFixture.php';
304
		$this->Task->expects($this->at(0))->method('createFile')
305
			->with($filename, $this->stringContains("'title' => 'Third Article'"));
306
 
307
		$filename = '/my/path/CommentFixture.php';
308
		$this->Task->expects($this->at(1))->method('createFile')
309
			->with($filename, $this->stringContains("'comment' => 'First Comment for First Article'"));
310
		$this->Task->expects($this->exactly(2))->method('createFile');
311
 
312
		$this->Task->all();
313
	}
314
 
315
/**
316
 * test using all() with -schema
317
 *
318
 * @return void
319
 */
320
	public function testAllWithSchemaImport() {
321
		$this->Task->connection = 'test';
322
		$this->Task->path = '/my/path/';
323
		$this->Task->args = array('all');
324
		$this->Task->params = array('schema' => true);
325
 
326
		$this->Task->Model->expects($this->any())->method('listAll')
327
			->will($this->returnValue(array('Articles', 'comments')));
328
 
329
		$filename = '/my/path/ArticleFixture.php';
330
		$this->Task->expects($this->at(0))->method('createFile')
331
			->with($filename, $this->stringContains('public $import = array(\'model\' => \'Article\''));
332
 
333
		$filename = '/my/path/CommentFixture.php';
334
		$this->Task->expects($this->at(1))->method('createFile')
335
			->with($filename, $this->stringContains('public $import = array(\'model\' => \'Comment\''));
336
		$this->Task->expects($this->exactly(2))->method('createFile');
337
 
338
		$this->Task->all();
339
	}
340
 
341
/**
342
 * test interactive mode of execute
343
 *
344
 * @return void
345
 */
346
	public function testExecuteInteractive() {
347
		$this->Task->connection = 'test';
348
		$this->Task->path = '/my/path/';
349
 
350
		$this->Task->expects($this->any())->method('in')->will($this->returnValue('y'));
351
		$this->Task->Model->expects($this->any())->method('getName')->will($this->returnValue('Article'));
352
		$this->Task->Model->expects($this->any())->method('getTable')
353
			->with('Article')
354
			->will($this->returnValue('articles'));
355
 
356
		$filename = '/my/path/ArticleFixture.php';
357
		$this->Task->expects($this->once())->method('createFile')
358
			->with($filename, $this->stringContains('class ArticleFixture'));
359
 
360
		$this->Task->execute();
361
	}
362
 
363
/**
364
 * Test that bake works
365
 *
366
 * @return void
367
 */
368
	public function testBake() {
369
		$this->Task->connection = 'test';
370
		$this->Task->path = '/my/path/';
371
 
372
		$result = $this->Task->bake('Article');
373
		$this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
374
		$this->assertContains('public $fields', $result);
375
		$this->assertContains('public $records', $result);
376
		$this->assertNotContains('public $import', $result);
377
 
378
		$result = $this->Task->bake('Article', 'comments');
379
		$this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
380
		$this->assertContains('public $table = \'comments\';', $result);
381
		$this->assertContains('public $fields = array(', $result);
382
 
383
		$result = $this->Task->bake('Article', 'comments', array('records' => true));
384
		$this->assertContains("public \$import = array('records' => true, 'connection' => 'test');", $result);
385
		$this->assertNotContains('public $records', $result);
386
 
387
		$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
388
		$this->assertContains("public \$import = array('model' => 'Article', 'connection' => 'test');", $result);
389
		$this->assertNotContains('public $fields', $result);
390
 
391
		$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
392
		$this->assertContains("public \$import = array('model' => 'Article', 'records' => true, 'connection' => 'test');", $result);
393
		$this->assertNotContains('public $fields', $result);
394
		$this->assertNotContains('public $records', $result);
395
	}
396
 
397
/**
398
 * test record generation with float and binary types
399
 *
400
 * @return void
401
 */
402
	public function testRecordGenerationForBinaryAndFloat() {
403
		$this->Task->connection = 'test';
404
		$this->Task->path = '/my/path/';
405
 
406
		$result = $this->Task->bake('Article', 'datatypes');
407
		$this->assertContains("'float_field' => 1", $result);
408
		$this->assertContains("'bool' => 1", $result);
409
 
410
		$result = $this->Task->bake('Article', 'binary_tests');
411
		$this->assertContains("'data' => 'Lorem ipsum dolor sit amet'", $result);
412
	}
413
 
414
/**
415
 * Test that file generation includes headers and correct path for plugins.
416
 *
417
 * @return void
418
 */
419
	public function testGenerateFixtureFile() {
420
		$this->Task->connection = 'test';
421
		$this->Task->path = '/my/path/';
422
		$filename = '/my/path/ArticleFixture.php';
423
 
424
		$this->Task->expects($this->at(0))->method('createFile')
425
			->with($filename, $this->stringContains('ArticleFixture'));
426
 
427
		$this->Task->expects($this->at(1))->method('createFile')
428
			->with($filename, $this->stringContains('<?php'));
429
 
430
		$result = $this->Task->generateFixtureFile('Article', array());
431
 
432
		$result = $this->Task->generateFixtureFile('Article', array());
433
	}
434
 
435
/**
436
 * test generating files into plugins.
437
 *
438
 * @return void
439
 */
440
	public function testGeneratePluginFixtureFile() {
441
		$this->Task->connection = 'test';
442
		$this->Task->path = '/my/path/';
443
		$this->Task->plugin = 'TestFixture';
444
		$filename = APP . 'Plugin' . DS . 'TestFixture' . DS . 'Test' . DS . 'Fixture' . DS . 'ArticleFixture.php';
445
 
446
		//fake plugin path
447
		CakePlugin::load('TestFixture', array('path' => APP . 'Plugin' . DS . 'TestFixture' . DS));
448
		$this->Task->expects($this->at(0))->method('createFile')
449
			->with($filename, $this->stringContains('class Article'));
450
 
451
		$this->Task->generateFixtureFile('Article', array());
452
		CakePlugin::unload();
453
	}
454
 
455
}