Skip to content

Commit 9466da0

Browse files
committed
update tests
1 parent 789868c commit 9466da0

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

src/Http/Livewire/CsvImporter.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CsvImporter extends Component
1818
use Concerns\HasCsvProperties;
1919

2020
/** @var string */
21-
public string $model;
21+
public $model;
2222

2323
/** @var object */
2424
public $file;
@@ -134,6 +134,7 @@ protected function importCsv()
134134
);
135135

136136
Bus::batch($jobs)
137+
->name('import-csv')
137138
->finally(
138139
fn () => $import->touch('compoleted_at')
139140
)->dispatch();

tests/CsvImporterTest.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?php
22

33
use Coderflex\LaravelCsv\Http\Livewire\CsvImporter;
4+
use Coderflex\LaravelCsv\Jobs\ImportCsv;
45
use Coderflex\LaravelCsv\Models\Import;
56
use Coderflex\LaravelCsv\Tests\Models\Customer;
7+
use Illuminate\Bus\PendingBatch;
68
use Illuminate\Http\UploadedFile;
9+
use Illuminate\Support\Facades\Bus;
710
use Illuminate\Support\Facades\Storage;
811
use function Pest\Livewire\livewire;
912

@@ -220,8 +223,16 @@
220223
->assertHasErrors(['columnsToMap.name', 'columnsToMap.email']);
221224
});
222225

223-
it('creates a new import records', function () {
226+
it('ensures the imports is batched', function () {
224227
Storage::fake('documents');
228+
Bus::fake();
229+
230+
$columnsToMap = [
231+
'id',
232+
'first_name',
233+
'last_name',
234+
'email',
235+
];
225236

226237
$file = UploadedFile::fake()
227238
->createWithContent(
@@ -233,11 +244,23 @@
233244

234245
livewire(CsvImporter::class, [
235246
'model' => $model,
247+
'columnsToMap' => $columnsToMap,
236248
])
237249
->set('file', $file)
250+
->set('columnsToMap', [
251+
'id' => 'id',
252+
'fist_name' => 'fist_name',
253+
'last_name' => 'last_name',
254+
'email' => 'email',
255+
])
238256
->call('import')
239257
->assertHasNoErrors();
240258

259+
Bus::assertBatched(function (PendingBatch $batch) {
260+
return $batch->name == 'import-csv' &&
261+
$batch->jobs->count() === 100;
262+
});
263+
241264
$this->assertEquals(Import::count(), 1);
242265
$this->assertEquals(Import::forModel(Customer::class)->count(), 1);
243266
});

tests/Database/Migrations/create_customers_table.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
{
99
public function up()
1010
{
11-
Schema::create('users', function (Blueprint $table) {
11+
Schema::create('customers', function (Blueprint $table) {
1212
$table->id();
1313
$table->string('first_name');
1414
$table->string('last_name');
1515
$table->string('email')->unique();
16+
$table->string('company')->nullable();
17+
$table->boolean('vip')->default(false);
18+
$table->date('birthday')->nullable();
1619
$table->timestamps();
1720
});
1821
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('job_batches', function (Blueprint $table) {
17+
$table->string('id')->primary();
18+
$table->string('name');
19+
$table->integer('total_jobs');
20+
$table->integer('pending_jobs');
21+
$table->integer('failed_jobs');
22+
$table->text('failed_job_ids');
23+
$table->mediumText('options')->nullable();
24+
$table->integer('cancelled_at')->nullable();
25+
$table->integer('created_at');
26+
$table->integer('finished_at')->nullable();
27+
});
28+
}
29+
30+
/**
31+
* Reverse the migrations.
32+
*
33+
* @return void
34+
*/
35+
public function down()
36+
{
37+
Schema::dropIfExists('job_batches');
38+
}
39+
};

tests/TestCase.php

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public function getEnvironmentSetUp($app)
4242

4343
$migration = include __DIR__.'/Database/Migrations/create_customers_table.php';
4444
$migration->up();
45+
46+
$migration = include __DIR__. '/Database/Migrations/create_job_batches_table.php';
47+
$migration->up();
4548
}
4649

4750
public function registerLivewireComponents()

0 commit comments

Comments
 (0)