Advanced

Testing

Patterns for testing Tapix importers, models, events, and queue jobs with Pest.

This guide covers common patterns for testing Tapix importers using Pest PHP.

Testing Importers

Test your importer's field definitions directly by instantiating the class:

use App\Importers\ContactImporter;

it('defines required fields', function () {
    $importer = new ContactImporter();
    $fields = $importer->fields();

    expect($fields)->toHaveCount(5);
    expect($fields->get('email'))->not->toBeNull();
    expect($fields->get('email')->required)->toBeTrue();
});

Testing with Models

Use the Import and ImportRow models to set up import state in your tests:

use Tapix\Core\Models\Import;
use Tapix\Core\Enums\ImportStatus;

it('tracks import progress', function () {
    $import = Import::create([
        'user_id' => $user->id,
        'importer' => ContactImporter::class,
        'file_name' => 'contacts.csv',
        'status' => ImportStatus::Uploading,
        'headers' => ['name', 'email'],
        'column_mappings' => [],
        'total_rows' => 100,
    ]);

    expect($import->status)->toBe(ImportStatus::Uploading);
});

Event Assertions

Verify that Tapix dispatches the correct events during the import lifecycle:

use Illuminate\Support\Facades\Event;
use Tapix\Core\Events\ImportCompleted;

it('dispatches ImportCompleted event', function () {
    Event::fake([ImportCompleted::class]);

    // ... trigger import ...

    Event::assertDispatched(ImportCompleted::class, function ($event) use ($import) {
        return $event->import->id === $import->id;
    });
});

Queue Testing

Assert that the correct jobs are dispatched when an import is triggered:

use Illuminate\Support\Facades\Queue;
use Tapix\Core\Jobs\ExecuteImportJob;

it('dispatches import job', function () {
    Queue::fake([ExecuteImportJob::class]);

    // ... trigger import ...

    Queue::assertPushed(ExecuteImportJob::class);
});

Testing Lifecycle Hooks

Test custom lifecycle hooks by creating an anonymous importer class:

it('runs beforeSave hook', function () {
    $importer = new class extends BaseImporter {
        public bool $hookCalled = false;

        public function model(): string { return Contact::class; }
        public function fields(): ImportFieldCollection { return ImportFieldCollection::make([]); }

        public function beforeSave(Model $model, array $data): void
        {
            $this->hookCalled = true;
        }
    };

    // ... process a row ...

    expect($importer->hookCalled)->toBeTrue();
});
Copyright © 2026