Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 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 bool
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
		$this->Task->expects($this->at(1))->method('in')
190
			->with($this->anything(), $this->anything(), '3')
191
			->will($this->returnValue('2'));
192
 
193
		$this->Task->connection = 'test';
194
		$this->Task->path = '/my/path/';
195
 
196
		$result = $this->Task->bake('Article', false, array(
197
			'fromTable' => true, 'schema' => 'Article', 'records' => false
198
		));
199
 
200
		$this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
201
		$this->assertContains('public $records', $result);
202
		$this->assertContains('public $import', $result);
203
		$this->assertContains("'title' => 'First Article'", $result, 'Missing import data');
204
		$this->assertContains('Second Article', $result, 'Missing import data');
205
	}
206
 
207
/**
208
 * test that connection gets set to the import options when a different connection is used.
209
 *
210
 * @return void
211
 */
212
	public function testImportOptionsAlternateConnection() {
213
		$this->Task->connection = 'test';
214
		$result = $this->Task->bake('Article', false, array('schema' => 'Article'));
215
		$this->assertContains("'connection' => 'test'", $result);
216
	}
217
 
218
/**
219
 * Ensure that fixture data doesn't get overly escaped.
220
 *
221
 * @return void
222
 */
223
	public function testImportRecordsNoEscaping() {
224
		$db = ConnectionManager::getDataSource('test');
225
		if ($db instanceof Sqlserver) {
226
			$this->markTestSkipped('This test does not run on SQLServer');
227
		}
228
 
229
		$Article = ClassRegistry::init('Article');
230
		$Article->updateAll(array('body' => "'Body \"value\"'"));
231
 
232
		$this->Task->interactive = true;
233
		$this->Task->expects($this->at(0))
234
			->method('in')
235
			->will($this->returnValue('WHERE 1=1 LIMIT 10'));
236
 
237
		$this->Task->connection = 'test';
238
		$this->Task->path = '/my/path/';
239
		$result = $this->Task->bake('Article', false, array(
240
			'fromTable' => true,
241
			'schema' => 'Article',
242
			'records' => false
243
		));
244
		$this->assertContains("'body' => 'Body \"value\"'", $result, 'Data has bad escaping');
245
	}
246
 
247
/**
248
 * test that execute passes runs bake depending with named model.
249
 *
250
 * @return void
251
 */
252
	public function testExecuteWithNamedModel() {
253
		$this->Task->connection = 'test';
254
		$this->Task->path = '/my/path/';
255
		$this->Task->args = array('article');
256
		$filename = '/my/path/ArticleFixture.php';
257
 
258
		$this->Task->expects($this->at(0))->method('createFile')
259
			->with($filename, $this->stringContains('class ArticleFixture'));
260
 
261
		$this->Task->execute();
262
	}
263
 
264
/**
265
 * test that execute runs all() when args[0] = all
266
 *
267
 * @return void
268
 */
269
	public function testExecuteIntoAll() {
270
		$this->Task->connection = 'test';
271
		$this->Task->path = '/my/path/';
272
		$this->Task->args = array('all');
273
		$this->Task->Model->expects($this->any())
274
			->method('listAll')
275
			->will($this->returnValue(array('articles', 'comments')));
276
 
277
		$filename = '/my/path/ArticleFixture.php';
278
		$this->Task->expects($this->at(0))
279
			->method('createFile')
280
			->with($filename, $this->stringContains('class ArticleFixture'));
281
 
282
		$filename = '/my/path/CommentFixture.php';
283
		$this->Task->expects($this->at(1))
284
			->method('createFile')
285
			->with($filename, $this->stringContains('class CommentFixture'));
286
 
287
		$this->Task->execute();
288
	}
289
 
290
/**
291
 * test using all() with -count and -records
292
 *
293
 * @return void
294
 */
295
	public function testAllWithCountAndRecordsFlags() {
296
		$this->Task->connection = 'test';
297
		$this->Task->path = '/my/path/';
298
		$this->Task->args = array('all');
299
		$this->Task->params = array('count' => 10, 'records' => true);
300
 
301
		$this->Task->Model->expects($this->any())->method('listAll')
302
			->will($this->returnValue(array('Articles', 'comments')));
303
 
304
		$filename = '/my/path/ArticleFixture.php';
305
		$this->Task->expects($this->at(0))->method('createFile')
306
			->with($filename, $this->stringContains("'title' => 'Third Article'"));
307
 
308
		$filename = '/my/path/CommentFixture.php';
309
		$this->Task->expects($this->at(1))->method('createFile')
310
			->with($filename, $this->stringContains("'comment' => 'First Comment for First Article'"));
311
		$this->Task->expects($this->exactly(2))->method('createFile');
312
 
313
		$this->Task->all();
314
	}
315
 
316
/**
317
 * test using all() with -schema
318
 *
319
 * @return void
320
 */
321
	public function testAllWithSchemaImport() {
322
		$this->Task->connection = 'test';
323
		$this->Task->path = '/my/path/';
324
		$this->Task->args = array('all');
325
		$this->Task->params = array('schema' => true);
326
 
327
		$this->Task->Model->expects($this->any())->method('listAll')
328
			->will($this->returnValue(array('Articles', 'comments')));
329
 
330
		$filename = '/my/path/ArticleFixture.php';
331
		$this->Task->expects($this->at(0))->method('createFile')
332
			->with($filename, $this->stringContains('public $import = array(\'model\' => \'Article\''));
333
 
334
		$filename = '/my/path/CommentFixture.php';
335
		$this->Task->expects($this->at(1))->method('createFile')
336
			->with($filename, $this->stringContains('public $import = array(\'model\' => \'Comment\''));
337
		$this->Task->expects($this->exactly(2))->method('createFile');
338
 
339
		$this->Task->all();
340
	}
341
 
342
/**
343
 * test interactive mode of execute
344
 *
345
 * @return void
346
 */
347
	public function testExecuteInteractive() {
348
		$this->Task->connection = 'test';
349
		$this->Task->path = '/my/path/';
350
 
351
		$this->Task->expects($this->any())->method('in')->will($this->returnValue('y'));
352
		$this->Task->Model->expects($this->any())->method('getName')->will($this->returnValue('Article'));
353
		$this->Task->Model->expects($this->any())->method('getTable')
354
			->with('Article')
355
			->will($this->returnValue('articles'));
356
 
357
		$filename = '/my/path/ArticleFixture.php';
358
		$this->Task->expects($this->once())->method('createFile')
359
			->with($filename, $this->stringContains('class ArticleFixture'));
360
 
361
		$this->Task->execute();
362
	}
363
 
364
/**
365
 * Test that bake works
366
 *
367
 * @return void
368
 */
369
	public function testBake() {
370
		$this->Task->connection = 'test';
371
		$this->Task->path = '/my/path/';
372
 
373
		$result = $this->Task->bake('Article');
374
		$this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
375
		$this->assertContains('public $fields', $result);
376
		$this->assertContains('public $records', $result);
377
		$this->assertNotContains('public $import', $result);
378
 
379
		$result = $this->Task->bake('Article', 'comments');
380
		$this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
381
		$this->assertContains('public $table = \'comments\';', $result);
382
		$this->assertContains('public $fields = array(', $result);
383
 
384
		$result = $this->Task->bake('Article', 'comments', array('records' => true));
385
		$this->assertContains("public \$import = array('records' => true, 'connection' => 'test');", $result);
386
		$this->assertNotContains('public $records', $result);
387
 
388
		$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
389
		$this->assertContains("public \$import = array('model' => 'Article', 'connection' => 'test');", $result);
390
		$this->assertNotContains('public $fields', $result);
391
 
392
		$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
393
		$this->assertContains("public \$import = array('model' => 'Article', 'records' => true, 'connection' => 'test');", $result);
394
		$this->assertNotContains('public $fields', $result);
395
		$this->assertNotContains('public $records', $result);
396
	}
397
 
398
/**
399
 * test record generation with float and binary types
400
 *
401
 * @return void
402
 */
403
	public function testRecordGenerationForBinaryAndFloat() {
404
		$this->Task->connection = 'test';
405
		$this->Task->path = '/my/path/';
406
 
407
		$result = $this->Task->bake('Article', 'datatypes');
408
		$this->assertContains("'float_field' => 1", $result);
409
		$this->assertContains("'bool' => 1", $result);
410
 
411
		$result = $this->Task->bake('Article', 'binary_tests');
412
		$this->assertContains("'data' => 'Lorem ipsum dolor sit amet'", $result);
413
	}
414
 
415
/**
416
 * Test that file generation includes headers and correct path for plugins.
417
 *
418
 * @return void
419
 */
420
	public function testGenerateFixtureFile() {
421
		$this->Task->connection = 'test';
422
		$this->Task->path = '/my/path/';
423
		$filename = '/my/path/ArticleFixture.php';
424
 
425
		$this->Task->expects($this->at(0))->method('createFile')
426
			->with($filename, $this->stringContains('ArticleFixture'));
427
 
428
		$this->Task->expects($this->at(1))->method('createFile')
429
			->with($filename, $this->stringContains('<?php'));
430
 
431
		$this->Task->generateFixtureFile('Article', array());
432
		$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
}