~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/cases/console/libs/tasks/fixture.test.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * FixtureTask Test case
 
4
 *
 
5
 * PHP versions 4 and 5
 
6
 *
 
7
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 
8
 * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
9
 *
 
10
 * Licensed under The MIT License
 
11
 * Redistributions of files must retain the above copyright notice.
 
12
 *
 
13
 * @copyright     Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
14
 * @link          http://cakephp.org CakePHP(tm) Project
 
15
 * @package       cake
 
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)
 
19
 */
 
20
App::import('Shell', 'Shell', false);
 
21
 
 
22
if (!defined('DISABLE_AUTO_DISPATCH')) {
 
23
        define('DISABLE_AUTO_DISPATCH', true);
 
24
}
 
25
 
 
26
if (!class_exists('ShellDispatcher')) {
 
27
        ob_start();
 
28
        $argv = false;
 
29
        require CAKE . 'console' .  DS . 'cake.php';
 
30
        ob_end_clean();
 
31
}
 
32
 
 
33
require_once CAKE . 'console' .  DS . 'libs' . DS . 'tasks' . DS . 'template.php';
 
34
require_once CAKE . 'console' .  DS . 'libs' . DS . 'tasks' . DS . 'fixture.php';
 
35
 
 
36
Mock::generatePartial(
 
37
        'ShellDispatcher', 'TestFixtureTaskMockShellDispatcher',
 
38
        array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
 
39
);
 
40
 
 
41
Mock::generatePartial(
 
42
        'FixtureTask', 'MockFixtureTask',
 
43
        array('in', 'out', 'err', 'createFile', '_stop')
 
44
);
 
45
 
 
46
Mock::generatePartial(
 
47
        'Shell', 'MockFixtureModelTask',
 
48
        array('in', 'out', 'err', 'createFile', '_stop', 'getName', 'getTable', 'listAll')
 
49
);
 
50
 
 
51
/**
 
52
 * FixtureTaskTest class
 
53
 *
 
54
 * @package       cake
 
55
 * @subpackage    cake.tests.cases.console.libs.tasks
 
56
 */
 
57
class FixtureTaskTest extends CakeTestCase {
 
58
 
 
59
/**
 
60
 * fixtures
 
61
 *
 
62
 * @var array
 
63
 * @access public
 
64
 */
 
65
        var $fixtures = array('core.article', 'core.comment', 'core.datatype', 'core.binary_test');
 
66
 
 
67
/**
 
68
 * startTest method
 
69
 *
 
70
 * @return void
 
71
 * @access public
 
72
 */
 
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();
 
81
        }
 
82
 
 
83
/**
 
84
 * endTest method
 
85
 *
 
86
 * @return void
 
87
 * @access public
 
88
 */
 
89
        function endTest() {
 
90
                unset($this->Task, $this->Dispatcher);
 
91
                ClassRegistry::flush();
 
92
        }
 
93
 
 
94
/**
 
95
 * test that initialize sets the path
 
96
 *
 
97
 * @return void
 
98
 * @access public
 
99
 */
 
100
        function testConstruct() {
 
101
                $this->Dispatch->params['working'] = DS . 'my' . DS . 'path';
 
102
                $Task =& new FixtureTask($this->Dispatch);
 
103
 
 
104
                $expected = DS . 'my' . DS . 'path' . DS . 'tests' . DS . 'fixtures' . DS;
 
105
                $this->assertEqual($Task->path, $expected);
 
106
        }
 
107
 
 
108
/**
 
109
 * test import option array generation
 
110
 *
 
111
 * @return void
 
112
 * @access public
 
113
 */
 
114
        function testImportOptions() {
 
115
                $this->Task->setReturnValueAt(0, 'in', 'y');
 
116
                $this->Task->setReturnValueAt(1, 'in', 'y');
 
117
 
 
118
                $result = $this->Task->importOptions('Article');
 
119
                $expected = array('schema' => 'Article', 'records' => true);
 
120
                $this->assertEqual($result, $expected);
 
121
 
 
122
                $this->Task->setReturnValueAt(2, 'in', 'n');
 
123
                $this->Task->setReturnValueAt(3, 'in', 'n');
 
124
                $this->Task->setReturnValueAt(4, 'in', 'n');
 
125
 
 
126
                $result = $this->Task->importOptions('Article');
 
127
                $expected = array();
 
128
                $this->assertEqual($result, $expected);
 
129
 
 
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);
 
136
        }
 
137
 
 
138
/**
 
139
 * test generating a fixture with database conditions.
 
140
 *
 
141
 * @return void
 
142
 * @access public
 
143
 */
 
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));
 
150
 
 
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');
 
157
        }
 
158
 
 
159
/**
 
160
 * test that execute passes runs bake depending with named model.
 
161
 *
 
162
 * @return void
 
163
 * @access public
 
164
 */
 
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();
 
172
        }
 
173
 
 
174
/**
 
175
 * test that execute passes runs bake depending with named model.
 
176
 *
 
177
 * @return void
 
178
 * @access public
 
179
 */
 
180
        function testExecuteWithNamedModelVariations() {
 
181
                $this->Task->connection = 'test_suite';
 
182
                $this->Task->path = '/my/path/';
 
183
 
 
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();
 
188
 
 
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();
 
193
 
 
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();
 
198
 
 
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();
 
203
        }
 
204
 
 
205
/**
 
206
 * test that execute runs all() when args[0] = all
 
207
 *
 
208
 * @return void
 
209
 * @access public
 
210
 */
 
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'));
 
216
 
 
217
                $filename = '/my/path/article_fixture.php';
 
218
                $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
 
219
                $this->Task->execute();
 
220
 
 
221
                $filename = '/my/path/comment_fixture.php';
 
222
                $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class CommentFixture/')));
 
223
                $this->Task->execute();
 
224
        }
 
225
 
 
226
/**
 
227
 * test using all() with -count and -records
 
228
 *
 
229
 * @return void
 
230
 * @access public
 
231
 */
 
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'));
 
238
 
 
239
                $filename = '/my/path/article_fixture.php';
 
240
                $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/title\' => \'Third Article\'/')));
 
241
 
 
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);
 
245
                $this->Task->all();
 
246
        }
 
247
 
 
248
/**
 
249
 * test interactive mode of execute
 
250
 *
 
251
 * @return void
 
252
 * @access public
 
253
 */
 
254
        function testExecuteInteractive() {
 
255
                $this->Task->connection = 'test_suite';
 
256
                $this->Task->path = '/my/path/';
 
257
 
 
258
                $this->Task->setReturnValue('in', 'y');
 
259
                $this->Task->Model->setReturnValue('getName', 'Article');
 
260
                $this->Task->Model->setReturnValue('getTable', 'articles', array('Article'));
 
261
 
 
262
                $filename = '/my/path/article_fixture.php';
 
263
                $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
 
264
                $this->Task->execute();
 
265
        }
 
266
 
 
267
/**
 
268
 * Test that bake works
 
269
 *
 
270
 * @return void
 
271
 * @access public
 
272
 */
 
273
        function testBake() {
 
274
                $this->Task->connection = 'test_suite';
 
275
                $this->Task->path = '/my/path/';
 
276
 
 
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);
 
282
 
 
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);
 
288
 
 
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);
 
292
 
 
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);
 
296
 
 
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);
 
301
        }
 
302
 
 
303
/**
 
304
 * test record generation with float and binary types
 
305
 *
 
306
 * @return void
 
307
 * @access public
 
308
 */
 
309
        function testRecordGenerationForBinaryAndFloat() {
 
310
                $this->Task->connection = 'test_suite';
 
311
                $this->Task->path = '/my/path/';
 
312
 
 
313
                $result = $this->Task->bake('Article', 'datatypes');
 
314
                $this->assertPattern("/'float_field' => 1/", $result);
 
315
 
 
316
                $result = $this->Task->bake('Article', 'binary_tests');
 
317
                $this->assertPattern("/'data' => 'Lorem ipsum dolor sit amet'/", $result);
 
318
        }
 
319
 
 
320
/**
 
321
 * Test that file generation includes headers and correct path for plugins.
 
322
 *
 
323
 * @return void
 
324
 * @access public
 
325
 */
 
326
        function testGenerateFixtureFile() {
 
327
                $this->Task->connection = 'test_suite';
 
328
                $this->Task->path = '/my/path/';
 
329
                $filename = '/my/path/article_fixture.php';
 
330
 
 
331
                $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/Article/')));
 
332
                $result = $this->Task->generateFixtureFile('Article', array());
 
333
 
 
334
                $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/\<\?php(.*)\?\>/ms')));
 
335
                $result = $this->Task->generateFixtureFile('Article', array());
 
336
        }
 
337
 
 
338
/**
 
339
 * test generating files into plugins.
 
340
 *
 
341
 * @return void
 
342
 * @access public
 
343
 */
 
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';
 
349
 
 
350
                $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/Article/')));
 
351
                $result = $this->Task->generateFixtureFile('Article', array());
 
352
        }
 
353
}