Skip to content

Commit

Permalink
Fix broken options in FixtureTask.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Aug 2, 2014
1 parent 4923387 commit 77a8207
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 118 deletions.
65 changes: 21 additions & 44 deletions src/Console/Command/Task/FixtureTask.php
Expand Up @@ -71,9 +71,12 @@ public function getOptionParser() {
'short' => 's',
'boolean' => true
])->addOption('records', [
'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.'),
'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.'),
'short' => 'r',
'boolean' => true
])->addOption('import-records', [
'help' => __d('cake_console', 'Set to true to import records from the live table when the generated fixture is used.'),
'boolean' => true
])->addOption('conditions', [
'help' => __d('cake_console', 'The SQL snippet to use when importing records.'),
'default' => '1=1',
Expand Down Expand Up @@ -118,66 +121,40 @@ public function all() {
$tables = $this->Model->listAll($this->connection, false);

foreach ($tables as $table) {
$model = $this->_modelName($table);
$importOptions = [];
if (!empty($this->params['schema'])) {
$importOptions['schema'] = $model;
}
$this->bake($model, false, $importOptions);
}
}
/**
* Interacts with the User to setup an array of import options. For a fixture.
*
* @param string $modelName Name of model you are dealing with.
* @return array Array of import options.
*/
public function importOptions($modelName) {
$options = [];

if (!empty($this->params['schema'])) {
$options['schema'] = $modelName;
}
if (!empty($this->params['records'])) {
$options['records'] = true;
$options['fromTable'] = true;
$this->main($table);
}
return $options;
}

/**
* Assembles and writes a Fixture file
*
* @param string $model Name of model to bake.
* @param string $useTable Name of table to use.
* @param array $importOptions Options for public $import
* @return string Baked fixture content
* @throws \RuntimeException
*/
public function bake($model, $useTable = false, array $importOptions = []) {
public function bake($model, $useTable = false) {
$table = $schema = $records = $import = $modelImport = null;
$importBits = [];

if (!$useTable) {
$useTable = Inflector::tableize($model);
} elseif ($useTable != Inflector::tableize($model)) {
$table = $useTable;
}

if (!empty($importOptions)) {
if (isset($importOptions['schema'])) {
$modelImport = true;
$importBits[] = "'model' => '{$importOptions['schema']}'";
}
if (isset($importOptions['records'])) {
$importBits[] = "'records' => true";
}
if ($this->connection !== 'default') {
$importBits[] .= "'connection' => '{$this->connection}'";
}
if (!empty($importBits)) {
$import = sprintf("[%s]", implode(', ', $importBits));
}
$importBits = [];
if (!empty($this->params['schema'])) {
$modelImport = true;
$importBits[] = "'model' => '{$model}'";
}
if (!empty($this->params['import-records'])) {
$importBits[] = "'records' => true";
}
if (!empty($importBits) && $this->connection !== 'default') {
$importBits[] = "'connection' => '{$this->connection}'";
}
if (!empty($importBits)) {
$import = sprintf("[%s]", implode(', ', $importBits));
}

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

if (empty($importOptions['records']) && !isset($importOptions['fromTable'])) {
if (empty($this->params['records']) && empty($this->params['import-records'])) {
$recordCount = 1;
if (isset($this->params['count'])) {
$recordCount = $this->params['count'];
}
$records = $this->_makeRecordString($this->_generateRecords($data, $recordCount));
}
if (!empty($this->params['records']) || isset($importOptions['fromTable'])) {
if (!empty($this->params['records']) && empty($this->params['import-records'])) {
$records = $this->_makeRecordString($this->_getRecordsFromTable($model, $useTable));
}
return $this->generateFixtureFile($model, compact('records', 'table', 'schema', 'import'));
Expand Down
123 changes: 49 additions & 74 deletions tests/TestCase/Console/Command/Task/FixtureTaskTest.php
Expand Up @@ -88,58 +88,16 @@ public function testGetPath() {
$this->assertPathEquals(ROOT . '/tests/Fixture/', $this->Task->getPath());
}

/**
* test importOptions with overwriting command line options.
*
* @return void
*/
public function testImportOptionsWithCommandLineOptions() {
$this->Task->params = ['schema' => true, 'records' => true];

$result = $this->Task->importOptions('Article');
$expected = ['fromTable' => true, 'schema' => 'Article', 'records' => true];
$this->assertEquals($expected, $result);
}

/**
* test importOptions with schema.
*
* @return void
*/
public function testImportOptionsWithSchema() {
$this->Task->params = ['schema' => true];

$result = $this->Task->importOptions('Articles');
$expected = ['schema' => 'Articles'];
$this->assertEquals($expected, $result);
}

/**
* test importOptions with records.
*
* @return void
*/
public function testImportOptionsWithRecords() {
$this->Task->params = array('records' => true);

$result = $this->Task->importOptions('Article');
$expected = array('fromTable' => true, 'records' => true);
$this->assertEquals($expected, $result);
}

/**
* test generating a fixture with database conditions.
*
* @return void
*/
public function testImportRecordsFromDatabaseWithConditionsPoo() {
$this->Task->connection = 'test';
$this->Task->params = ['schema' => true, 'records' => true];

$result = $this->Task->bake('Articles', false, array(
'fromTable' => true,
'schema' => 'Articles',
'records' => false
));
$result = $this->Task->bake('Articles');

$this->assertContains('namespace App\Test\Fixture;', $result);
$this->assertContains('use Cake\TestSuite\Fixture\TestFixture;', $result);
Expand All @@ -158,7 +116,8 @@ public function testImportRecordsFromDatabaseWithConditionsPoo() {
*/
public function testImportOptionsAlternateConnection() {
$this->Task->connection = 'test';
$result = $this->Task->bake('Article', false, array('schema' => 'Article'));
$this->Task->params = ['schema' => true];
$result = $this->Task->bake('Article');
$this->assertContains("'connection' => 'test'", $result);
}

Expand All @@ -168,20 +127,12 @@ public function testImportOptionsAlternateConnection() {
* @return void
*/
public function testImportRecordsNoEscaping() {
$db = ConnectionManager::get('test');
if ($db instanceof Sqlserver) {
$this->markTestSkipped('This test does not run on SQLServer');
}

$articles = TableRegistry::get('Articles');
$articles->updateAll(['body' => "Body \"value\""], []);

$this->Task->connection = 'test';
$result = $this->Task->bake('Article', false, array(
'fromTable' => true,
'schema' => 'Article',
'records' => false
));
$this->Task->params = ['schema' => 'true', 'records' => true];
$result = $this->Task->bake('Article');
$this->assertContains("'body' => 'Body \"value\"'", $result, 'Data has bad escaping');
}

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

$result = $this->Task->bake('Article');
$this->assertContains('class ArticleFixture extends TestFixture', $result);
$this->assertContains('public $fields', $result);
$this->assertContains('public $records', $result);
$this->assertNotContains('public $import', $result);
$this->Task->expects($this->at(0))
->method('createFile')
->with($this->anything(), $this->logicalAnd(
$this->stringContains('class ArticleFixture extends TestFixture'),
$this->stringContains('public $fields'),
$this->stringContains('public $records'),
$this->logicalNot($this->stringContains('public $import'))
));
$result = $this->Task->main('Article');
}

$result = $this->Task->bake('Article', 'comments');
$this->assertContains('class ArticleFixture extends TestFixture', $result);
$this->assertContains('public $table = \'comments\';', $result);
$this->assertContains('public $fields = [', $result);
/**
* test main() with importing records
*
* @return void
*/
public function testMainImportRecords() {
$this->Task->connection = 'test';
$this->Task->params = ['import-records' => true];

$this->Task->expects($this->at(0))
->method('createFile')
->with($this->anything(), $this->logicalAnd(
$this->stringContains("public \$import = ['records' => true, 'connection' => 'test'];"),
$this->logicalNot($this->stringContains('public $records'))
));

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

$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
$this->assertContains("public \$import = ['model' => 'Article', 'connection' => 'test'];", $result);
$this->assertNotContains('public $fields', $result);
/**
* test main() with importing schema.
*
* @return void
*/
public function testMainImportSchema() {
$this->Task->connection = 'test';
$this->Task->params = ['schema' => true, 'import-records' => true];

$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
$this->assertContains("public \$import = ['model' => 'Article', 'records' => true, 'connection' => 'test'];", $result);
$this->assertNotContains('public $fields', $result);
$this->assertNotContains('public $records', $result);
$this->Task->expects($this->once())
->method('createFile')
->with($this->anything(), $this->logicalAnd(
$this->stringContains("public \$import = ['model' => 'Article', 'records' => true, 'connection' => 'test'];"),
$this->logicalNot($this->stringContains('public $fields')),
$this->logicalNot($this->stringContains('public $records'))
));
$this->Task->bake('Article', 'comments');
}

/**
Expand Down

0 comments on commit 77a8207

Please sign in to comment.