2018-10-26 17:27:48 +03:00
|
|
|
<?php
|
|
|
|
|
2019-12-31 15:49:09 +03:00
|
|
|
namespace Tests\Feature\Purchases;
|
2018-10-26 17:27:48 +03:00
|
|
|
|
2021-02-12 12:16:57 +03:00
|
|
|
use App\Exports\Purchases\Bills as Export;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Jobs\Document\CreateDocument;
|
|
|
|
use App\Models\Document\Document;
|
2021-02-12 12:16:57 +03:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2021-10-29 21:13:20 +06:00
|
|
|
use Illuminate\Support\Carbon;
|
2021-02-12 12:16:57 +03:00
|
|
|
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-26 17:27:48 +03:00
|
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
|
|
|
|
class BillsTest extends FeatureTestCase
|
|
|
|
{
|
|
|
|
public function testItShouldSeeBillListPage()
|
|
|
|
{
|
|
|
|
$this->loginAs()
|
|
|
|
->get(route('bills.index'))
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertSeeText(trans_choice('general.bills', 2));
|
|
|
|
}
|
|
|
|
|
2021-06-30 12:56:46 +03:00
|
|
|
public function testItShouldSeeBillShowPage()
|
|
|
|
{
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
|
|
|
$bill = $this->dispatch(new CreateDocument($request));
|
|
|
|
|
|
|
|
$this->loginAs()
|
|
|
|
->get(route('bills.show', $bill->id))
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertSee($bill->contact_email);
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:27:48 +03:00
|
|
|
public function testItShouldSeeBillCreatePage()
|
|
|
|
{
|
|
|
|
$this->loginAs()
|
|
|
|
->get(route('bills.create'))
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertSeeText(trans('general.title.new', ['type' => trans_choice('general.bills', 1)]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldCreateBill()
|
|
|
|
{
|
2020-07-25 17:22:50 +03:00
|
|
|
$request = $this->getRequest();
|
|
|
|
|
2018-10-26 17:27:48 +03:00
|
|
|
$this->loginAs()
|
2020-07-25 17:22:50 +03:00
|
|
|
->post(route('bills.store'), $request)
|
2019-11-16 10:21:14 +03:00
|
|
|
->assertStatus(200);
|
2018-10-26 17:27:48 +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-26 17:27:48 +03:00
|
|
|
}
|
|
|
|
|
2021-10-29 21:13:20 +06:00
|
|
|
public function testItShouldCreateBillWithAttachment()
|
|
|
|
{
|
|
|
|
Storage::fake('uploads');
|
|
|
|
Carbon::setTestNow(Carbon::create(2021, 05, 15));
|
|
|
|
|
|
|
|
$file = new UploadedFile(
|
|
|
|
base_path('public/img/empty_pages/bills.png'),
|
|
|
|
'bills.png',
|
|
|
|
'image/png',
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$request['attachment'] = [$file];
|
|
|
|
|
|
|
|
$this->loginAs()
|
|
|
|
->post(route('bills.store'), $request)
|
|
|
|
->assertStatus(200);
|
|
|
|
|
|
|
|
$this->assertFlashLevel('success');
|
|
|
|
|
|
|
|
Storage::disk('uploads')->assertExists('2021/05/15/1/bills/bills.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/bills',
|
|
|
|
'filename' => 'bills',
|
|
|
|
'extension' => 'png',
|
|
|
|
'mime_type' => 'image/png',
|
|
|
|
'aggregate_type' => 'image',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:27:48 +03:00
|
|
|
public function testItShouldCreateBillWithRecurring()
|
|
|
|
{
|
2020-07-25 17:22:50 +03:00
|
|
|
$request = $this->getRequest(true);
|
|
|
|
|
2018-10-26 17:27:48 +03:00
|
|
|
$this->loginAs()
|
2022-06-01 10:15:55 +03:00
|
|
|
->post(route('recurring-bills.store'), $request)
|
2019-11-16 10:21:14 +03:00
|
|
|
->assertStatus(200);
|
2018-10-26 17:27:48 +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::BILL_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-26 17:27:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldSeeBillUpdatePage()
|
|
|
|
{
|
2020-07-25 17:22:50 +03:00
|
|
|
$request = $this->getRequest();
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
$bill = $this->dispatch(new CreateDocument($request));
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2018-10-26 17:27:48 +03:00
|
|
|
$this->loginAs()
|
2020-01-13 10:55:19 +03:00
|
|
|
->get(route('bills.edit', $bill->id))
|
2018-10-26 17:27:48 +03:00
|
|
|
->assertStatus(200)
|
2019-11-16 10:21:14 +03:00
|
|
|
->assertSee($bill->contact_email);
|
2018-10-26 17:27:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldUpdateBill()
|
|
|
|
{
|
2020-01-13 17:22:31 +03:00
|
|
|
$request = $this->getRequest();
|
2019-11-17 15:06:00 +03:00
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
$bill = $this->dispatch(new CreateDocument($request));
|
2018-10-26 17:27:48 +03:00
|
|
|
|
2020-03-19 19:00:18 +03:00
|
|
|
$request['contact_email'] = $this->faker->safeEmail;
|
2018-10-26 17:27:48 +03:00
|
|
|
|
|
|
|
$this->loginAs()
|
|
|
|
->patch(route('bills.update', $bill->id), $request)
|
2020-01-13 17:22:31 +03:00
|
|
|
->assertStatus(200)
|
2020-03-19 19:00:18 +03:00
|
|
|
->assertSee($request['contact_email']);
|
2018-10-26 17:27:48 +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-26 17:27:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldDeleteBill()
|
|
|
|
{
|
2020-07-25 17:22:50 +03:00
|
|
|
$request = $this->getRequest();
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
$bill = $this->dispatch(new CreateDocument($request));
|
2018-10-26 17:27:48 +03:00
|
|
|
|
|
|
|
$this->loginAs()
|
|
|
|
->delete(route('bills.destroy', $bill->id))
|
2019-11-17 15:06:00 +03:00
|
|
|
->assertStatus(200);
|
2018-10-26 17:27:48 +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-26 17:27:48 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 12:16:57 +03:00
|
|
|
public function testItShouldExportBills()
|
|
|
|
{
|
|
|
|
$count = 5;
|
|
|
|
Document::factory()->bill()->count($count)->create();
|
|
|
|
|
2022-12-15 12:21:04 +03:00
|
|
|
Excel::fake();
|
2021-02-12 12:16:57 +03:00
|
|
|
|
|
|
|
$this->loginAs()
|
|
|
|
->get(route('bills.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(
|
2022-12-16 10:36:57 +03:00
|
|
|
'/' . str()->filename(trans_choice('general.bills', 2)) . '-\d{10}\.xlsx/',
|
2021-02-12 12:16:57 +03:00
|
|
|
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;
|
2021-02-12 12:16:57 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldExportSelectedBills()
|
|
|
|
{
|
2021-05-23 17:13:13 +03:00
|
|
|
$create_count = 5;
|
|
|
|
$select_count = 3;
|
|
|
|
|
|
|
|
$bills = Document::factory()->bill()->count($create_count)->create();
|
2021-02-12 12:16:57 +03:00
|
|
|
|
2022-12-15 12:21:04 +03:00
|
|
|
Excel::fake();
|
2021-02-12 12:16:57 +03:00
|
|
|
|
|
|
|
$this->loginAs()
|
|
|
|
->post(
|
|
|
|
route('bulk-actions.action', ['group' => 'purchases', 'type' => 'bills']),
|
2021-05-23 17:13:13 +03:00
|
|
|
['handle' => 'export', 'selected' => $bills->take($select_count)->pluck('id')->toArray()]
|
2021-02-12 12:16:57 +03:00
|
|
|
)
|
|
|
|
->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(
|
2022-12-16 10:36:57 +03:00
|
|
|
'/' . str()->filename(trans_choice('general.bills', 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;
|
2021-02-12 12:16:57 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldImportBills()
|
|
|
|
{
|
2022-12-15 12:21:04 +03:00
|
|
|
Excel::fake();
|
2021-02-12 12:16:57 +03:00
|
|
|
|
|
|
|
$this->loginAs()
|
|
|
|
->post(
|
|
|
|
route('bills.import'),
|
|
|
|
[
|
|
|
|
'import' => UploadedFile::fake()->createWithContent(
|
|
|
|
'bills.xlsx',
|
|
|
|
File::get(public_path('files/import/bills.xlsx'))
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
->assertStatus(200);
|
|
|
|
|
2022-12-15 12:21:04 +03:00
|
|
|
Excel::assertImported('bills.xlsx');
|
2021-02-12 12:16:57 +03:00
|
|
|
|
|
|
|
$this->assertFlashLevel('success');
|
|
|
|
}
|
|
|
|
|
2020-01-13 17:22:31 +03:00
|
|
|
public function getRequest($recurring = false)
|
2018-10-26 17:27:48 +03:00
|
|
|
{
|
2020-12-24 01:28:38 +03:00
|
|
|
$factory = Document::factory();
|
2020-01-13 17:22:31 +03:00
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
$factory = $recurring ? $factory->bill()->items()->recurring() : $factory->bill()->items();
|
2020-01-13 17:22:31 +03:00
|
|
|
|
|
|
|
return $factory->raw();
|
2018-10-26 17:27:48 +03:00
|
|
|
}
|
2019-04-02 18:51:06 +03:00
|
|
|
}
|