akaunting/tests/Feature/Sales/InvoicesTest.php

193 lines
5.4 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;
use Illuminate\Support\Facades\File;
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));
}
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
}
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()
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
}
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();
\Excel::fake();
$this->loginAs()
->get(route('invoices.export'))
->assertStatus(200);
\Excel::assertDownloaded(
\Str::filename(trans_choice('general.invoices', 2)) . '.xlsx',
function (Export $export) use ($count) {
// Assert that the correct export is downloaded.
return $export->sheets()['invoices']->collection()->count() === $count;
}
);
}
public function testItShouldExportSelectedInvoices()
{
$count = 5;
$invoices = Document::factory()->invoice()->count($count)->create();
\Excel::fake();
$this->loginAs()
->post(
route('bulk-actions.action', ['group' => 'sales', 'type' => 'invoices']),
['handle' => 'export', 'selected' => [$invoices->random()->id]]
)
->assertStatus(200);
\Excel::assertDownloaded(
\Str::filename(trans_choice('general.invoices', 2)) . '.xlsx',
function (Export $export) {
return $export->sheets()['invoices']->collection()->count() === 1;
}
);
}
public function testItShouldImportInvoices()
{
\Excel::fake();
$this->loginAs()
->post(
route('invoices.import'),
[
'import' => UploadedFile::fake()->createWithContent(
'invoices.xlsx',
File::get(public_path('files/import/invoices.xlsx'))
),
]
)
->assertStatus(200);
\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
}