akaunting/tests/Feature/Purchases/VendorsTest.php

189 lines
5.1 KiB
PHP
Raw Normal View History

2018-10-09 11:24:50 +03:00
<?php
2019-12-31 15:49:09 +03:00
namespace Tests\Feature\Purchases;
2018-10-09 11:24:50 +03:00
2021-02-12 13:07:05 +03:00
use App\Exports\Purchases\Vendors as Export;
2019-11-17 15:06:00 +03:00
use App\Jobs\Common\CreateContact;
2020-01-07 01:28:05 +03:00
use App\Models\Common\Contact;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
2022-12-15 12:21:04 +03:00
use Maatwebsite\Excel\Facades\Excel;
2018-10-09 11:24:50 +03:00
use Tests\Feature\FeatureTestCase;
class VendorsTest extends FeatureTestCase
{
public function testItShouldSeeVendorListPage()
{
$this->loginAs()
->get(route('vendors.index'))
->assertStatus(200)
->assertSeeText(trans_choice('general.vendors', 2));
}
2021-06-30 12:56:46 +03:00
public function testItShouldSeeVendorShowPage()
{
$request = $this->getRequest();
$vendor = $this->dispatch(new CreateContact($request));
$this->loginAs()
->get(route('vendors.show', $vendor->id))
->assertStatus(200)
->assertSee($vendor->email);
}
2018-10-09 11:24:50 +03:00
public function testItShouldSeeVendorCreatePage()
{
$this->loginAs()
->get(route('vendors.create'))
->assertStatus(200)
->assertSeeText(trans('general.title.new', ['type' => trans_choice('general.vendors', 1)]));
}
public function testItShouldCreateVendor()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest();
2018-10-09 11:24:50 +03:00
$this->loginAs()
2020-07-25 17:22:50 +03:00
->post(route('vendors.store'), $request)
2019-11-16 10:21:14 +03:00
->assertStatus(200);
2018-10-09 11:24:50 +03:00
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
$this->assertDatabaseHas('contacts', $request);
2018-10-09 11:24:50 +03:00
}
public function testItShouldSeeVendorDetailPage()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest();
$vendor = $this->dispatch(new CreateContact($request));
2018-10-09 11:24:50 +03:00
$this->loginAs()
2020-01-13 10:55:19 +03:00
->get(route('vendors.show', $vendor->id))
2018-10-09 11:24:50 +03:00
->assertStatus(200)
->assertSee($vendor->email);
}
public function testItShouldSeeVendorUpdatePage()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest();
$vendor = $this->dispatch(new CreateContact($request));
2018-10-09 11:24:50 +03:00
$this->loginAs()
2020-01-13 10:55:19 +03:00
->get(route('vendors.edit', $vendor->id))
2018-10-09 11:24:50 +03:00
->assertStatus(200)
2020-01-13 10:55:19 +03:00
->assertSee($vendor->email);
2020-07-25 17:22:50 +03:00
$this->assertDatabaseHas('contacts', $request);
2018-10-09 11:24:50 +03:00
}
public function testItShouldUpdateVendor()
{
2020-01-07 01:28:05 +03:00
$request = $this->getRequest();
2018-10-09 11:24:50 +03:00
2019-11-17 15:06:00 +03:00
$vendor = $this->dispatch(new CreateContact($request));
2018-10-09 11:24:50 +03:00
2022-06-01 10:15:55 +03:00
$request['email'] = $this->faker->freeEmail;
2018-10-09 11:24:50 +03:00
$this->loginAs()
->patch(route('vendors.update', $vendor->id), $request)
2020-01-13 17:20:28 +03:00
->assertStatus(200)
2020-03-19 19:00:18 +03:00
->assertSee($request['email']);
2018-10-09 11:24:50 +03:00
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
$this->assertDatabaseHas('contacts', $request);
2018-10-09 11:24:50 +03:00
}
public function testItShouldDeleteVendor()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest();
$vendor = $this->dispatch(new CreateContact($request));
2018-10-09 11:24:50 +03:00
$this->loginAs()
->delete(route('vendors.destroy', $vendor->id))
2019-11-16 10:21:14 +03:00
->assertStatus(200);
2018-10-09 11:24:50 +03:00
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
$this->assertSoftDeleted('contacts', $request);
2018-10-09 11:24:50 +03:00
}
public function testItShouldExportVendors()
{
2022-11-01 13:10:08 +03:00
Contact::factory()->vendor()->count(5)->create();
$count = Contact::vendor()->count();
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->get(route('vendors.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.vendors', 2)) . '-\d{10}\.xlsx/',
function (Export $export) use ($count) {
// Assert that the correct export is downloaded.
return $export->collection()->count() === $count;
}
);
}
public function testItShouldExportSelectedVendors()
{
2021-05-23 17:13:13 +03:00
$create_count = 5;
$select_count = 3;
$vendors = Contact::factory()->vendor()->count($create_count)->create();
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->post(
route('bulk-actions.action', ['group' => 'purchases', 'type' => 'vendors']),
2021-05-23 17:13:13 +03:00
['handle' => 'export', 'selected' => $vendors->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.vendors', 2)) . '-\d{10}\.xlsx/',
2021-05-23 17:13:13 +03:00
function (Export $export) use ($select_count) {
return $export->collection()->count() === $select_count;
}
);
}
public function testItShouldImportVendors()
{
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->post(
route('vendors.import'),
[
'import' => UploadedFile::fake()->createWithContent(
'vendors.xlsx',
File::get(public_path('files/import/vendors.xlsx'))
),
]
)
->assertStatus(200);
2022-12-15 12:21:04 +03:00
Excel::assertImported('vendors.xlsx');
$this->assertFlashLevel('success');
}
2020-01-07 01:28:05 +03:00
public function getRequest()
2018-10-09 11:24:50 +03:00
{
2020-10-14 17:07:59 +03:00
return Contact::factory()->vendor()->enabled()->raw();
2018-10-09 11:24:50 +03:00
}
2019-04-02 18:51:06 +03:00
}