akaunting/tests/Feature/Sales/InvoicesTest.php

258 lines
7.3 KiB
PHP
Raw Normal View History

2018-10-30 15:55:41 +03:00
<?php
2019-12-31 15:49:09 +03:00
namespace Tests\Feature\Sales;
2018-10-30 15:55:41 +03:00
use App\Exports\Sales\Invoices as Export;
2020-12-24 01:28:38 +03:00
use App\Jobs\Document\CreateDocument;
use App\Models\Document\Document;
use Illuminate\Http\UploadedFile;
2021-10-29 21:13:20 +06:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\File;
2021-10-29 21:13:20 +06:00
use Illuminate\Support\Facades\Storage;
2022-12-15 12:21:04 +03:00
use Maatwebsite\Excel\Facades\Excel;
2018-10-30 15:55:41 +03:00
use Tests\Feature\FeatureTestCase;
class InvoicesTest extends FeatureTestCase
{
public function testItShouldSeeInvoiceListPage()
{
$this->loginAs()
2019-11-16 10:21:14 +03:00
->get(route('invoices.index'))
2018-10-30 15:55:41 +03:00
->assertStatus(200)
->assertSeeText(trans_choice('general.invoices', 2));
}
2021-06-30 12:56:46 +03:00
public function testItShouldSeeInvoiceShowPage()
{
$request = $this->getRequest();
$invoice = $this->dispatch(new CreateDocument($request));
$this->loginAs()
->get(route('invoices.show', $invoice->id))
->assertStatus(200)
->assertSee($invoice->contact_email);
}
2018-10-30 15:55:41 +03:00
public function testItShouldSeeInvoiceCreatePage()
{
$this->loginAs()
2019-11-16 10:21:14 +03:00
->get(route('invoices.create'))
2018-10-30 15:55:41 +03:00
->assertStatus(200)
2019-11-17 15:06:00 +03:00
->assertSeeText(trans('general.title.new', ['type' => trans_choice('general.invoices', 1)]));
2018-10-30 15:55:41 +03:00
}
public function testItShouldCreateInvoice()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest();
2018-10-30 15:55:41 +03:00
$this->loginAs()
2020-07-25 17:22:50 +03:00
->post(route('invoices.store'), $request)
2019-11-16 10:21:14 +03:00
->assertStatus(200);
2018-10-30 15:55:41 +03:00
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
2020-12-24 01:28:38 +03:00
$this->assertDatabaseHas('documents', [
'document_number' => $request['document_number'],
2020-07-25 17:22:50 +03:00
]);
2018-10-30 15:55:41 +03:00
}
2021-10-29 21:13:20 +06:00
public function testItShouldCreateInvoiceWithAttachment()
{
Storage::fake('uploads');
Carbon::setTestNow(Carbon::create(2021, 05, 15));
$file = new UploadedFile(
base_path('public/img/empty_pages/invoices.png'),
'invoices.png',
'image/png',
null,
true
);
$request = $this->getRequest();
$request['attachment'] = [$file];
$this->loginAs()
->post(route('invoices.store'), $request)
->assertStatus(200);
$this->assertFlashLevel('success');
Storage::disk('uploads')->assertExists('2021/05/15/1/invoices/invoices.png');
$this->assertDatabaseHas('documents', [
'document_number' => $request['document_number']
]);
2022-06-01 10:15:55 +03:00
2021-10-29 21:13:20 +06:00
$this->assertDatabaseHas('mediables', [
'mediable_type' => Document::class,
'tag' => 'attachment',
]);
2022-06-01 10:15:55 +03:00
2021-10-29 21:13:20 +06:00
$this->assertDatabaseHas('media', [
'disk' => 'uploads',
'directory' => '2021/05/15/1/invoices',
'filename' => 'invoices',
'extension' => 'png',
'mime_type' => 'image/png',
'aggregate_type' => 'image',
]);
}
public function testItShouldDuplicateInvoice()
{
$invoice = $this->dispatch(new CreateDocument($this->getRequest()));
$this->loginAs()
->get(route('invoices.duplicate', ['invoice' => $invoice->id]))
->assertStatus(302);
$this->assertFlashLevel('success');
}
2018-10-30 15:55:41 +03:00
public function testItShouldCreateInvoiceWithRecurring()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest(true);
2018-10-30 15:55:41 +03:00
$this->loginAs()
2022-06-01 10:15:55 +03:00
->post(route('recurring-invoices.store'), $request)
2019-11-16 10:21:14 +03:00
->assertStatus(200);
2018-10-30 15:55:41 +03:00
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
2020-12-24 01:28:38 +03:00
$this->assertDatabaseHas('documents', [
2022-06-01 10:15:55 +03:00
'type' => Document::INVOICE_RECURRING_TYPE,
2020-12-24 01:28:38 +03:00
'document_number' => $request['document_number'],
2020-07-25 17:22:50 +03:00
]);
2018-10-30 15:55:41 +03:00
}
2019-11-16 10:21:14 +03:00
public function testItShouldSeeInvoiceUpdatePage()
2018-10-30 15:55:41 +03:00
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest();
2020-12-24 01:28:38 +03:00
$invoice = $this->dispatch(new CreateDocument($request));
2018-10-30 15:55:41 +03:00
2019-11-16 10:21:14 +03:00
$this->loginAs()
2020-01-13 10:55:19 +03:00
->get(route('invoices.edit', $invoice->id))
2019-11-16 10:21:14 +03:00
->assertStatus(200)
->assertSee($invoice->contact_email);
}
2018-10-30 15:55:41 +03:00
2019-11-16 10:21:14 +03:00
public function testItShouldUpdateInvoice()
{
2020-01-13 15:16:35 +03:00
$request = $this->getRequest();
2019-11-17 15:06:00 +03:00
2020-12-24 01:28:38 +03:00
$invoice = $this->dispatch(new CreateDocument($request));
2019-11-16 10:21:14 +03:00
2020-03-19 19:00:18 +03:00
$request['contact_email'] = $this->faker->safeEmail;
2018-10-30 15:55:41 +03:00
$this->loginAs()
2019-11-16 10:21:14 +03:00
->patch(route('invoices.update', $invoice->id), $request)
2020-01-13 17:20:28 +03:00
->assertStatus(200)
2020-03-19 19:00:18 +03:00
->assertSee($request['contact_email']);
2018-10-30 15:55:41 +03:00
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
2020-12-24 01:28:38 +03:00
$this->assertDatabaseHas('documents', [
'document_number' => $request['document_number'],
2020-07-25 17:22:50 +03:00
'contact_email' => $request['contact_email'],
]);
2018-10-30 15:55:41 +03:00
}
public function testItShouldDeleteInvoice()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest();
2020-12-24 01:28:38 +03:00
$invoice = $this->dispatch(new CreateDocument($request));
2018-10-30 15:55:41 +03:00
$this->loginAs()
2019-11-16 10:21:14 +03:00
->delete(route('invoices.destroy', $invoice->id))
->assertStatus(200);
2018-10-30 15:55:41 +03:00
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
2020-12-24 01:28:38 +03:00
$this->assertSoftDeleted('documents', [
'document_number' => $request['document_number'],
2020-07-25 17:22:50 +03:00
]);
2018-10-30 15:55:41 +03:00
}
public function testItShouldExportInvoices()
{
$count = 5;
Document::factory()->invoice()->count($count)->create();
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->get(route('invoices.export'))
->assertStatus(200);
2022-12-15 12:21:04 +03:00
Excel::matchByRegex();
2021-05-23 17:13:13 +03:00
2022-12-15 12:21:04 +03:00
Excel::assertDownloaded(
'/' . str()->filename(trans_choice('general.invoices', 2), '-') . '-\d{10}\.xlsx/',
function (Export $export) use ($count) {
// Assert that the correct export is downloaded.
2021-05-23 17:13:13 +03:00
return $export->sheets()[0]->collection()->count() === $count;
}
);
}
public function testItShouldExportSelectedInvoices()
{
2021-05-23 17:13:13 +03:00
$create_count = 5;
$select_count = 3;
$invoices = Document::factory()->invoice()->count($create_count)->create();
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->post(
route('bulk-actions.action', ['group' => 'sales', 'type' => 'invoices']),
2021-05-23 17:13:13 +03:00
['handle' => 'export', 'selected' => $invoices->take($select_count)->pluck('id')->toArray()]
)
->assertStatus(200);
2022-12-15 12:21:04 +03:00
Excel::matchByRegex();
2021-05-23 17:13:13 +03:00
2022-12-15 12:21:04 +03:00
Excel::assertDownloaded(
'/' . str()->filename(trans_choice('general.invoices', 2), '-') . '-\d{10}\.xlsx/',
2021-05-23 17:13:13 +03:00
function (Export $export) use ($select_count) {
return $export->sheets()[0]->collection()->count() === $select_count;
}
);
}
public function testItShouldImportInvoices()
{
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->post(
route('invoices.import'),
[
'import' => UploadedFile::fake()->createWithContent(
'invoices.xlsx',
File::get(public_path('files/import/invoices.xlsx'))
),
]
)
->assertStatus(200);
2022-12-15 12:21:04 +03:00
Excel::assertImported('invoices.xlsx');
$this->assertFlashLevel('success');
}
2020-01-13 15:16:35 +03:00
public function getRequest($recurring = false)
2018-10-30 15:55:41 +03:00
{
2020-12-24 01:28:38 +03:00
$factory = Document::factory();
2020-01-13 15:16:35 +03:00
2020-12-24 01:28:38 +03:00
$factory = $recurring ? $factory->invoice()->items()->recurring() : $factory->invoice()->items();
2020-01-13 15:16:35 +03:00
return $factory->raw();
2018-10-30 15:55:41 +03:00
}
2019-04-02 18:51:06 +03:00
}