Skip to content

Commit

Permalink
Fix Seeder for Laravel 8 (#5093)
Browse files Browse the repository at this point in the history
* just check that test is failing for Laravel 8

* Publish seeders in the right folder and add namespace if Laravel 8 or greater

Removed Process and use Composer

* changed seedable

* fixed dummydatabaseseeder

* Check if class is namespaced

* fix StyleCI

* Fixed seeders not autoloading in tests

* revert composer changes run autoload from app

* Centralized getSeedsFolderName

* removed use

* Remove duplication calling addNamespaceIfNeeded

* Move method to helper class

Co-authored-by: Christoph Schweppe <info@cschweppe.de>
  • Loading branch information
MrCrayon and emptynick authored Jan 28, 2021
1 parent f34cc95 commit 2dde64c
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 46 deletions.
23 changes: 10 additions & 13 deletions publishable/database/seeds/VoyagerDatabaseSeeder.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
<?php

use Illuminate\Database\Seeder;
use TCG\Voyager\Traits\Seedable;

class VoyagerDatabaseSeeder extends Seeder
{
use Seedable;

protected $seedersPath = __DIR__.'/';

/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->seed('DataTypesTableSeeder');
$this->seed('DataRowsTableSeeder');
$this->seed('MenusTableSeeder');
$this->seed('MenuItemsTableSeeder');
$this->seed('RolesTableSeeder');
$this->seed('PermissionsTableSeeder');
$this->seed('PermissionRoleTableSeeder');
$this->seed('SettingsTableSeeder');
$this->call([
DataTypesTableSeeder::class,
DataRowsTableSeeder::class,
MenusTableSeeder::class,
MenuItemsTableSeeder::class,
RolesTableSeeder::class,
PermissionsTableSeeder::class,
PermissionRoleTableSeeder::class,
SettingsTableSeeder::class,
]);
}
}
20 changes: 8 additions & 12 deletions publishable/database/seeds/VoyagerDummyDatabaseSeeder.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
<?php

use Illuminate\Database\Seeder;
use TCG\Voyager\Traits\Seedable;

class VoyagerDummyDatabaseSeeder extends Seeder
{
use Seedable;

protected $seedersPath;

/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->seedersPath = database_path('seeds').'/';
$this->seed('CategoriesTableSeeder');
$this->seed('UsersTableSeeder');
$this->seed('PostsTableSeeder');
$this->seed('PagesTableSeeder');
$this->seed('TranslationsTableSeeder');
$this->seed('PermissionRoleTableSeeder');
$this->call([
CategoriesTableSeeder::class,
UsersTableSeeder::class,
PostsTableSeeder::class,
PagesTableSeeder::class,
TranslationsTableSeeder::class,
PermissionRoleTableSeeder::class,
]);
}
}
87 changes: 68 additions & 19 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Process;
use TCG\Voyager\Providers\VoyagerDummyServiceProvider;
use TCG\Voyager\Traits\Seedable;
use TCG\Voyager\Seed;
use TCG\Voyager\VoyagerServiceProvider;

class InstallCommand extends Command
{
use Seedable;

protected $seedersPath = __DIR__.'/../../publishable/database/seeds/';

/**
* The console command name.
*
Expand All @@ -30,6 +26,30 @@ class InstallCommand extends Command
*/
protected $description = 'Install the Voyager Admin package';

/**
* The Composer instance.
*
* @var \Illuminate\Foundation\Composer
*/
protected $composer;

/**
* Seed Folder name.
*
* @var string
*/
protected $seedFolder;

public function __construct(Composer $composer)
{
parent::__construct();

$this->composer = $composer;
$this->composer->setWorkingPath(base_path());

$this->seedFolder = Seed::getFolderName();
}

protected function getOptions()
{
return [
Expand Down Expand Up @@ -92,14 +112,6 @@ public function handle(Filesystem $filesystem)
$this->warn('You will need to update this manually. Change "extends Authenticatable" to "extends \TCG\Voyager\Models\User" in your User model');
}

$this->info('Dumping the autoloaded files and reloading all new files');

$composer = $this->findComposer();

$process = new Process([$composer.' dump-autoload']);
$process->setTimeout(null); // Setting timeout to null to prevent installation from stopping at a certain point in time
$process->setWorkingDirectory(base_path())->run();

$this->info('Adding Voyager routes to routes/web.php');
$routes_contents = $filesystem->get(base_path('routes/web.php'));
if (false === strpos($routes_contents, 'Voyager::routes()')) {
Expand All @@ -109,21 +121,39 @@ public function handle(Filesystem $filesystem)
);
}

$this->info('Seeding data into the database');
$this->seed('VoyagerDatabaseSeeder');
$publishablePath = dirname(__DIR__).'/../publishable';

if ($this->option('with-dummy')) {
$this->info('Publishing dummy content');
$tags = ['dummy_seeds', 'dummy_content', 'dummy_config', 'dummy_migrations'];
$this->call('vendor:publish', ['--provider' => VoyagerDummyServiceProvider::class, '--tag' => $tags]);

$this->addNamespaceIfNeeded(
collect($filesystem->files("{$publishablePath}/database/dummy_seeds/")),
$filesystem
);
} else {
$this->call('vendor:publish', ['--provider' => VoyagerServiceProvider::class, '--tag' => ['config', 'voyager_avatar']]);
}

$this->addNamespaceIfNeeded(
collect($filesystem->files("{$publishablePath}/database/seeds/")),
$filesystem
);

$this->info('Dumping the autoloaded files and reloading all new files');
$this->composer->dumpAutoloads();
require_once base_path('vendor/autoload.php');

$this->info('Seeding data into the database');
$this->call('db:seed', ['--class' => 'VoyagerDatabaseSeeder']);

if ($this->option('with-dummy')) {
$this->info('Migrating dummy tables');
$this->call('migrate');

$this->info('Seeding dummy data');
$this->seed('VoyagerDummyDatabaseSeeder');
} else {
$this->call('vendor:publish', ['--provider' => VoyagerServiceProvider::class, '--tag' => ['config', 'voyager_avatar']]);
$this->call('db:seed', ['--class' => 'VoyagerDummyDatabaseSeeder']);
}

$this->info('Setting up the hooks');
Expand All @@ -134,4 +164,23 @@ public function handle(Filesystem $filesystem)

$this->info('Successfully installed Voyager! Enjoy');
}

private function addNamespaceIfNeeded($seeds, Filesystem $filesystem)
{
if ($this->seedFolder != 'seeders') {
return;
}

$seeds->each(function ($file) use ($filesystem) {
$path = database_path('seeders').'/'.$file->getFilename();

$stub = str_replace(
"<?php\n\nuse",
"<?php\n\nnamespace Database\\Seeders;\n\nuse",
$filesystem->get($path)
);

$filesystem->put($path, $stub);
});
}
}
3 changes: 2 additions & 1 deletion src/Providers/VoyagerDummyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Arrilot\Widgets\ServiceProvider as WidgetServiceProvider;
use Illuminate\Support\ServiceProvider;
use TCG\Voyager\Seed;

class VoyagerDummyServiceProvider extends ServiceProvider
{
Expand All @@ -30,7 +31,7 @@ private function registerPublishableResources()

$publishable = [
'dummy_seeds' => [
"{$publishablePath}/database/dummy_seeds/" => database_path('seeds'),
"{$publishablePath}/database/dummy_seeds/" => database_path(Seed::getFolderName()),
],
'dummy_content' => [
"{$publishablePath}/dummy_content/" => storage_path('app/public'),
Expand Down
11 changes: 11 additions & 0 deletions src/Seed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace TCG\Voyager;

class Seed
{
public static function getFolderName()
{
return version_compare(app()->version(), '8.0') >= 0 ? 'seeders' : 'seeds';
}
}
3 changes: 2 additions & 1 deletion src/VoyagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use TCG\Voyager\Policies\SettingPolicy;
use TCG\Voyager\Providers\VoyagerDummyServiceProvider;
use TCG\Voyager\Providers\VoyagerEventServiceProvider;
use TCG\Voyager\Seed;
use TCG\Voyager\Translator\Collection as TranslatorCollection;

class VoyagerServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -254,7 +255,7 @@ private function registerPublishableResources()
"{$publishablePath}/dummy_content/users/" => storage_path('app/public/users'),
],
'seeds' => [
"{$publishablePath}/database/seeds/" => database_path('seeds'),
"{$publishablePath}/database/seeds/" => database_path(Seed::getFolderName()),
],
'config' => [
"{$publishablePath}/config/voyager.php" => config_path('voyager.php'),
Expand Down
30 changes: 30 additions & 0 deletions tests/Feature/SeederTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace TCG\Voyager\Tests\Feature;

use TCG\Voyager\Tests\TestCase;

class SeederTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

$this->install();
}

/**
* Test manually seeding is working.
*/
public function testVoyagerDatabaseSeederCanBeCalled()
{
$exception = null;

try {
$this->artisan('db:seed', ['--class' => 'VoyagerDatabaseSeeder']);
} catch (\Exception $exception) {
}

$this->assertNull($exception, 'An exception was thrown');
}
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public function setUp(): void
);
}

// Orchestra Testbench does not contain this file and can't create autoload without
if (!is_dir(base_path('tests/'))) {
mkdir(base_path('tests/'));

file_put_contents(
base_path('tests/TestCase.php'),
"<?php\n\n"
);
}

$this->app->make('Illuminate\Contracts\Http\Kernel')->pushMiddleware('Illuminate\Session\Middleware\StartSession');
$this->app->make('Illuminate\Contracts\Http\Kernel')->pushMiddleware('Illuminate\View\Middleware\ShareErrorsFromSession');

Expand Down

0 comments on commit 2dde64c

Please sign in to comment.