Skip to content

Commit

Permalink
Make tests work :)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Mar 4, 2014
1 parent 9859d83 commit 01f7361
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 36 deletions.
19 changes: 11 additions & 8 deletions src/Way/Generators/Commands/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,21 @@ public function fire()
}

/**
* Get the path to the target directory
* either through a command option, or
* from the configuration
* Get a directory path either through a
* command option, or from the configuration
*
* @param $option
* @param $configName
* @return array|string
* @return string
*/
protected function getTargetPathByOptionOrConfig($configName)
protected function getPathByOptionOrConfig($option, $configName)
{
return $this->option('path')
? $this->option('path')
: Config::get("generators::config.{$configName}");
if ($path = $this->option($option))
{
return $path;
}

return Config::get("generators::config.{$configName}");
}

}
11 changes: 6 additions & 5 deletions src/Way/Generators/Commands/MigrationGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function fire()
*/
protected function getFileGenerationPath()
{
$path = $this->getTargetPathByOptionOrConfig('model_target_path');
$path = $this->getPathByOptionOrConfig('path', 'model_target_path');
$fileName = $this->getDatePrefix() . '_' . $this->argument('migrationName') . '.php';

return "{$path}/{$fileName}";
Expand Down Expand Up @@ -127,7 +127,7 @@ protected function getTemplateData()
*/
protected function getTemplatePath()
{
return Config::get('generators::config.migration_template_path');
return $this->getPathByOptionOrConfig('templatePath', 'migration_template_path');
}

/**
Expand All @@ -150,9 +150,10 @@ protected function getArguments()
protected function getOptions()
{
return array(
array('fields', null, InputOption::VALUE_OPTIONAL, 'Fields for the migration'),
array('path', null, InputOption::VALUE_OPTIONAL, 'Where should the file be created?', app_path('database/migrations')),
array('testing', null, InputOption::VALUE_OPTIONAL, 'For internal use only.')
['fields', null, InputOption::VALUE_OPTIONAL, 'Fields for the migration'],
['path', null, InputOption::VALUE_OPTIONAL, 'Where should the file be created?', app_path('database/migrations')],
['templatePath', null, InputOption::VALUE_OPTIONAL, 'The location of the template for this generator'],
['testing', null, InputOption::VALUE_OPTIONAL, 'For internal use only.']
);
}

Expand Down
7 changes: 4 additions & 3 deletions src/Way/Generators/Commands/ModelGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ModelGeneratorCommand extends GeneratorCommand {
*/
protected function getFileGenerationPath()
{
$path = $this->getTargetPathByOptionOrConfig('model_target_path');
$path = $this->getPathByOptionOrConfig('path', 'model_target_path');

return $path. '/' . ucwords($this->argument('modelName')) . '.php';
}
Expand All @@ -51,7 +51,7 @@ protected function getTemplateData()
*/
protected function getTemplatePath()
{
return Config::get('generators::config.model_template_path');
return $this->getPathByOptionOrConfig('templatePath', 'model_template_path');
}

/**
Expand All @@ -74,7 +74,8 @@ protected function getArguments()
protected function getOptions()
{
return [
['path', null, InputOption::VALUE_OPTIONAL, 'Where should the file be created?']
['path', null, InputOption::VALUE_OPTIONAL, 'Where should the file be created?'],
['templatePath', null, InputOption::VALUE_OPTIONAL, 'The location of the template for this generator']
];
}

Expand Down
7 changes: 4 additions & 3 deletions src/Way/Generators/Commands/SeederGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SeederGeneratorCommand extends GeneratorCommand {
*/
protected function getFileGenerationPath()
{
$path = $this->getTargetPathByOptionOrConfig('model_target_path');
$path = $this->getPathByOptionOrConfig('path', 'model_target_path');
$tableName = ucwords($this->argument('tableName'));

return "{$path}/{$tableName}TableSeeder.php";
Expand Down Expand Up @@ -55,7 +55,7 @@ protected function getTemplateData()
*/
protected function getTemplatePath()
{
return Config::get('generators::config.seed_template_path');
return $this->getPathByOptionOrConfig('templatePath', 'seed_template_path');
}

/**
Expand All @@ -78,7 +78,8 @@ protected function getArguments()
protected function getOptions()
{
return [
['path', null, InputOption::VALUE_OPTIONAL, 'Where should the file be created?', app_path('database/seeds')]
['path', null, InputOption::VALUE_OPTIONAL, 'Where should the file be created?', app_path('database/seeds')],
['templatePath', null, InputOption::VALUE_OPTIONAL, 'The location of the template for this generator']
];
}

Expand Down
33 changes: 18 additions & 15 deletions tests/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public static function bootstrapLaravel()
*/
public function tearDown()
{
array_map('unlink', glob(app_path('database/migrations/*')));
array_map('unlink', glob(app_path('database/seeds/*')));
array_map('unlink', glob(app_path('models/*')));
array_map('unlink', glob(base_path('workbench/way/generators/tests/tmp/*')));

$this->tester = null;
}
Expand All @@ -51,10 +49,13 @@ public function tearDown()
public function iGenerateAMigrationWithNameAndFields($migrationName, $fields)
{
$this->tester = new CommandTester(App::make('Way\Generators\Commands\MigrationGeneratorCommand'));

$this->tester->execute([
'migrationName' => $migrationName,
'--fields' => $fields,
'--testing' => true
'--testing' => true,
'--path' => __DIR__.'/../../tmp',
'--templatePath' => __DIR__.'/../../../src/Way/Generators/templates/migration.txt'
]);
}

Expand All @@ -64,7 +65,12 @@ public function iGenerateAMigrationWithNameAndFields($migrationName, $fields)
public function iGenerateAModelWith($modelName)
{
$this->tester = new CommandTester(App::make('Way\Generators\Commands\ModelGeneratorCommand'));
$this->tester->execute(compact('modelName'));

$this->tester->execute([
'modelName' => $modelName,
'--path' => __DIR__.'/../../tmp',
'--templatePath' => __DIR__.'/../../../src/Way/Generators/templates/model.txt'
]);
}

/**
Expand All @@ -73,16 +79,13 @@ public function iGenerateAModelWith($modelName)
public function iGenerateASeedWith($tableName)
{
$this->tester = new CommandTester(App::make('Way\Generators\Commands\SeederGeneratorCommand'));
$this->tester->execute(compact('tableName'));
}

/**
* @When /^I generate a migration with "([^"]*)"$/
*/
public function iGenerateAMigrationWith($migrationName)
{
$this->tester = new CommandTester(App::make('Way\Generators\Commands\MigrationGeneratorCommand'));
$this->tester->execute(compact('migrationName'));
$this->tester->execute([
'tableName' => $tableName,
'--path' => __DIR__.'/../../tmp',
'--templatePath' => __DIR__.'/../../../src/Way/Generators/templates/seed.txt'
]);

}

/**
Expand All @@ -91,7 +94,7 @@ public function iGenerateAMigrationWith($migrationName)
public function theGeneratedMigrationShouldMatchMyStub($stubName)
{
$expected = file_get_contents(__DIR__."/../../stubs/{$stubName}.txt");
$actual = file_get_contents(glob(base_path('app/database/migrations/*'))[0]);
$actual = file_get_contents(glob(__DIR__."/../../tmp/*")[0]);

// Let's compare the stub against what was actually generated.
assertEquals($expected, $actual);
Expand Down
4 changes: 2 additions & 2 deletions tests/features/generators.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ Feature: Generators

Examples:
| command | argument | generatedFilePath |
| model | Order | app/models/Order.php |
| seed | orders | app/database/seeds/OrdersTableSeeder.php |
| model | Order | workbench/way/generators/tests/tmp/Order.php |
| seed | orders | workbench/way/generators/tests/tmp/OrdersTableSeeder.php |

0 comments on commit 01f7361

Please sign in to comment.