Skip to content

Commit 77a8207

Browse files
committed
Fix broken options in FixtureTask.
The options in FixtureTask were not as comprehensive as they needed to be. Previously the records flag was overloaded to do 2 jobs. It signaled that you wanted to pull records from the live table, and if schema was present that you wanted to import records at runtime. It was not possible to express a fixture that imported schema, but pulled records in from the live table at time of generation.
1 parent 4923387 commit 77a8207

File tree

2 files changed

+70
-118
lines changed

2 files changed

+70
-118
lines changed

src/Console/Command/Task/FixtureTask.php

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ public function getOptionParser() {
7171
'short' => 's',
7272
'boolean' => true
7373
])->addOption('records', [
74-
'help' => __d('cake_console', 'Used with --count and <name>/all commands to pull [n] records from the live tables, where [n] is either --count or the default of 10.'),
74+
'help' => __d('cake_console', 'Generate a fixture with records from the non-test database. Used with --count and --conditions to limit which records are added to the fixture.'),
7575
'short' => 'r',
7676
'boolean' => true
77+
])->addOption('import-records', [
78+
'help' => __d('cake_console', 'Set to true to import records from the live table when the generated fixture is used.'),
79+
'boolean' => true
7780
])->addOption('conditions', [
7881
'help' => __d('cake_console', 'The SQL snippet to use when importing records.'),
7982
'default' => '1=1',
@@ -118,66 +121,40 @@ public function all() {
118121
$tables = $this->Model->listAll($this->connection, false);
119122

120123
foreach ($tables as $table) {
121-
$model = $this->_modelName($table);
122-
$importOptions = [];
123-
if (!empty($this->params['schema'])) {
124-
$importOptions['schema'] = $model;
125-
}
126-
$this->bake($model, false, $importOptions);
127-
}
128-
}
129-
/**
130-
* Interacts with the User to setup an array of import options. For a fixture.
131-
*
132-
* @param string $modelName Name of model you are dealing with.
133-
* @return array Array of import options.
134-
*/
135-
public function importOptions($modelName) {
136-
$options = [];
137-
138-
if (!empty($this->params['schema'])) {
139-
$options['schema'] = $modelName;
140-
}
141-
if (!empty($this->params['records'])) {
142-
$options['records'] = true;
143-
$options['fromTable'] = true;
124+
$this->main($table);
144125
}
145-
return $options;
146126
}
147127

148128
/**
149129
* Assembles and writes a Fixture file
150130
*
151131
* @param string $model Name of model to bake.
152132
* @param string $useTable Name of table to use.
153-
* @param array $importOptions Options for public $import
154133
* @return string Baked fixture content
155134
* @throws \RuntimeException
156135
*/
157-
public function bake($model, $useTable = false, array $importOptions = []) {
136+
public function bake($model, $useTable = false) {
158137
$table = $schema = $records = $import = $modelImport = null;
159-
$importBits = [];
160138

161139
if (!$useTable) {
162140
$useTable = Inflector::tableize($model);
163141
} elseif ($useTable != Inflector::tableize($model)) {
164142
$table = $useTable;
165143
}
166144

167-
if (!empty($importOptions)) {
168-
if (isset($importOptions['schema'])) {
169-
$modelImport = true;
170-
$importBits[] = "'model' => '{$importOptions['schema']}'";
171-
}
172-
if (isset($importOptions['records'])) {
173-
$importBits[] = "'records' => true";
174-
}
175-
if ($this->connection !== 'default') {
176-
$importBits[] .= "'connection' => '{$this->connection}'";
177-
}
178-
if (!empty($importBits)) {
179-
$import = sprintf("[%s]", implode(', ', $importBits));
180-
}
145+
$importBits = [];
146+
if (!empty($this->params['schema'])) {
147+
$modelImport = true;
148+
$importBits[] = "'model' => '{$model}'";
149+
}
150+
if (!empty($this->params['import-records'])) {
151+
$importBits[] = "'records' => true";
152+
}
153+
if (!empty($importBits) && $this->connection !== 'default') {
154+
$importBits[] = "'connection' => '{$this->connection}'";
155+
}
156+
if (!empty($importBits)) {
157+
$import = sprintf("[%s]", implode(', ', $importBits));
181158
}
182159

183160
$connection = ConnectionManager::get($this->connection);
@@ -193,14 +170,14 @@ public function bake($model, $useTable = false, array $importOptions = []) {
193170
$schema = $this->_generateSchema($data);
194171
}
195172

196-
if (empty($importOptions['records']) && !isset($importOptions['fromTable'])) {
173+
if (empty($this->params['records']) && empty($this->params['import-records'])) {
197174
$recordCount = 1;
198175
if (isset($this->params['count'])) {
199176
$recordCount = $this->params['count'];
200177
}
201178
$records = $this->_makeRecordString($this->_generateRecords($data, $recordCount));
202179
}
203-
if (!empty($this->params['records']) || isset($importOptions['fromTable'])) {
180+
if (!empty($this->params['records']) && empty($this->params['import-records'])) {
204181
$records = $this->_makeRecordString($this->_getRecordsFromTable($model, $useTable));
205182
}
206183
return $this->generateFixtureFile($model, compact('records', 'table', 'schema', 'import'));

tests/TestCase/Console/Command/Task/FixtureTaskTest.php

Lines changed: 49 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -88,58 +88,16 @@ public function testGetPath() {
8888
$this->assertPathEquals(ROOT . '/tests/Fixture/', $this->Task->getPath());
8989
}
9090

91-
/**
92-
* test importOptions with overwriting command line options.
93-
*
94-
* @return void
95-
*/
96-
public function testImportOptionsWithCommandLineOptions() {
97-
$this->Task->params = ['schema' => true, 'records' => true];
98-
99-
$result = $this->Task->importOptions('Article');
100-
$expected = ['fromTable' => true, 'schema' => 'Article', 'records' => true];
101-
$this->assertEquals($expected, $result);
102-
}
103-
104-
/**
105-
* test importOptions with schema.
106-
*
107-
* @return void
108-
*/
109-
public function testImportOptionsWithSchema() {
110-
$this->Task->params = ['schema' => true];
111-
112-
$result = $this->Task->importOptions('Articles');
113-
$expected = ['schema' => 'Articles'];
114-
$this->assertEquals($expected, $result);
115-
}
116-
117-
/**
118-
* test importOptions with records.
119-
*
120-
* @return void
121-
*/
122-
public function testImportOptionsWithRecords() {
123-
$this->Task->params = array('records' => true);
124-
125-
$result = $this->Task->importOptions('Article');
126-
$expected = array('fromTable' => true, 'records' => true);
127-
$this->assertEquals($expected, $result);
128-
}
129-
13091
/**
13192
* test generating a fixture with database conditions.
13293
*
13394
* @return void
13495
*/
13596
public function testImportRecordsFromDatabaseWithConditionsPoo() {
13697
$this->Task->connection = 'test';
98+
$this->Task->params = ['schema' => true, 'records' => true];
13799

138-
$result = $this->Task->bake('Articles', false, array(
139-
'fromTable' => true,
140-
'schema' => 'Articles',
141-
'records' => false
142-
));
100+
$result = $this->Task->bake('Articles');
143101

144102
$this->assertContains('namespace App\Test\Fixture;', $result);
145103
$this->assertContains('use Cake\TestSuite\Fixture\TestFixture;', $result);
@@ -158,7 +116,8 @@ public function testImportRecordsFromDatabaseWithConditionsPoo() {
158116
*/
159117
public function testImportOptionsAlternateConnection() {
160118
$this->Task->connection = 'test';
161-
$result = $this->Task->bake('Article', false, array('schema' => 'Article'));
119+
$this->Task->params = ['schema' => true];
120+
$result = $this->Task->bake('Article');
162121
$this->assertContains("'connection' => 'test'", $result);
163122
}
164123

@@ -168,20 +127,12 @@ public function testImportOptionsAlternateConnection() {
168127
* @return void
169128
*/
170129
public function testImportRecordsNoEscaping() {
171-
$db = ConnectionManager::get('test');
172-
if ($db instanceof Sqlserver) {
173-
$this->markTestSkipped('This test does not run on SQLServer');
174-
}
175-
176130
$articles = TableRegistry::get('Articles');
177131
$articles->updateAll(['body' => "Body \"value\""], []);
178132

179133
$this->Task->connection = 'test';
180-
$result = $this->Task->bake('Article', false, array(
181-
'fromTable' => true,
182-
'schema' => 'Article',
183-
'records' => false
184-
));
134+
$this->Task->params = ['schema' => 'true', 'records' => true];
135+
$result = $this->Task->bake('Article');
185136
$this->assertContains("'body' => 'Body \"value\"'", $result, 'Data has bad escaping');
186137
}
187138

@@ -321,29 +272,53 @@ public function testMainNoArgs() {
321272
public function testBake() {
322273
$this->Task->connection = 'test';
323274

324-
$result = $this->Task->bake('Article');
325-
$this->assertContains('class ArticleFixture extends TestFixture', $result);
326-
$this->assertContains('public $fields', $result);
327-
$this->assertContains('public $records', $result);
328-
$this->assertNotContains('public $import', $result);
275+
$this->Task->expects($this->at(0))
276+
->method('createFile')
277+
->with($this->anything(), $this->logicalAnd(
278+
$this->stringContains('class ArticleFixture extends TestFixture'),
279+
$this->stringContains('public $fields'),
280+
$this->stringContains('public $records'),
281+
$this->logicalNot($this->stringContains('public $import'))
282+
));
283+
$result = $this->Task->main('Article');
284+
}
329285

330-
$result = $this->Task->bake('Article', 'comments');
331-
$this->assertContains('class ArticleFixture extends TestFixture', $result);
332-
$this->assertContains('public $table = \'comments\';', $result);
333-
$this->assertContains('public $fields = [', $result);
286+
/**
287+
* test main() with importing records
288+
*
289+
* @return void
290+
*/
291+
public function testMainImportRecords() {
292+
$this->Task->connection = 'test';
293+
$this->Task->params = ['import-records' => true];
294+
295+
$this->Task->expects($this->at(0))
296+
->method('createFile')
297+
->with($this->anything(), $this->logicalAnd(
298+
$this->stringContains("public \$import = ['records' => true, 'connection' => 'test'];"),
299+
$this->logicalNot($this->stringContains('public $records'))
300+
));
334301

335-
$result = $this->Task->bake('Article', 'comments', array('records' => true));
336-
$this->assertContains("public \$import = ['records' => true, 'connection' => 'test'];", $result);
337-
$this->assertNotContains('public $records', $result);
302+
$this->Task->main('Article');
303+
}
338304

339-
$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
340-
$this->assertContains("public \$import = ['model' => 'Article', 'connection' => 'test'];", $result);
341-
$this->assertNotContains('public $fields', $result);
305+
/**
306+
* test main() with importing schema.
307+
*
308+
* @return void
309+
*/
310+
public function testMainImportSchema() {
311+
$this->Task->connection = 'test';
312+
$this->Task->params = ['schema' => true, 'import-records' => true];
342313

343-
$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
344-
$this->assertContains("public \$import = ['model' => 'Article', 'records' => true, 'connection' => 'test'];", $result);
345-
$this->assertNotContains('public $fields', $result);
346-
$this->assertNotContains('public $records', $result);
314+
$this->Task->expects($this->once())
315+
->method('createFile')
316+
->with($this->anything(), $this->logicalAnd(
317+
$this->stringContains("public \$import = ['model' => 'Article', 'records' => true, 'connection' => 'test'];"),
318+
$this->logicalNot($this->stringContains('public $fields')),
319+
$this->logicalNot($this->stringContains('public $records'))
320+
));
321+
$this->Task->bake('Article', 'comments');
347322
}
348323

349324
/**

0 commit comments

Comments
 (0)