PHP code example of joshbonnick / filament-faker

1. Go to this page and download the library: Download joshbonnick/filament-faker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

joshbonnick / filament-faker example snippets




$data = PostResource::fake();

[
  "title" => "Hello World",
  "slug" => "hello-world",
  "meta-description" => "Ut voluptas molestiae sint repudiandae sint et quis.",
  "content" => [
    [
      "type" => "App\Filament\Blocks\Heading",
      "data" => [
        "content" => "Impedit ex odio nostrum.",
        "level" => "h5",
      ],
    ],
    [
      "type" => "App\Filament\Blocks\RichEditor",
      "data" => [
        "content" => "<p>Non est molestiae et quia reiciendis et iste.</p>",
      ],
    ],
    [
      "type" => "App\Filament\Blocks\Image",
      "data" => [
        "file" => "https://placehold.co/600x400.png",
        "alt" => "Et nam aut nobis alias possimus voluptatem.",
      ],
    ],
  ],
  "status" => "draft",
  "categories" => [2],
]



namespace Tests\Feature\Filament\Blog;

use App\Enums\PostStatus;
use App\Filament\Resources\Blog\PostResource;
use App\Models\Category;
use App\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use PHPUnit\Framework\Attributes\Test;
use Tests\FilamentTestCase;

class PostCreateTest extends FilamentTestCase
{
    use RefreshDatabase;

    #[Test]
    public function it_can_create_posts(): void
    {
        Category::factory()->count(2)->create();

        $data = PostResource::faker()->fake();

        Livewire::test(PostResource\Pages\CreatePost::class)
            ->fillForm($data)
            ->call('create')
            ->assertHasNoFormErrors();

        $this->assertDatabaseHas(Post::class, $data);
    }
}




use Filament\Forms\Components\SpatieMediaLibraryFileUpload;

return [
    'fakes' => [
        SpatieMediaLibraryFileUpload::class => fn (SpatieMediaLibraryFileUpload $component) => fake()->imageUrl(),
    ],
];



use Filament\Forms\Components\Field;
use Illuminate\Support\Str;
use App\Services\InjectableService;

$data = PostResource::faker()->mutateFake(function (Field $component, InjectableService $service): mixed {
    return match ($component->getName()) {
        'title' => fake()->jobTitle(),
        default => null,
    };
});



namespace App\Filament\Components;

use Filament\Forms\Components\Field;
use Filament\Forms\Components\TextInput;
use App\Services\InjectableService;

class MutatedComponent extends TextInput
{
    public function mutateFake(Field $component, InjectableService $service): string
    {
        return $service->getSomething();
    }
}



$data = PostResource::faker()->shouldFakeUsingComponentName(false)->fake();
// or
$data = PostResource::faker()->getForm()->shouldFakeUsingComponentName(false)->fake();
// or
$data = MyCustomBlock::faker()->shouldFakeUsingComponentName(false)->fake();



namespace Tests\Feature\Services\ContentFormatting;

use App\Contracts\ContentFormatter;
use App\Filament\Resources\PostResource;
use Tests\TestCase;

class FormatBlocksTest extends TestCase
{
    public function test_it_formats_blocks()
    {
        $data = PostResource::faker()->withFactory()->fake();

        $service = app(ContentFormatter::class);
        $content = $service->format($data);
        
        // Make assertions of your formatted content...
    }
}

$data = PostResource::faker()->withFactory(onlyAttributes: ['title', 'slug'])->fake();

$data = PostResource::faker()->onlyFields('title', 'slug', 'published_at')->fake();
bash
php artisan vendor:publish --tag="filament-faker-config"