Skip to content

Instantly share code, notes, and snippets.

@chengkangzai
Last active December 3, 2022 08:41
Show Gist options
  • Save chengkangzai/08786c0e3a3f35fe52794948063f53fc to your computer and use it in GitHub Desktop.
Save chengkangzai/08786c0e3a3f35fe52794948063f53fc to your computer and use it in GitHub Desktop.
Auto generate Filament Resource Test
<?php
Route::get('test', function () {
//get all the files under tests/Filament
$files = File::allFiles(base_path('tests/Feature/Filament/Resources'));
foreach ($files as $file) {
//get the className
$className = str(pathinfo($file->getFilename(), PATHINFO_FILENAME))->replace('Test', '');
//get the resource name
$modelName = str($className)->replace('Resource', '');
$modelVariables = str($modelName)->camel()->plural();
$modelVariable = str($modelName)->camel()->replace('_', ' ');
$text = '<?php
use App\Models\User;
use App\Models\\' . $modelName . ';
use App\Filament\Resources\\' . $className . ';
use Database\Seeders\PermissionSeeder;
use Database\Seeders\UserRoleSeeder;
use Filament\Pages\Actions\DeleteAction;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\assertSoftDeleted;
use function Pest\Laravel\seed;
use function Pest\Livewire\livewire;
use function Pest\Laravel\assertModelExists;
use function Pest\Laravel\get;
beforeEach(function (){
seed([
PermissionSeeder::class,
UserRoleSeeder::class,
]);
actingAs(User::factory()->superAdmin()->create());
});
it(\'should render ' . $modelVariable . ' index page\', function () {
get(' . $className . '::getUrl(\'index\'))
->assertSuccessful();
});
it(\'should render list ' . $modelVariable . ' component \', function () {
$' . $modelVariables . ' = ' . $modelName . '::factory()->count(10)->create();
livewire(' . $className . '\Pages\\List' . str($modelName)->plural() . ' ::class)
->assertCanSeeTableRecords($' . $modelVariables . ');
});
it(\'should render ' . $modelVariable . ' create page\', function () {
get(' . $className . '::getUrl(\'create\'))
->assertSuccessful();
});
it(\'should create ' . $modelVariable . '\', function () {
$' . $modelVariable . ' = ' . $modelName . '::factory()->make();
livewire(' . $className . '\Pages\Create' . $modelName . '::class)
->fillForm([
//
])
->call(\'create\')
->assertHasNoFormErrors();
assertModelExists($' . $modelVariable . ');
});
it(\'can validate input\', function () {
livewire(' . $className . '\Pages\Create' . $modelName . '::class)
->fillForm([
//
])
->call(\'create\')
->assertHasFormErrors([
//
]);
});
it(\'should render ' . $modelVariable . ' view page\', function () {
get(' . $className . '::getUrl(\'view\', [
\'record\' => ' . $modelName . '::factory()->create()
]))->assertSuccessful();
});
it(\'should render page to view ' . $modelVariable . ' record \', function () {
$' . $modelVariable . ' = ' . $modelName . '::factory()->create();
livewire(' . $className . '\Pages\View' . $modelName . '::class, [
\'record\' => $' . $modelVariable . '->getKey(),
])
->assertFormSet([
//
]);
});
it(\'should render ' . $modelVariable . ' edit page\', function () {
get(' . $className . '::getUrl(\'edit\', [
\'record\' => ' . $modelName . '::factory()->create()
]))->assertSuccessful();
});
it(\'should render page to show ' . $modelVariable . ' record in edit view\', function () {
$' . $modelVariable . ' = ' . $modelName . '::factory()->create();
livewire(' . $className . '\Pages\Edit' . $modelName . '::class, [
\'record\' => $' . $modelVariable . '->getKey(),
])
->assertFormSet([
//
]);
});
it(\'should edit ' . $modelVariable . '\', function () {
$' . $modelVariable . ' = ' . $modelName . '::factory()->create();
$new' . str($modelVariable)->ucfirst() . ' = ' . $modelName . '::factory()->make();
livewire(' . $className . '\Pages\Edit' . $modelName . '::class, [
\'record\' => $' . $modelVariable . '->getKey(),
])
->fillForm([
//
])
->call(\'save\')
->assertHasNoFormErrors();
expect($' . $modelVariable . '->refresh());
//;
});
it(\'should delete ' . $modelVariable . '\', function () {
$' . $modelVariable . ' = ' . $modelName . '::factory()->create();
livewire(' . $className . '\Pages\Edit' . $modelName . '::class, [
\'record\' => $' . $modelVariable . '->getKey(),
])
->callPageAction(DeleteAction::class);
assertSoftDeleted($' . $modelVariable . ');
});
';
//clear the file content
file_put_contents($file, $text);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment