---
title: "Testovanie — Imports for Laravel | Roundly"
description: "Imports::fake() zaznamenáva a prepúšťa ďalej — každá dvojica asercií, zaznamenané operácie, withoutRunning(), generátory tabuliek, factories a workbench."
url: https://roundly-consulting.com/sk/open-source/navody/imports-for-laravel/testovanie
language: sk
---

[Všetky balíky](https://roundly-consulting.com/sk/open-source.md)

[Imports for Laravel](https://roundly-consulting.com/sk/open-source/navody/imports-for-laravel.md)

# Testovanie

Imports::fake() nahradí manažér triedou ImportsFake za fasádou aj v kontajneri a vráti ju:

```php
use Illuminate\Support\Facades\Bus;
use RoundlyConsulting\Imports\Facades\Imports;
use RoundlyConsulting\Imports\Models\ImportSession;
use RoundlyConsulting\Imports\Testing\SpreadsheetFactory;

it('imports the uploaded assets', function () {
    $fake = Imports::fake();   // records every operation and passes it through
    Bus::fake();               // optional: keep commits from running

    $this->actingAs($admin)->post('/assets/import', ['file' => SpreadsheetFactory::csv(['Asset tag', 'Name'], [['AST-1', 'Laptop']])]);

    Imports::assertUploaded('assets', fn (ImportSession $session) => $session->owner->is($team));
    Imports::assertPreviewed();
    Imports::assertNothingCommitted();
    expect($fake->recorded('upload'))->toHaveCount(1);
});
```

- Zaznamená každú operáciu a prepustí ju ďalej: handle stále vracajú skutočné relácie a dávky. Ak nechcete, aby sa importy spustili, pridajte Bus::fake().
- Zaznamenáva volania cez fasádu, injektovaný manažér (ImportsFake rozširuje ImportsManager), každý handle aj pomocníkov na modeloch ($session->handle(), $batch->handle()).
- Čítanie — status(), counts(), rows(), suggestMapping(), … — sa nezaznamenáva.
- \->withoutRunning() zaznamená bez spustenia operácie, ktoré nevracajú nič, čo by test potreboval — cancel, discard, resume, rollback, mazanie mapovaní, prune a sweep (vrátia null alebo prázdny report). mappings(…)->delete() vráti true, ako pri existujúcom mapovaní, takže endpoint na zmazanie mapovania odpovie 200; zaznamenaný result zostane null. Všetko ostatné stále beží.

```php
use RoundlyConsulting\Imports\Models\ImportBatch;

$fake = Imports::fake()->withoutRunning();   // cancel, discard, resume, rollback, deleteMapping, prune, sweep: record only

Imports::for($team)->by($admin)->batch($batchId)->rollback();   // recorded, not run

Imports::assertRolledBack(fn (ImportBatch $batch) => $batch->getKey() === $batchId);
$fake->recorded('rollback')[0]->action;     // the action class it would have run
```

## Asercie

Každá asercia má negatívny pár a je uvedená na fasáde, takže Imports::assertModeChosen() sa v IDE doplní aj po Imports::fake(). Callback dostane model a vráti bool. assertMappingSaved() pokrýva uloženia z relácie aj cez mappings()->save():

| Asercia | Negatívna | Callback dostane |
| --- | --- | --- |
| `assertUploaded(?string $importer = null, ?Closure $callback = null)` | `assertNothingUploaded()` | ImportSession |
| `assertFilesUploaded(?string $importer = null, ?Closure $callback = null)` | `assertNoFilesUploaded()` | ImportSession |
| `assertSourceConfigured(?Closure $callback = null)` | `assertNoSourceConfigured()` | ImportSession |
| `assertMapped(?Closure $callback = null)` | `assertNothingMapped()` | ImportSession |
| `assertMappingSaved(?string $name = null)` | `assertNoMappingSaved()` | — |
| `assertMappingDeleted(?string $name = null)` | `assertNoMappingDeleted()` | — |
| `assertModeChosen(?DuplicateMode $mode = null, ?Closure $callback = null)` | `assertNoModeChosen()` | ImportSession |
| `assertPreviewed(?Closure $callback = null)` | `assertNothingPreviewed()` | ImportSession |
| `assertCommitted(?string $importer = null, ?Closure $callback = null)` | `assertNothingCommitted()` | ImportBatch |
| `assertCancelled(?Closure $callback = null)` | `assertNothingCancelled()` | ImportBatch |
| `assertResumed(?Closure $callback = null)` | `assertNothingResumed()` | ImportBatch |
| `assertRolledBack(?Closure $callback = null)` | `assertNothingRolledBack()` | ImportBatch |
| `assertDiscarded(?Closure $callback = null)` | `assertNothingDiscarded()` | ImportSession |
| `assertTemplateBuilt(?string $importer = null, ?TemplateFormat $format = null, ?string $locale = null)` | `assertNoTemplateBuilt()` | — |
| `assertErrorReportBuilt(?Closure $callback = null)` | `assertNoErrorReportBuilt()` | ImportSession |
| `assertPruned()` | `assertNotPruned()` | — |
| `assertSwept()` | `assertNotSwept()` | — |
| `assertNothingHappened()` | — | — |

## Zaznamenané operácie

recorded(?string $operation = null) vráti zoznam RecordedOperation — každá s operation, context (relácia, dávka, importér, vlastník, režim, formát, … s ktorými bola volaná), result a action (trieda akcie, ktorá ju spustila). Názvy operácií: upload, uploadFiles, configureSource, map, saveMapping, deleteMapping, mode, preview, template, errorReport, commit, cancel, resume, rollback, discard, prune, sweep.

## Vytváranie súborov

SpreadsheetFactory vytvára nahraté súbory CSV, XLSX a ZIP — každá metóda vráti UploadedFile. Prázdny zoznam hlavičiek nezapíše riadok hlavičky, riadky XLSX prijímajú aj hodnoty DateTimeInterface a encoding prijíma aj UTF-16LE, UTF-16BE, Windows-1252 a Windows-1250 na testovanie odmietnutí:

```php
use RoundlyConsulting\Imports\Testing\SpreadsheetFactory;

SpreadsheetFactory::csv(['Asset tag', 'Name'], [['AST-1', 'Laptop']], delimiter: ';', bom: true, encoding: 'UTF-8', eol: "\n", name: 'import.csv');
SpreadsheetFactory::xlsx(['Asset tag', 'Name'], [['AST-1', 'Laptop']], sheet: 'Assets', extraSheets: ['Other' => [['A']]], date1904: false, sharedStrings: true, name: 'import.xlsx');
SpreadsheetFactory::zip(['a.pdf' => '%PDF', 'b.pdf' => '%PDF'], name: 'files.zip');
SpreadsheetFactory::file('notes.txt', 'hello', mime: 'text/plain');

SpreadsheetFactory::csv(['Name'], [['Žltý kôň']], encoding: 'Windows-1250');   // to test the not_utf8 rejection
```

## Factories

ImportSession, ImportBatch, ImportRowResult aj ImportedRecord majú factories:

```php
use RoundlyConsulting\Imports\Enums\SessionStatus;
use RoundlyConsulting\Imports\Models\{ImportedRecord, ImportSession};

ImportSession::factory()->owner($team)->status(SessionStatus::Previewed)->create();
ImportedRecord::factory()->inBatch($batch)->record($asset)->create();
```

## Náhľady a importy vo fronte

```php
// Run queued previews and commits in a test
Artisan::call('queue:work', ['--stop-when-empty' => true]);

// or run everything inline
config(['imports.queue.enabled' => false]);
```

## Vyskúšajte si to lokálne

Repozitár balíka obsahuje workbench aplikáciu na ručné vyskúšanie: dva tímy, v každom správca, ktorý smie importovať, a člen, ktorý nesmie, dodávateľov na odkazovanie, tri importéry (assets, people, documents) a zapnuté JSON API:

```bash
vendor/bin/testbench workbench:build      # a fresh SQLite database, every migration, the seeder
vendor/bin/testbench serve                # http://127.0.0.1:8000
vendor/bin/testbench queue:work           # in a second terminal: large previews, commits and undo
```

## Prejavte lásku k open source

Tento balík je zadarmo pod licenciou MIT. Ak vám šetrí čas, jednorazový príspevok alebo členstvo na Patreone nám pomôže ho ďalej udržiavať, testovať a dokumentovať.

[Ďalšie spôsoby podpory vrátane kryptomien](https://roundly-consulting.com/sk/podporte-nas.md)

Odoslaním daru súhlasíte s našimi [podmienkami prijímania darov](https://roundly-consulting.com/sk/podmienky-darov.md).

[Podporte našu open-source prácu (otvorí sa v novej karte)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Pridajte sa k nám na Patreone (otvorí sa v novej karte)](https://www.patreon.com/cw/roundly)

## Chcete to zabudovať do svojho produktu?

Naše balíky integrujeme do zákazkových Laravel a AI riešení. Napíšte nám, na čom pracujete, a ozveme sa do 48 hodín.

[Ponuka do 48 hodín](https://roundly-consulting.com/sk/kontakt.md) [Prezrieť všetky balíky](https://roundly-consulting.com/sk/open-source/navody/imports-for-laravel.md)
