3
* FixtureTask Test case
7
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://cakephp.org CakePHP(tm) Project
16
* @subpackage cake.tests.cases.console.libs.tasks
17
* @since CakePHP(tm) v 1.3
18
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20
App::import('Shell', 'Shell', false);
22
if (!defined('DISABLE_AUTO_DISPATCH')) {
23
define('DISABLE_AUTO_DISPATCH', true);
26
if (!class_exists('ShellDispatcher')) {
29
require CAKE . 'console' . DS . 'cake.php';
33
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php';
34
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php';
36
Mock::generatePartial(
37
'ShellDispatcher', 'TestFixtureTaskMockShellDispatcher',
38
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
41
Mock::generatePartial(
42
'FixtureTask', 'MockFixtureTask',
43
array('in', 'out', 'err', 'createFile', '_stop')
46
Mock::generatePartial(
47
'Shell', 'MockFixtureModelTask',
48
array('in', 'out', 'err', 'createFile', '_stop', 'getName', 'getTable', 'listAll')
52
* FixtureTaskTest class
55
* @subpackage cake.tests.cases.console.libs.tasks
57
class FixtureTaskTest extends CakeTestCase {
65
var $fixtures = array('core.article', 'core.comment', 'core.datatype', 'core.binary_test');
73
function startTest() {
74
$this->Dispatcher =& new TestFixtureTaskMockShellDispatcher();
75
$this->Task =& new MockFixtureTask();
76
$this->Task->Model =& new MockFixtureModelTask();
77
$this->Task->Dispatch =& $this->Dispatcher;
78
$this->Task->Template =& new TemplateTask($this->Task->Dispatch);
79
$this->Task->Dispatch->shellPaths = App::path('shells');
80
$this->Task->Template->initialize();
90
unset($this->Task, $this->Dispatcher);
91
ClassRegistry::flush();
95
* test that initialize sets the path
100
function testConstruct() {
101
$this->Dispatch->params['working'] = DS . 'my' . DS . 'path';
102
$Task =& new FixtureTask($this->Dispatch);
104
$expected = DS . 'my' . DS . 'path' . DS . 'tests' . DS . 'fixtures' . DS;
105
$this->assertEqual($Task->path, $expected);
109
* test import option array generation
114
function testImportOptions() {
115
$this->Task->setReturnValueAt(0, 'in', 'y');
116
$this->Task->setReturnValueAt(1, 'in', 'y');
118
$result = $this->Task->importOptions('Article');
119
$expected = array('schema' => 'Article', 'records' => true);
120
$this->assertEqual($result, $expected);
122
$this->Task->setReturnValueAt(2, 'in', 'n');
123
$this->Task->setReturnValueAt(3, 'in', 'n');
124
$this->Task->setReturnValueAt(4, 'in', 'n');
126
$result = $this->Task->importOptions('Article');
128
$this->assertEqual($result, $expected);
130
$this->Task->setReturnValueAt(5, 'in', 'n');
131
$this->Task->setReturnValueAt(6, 'in', 'n');
132
$this->Task->setReturnValueAt(7, 'in', 'y');
133
$result = $this->Task->importOptions('Article');
134
$expected = array('fromTable' => true);
135
$this->assertEqual($result, $expected);
139
* test generating a fixture with database conditions.
144
function testImportRecordsFromDatabaseWithConditions() {
145
$this->Task->interactive = true;
146
$this->Task->setReturnValueAt(0, 'in', 'WHERE 1=1 LIMIT 10');
147
$this->Task->connection = 'test_suite';
148
$this->Task->path = '/my/path/';
149
$result = $this->Task->bake('Article', false, array('fromTable' => true, 'schema' => 'Article', 'records' => false));
151
$this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
152
$this->assertPattern('/var \$records/', $result);
153
$this->assertPattern('/var \$import/', $result);
154
$this->assertPattern("/'title' => 'First Article'/", $result, 'Missing import data %s');
155
$this->assertPattern('/Second Article/', $result, 'Missing import data %s');
156
$this->assertPattern('/Third Article/', $result, 'Missing import data %s');
160
* test that execute passes runs bake depending with named model.
165
function testExecuteWithNamedModel() {
166
$this->Task->connection = 'test_suite';
167
$this->Task->path = '/my/path/';
168
$this->Task->args = array('article');
169
$filename = '/my/path/article_fixture.php';
170
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
171
$this->Task->execute();
175
* test that execute passes runs bake depending with named model.
180
function testExecuteWithNamedModelVariations() {
181
$this->Task->connection = 'test_suite';
182
$this->Task->path = '/my/path/';
184
$this->Task->args = array('article');
185
$filename = '/my/path/article_fixture.php';
186
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
187
$this->Task->execute();
189
$this->Task->args = array('articles');
190
$filename = '/my/path/article_fixture.php';
191
$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
192
$this->Task->execute();
194
$this->Task->args = array('Articles');
195
$filename = '/my/path/article_fixture.php';
196
$this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
197
$this->Task->execute();
199
$this->Task->args = array('Article');
200
$filename = '/my/path/article_fixture.php';
201
$this->Task->expectAt(3, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
202
$this->Task->execute();
206
* test that execute runs all() when args[0] = all
211
function testExecuteIntoAll() {
212
$this->Task->connection = 'test_suite';
213
$this->Task->path = '/my/path/';
214
$this->Task->args = array('all');
215
$this->Task->Model->setReturnValue('listAll', array('articles', 'comments'));
217
$filename = '/my/path/article_fixture.php';
218
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
219
$this->Task->execute();
221
$filename = '/my/path/comment_fixture.php';
222
$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class CommentFixture/')));
223
$this->Task->execute();
227
* test using all() with -count and -records
232
function testAllWithCountAndRecordsFlags() {
233
$this->Task->connection = 'test_suite';
234
$this->Task->path = '/my/path/';
235
$this->Task->args = array('all');
236
$this->Task->params = array('count' => 10, 'records' => true);
237
$this->Task->Model->setReturnValue('listAll', array('articles', 'comments'));
239
$filename = '/my/path/article_fixture.php';
240
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/title\' => \'Third Article\'/')));
242
$filename = '/my/path/comment_fixture.php';
243
$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/comment\' => \'First Comment for First Article/')));
244
$this->Task->expectCallCount('createFile', 2);
249
* test interactive mode of execute
254
function testExecuteInteractive() {
255
$this->Task->connection = 'test_suite';
256
$this->Task->path = '/my/path/';
258
$this->Task->setReturnValue('in', 'y');
259
$this->Task->Model->setReturnValue('getName', 'Article');
260
$this->Task->Model->setReturnValue('getTable', 'articles', array('Article'));
262
$filename = '/my/path/article_fixture.php';
263
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
264
$this->Task->execute();
268
* Test that bake works
273
function testBake() {
274
$this->Task->connection = 'test_suite';
275
$this->Task->path = '/my/path/';
277
$result = $this->Task->bake('Article');
278
$this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
279
$this->assertPattern('/var \$fields/', $result);
280
$this->assertPattern('/var \$records/', $result);
281
$this->assertNoPattern('/var \$import/', $result);
283
$result = $this->Task->bake('Article', 'comments');
284
$this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
285
$this->assertPattern('/var \$name \= \'Article\';/', $result);
286
$this->assertPattern('/var \$table \= \'comments\';/', $result);
287
$this->assertPattern('/var \$fields = array\(/', $result);
289
$result = $this->Task->bake('Article', 'comments', array('records' => true));
290
$this->assertPattern("/var \\\$import \= array\('records' \=\> true\);/", $result);
291
$this->assertNoPattern('/var \$records/', $result);
293
$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
294
$this->assertPattern("/var \\\$import \= array\('model' \=\> 'Article'\);/", $result);
295
$this->assertNoPattern('/var \$fields/', $result);
297
$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
298
$this->assertPattern("/var \\\$import \= array\('model' \=\> 'Article'\, 'records' \=\> true\);/", $result);
299
$this->assertNoPattern('/var \$fields/', $result);
300
$this->assertNoPattern('/var \$records/', $result);
304
* test record generation with float and binary types
309
function testRecordGenerationForBinaryAndFloat() {
310
$this->Task->connection = 'test_suite';
311
$this->Task->path = '/my/path/';
313
$result = $this->Task->bake('Article', 'datatypes');
314
$this->assertPattern("/'float_field' => 1/", $result);
316
$result = $this->Task->bake('Article', 'binary_tests');
317
$this->assertPattern("/'data' => 'Lorem ipsum dolor sit amet'/", $result);
321
* Test that file generation includes headers and correct path for plugins.
326
function testGenerateFixtureFile() {
327
$this->Task->connection = 'test_suite';
328
$this->Task->path = '/my/path/';
329
$filename = '/my/path/article_fixture.php';
331
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/Article/')));
332
$result = $this->Task->generateFixtureFile('Article', array());
334
$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/\<\?php(.*)\?\>/ms')));
335
$result = $this->Task->generateFixtureFile('Article', array());
339
* test generating files into plugins.
344
function testGeneratePluginFixtureFile() {
345
$this->Task->connection = 'test_suite';
346
$this->Task->path = '/my/path/';
347
$this->Task->plugin = 'TestFixture';
348
$filename = APP . 'plugins' . DS . 'test_fixture' . DS . 'tests' . DS . 'fixtures' . DS . 'article_fixture.php';
350
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/Article/')));
351
$result = $this->Task->generateFixtureFile('Article', array());