Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ousid committed Sep 3, 2022
1 parent 2c64027 commit d0b7761
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 25 deletions.
4 changes: 2 additions & 2 deletions database/migrations/create_csv_imports_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ return new class extends Migration
{
Schema::create('csv_imports', function (Blueprint $table) {
$table->id();
$table->nullableMorphs('importable');
$table->foreignId('user_id')->nullable();
$table->foreignId('user_id')->constrained();
$table->string('model');
$table->string('file_path');
$table->string('file_name');
$table->unsignedBigInteger('total_rows');
Expand Down
8 changes: 4 additions & 4 deletions src/Concerns/HasCsvImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
namespace Coderflex\LaravelCsv\Concerns;

use Coderflex\LaravelCsv\Models\Import;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

trait HasCsvImports
{
/**
* Has imports relationship
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<Import>
* @return \Illuminate\Database\Eloquent\Relations\HasMany<Import>
*/
public function imports(): MorphMany
public function imports(): HasMany
{
return $this->morphMany(Import::class, 'importable');
return $this->hasMany(Import::class, 'user_id');
}
}
24 changes: 14 additions & 10 deletions src/Http/Livewire/CsvImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Coderflex\LaravelCsv\Concerns;
use Coderflex\LaravelCsv\Jobs\ImportCsv;
use Coderflex\LaravelCsv\Utilities\ChunkIterator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Validator;
Expand Down Expand Up @@ -108,16 +109,6 @@ protected function setCsvProperties()
[$this->fileHeaders, $this->fileRowCount] = $this->handleCsvProperties();
}

protected function createNewImport()
{
return (new $this->model)->imports()->create([
'user_id' => auth()->id() ?? null,
'file_path' => $this->file->getRealPath(),
'file_name' => $this->file->getClientOriginalName(),
'total_rows' => $this->fileRowCount,
]);
}

protected function importCsv()
{
$import = $this->createNewImport();
Expand All @@ -139,4 +130,17 @@ protected function importCsv()
fn () => $import->touch('compoleted_at')
)->dispatch();
}

protected function createNewImport()
{
/** @var \Illuminate\Foundation\Auth\User */
$user = auth()->user();

return $user->imports()->create([
'model' => $this->model,
'file_path' => $this->file->getRealPath(),
'file_name' => $this->file->getClientOriginalName(),
'total_rows' => $this->fileRowCount,
]);
}
}
2 changes: 1 addition & 1 deletion src/Scopes/ImportScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function scopeNotCompleted(Builder $builder): Builder
*/
public function scopeForModel(Builder $builder, string $model): Builder
{
return $builder->where('importable_type', $model);
return $builder->where('model', $model);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/CsvImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Coderflex\LaravelCsv\Http\Livewire\CsvImporter;
use Coderflex\LaravelCsv\Models\Import;
use Coderflex\LaravelCsv\Tests\Models\Customer;
use Coderflex\LaravelCsv\Tests\Models\User;
use Illuminate\Bus\PendingBatch;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Bus;
Expand Down Expand Up @@ -223,6 +224,8 @@
});

it('ensures the imports is batched', function () {
$this->actingAs(User::factory()->create());

Storage::fake('documents');
Bus::fake();

Expand Down Expand Up @@ -258,6 +261,8 @@
});

it('creates customers records on top of csv file', function () {
$this->actingAs(User::factory()->create());

$file = UploadedFile::fake()
->createWithContent(
'customers.csv',
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/Factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Coderflex\LaravelCsv\Tests\Database\Factories;

use Coderflex\LaravelCsv\Tests\Models\User;

class UserFactory extends \Orchestra\Testbench\Factories\UserFactory
{
protected $model = User::class;
}
21 changes: 21 additions & 0 deletions tests/Database/Migrations/create_users_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
};
1 change: 0 additions & 1 deletion tests/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
class Customer extends Model
{
use HasFactory;
use HasCsvImports;

protected $guarded = [];

Expand Down
20 changes: 20 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Coderflex\LaravelCsv\Tests\Models;

use Coderflex\LaravelCsv\Concerns\HasCsvImports;
use Coderflex\LaravelCsv\Tests\Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class User extends \Illuminate\Foundation\Auth\User
{
use HasFactory;
use HasCsvImports;

protected $guarded = [];

protected static function newFactory()
{
return UserFactory::new();
}
}
14 changes: 7 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');

$migration = include __DIR__.'/../database/migrations/create_csv_imports_table.php.stub';
$migration->up();

$migration = include __DIR__.'/Database/Migrations/create_customers_table.php';
$migration->up();
$migrations = [
include __DIR__ . '/../database/migrations/create_csv_imports_table.php.stub',
include __DIR__ . '/Database/Migrations/create_customers_table.php',
include __DIR__ . '/Database/Migrations/create_users_table.php',
include __DIR__ . '/Database/Migrations/create_job_batches_table.php',
];

$migration = include __DIR__.'/Database/Migrations/create_job_batches_table.php';
$migration->up();
collect($migrations)->each(fn ($path) => $path->up());
}

public function registerLivewireComponents()
Expand Down

0 comments on commit d0b7761

Please sign in to comment.