Skip to content

Commit

Permalink
Move DefaultPastries to Seed
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Oct 1, 2023
1 parent 9d109de commit 0788cbf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
5 changes: 5 additions & 0 deletions app/src/Database/Migrations/V100/PastriesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Illuminate\Database\Schema\Blueprint;
use UserFrosting\Sprinkle\Core\Database\Migration;
use UserFrosting\Sprinkle\Pastries\Database\Seeds\DefaultPastries;

class PastriesTable extends Migration
{
Expand All @@ -31,6 +32,10 @@ public function up(): void
$table->collation = 'utf8_unicode_ci';
$table->charset = 'utf8';
});

// Run Seed for default pastries
$seed = new DefaultPastries();
$seed->run();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* UserFrosting Pastries Sprinkle
*
Expand All @@ -8,43 +10,27 @@
* @license https://github.com/userfrosting/pastries/blob/master/LICENSE (MIT License)
*/

namespace UserFrosting\Sprinkle\Pastries\Database\Migrations\V100;
namespace UserFrosting\Sprinkle\Pastries\Database\Seeds;

use UserFrosting\Sprinkle\Core\Database\Migration;
use UserFrosting\Sprinkle\Core\Seeder\SeedInterface;
use UserFrosting\Sprinkle\Pastries\Database\Models\Pastries;

class DefaultPastries extends Migration
/**
* Seeder for the default pastries.
*/
class DefaultPastries implements SeedInterface
{
/**
* {@inheritdoc}
*/
public static $dependencies = [
PastriesTable::class,
];

/**
* {@inheritdoc}
*/
public function up(): void
public function run(): void
{
foreach ($this->pastries() as $pastry) {
$pastry = new Pastries($pastry);
$pastry->save();
}
}

/**
* {@inheritdoc}
*/
public function down(): void
{
foreach ($this->pastries() as $pastry) {
/** @var Pastries */
$pastry = Pastries::where($pastry)->first();
$pastry->delete();
}
}

/**
* @return string[][]
*/
Expand Down
18 changes: 15 additions & 3 deletions app/src/MyApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
use UserFrosting\Sprinkle\Admin\Admin;
use UserFrosting\Sprinkle\Core\Core;
use UserFrosting\Sprinkle\Core\Sprinkle\Recipe\MigrationRecipe;
use UserFrosting\Sprinkle\Pastries\Database\Migrations\V100\DefaultPastries;
use UserFrosting\Sprinkle\Core\Sprinkle\Recipe\SeedRecipe;
use UserFrosting\Sprinkle\Pastries\Database\Migrations\V100\PastriesPermissions;
use UserFrosting\Sprinkle\Pastries\Database\Migrations\V100\PastriesTable;
use UserFrosting\Sprinkle\Pastries\Database\Seeds\DefaultPastries;
use UserFrosting\Sprinkle\SprinkleRecipe;
use UserFrosting\Theme\AdminLTE\AdminLTE;

class MyApp implements SprinkleRecipe, MigrationRecipe
class MyApp implements SprinkleRecipe, MigrationRecipe, SeedRecipe
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -82,8 +83,19 @@ public function getMigrations(): array
{
return [
PastriesTable::class,
DefaultPastries::class,
PastriesPermissions::class,
];
}

/**
* {@inheritDoc}
*
* @codeCoverageIgnore
*/
public function getSeeds(): array
{
return [
DefaultPastries::class,
];
}
}
8 changes: 1 addition & 7 deletions app/tests/PastriesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
use UserFrosting\Sprinkle\Account\Database\Models\User;
use UserFrosting\Sprinkle\Account\Testing\WithTestUser;
use UserFrosting\Sprinkle\Core\Testing\RefreshDatabase;
use UserFrosting\Sprinkle\Pastries\MyApp;
use UserFrosting\Sprinkle\Pastries\Database\Migrations\V100\DefaultPastries;
use UserFrosting\Sprinkle\Pastries\Database\Migrations\V100\PastriesPermissions;
use UserFrosting\Sprinkle\Pastries\Database\Migrations\V100\PastriesTable;
use UserFrosting\Sprinkle\Pastries\Database\Models\Pastries;
use UserFrosting\Sprinkle\Pastries\MyApp;
use UserFrosting\Testing\TestCase;

/**
Expand Down Expand Up @@ -60,12 +59,8 @@ public function testMigrations(): void
$this->assertNotNull(Permission::where('slug', 'see_pastry_origin')->first());

// Migration down data migration
$this->ci->get(DefaultPastries::class)->down();
$this->ci->get(PastriesPermissions::class)->down();

// Make sure Pastries were removed
$this->assertSame(0, Pastries::count());

// Make sure permissions were removed
$this->assertNull(Permission::where('slug', 'see_pastries')->first());
$this->assertNull(Permission::where('slug', 'see_pastry_origin')->first());
Expand All @@ -76,7 +71,6 @@ public function testMigrations(): void

// Run back on, to avoid conflict with further tests
$this->ci->get(PastriesTable::class)->up();
$this->ci->get(DefaultPastries::class)->up();
$this->ci->get(PastriesPermissions::class)->up();
}

Expand Down

0 comments on commit 0788cbf

Please sign in to comment.