Tests for File Uploading

This commit is contained in:
David Bomba 2019-06-03 10:28:12 +10:00
parent ea9430f691
commit c2791815a7
3 changed files with 74 additions and 9 deletions

View File

@ -54,8 +54,6 @@ class InvoiceController extends BaseController
*/ */
protected $invoice_repo; protected $invoice_repo;
protected $base_repo;
/** /**
* InvoiceController constructor. * InvoiceController constructor.
* *

View File

@ -16,9 +16,15 @@ use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManager; use Intervention\Image\ImageManager;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class UploadFile class UploadFile implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use MakesHash; use MakesHash;
@ -28,7 +34,7 @@ class UploadFile
protected $company; protected $company;
$entity; public $entity;
/** /**
* Create a new job instance. * Create a new job instance.
@ -52,14 +58,18 @@ class UploadFile
public function handle() : ?Document public function handle() : ?Document
{ {
$path = $this->encodePrimaryKey($this->company->id) . '/' . microtime() . '_' . str_replace(" ", "", $this->file->getClientOriginalName()); //$path = $this->encodePrimaryKey($this->company->id) . '/' . sha1(time()) . '_' . str_replace(" ", "", $this->file->getClientOriginalName());
$path = $this->encodePrimaryKey($this->company->id);
$file_path = $path . '/' . $this->file->hashName();
// Storage::makeDirectory($path);
Storage::put($path, $this->file); Storage::put($path, $this->file);
$width = 0; $width = 0;
$height = 0; $height = 0;
if (in_array($this->file->getClientOriginalExtension(), ['jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd'])) if (in_array($this->file->getClientOriginalExtension(),['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd']))
{ {
$imageSize = getimagesize($this->file); $imageSize = getimagesize($this->file);
$width = $imageSize[0]; $width = $imageSize[0];
@ -73,17 +83,19 @@ class UploadFile
$document->name = $this->file->getClientOriginalName(); $document->name = $this->file->getClientOriginalName();
$document->type = $this->file->getClientOriginalExtension(); $document->type = $this->file->getClientOriginalExtension();
$document->disk = config('filesystems.default'); $document->disk = config('filesystems.default');
$document->hash = $this->createHash(); $document->hash = $this->file->hashName();
$document->size = filesize($filePath); $document->size = filesize(Storage::path($file_path));
$document->width = $width; $document->width = $width;
$document->height = $height; $document->height = $height;
$preview_path = $this->encodePrimaryKey($this->company->id) . '/' . microtime() . '_preview_' . str_replace(" ", "", $this->file->getClientOriginalName()); $preview_path = $this->encodePrimaryKey($this->company->id) . '/' . sha1(time()) . '_preview_' . str_replace(" ", "", $this->file->getClientOriginalName());
$document->preview = $this->generatePreview($preview_path); $document->preview = $this->generatePreview($preview_path);
$this->entity->documents()->save($document); $this->entity->documents()->save($document);
return $document;
} }
private function generatePreview($preview_path) : string private function generatePreview($preview_path) : string

View File

@ -0,0 +1,55 @@
<?php
namespace Tests\Integration;
use App\Events\Invoice\InvoiceWasCreated;
use App\Events\Invoice\InvoiceWasUpdated;
use App\Events\Payment\PaymentWasCreated;
use App\Jobs\Invoice\MarkInvoicePaid;
use App\Jobs\Util\UploadFile;
use App\Models\Account;
use App\Models\Activity;
use App\Models\Company;
use App\Models\CompanyLedger;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\User;
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Log;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Jobs\Util\UploadFile
*/
class UploadFileTest extends TestCase
{
use MockAccountData;
use DatabaseTransactions;
public function setUp() :void
{
parent::setUp();
$this->makeTestData();
}
public function testFileUploadWorks()
{
$document = UploadFile::dispatchNow(UploadedFile::fake()->image('avatar.jpg'), $this->invoice->user, $this->invoice->company, $this->invoice);
$this->assertNotNull($document);
Log::error($document);
// $this->assertEquals($invoice->amount, $payment);
}
}