pivot table seeder generator added and tested

This commit is contained in:
Amine Ben hammou 2016-02-22 00:38:41 +01:00
parent 5f8f2f2888
commit b6d47df699
8 changed files with 181 additions and 47 deletions

4
.gitignore vendored
View File

@ -4,5 +4,5 @@
tests/_output/*
lumen-test/app/*
lumen-test/database/*
lumen-test/app
lumen-test/database

View File

@ -3,7 +3,4 @@
tests/_output/*
composer.lock
lumen-test/app
lumen-test/database
composer.lock

View File

@ -0,0 +1,29 @@
<?php
$I = new AcceptanceTester($scenario);
$I->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');

View File

@ -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', "<?php
$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', "<?php
// /*
// |--------------------------------------------------------------------------
// | Model Factories
// |--------------------------------------------------------------------------
// |
// | Here you may define all of your model factories. Model factories give
// | you a convenient way to create models for testing and seeding your
// | database. Just tell the factory how a default model should look.
// |
// */
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
// \$factory->define(App\User::class, function (\$faker) {
// return [
// 'name' => \$faker->name,
// 'email' => \$faker->email,
// 'password' => str_random(10),
// 'remember_token' => str_random(10),
// ];
// });
// ");
\$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');

View File

@ -0,0 +1,59 @@
<?php namespace Wn\Generators\Commands;
class PivotSeederCommand extends BaseCommand {
protected $signature = 'wn:pivot-seeder
{model1 : Name of the first model or table}
{model2 : Name of the second model or table}
{--count=10 : number of elements to add in database.}
';
protected $description = 'Generates seeder for pivot table';
public function handle()
{
$resources = $this->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);
}
}

View File

@ -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
]);
}

View File

@ -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');
}
}

View File

@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Seeder;
use Faker\Factory as Faker;
class {{name}} extends Seeder
{
public function run()
{
$faker = Faker::create();
$firstIds = DB::table('{{first_table}}')->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)
]);
}
}
}