PHP code example of binarysolutions / test-data-blueprints

1. Go to this page and download the library: Download binarysolutions/test-data-blueprints 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/ */

    

binarysolutions / test-data-blueprints example snippets



// database/blueprints/saas_company.php

use BinarySolutions\TestDataBlueprints\Fluent\Blueprint;
use App\Models\Company;
use App\Models\User;
use App\Models\Subscription;

return Blueprint::define('saas-company')
    ->at('2025-01-01')
    ->create(Company::factory()->state(['name' => 'ACME BV']), as: 'company')
    ->create(User::factory()->count(3)->state(['role' => 'admin']), as: 'admins', for: '@company')
    ->create(Subscription::factory()->active(), as: 'subscription', for: '@company');

->create(
    factory: Company::factory()->state(['vat_number' => 'NL123456789B01']),
    as: 'company',
)

// With count and a @ref token for the relationship
->create(User::factory(), as: 'users', count: 5, for: '@company')

->make(Company::factory(), as: 'company')
->make(User::factory(), as: 'users', count: 3, for: '@company')

->tap(function (BlueprintContext $ctx, BlueprintResult $result) {
    $n = (int) $ctx->param('users', 3);
    return collect(range(1, $n))->map(fn ($i) => (object) ['id' => $i]);
}, as: 'users')

->create(Company::factory(), as: 'company')

// Pass the actual company model to the factory's ->for() method
->create(User::factory(), as: 'users', for: '@company')

// Or inline in a state array
->create(Invoice::factory(), as: 'invoice', state: ['company_id' => '@company.id'])

// database/blueprints/company_overdue.php
return Blueprint::define('company-overdue')
    ->use('saas-company')           // imports: company, admins, subscription
    ->create(Invoice::factory()->overdue()->count(3), as: 'overdue_invoices', for: '@company')
    ->create(AuditLog::factory()->state(['event' => 'payment_failed']), as: 'payment_log', for: '@company');

return Blueprint::define('overdue-invoice')
    ->at('2024-01-01')                          // Carbon::setTestNow('2024-01-01')
    ->create(Invoice::factory()->due(), as: 'invoice')
    // Carbon is restored to its previous value after the blueprint runs

return Blueprint::define('in-memory-scenario')
    ->make(Company::factory(), as: 'company')
    ->make(User::factory()->count(3), as: 'users', for: '@company')
    ->snapshot(); // cache the result for the lifetime of the test process

class MyTest extends TestCase
{
    use UsesBlueprints;

    // Disable auto-clear when no blueprints in this class touch the database
    protected bool $autoClearBlueprintSnapshots = false;
}

use BinarySolutions\TestDataBlueprints\Testing\UsesBlueprints;
use Illuminate\Foundation\Testing\RefreshDatabase;

class SubscriptionTest extends TestCase
{
    use RefreshDatabase, UsesBlueprints;
    // UsesBlueprints clears the snapshot cache before each test automatically.
    // Do NOT add ->snapshot() to blueprints that use ->create().

    public function test_suspended_company_cannot_access_dashboard(): void
    {
        $refs    = $this->blueprint('company-suspended');
        $company = $refs->get('company');
        $admin   = $refs->get('admins')->first();

        $this->actingAs($admin)
             ->get(route('dashboard', $company))
             ->assertForbidden();
    }

    public function test_blueprint_accepts_runtime_parameters(): void
    {
        $refs = $this->blueprint('saas-company', ['users' => 10]);
        $this->assertCount(10, $refs->get('admins'));
    }
}

use BinarySolutions\TestDataBlueprints\Testing\UsesBlueprints;

uses(UsesBlueprints::class)->in('Feature', 'Unit');

it('sends an overdue reminder to all admins', function () {
    Mail::fake();
    $refs = $this->blueprint('company-overdue');

    $this->artisan('invoices:send-reminders');

    Mail::assertSent(OverdueReminderMail::class, $refs->get('admins')->count());
});

// database/seeders/DevelopmentSeeder.php
class DevelopmentSeeder extends Seeder
{
    public function run(): void
    {
        if (! app()->isLocal()) {
            throw new \RuntimeException('DevelopmentSeeder must not run outside local.');
        }

        $engine = app(\BinarySolutions\TestDataBlueprints\BlueprintEngine::class);

        $engine->run(app(), 'saas-company', ['company_name' => 'Demo Corp']);
        $engine->run(app(), 'company-overdue');

        $this->command->info('Development seed complete.');
    }
}

// config/test-data-blueprints.php
return [
    // Directories scanned for blueprint files
    'paths' => [
        base_path('database/blueprints'),
    ],

    // Glob patterns applied within each path
    'patterns' => ['*.php'],

    // Cache blueprint instances in memory after the first file load
    'cache_in_memory' => true,
];

'paths' => [
    base_path('database/blueprints'),
    base_path('packages/billing/database/blueprints'),
],
bash
php artisan make:blueprint SaasCompany
# Creates: database/blueprints/saas_company.php
# Blueprint name: saas-company
bash
php artisan make:blueprint SaasCompany
php artisan make:blueprint CompanyWithSubscription --force
bash
php artisan blueprints:list