1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

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 $base_repo;
/**
* InvoiceController constructor.
*

View File

@ -16,9 +16,15 @@ use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
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;
@ -28,7 +34,7 @@ class UploadFile
protected $company;
$entity;
public $entity;
/**
* Create a new job instance.
@ -52,14 +58,18 @@ class UploadFile
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);
$width = 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);
$width = $imageSize[0];
@ -73,17 +83,19 @@ class UploadFile
$document->name = $this->file->getClientOriginalName();
$document->type = $this->file->getClientOriginalExtension();
$document->disk = config('filesystems.default');
$document->hash = $this->createHash();
$document->size = filesize($filePath);
$document->hash = $this->file->hashName();
$document->size = filesize(Storage::path($file_path));
$document->width = $width;
$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);
$this->entity->documents()->save($document);
return $document;
}
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);
}
}