mirror of
https://github.com/ZorgCC/lumen-generators.git
synced 2024-12-27 06:25:28 +03:00
pivot table seeder generator added and tested
This commit is contained in:
parent
5f8f2f2888
commit
b6d47df699
4
.gitignore
vendored
4
.gitignore
vendored
@ -4,5 +4,5 @@
|
||||
|
||||
tests/_output/*
|
||||
|
||||
lumen-test/app/*
|
||||
lumen-test/database/*
|
||||
lumen-test/app
|
||||
lumen-test/database
|
3
lumen-test/.gitignore
vendored
3
lumen-test/.gitignore
vendored
@ -4,6 +4,3 @@
|
||||
tests/_output/*
|
||||
|
||||
composer.lock
|
||||
|
||||
lumen-test/app
|
||||
lumen-test/database
|
29
lumen-test/tests/acceptance/PivotSeederCommandCept.php
Normal file
29
lumen-test/tests/acceptance/PivotSeederCommandCept.php
Normal 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');
|
@ -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');
|
59
src/Commands/PivotSeederCommand.php
Normal file
59
src/Commands/PivotSeederCommand.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
|
@ -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');
|
||||
}
|
||||
|
||||
}
|
||||
|
22
templates/pivot-seeder.wnt
Normal file
22
templates/pivot-seeder.wnt
Normal 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)
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user