From b6d47df699c63ca6419301414bdd125a9543014a Mon Sep 17 00:00:00 2001 From: Amine Ben hammou Date: Mon, 22 Feb 2016 00:38:41 +0100 Subject: [PATCH] pivot table seeder generator added and tested --- .gitignore | 4 +- lumen-test/.gitignore | 5 +- .../acceptance/PivotSeederCommandCept.php | 29 +++++++ .../tests/acceptance/ResourceCommandCept.php | 78 +++++++++++-------- src/Commands/PivotSeederCommand.php | 59 ++++++++++++++ src/Commands/ResourceCommand.php | 9 ++- src/CommandsServiceProvider.php | 22 ++++-- templates/pivot-seeder.wnt | 22 ++++++ 8 files changed, 181 insertions(+), 47 deletions(-) create mode 100644 lumen-test/tests/acceptance/PivotSeederCommandCept.php create mode 100644 src/Commands/PivotSeederCommand.php create mode 100644 templates/pivot-seeder.wnt diff --git a/.gitignore b/.gitignore index 9092799..5c15255 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ tests/_output/* -lumen-test/app/* -lumen-test/database/* \ No newline at end of file +lumen-test/app +lumen-test/database \ No newline at end of file diff --git a/lumen-test/.gitignore b/lumen-test/.gitignore index 02b03f3..37201af 100644 --- a/lumen-test/.gitignore +++ b/lumen-test/.gitignore @@ -3,7 +3,4 @@ tests/_output/* -composer.lock - -lumen-test/app -lumen-test/database \ No newline at end of file +composer.lock \ No newline at end of file diff --git a/lumen-test/tests/acceptance/PivotSeederCommandCept.php b/lumen-test/tests/acceptance/PivotSeederCommandCept.php new file mode 100644 index 0000000..6ea607a --- /dev/null +++ b/lumen-test/tests/acceptance/PivotSeederCommandCept.php @@ -0,0 +1,29 @@ +wantTo('generate a pivot table seeder'); +$I->runShellCommand('php artisan wn:pivot-seeder tasks ShortTag'); +$I->seeInShellOutput('ShortTagTaskTableSeeder generated'); +$I->openFile('./database/seeds/ShortTagTaskTableSeeder.php'); +$I->seeInThisFile(" +use Illuminate\Database\Seeder; +use Faker\Factory as Faker; + +class ShortTagTaskTableSeeder extends Seeder +{ + public function run() + { + \$faker = Faker::create(); + + \$firstIds = DB::table('short_tags')->lists('id'); + \$secondIds = DB::table('tasks')->lists('id'); + + for(\$i = 0; \$i < 10; \$i++) { + DB::table('short_tag_task')->insert([ + 'short_tag_id' => \$faker->randomElement(\$firstIds), + 'task_id' => \$faker->randomElement(\$secondIds) + ]); + } + } +}"); +$I->deleteFile('./database/seeds/ShortTagTaskTableSeeder.php'); \ No newline at end of file diff --git a/lumen-test/tests/acceptance/ResourceCommandCept.php b/lumen-test/tests/acceptance/ResourceCommandCept.php index eb54a56..777284f 100644 --- a/lumen-test/tests/acceptance/ResourceCommandCept.php +++ b/lumen-test/tests/acceptance/ResourceCommandCept.php @@ -105,37 +105,51 @@ $app->get("/", function () use ($app) { '); // Checking model factory -// $I->openFile('./database/factories/ModelFactory.php'); -// $I->seeInThisFile(" -// /** -// * Factory definition for model App\TaskCategory. -// */ -// \$factory->define(App\TaskCategory::class, function (\$faker) { -// return [ -// 'name' => \$faker->word, -// 'descr' => \$faker->paragraph, -// 'due' => \$faker->date, -// ]; -// });"); -// $I->writeToFile('./database/factories/ModelFactory.php', "openFile('./database/factories/ModelFactory.php'); +$I->seeInThisFile(" +/** + * Factory definition for model App\TaskCategory. + */ +\$factory->define(App\TaskCategory::class, function (\$faker) { + return [ + 'name' => \$faker->word, + 'descr' => \$faker->paragraph, + 'due' => \$faker->date, + ]; +});"); +$I->writeToFile('./database/factories/ModelFactory.php', "define(App\User::class, function (\$faker) { -// return [ -// 'name' => \$faker->name, -// 'email' => \$faker->email, -// 'password' => str_random(10), -// 'remember_token' => str_random(10), -// ]; -// }); -// "); \ No newline at end of file +\$factory->define(App\User::class, function (\$faker) { + return [ + 'name' => \$faker->name, + 'email' => \$faker->email, + 'password' => str_random(10), + 'remember_token' => str_random(10), + ]; +}); +"); + +// Checking database seeder +$I->openFile('./database/seeds/TaskCategoriesTableSeeder.php'); +$I->seeInThisFile(' +use Illuminate\Database\Seeder; + +class TaskCategoriesTableSeeder extends Seeder +{ + public function run() + { + factory(App\TaskCategory::class, 10)->create(); + } +}'); +$I->deleteFile('./database/seeds/TaskCategoriesTableSeeder.php'); \ No newline at end of file diff --git a/src/Commands/PivotSeederCommand.php b/src/Commands/PivotSeederCommand.php new file mode 100644 index 0000000..b79f76b --- /dev/null +++ b/src/Commands/PivotSeederCommand.php @@ -0,0 +1,59 @@ +getResources(); + $name = $this->getSeederName($resources); + $tables = $this->getTableNames($resources); + $file = "./database/seeds/{$name}.php"; + + $content = $this->getTemplate('pivot-seeder') + ->with([ + 'first_resource' => $resources[0], + 'second_resource' => $resources[1], + 'first_table' => $tables[0], + 'second_table' => $tables[1], + 'name' => $name, + 'count' => $this->option('count') + ]) + ->get(); + + $this->save($content, $file); + + $this->info("{$name} generated !"); + } + + protected function getResources() + { + $resources = array_map(function($arg) { + return snake_case(str_singular($this->argument($arg))); + }, ['model1', 'model2']); + + sort($resources); + + return $resources; + } + + protected function getSeederName($resources) { + $resources = array_map(function($resource){ + return ucwords(camel_case($resource)); + }, $resources); + return implode('', $resources) . 'TableSeeder'; + } + + protected function getTableNames($resources) { + return array_map('str_plural', $resources); + } + +} \ No newline at end of file diff --git a/src/Commands/ResourceCommand.php b/src/Commands/ResourceCommand.php index 626c2b4..0c561fe 100644 --- a/src/Commands/ResourceCommand.php +++ b/src/Commands/ResourceCommand.php @@ -61,11 +61,16 @@ class ResourceCommand extends BaseCommand { ]); // generating model factory - /*$this->call('wn:factory', [ + $this->call('wn:factory', [ 'model' => 'App\\' . $modelName, '--fields' => $this->factoryFields(), '--parsed' => true - ]);*/ + ]); + + // generating database seeder + $this->call('wn:seeder', [ + 'model' => 'App\\' . $modelName + ]); } diff --git a/src/CommandsServiceProvider.php b/src/CommandsServiceProvider.php index dba4203..a37b15e 100644 --- a/src/CommandsServiceProvider.php +++ b/src/CommandsServiceProvider.php @@ -17,6 +17,7 @@ class CommandsServiceProvider extends ServiceProvider $this->registerPivotTableCommand(); $this->registerFactoryCommand(); $this->registerSeederCommand(); + $this->registerPivotSeederCommand(); // $this->registerTestCommand(); } @@ -48,13 +49,6 @@ class CommandsServiceProvider extends ServiceProvider $this->commands('command.wn.migration'); } - protected function registerSeederCommand(){ - $this->app->singleton('command.wn.seeder', function($app){ - return $app['Wn\Generators\Commands\SeederCommand']; - }); - $this->commands('command.wn.seeder'); - } - protected function registerRouteCommand(){ $this->app->singleton('command.wn.route', function($app){ return $app['Wn\Generators\Commands\RouteCommand']; @@ -97,4 +91,18 @@ class CommandsServiceProvider extends ServiceProvider $this->commands('command.wn.factory'); } + protected function registerSeederCommand(){ + $this->app->singleton('command.wn.seeder', function($app){ + return $app['Wn\Generators\Commands\SeederCommand']; + }); + $this->commands('command.wn.seeder'); + } + + protected function registerPivotSeederCommand(){ + $this->app->singleton('command.wn.pivot.seeder', function($app){ + return $app['Wn\Generators\Commands\PivotSeederCommand']; + }); + $this->commands('command.wn.pivot.seeder'); + } + } diff --git a/templates/pivot-seeder.wnt b/templates/pivot-seeder.wnt new file mode 100644 index 0000000..7e9ca7e --- /dev/null +++ b/templates/pivot-seeder.wnt @@ -0,0 +1,22 @@ +lists('id'); + $secondIds = DB::table('{{second_table}}')->lists('id'); + + for($i = 0; $i < {{count}}; $i++) { + DB::table('{{first_resource}}_{{second_resource}}')->insert([ + '{{first_resource}}_id' => $faker->randomElement($firstIds), + '{{second_resource}}_id' => $faker->randomElement($secondIds) + ]); + } + } +}