1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
App::uses('AppShell', 'Console/Command'); |
4
|
|
|
App::uses('SeederShell', 'FakeSeeder.Console/Command'); |
5
|
|
|
App::uses('SeederTaskBase', 'FakeSeeder.Console'); |
6
|
|
|
App::uses('ConsoleOutput', 'Console'); |
7
|
|
|
App::uses('ConsoleInput', 'Console'); |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A test implementation of SeederShell with seeders defined |
11
|
|
|
*/ |
12
|
|
|
class SeederShellWithTasks extends SeederShell { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Two seeders configured |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $_seeders = array('Apple', 'Banana'); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* One separate, one duplicated model |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $_modelsToTruncate = array('Pear', 'Banana'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* SederShell Test |
31
|
|
|
* |
32
|
|
|
* @coversDefaultClass SeederShell |
33
|
|
|
*/ |
34
|
|
|
class SeederShellTest extends CakeTestCase { |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The fixtures |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
public $fixtures = array( |
42
|
|
|
'plugin.fake_seeder.apple', |
43
|
|
|
'plugin.fake_seeder.banana', |
44
|
|
|
'plugin.fake_seeder.pear', |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Disable auto loading the fixtures as we rarely need them |
49
|
|
|
* |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
public $autoFixtures = false; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The shell under test |
56
|
|
|
* |
57
|
|
|
* @var null|SeederShell |
58
|
|
|
*/ |
59
|
|
|
protected $_shell = null; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Setup the shell under test |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function setUp() { |
|
|
|
|
67
|
|
|
parent::setUp(); |
68
|
|
|
|
69
|
|
|
$this->_createShellMock( |
70
|
|
|
array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop', '_showInfo', 'dispatchShell', '_getSeederTasks') |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates a shell mock |
76
|
|
|
* |
77
|
|
|
* @param array $methods A list of methods to mock. |
78
|
|
|
* @param string $className Optional name of the seeder shell class to mock. |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
protected function _createShellMock($methods, $className = 'SeederShell') { |
|
|
|
|
82
|
|
|
$out = $this->getMock('ConsoleOutput', array(), array(), '', false); |
83
|
|
|
$in = $this->getMock('ConsoleInput', array(), array(), '', false); |
84
|
|
|
$this->_shell = $this->getMock( |
85
|
|
|
$className, |
86
|
|
|
$methods, |
87
|
|
|
array($out, $out, $in) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test the seedability check on init |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
* @covers ::initialize |
96
|
|
|
* @covers ::_checkSeedable |
97
|
|
|
*/ |
98
|
|
|
public function testNotSeedable() { |
99
|
|
|
Configure::delete('FakeSeeder.seedable'); |
100
|
|
|
$this->_shell->expects($this->at(0))->method('_stop')->with( |
101
|
|
|
$this->equalTo('Seeding is not activated in configuration in "FakeSeeder.seedable"!') |
102
|
|
|
); |
103
|
|
|
$this->_shell->initialize(); |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
$debug = Configure::read('debug'); |
107
|
|
|
Configure::write('debug', 0); |
108
|
|
|
$this->_shell->expects($this->at(1))->method('_stop')->with( |
109
|
|
|
$this->equalTo('Seeding is allowed only in debug mode!') |
110
|
|
|
); |
111
|
|
|
$this->_shell->initialize(); |
112
|
|
|
Configure::write('debug', $debug); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Tests the getOptionParser method |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
* @covers ::getOptionParser |
120
|
|
|
*/ |
121
|
|
|
public function testGetOptionParser() { |
122
|
|
|
$perser = $this->_shell->getOptionParser(); |
123
|
|
|
|
124
|
|
|
$this->assertNotEmpty($perser->description()); |
125
|
|
|
|
126
|
|
|
$arguments = $perser->arguments(); |
127
|
|
|
$this->assertArrayHasKey(0, $arguments); |
128
|
|
|
$arg = $arguments[0]; |
129
|
|
|
$this->assertEquals('model', $arg->name()); |
130
|
|
|
$this->assertFalse($arg->isRequired()); |
131
|
|
|
$this->assertNotEmpty($arg->help()); |
132
|
|
|
|
133
|
|
|
$options = $perser->options(); |
134
|
|
|
|
135
|
|
|
$this->assertArrayHasKey('mode', $options); |
136
|
|
|
$this->assertArrayHasKey('locale', $options); |
137
|
|
|
$this->assertArrayHasKey('records', $options); |
138
|
|
|
$this->assertArrayHasKey('validate', $options); |
139
|
|
|
$this->assertArrayHasKey('seed', $options); |
140
|
|
|
$this->assertArrayHasKey('no-truncate', $options); |
141
|
|
|
|
142
|
|
|
$option = $options['mode']; |
143
|
|
|
$this->assertEquals('mode', $option->name()); |
144
|
|
|
$this->assertAttributeNotEmpty('_help', $option); |
145
|
|
|
$this->assertEquals('m', $option->short()); |
146
|
|
|
$this->assertFalse($option->isBoolean()); |
147
|
|
|
$this->assertEquals('', $option->defaultValue()); |
148
|
|
|
$this->assertAttributeEquals(array('manual', 'auto', 'mixed'), '_choices', $option); |
149
|
|
|
|
150
|
|
|
$option = $options['locale']; |
151
|
|
|
$this->assertEquals('locale', $option->name()); |
152
|
|
|
$this->assertAttributeNotEmpty('_help', $option); |
153
|
|
|
$this->assertEquals('l', $option->short()); |
154
|
|
|
$this->assertFalse($option->isBoolean()); |
155
|
|
|
$this->assertEquals('', $option->defaultValue()); |
156
|
|
|
$this->assertAttributeEmpty('_choices', $option); |
157
|
|
|
|
158
|
|
|
$option = $options['records']; |
159
|
|
|
$this->assertEquals('records', $option->name()); |
160
|
|
|
$this->assertAttributeNotEmpty('_help', $option); |
161
|
|
|
$this->assertEquals('r', $option->short()); |
162
|
|
|
$this->assertFalse($option->isBoolean()); |
163
|
|
|
$this->assertEquals('', $option->defaultValue()); |
164
|
|
|
$this->assertAttributeEmpty('_choices', $option); |
165
|
|
|
|
166
|
|
|
$option = $options['validate']; |
167
|
|
|
$this->assertEquals('validate', $option->name()); |
168
|
|
|
$this->assertAttributeNotEmpty('_help', $option); |
169
|
|
|
$this->assertEmpty($option->short()); |
170
|
|
|
$this->assertFalse($option->isBoolean()); |
171
|
|
|
$this->assertEquals('', $option->defaultValue()); |
172
|
|
|
$this->assertAttributeEquals(array('first', true, false), '_choices', $option); |
173
|
|
|
|
174
|
|
|
$option = $options['seed']; |
175
|
|
|
$this->assertEquals('seed', $option->name()); |
176
|
|
|
$this->assertAttributeNotEmpty('_help', $option); |
177
|
|
|
$this->assertEquals('s', $option->short()); |
178
|
|
|
$this->assertFalse($option->isBoolean()); |
179
|
|
|
$this->assertEquals('', $option->defaultValue()); |
180
|
|
|
$this->assertAttributeEmpty('_choices', $option); |
181
|
|
|
|
182
|
|
|
$option = $options['no-truncate']; |
183
|
|
|
$this->assertEquals('no-truncate', $option->name()); |
184
|
|
|
$this->assertAttributeNotEmpty('_help', $option); |
185
|
|
|
$this->assertEmpty($option->short()); |
186
|
|
|
$this->assertTrue($option->isBoolean()); |
187
|
|
|
$this->assertEquals('', $option->defaultValue()); |
188
|
|
|
$this->assertAttributeEmpty('_choices', $option); |
189
|
|
|
|
190
|
|
|
$this->assertNotEmpty($perser->epilog()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Test the main method when quitting the tasks prompt |
195
|
|
|
* |
196
|
|
|
* @return void |
197
|
|
|
* @covers ::main |
198
|
|
|
* @covers ::_promptForSeeder |
199
|
|
|
*/ |
200
|
|
|
public function testMainQuitPrompt() { |
201
|
|
|
$tasks = array('Apple', 'Banana'); |
202
|
|
|
$this->_shell->expects($this->at(0))->method('_getSeederTasks')->will($this->returnValue($tasks)); |
203
|
|
|
$this->_shell->expects($this->at(1))->method('out')->with( |
204
|
|
|
$this->equalTo('Choose one seeder shell task to execute:') |
205
|
|
|
); |
206
|
|
|
$this->_shell->expects($this->at(2))->method('out')->with( |
207
|
|
|
$this->equalTo('1. Apple') |
208
|
|
|
); |
209
|
|
|
$this->_shell->expects($this->at(3))->method('out')->with( |
210
|
|
|
$this->equalTo('2. Banana') |
211
|
|
|
); |
212
|
|
|
$this->_shell->expects($this->at(4))->method('in')->with( |
213
|
|
|
$this->equalTo("Enter a number from the list above,\n" . |
214
|
|
|
"type in the name of another seeder shell task,\n" . |
215
|
|
|
"type in the name of a model,\n" . |
216
|
|
|
"or 'q' to exit"))->will($this->returnValue('q')); |
217
|
|
|
|
218
|
|
|
$this->_shell->main(); |
219
|
|
|
|
220
|
|
|
$this->assertArrayNotHasKey(0, $this->_shell->args); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Test the main method when providing an invalid seeder name |
225
|
|
|
* |
226
|
|
|
* @return void |
227
|
|
|
* @covers ::main |
228
|
|
|
* @covers ::_promptForSeeder |
229
|
|
|
*/ |
230
|
|
|
public function testMainInvalidSeederPrompt() { |
231
|
|
|
$tasks = array('Apple', 'Banana'); |
232
|
|
|
$this->_shell->expects($this->at(0))->method('_getSeederTasks')->will($this->returnValue($tasks)); |
233
|
|
|
$this->_shell->expects($this->at(4))->method('in')->will($this->returnValue('999999999')); |
234
|
|
|
$this->_shell->expects($this->at(5))->method('err')->with( |
235
|
|
|
$this->equalTo("The seeder shell task name you supplied was empty,\n" . |
236
|
|
|
"or the number you selected was not a valid option. Please try again.") |
237
|
|
|
); |
238
|
|
|
|
239
|
|
|
$this->_shell->main(); |
240
|
|
|
|
241
|
|
|
$this->assertArrayNotHasKey(0, $this->_shell->args); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Test the main method when executing one of the prompted (non-existing) seeders by name |
246
|
|
|
* |
247
|
|
|
* @return void |
248
|
|
|
* @covers ::main |
249
|
|
|
* @covers ::_promptForSeeder |
250
|
|
|
* @covers ::_executeSeederTask |
251
|
|
|
*/ |
252
|
|
View Code Duplication |
public function testMainExecuteByNameSeederPrompt() { |
|
|
|
|
253
|
|
|
$this->_createShellMock( |
254
|
|
|
array('out', 'in', '_getSeederTasks', '_executeSeederTask') |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
$tasks = array('Apple', 'Banana'); |
258
|
|
|
$this->_shell->params['no-truncate'] = 'foo'; |
259
|
|
|
$this->_shell->expects($this->at(0))->method('_getSeederTasks')->will($this->returnValue($tasks)); |
260
|
|
|
$this->_shell->expects($this->at(4))->method('in')->will($this->returnValue('Apple')); |
261
|
|
|
$this->_shell->expects($this->at(5))->method('out')->with( |
262
|
|
|
$this->equalTo('Execute Apple seeder...') |
263
|
|
|
); |
264
|
|
|
$this->_shell->expects($this->at(6))->method('_executeSeederTask')->with( |
265
|
|
|
$this->equalTo('Apple'), $this->equalTo('foo') |
266
|
|
|
); |
267
|
|
|
|
268
|
|
|
$this->_shell->main(); |
269
|
|
|
|
270
|
|
|
$this->assertEquals('Apple', $this->_shell->args[0]); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Test the main method when executing one of the prompted (non-existing) seeders by number |
275
|
|
|
* |
276
|
|
|
* @return void |
277
|
|
|
* @covers ::main |
278
|
|
|
* @covers ::_promptForSeeder |
279
|
|
|
* @covers ::_executeSeederTask |
280
|
|
|
*/ |
281
|
|
View Code Duplication |
public function testMainExecuteByNumberSeederPrompt() { |
|
|
|
|
282
|
|
|
$this->_createShellMock( |
283
|
|
|
array('out', 'in', '_getSeederTasks', '_executeSeederTask') |
284
|
|
|
); |
285
|
|
|
|
286
|
|
|
$tasks = array('Apple', 'Banana'); |
287
|
|
|
$this->_shell->params['no-truncate'] = 'foo'; |
288
|
|
|
$this->_shell->expects($this->at(0))->method('_getSeederTasks')->will($this->returnValue($tasks)); |
289
|
|
|
$this->_shell->expects($this->at(4))->method('in')->will($this->returnValue('1')); |
290
|
|
|
$this->_shell->expects($this->at(5))->method('out')->with( |
291
|
|
|
$this->equalTo('Execute Apple seeder...') |
292
|
|
|
); |
293
|
|
|
$this->_shell->expects($this->at(6))->method('_executeSeederTask')->with( |
294
|
|
|
$this->equalTo('Apple'), $this->equalTo('foo') |
295
|
|
|
); |
296
|
|
|
|
297
|
|
|
$this->_shell->main(); |
298
|
|
|
|
299
|
|
|
$this->assertEquals('Apple', $this->_shell->args[0]); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Test the main method when executing a seeder given by parameter |
304
|
|
|
* |
305
|
|
|
* @return void |
306
|
|
|
* @covers ::main |
307
|
|
|
*/ |
308
|
|
View Code Duplication |
public function testMainExecuteSeederGivenByParameter() { |
|
|
|
|
309
|
|
|
$this->_createShellMock( |
310
|
|
|
array('out', '_executeSeederTask') |
311
|
|
|
); |
312
|
|
|
|
313
|
|
|
$this->_shell->args[0] = 'Apple'; |
314
|
|
|
$this->_shell->params['no-truncate'] = 'foo'; |
315
|
|
|
|
316
|
|
|
$this->_shell->expects($this->at(0))->method('out')->with( |
317
|
|
|
$this->equalTo('Execute Apple seeder...') |
318
|
|
|
); |
319
|
|
|
$this->_shell->expects($this->at(1))->method('_executeSeederTask')->with( |
320
|
|
|
$this->equalTo("Apple"), |
321
|
|
|
$this->_shell->params['no-truncate'] |
322
|
|
|
); |
323
|
|
|
|
324
|
|
|
$this->_shell->main(); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Test the main method when executing a seeder given by parameter when there are seeders defined |
329
|
|
|
* |
330
|
|
|
* Should execute the given seeder nonetheless. |
331
|
|
|
* |
332
|
|
|
* @return void |
333
|
|
|
* @covers ::main |
334
|
|
|
*/ |
335
|
|
View Code Duplication |
public function testMainExecuteSeederGivenByParameterWithDefinedSeeders() { |
|
|
|
|
336
|
|
|
$this->_createShellMock( |
337
|
|
|
array('out', '_executeSeederTask'), |
338
|
|
|
'SeederShellWithTasks' |
339
|
|
|
); |
340
|
|
|
|
341
|
|
|
$this->_shell->args[0] = 'Apple'; |
342
|
|
|
$this->_shell->params['no-truncate'] = 'foo'; |
343
|
|
|
|
344
|
|
|
$this->_shell->expects($this->at(0))->method('out')->with( |
345
|
|
|
$this->equalTo('Execute Apple seeder...') |
346
|
|
|
); |
347
|
|
|
$this->_shell->expects($this->at(1))->method('_executeSeederTask')->with( |
348
|
|
|
$this->equalTo("Apple"), |
349
|
|
|
$this->_shell->params['no-truncate'] |
350
|
|
|
); |
351
|
|
|
|
352
|
|
|
$this->_shell->main(); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Test the _executeSeederTask method when trying to execute an existing seeder |
357
|
|
|
* |
358
|
|
|
* @return void |
359
|
|
|
* @covers ::main |
360
|
|
|
* @covers ::_executeSeederTask |
361
|
|
|
*/ |
362
|
|
|
public function testMainExecuteSeederTaskByName() { |
363
|
|
|
$this->loadFixtures('Apple'); |
364
|
|
|
|
365
|
|
|
$this->_createShellMock( |
366
|
|
|
array('out'), |
367
|
|
|
'SeederShellWithTasks' |
368
|
|
|
); |
369
|
|
|
$this->_shell->Tasks = $this->getMock( |
370
|
|
|
'TaskCollection', |
371
|
|
|
array('load'), |
372
|
|
|
array(), |
373
|
|
|
'', |
374
|
|
|
false |
375
|
|
|
); |
376
|
|
|
$seederTask = $this->getMock( |
377
|
|
|
'SeederTaskBase', |
378
|
|
|
array('initialize', 'execute', 'fieldFormatters') |
379
|
|
|
); |
380
|
|
|
|
381
|
|
|
$this->_shell->Tasks->expects($this->once())->method('load')->with($this->equalTo('AppleSeeder'))->will($this->returnValue($seederTask)); |
382
|
|
|
|
383
|
|
|
$seederTask->expects($this->at(0))->method('initialize'); |
384
|
|
|
$seederTask->expects($this->at(1))->method('execute'); |
385
|
|
|
|
386
|
|
|
$this->_shell->args[0] = 'Apple'; |
387
|
|
|
$this->_shell->params['no-truncate'] = 'foo'; |
388
|
|
|
// Add an additional option parameter |
389
|
|
|
$this->_shell->params['records'] = 50; |
390
|
|
|
|
391
|
|
|
$this->_shell->main(); |
392
|
|
|
|
393
|
|
|
$this->assertAttributeEquals($this->_shell->args, 'args', $seederTask); |
394
|
|
|
$this->assertAttributeEquals($this->_shell->params, 'params', $seederTask); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Test the _executeSeederTask method when trying to execute a non existing seeder |
399
|
|
|
* |
400
|
|
|
* Should execute the built-in DynamicModelSeeder instead. |
401
|
|
|
* |
402
|
|
|
* @return void |
403
|
|
|
* @covers ::main |
404
|
|
|
* @covers ::_executeSeederTask |
405
|
|
|
*/ |
406
|
|
|
public function testMainExecuteSeederTaskDynamicModelSeeder() { |
407
|
|
|
$this->loadFixtures('Apple'); |
408
|
|
|
|
409
|
|
|
$this->_createShellMock( |
410
|
|
|
array('out'), |
411
|
|
|
'SeederShellWithTasks' |
412
|
|
|
); |
413
|
|
|
$this->_shell->Tasks = $this->getMock( |
414
|
|
|
'TaskCollection', |
415
|
|
|
array('load'), |
416
|
|
|
array(), |
417
|
|
|
'', |
418
|
|
|
false |
419
|
|
|
); |
420
|
|
|
$seederTask = $this->getMock( |
421
|
|
|
'SeederTaskBase', |
422
|
|
|
array('initialize', 'execute', 'fieldFormatters') |
423
|
|
|
); |
424
|
|
|
|
425
|
|
|
$this->_shell->Tasks->expects($this->at(0))->method('load')->with($this->equalTo('AppleSeeder'))->will($this->throwException(new MissingTaskException(''))); |
426
|
|
|
$this->_shell->Tasks->expects($this->at(1))->method('load')->with($this->equalTo('FakeSeeder.DynamicModelSeeder'))->will($this->returnValue($seederTask)); |
427
|
|
|
|
428
|
|
|
$seederTask->expects($this->at(0))->method('initialize'); |
429
|
|
|
$seederTask->expects($this->at(1))->method('execute'); |
430
|
|
|
|
431
|
|
|
$this->_shell->args[0] = 'Apple'; |
432
|
|
|
$this->_shell->params['no-truncate'] = 'foo'; |
433
|
|
|
// Add an additional option parameter |
434
|
|
|
$this->_shell->params['records'] = 50; |
435
|
|
|
|
436
|
|
|
$this->_shell->main(); |
437
|
|
|
|
438
|
|
|
$this->assertAttributeEquals($this->_shell->args, 'args', $seederTask); |
439
|
|
|
$this->assertAttributeEquals($this->_shell->params, 'params', $seederTask); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Test the _executeSeederTask method when trying to execute a non existing seeder for which no model/table exists |
444
|
|
|
* |
445
|
|
|
* @return void |
446
|
|
|
* @covers ::main |
447
|
|
|
* @covers ::_executeSeederTask |
448
|
|
|
*/ |
449
|
|
|
public function testMainExecuteSeederTaskNoTable() { |
450
|
|
|
$this->_createShellMock( |
451
|
|
|
array('out', '_loadSeederModel'), |
452
|
|
|
'SeederShellWithTasks' |
453
|
|
|
); |
454
|
|
|
$this->_shell->Tasks = $this->getMock( |
455
|
|
|
'TaskCollection', |
456
|
|
|
array('load'), |
457
|
|
|
array(), |
458
|
|
|
'', |
459
|
|
|
false |
460
|
|
|
); |
461
|
|
|
$seederTask = $this->getMock( |
462
|
|
|
'SeederTaskBase', |
463
|
|
|
array('initialize', 'execute', 'fieldFormatters') |
464
|
|
|
); |
465
|
|
|
|
466
|
|
|
$this->_shell->Tasks->expects($this->once())->method('load')->with($this->equalTo('NonExistingModelSeeder')) |
467
|
|
|
->will($this->throwException(new MissingTaskException(''))); |
468
|
|
|
$this->_shell->expects($this->once())->method('_loadSeederModel')->with($this->equalTo('NonExistingModel')) |
469
|
|
|
->will($this->returnValue(false)); |
470
|
|
|
|
471
|
|
|
$seederTask->expects($this->never())->method('initialize'); |
472
|
|
|
$seederTask->expects($this->never())->method('execute'); |
473
|
|
|
|
474
|
|
|
$this->_shell->args[0] = 'NonExistingModel'; |
475
|
|
|
$this->_shell->params['no-truncate'] = 'foo'; |
476
|
|
|
|
477
|
|
|
$this->_shell->main(); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Test the main method with seeders defined and no truncate |
482
|
|
|
* |
483
|
|
|
* @return void |
484
|
|
|
* @covers ::main |
485
|
|
|
*/ |
486
|
|
View Code Duplication |
public function testMainExecuteDefinedSeedersNoTruncate() { |
|
|
|
|
487
|
|
|
$this->_createShellMock( |
488
|
|
|
array('out', '_truncateModels', '_callSeeders'), |
489
|
|
|
'SeederShellWithTasks' |
490
|
|
|
); |
491
|
|
|
|
492
|
|
|
// Disable truncate |
493
|
|
|
$this->_shell->params['no-truncate'] = true; |
494
|
|
|
|
495
|
|
|
$this->_shell->expects($this->never())->method('_truncateModels'); |
496
|
|
|
$this->_shell->expects($this->at(0))->method('_callSeeders'); |
497
|
|
|
|
498
|
|
|
$this->_shell->main(); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Test the main method with seeders defined with truncate |
503
|
|
|
* |
504
|
|
|
* @return void |
505
|
|
|
* @covers ::main |
506
|
|
|
*/ |
507
|
|
View Code Duplication |
public function testMainExecuteDefinedSeedersWithTruncate() { |
|
|
|
|
508
|
|
|
$this->_createShellMock( |
509
|
|
|
array('out', '_truncateModels', '_callSeeders'), |
510
|
|
|
'SeederShellWithTasks' |
511
|
|
|
); |
512
|
|
|
|
513
|
|
|
// Enable truncate |
514
|
|
|
$this->_shell->params['no-truncate'] = false; |
515
|
|
|
|
516
|
|
|
$this->_shell->expects($this->at(0))->method('_truncateModels'); |
517
|
|
|
$this->_shell->expects($this->at(1))->method('_callSeeders'); |
518
|
|
|
|
519
|
|
|
$this->_shell->main(); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Test the _truncateModels method |
524
|
|
|
* |
525
|
|
|
* @return void |
526
|
|
|
* @covers ::_truncateModels |
527
|
|
|
* @covers ::_getModelsToTruncateFromSeederTask |
528
|
|
|
*/ |
529
|
|
|
public function testTruncateModels() { |
530
|
|
|
$this->loadFixtures('Apple', 'Banana', 'Pear'); |
531
|
|
|
|
532
|
|
|
$this->_createShellMock( |
533
|
|
|
array('out', '_callSeeders', '_getModelTruncator'), |
534
|
|
|
'SeederShellWithTasks' |
535
|
|
|
); |
536
|
|
|
$this->_shell->Tasks = $this->getMock( |
537
|
|
|
'TaskCollection', |
538
|
|
|
array('load'), |
539
|
|
|
array(), |
540
|
|
|
'', |
541
|
|
|
false |
542
|
|
|
); |
543
|
|
|
$seederTask = $this->getMock( |
544
|
|
|
'SeederTaskBase', |
545
|
|
|
array('getModelsToTruncate', 'fieldFormatters') |
546
|
|
|
); |
547
|
|
|
$modelTruncator = $this->getMock( |
548
|
|
|
'ShellModelTruncator', |
549
|
|
|
array('truncateModels'), |
550
|
|
|
array(), |
551
|
|
|
'', |
552
|
|
|
false |
553
|
|
|
); |
554
|
|
|
|
555
|
|
|
$this->_shell->expects($this->at(0))->method('out')->with($this->equalTo('Truncating models...')); |
556
|
|
|
$this->_shell->expects($this->at(1))->method('_getModelTruncator')->will($this->returnValue($modelTruncator)); |
557
|
|
|
$this->_shell->expects($this->at(2))->method('out')->with($this->equalTo('Finished truncating models.')); |
558
|
|
|
|
559
|
|
|
$this->_shell->Tasks->expects($this->at(0))->method('load')->with($this->equalTo('AppleSeeder'))->will($this->returnValue($seederTask)); |
560
|
|
|
$this->_shell->Tasks->expects($this->at(1))->method('load')->with($this->equalTo('BananaSeeder'))->will($this->returnValue($seederTask)); |
561
|
|
|
|
562
|
|
|
$seederTask->expects($this->at(0))->method('getModelsToTruncate')->will($this->returnValue(array('Apple'))); |
563
|
|
|
$seederTask->expects($this->at(1))->method('getModelsToTruncate')->will($this->returnValue(array('Banana'))); |
564
|
|
|
|
565
|
|
|
$modelsToTruncate = array('Apple', 'Banana', 'Pear'); |
566
|
|
|
$modelTruncator->expects($this->at(0))->method('truncateModels')->with($this->equalTo($modelsToTruncate)); |
567
|
|
|
|
568
|
|
|
// Enable truncate |
569
|
|
|
$this->_shell->params['no-truncate'] = false; |
570
|
|
|
|
571
|
|
|
$this->_shell->main(); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* Test the _callSeeders method |
576
|
|
|
* |
577
|
|
|
* @return void |
578
|
|
|
* @covers ::_callSeeders |
579
|
|
|
*/ |
580
|
|
|
public function testCallSeeders() { |
581
|
|
|
$this->_createShellMock( |
582
|
|
|
array('out', '_executeSeederTask'), |
583
|
|
|
'SeederShellWithTasks' |
584
|
|
|
); |
585
|
|
|
|
586
|
|
|
$this->_shell->expects($this->at(0))->method('out')->with($this->equalTo('Execute seeders...')); |
587
|
|
|
$this->_shell->expects($this->at(1))->method('out')->with($this->equalTo('Execute Apple seeder...')); |
588
|
|
|
$this->_shell->expects($this->at(2))->method('_executeSeederTask')->with( |
589
|
|
|
$this->equalTo("Apple"), |
590
|
|
|
true |
591
|
|
|
); |
592
|
|
|
$this->_shell->expects($this->at(3))->method('out')->with($this->equalTo('Execute Banana seeder...')); |
593
|
|
|
$this->_shell->expects($this->at(4))->method('_executeSeederTask')->with( |
594
|
|
|
$this->equalTo("Banana"), |
595
|
|
|
true |
596
|
|
|
); |
597
|
|
|
$this->_shell->expects($this->at(0))->method('out')->with($this->equalTo('Execute seeders...')); |
598
|
|
|
|
599
|
|
|
// Set no-truncate to something else than boolean true, |
600
|
|
|
// so we can verify _executeSeederTask gets called with literal true |
601
|
|
|
$this->_shell->params['no-truncate'] = 'foo'; |
602
|
|
|
|
603
|
|
|
$this->_shell->main(); |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.