akaunting/tests/Feature/Common/ItemsTest.php

164 lines
4.0 KiB
PHP
Raw Normal View History

2018-07-14 13:26:29 +03:00
<?php
namespace Tests\Feature\Common;
use App\Exports\Common\Items as Export;
2019-11-17 15:06:00 +03:00
use App\Jobs\Common\CreateItem;
2019-11-22 17:35:46 +03:00
use App\Models\Common\Item;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
2022-12-15 12:21:04 +03:00
use Maatwebsite\Excel\Facades\Excel;
2018-07-14 13:26:29 +03:00
use Tests\Feature\FeatureTestCase;
class ItemsTest extends FeatureTestCase
{
2018-09-29 22:41:17 +03:00
public function testItShouldSeeItemListPage()
2018-07-14 13:26:29 +03:00
{
2018-09-29 17:28:34 +03:00
$this->loginAs()
2018-07-14 13:26:29 +03:00
->get(route('items.index'))
->assertStatus(200)
2018-09-29 22:41:17 +03:00
->assertSeeText(trans_choice('general.items', 2));
2018-07-14 13:26:29 +03:00
}
2018-09-29 22:41:17 +03:00
public function testItShouldSeeItemCreatePage()
2018-07-14 13:26:29 +03:00
{
2018-09-29 17:28:34 +03:00
$this->loginAs()
2018-07-14 13:26:29 +03:00
->get(route('items.create'))
->assertStatus(200)
2018-09-29 22:41:17 +03:00
->assertSeeText(trans('general.title.new', ['type' => trans_choice('general.items', 1)]));
2018-07-14 13:26:29 +03:00
}
2018-09-29 22:41:17 +03:00
public function testItShouldCreateItem()
2018-07-14 13:26:29 +03:00
{
2020-06-21 01:27:20 +03:00
$request = $this->getRequest();
2018-09-29 17:28:34 +03:00
$this->loginAs()
2020-06-21 01:27:20 +03:00
->post(route('items.store'), $request)
2019-11-16 10:21:14 +03:00
->assertStatus(200);
2018-09-29 17:28:34 +03:00
2018-07-14 13:26:29 +03:00
$this->assertFlashLevel('success');
2020-06-21 01:27:20 +03:00
2020-12-14 16:04:54 +03:00
$this->assertDatabaseHas('items', $request);
2018-07-14 13:26:29 +03:00
}
2018-09-29 22:41:17 +03:00
public function testItShouldSeeItemUpdatePage()
2018-07-14 13:26:29 +03:00
{
2020-06-21 01:27:20 +03:00
$request = $this->getRequest();
$item = $this->dispatch(new CreateItem($request));
2018-07-14 13:26:29 +03:00
2018-09-29 17:28:34 +03:00
$this->loginAs()
2020-01-13 10:55:19 +03:00
->get(route('items.edit', $item->id))
2018-07-14 13:26:29 +03:00
->assertStatus(200)
->assertSee($item->name);
}
2018-09-29 22:41:17 +03:00
public function testItShouldUpdateItem()
{
2020-01-07 01:28:05 +03:00
$request = $this->getRequest();
2018-09-29 22:41:17 +03:00
2019-11-17 15:06:00 +03:00
$item = $this->dispatch(new CreateItem($request));
2018-09-29 22:41:17 +03:00
2019-11-18 10:54:26 +03:00
$request['name'] = $this->faker->text(15);
2018-09-29 22:41:17 +03:00
$this->loginAs()
->patch(route('items.update', $item->id), $request)
2020-01-13 17:20:28 +03:00
->assertStatus(200)
->assertSee($request['name']);
2018-09-29 22:41:17 +03:00
$this->assertFlashLevel('success');
2020-06-21 01:27:20 +03:00
2020-12-14 16:04:54 +03:00
$this->assertDatabaseHas('items', $request);
2018-09-29 22:41:17 +03:00
}
2018-07-14 13:26:29 +03:00
public function testItShouldDeleteItem()
{
2020-06-21 01:27:20 +03:00
$request = $this->getRequest();
$item = $this->dispatch(new CreateItem($request));
2018-07-14 13:26:29 +03:00
2018-09-29 17:28:34 +03:00
$this->loginAs()
2020-01-13 10:55:19 +03:00
->delete(route('items.destroy', $item->id))
2019-11-16 10:21:14 +03:00
->assertStatus(200);
2018-07-14 13:26:29 +03:00
$this->assertFlashLevel('success');
2020-06-21 01:27:20 +03:00
2020-12-14 16:04:54 +03:00
$this->assertSoftDeleted('items', $request);
2018-07-14 13:26:29 +03:00
}
2020-01-07 01:28:05 +03:00
public function testItShouldExportItems()
{
$count = 5;
Item::factory()->count($count)->create();
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->get(route('items.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.items', 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 testItShouldExportSelectedItems()
{
2021-05-23 17:13:13 +03:00
$create_count = 5;
$select_count = 3;
$items = Item::factory()->count($create_count)->create();
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->post(
route('bulk-actions.action', ['group' => 'common', 'type' => 'items']),
2021-05-23 17:13:13 +03:00
['handle' => 'export', 'selected' => $items->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.items', 2), '-') . '-\d{10}\.xlsx/',
2021-05-23 17:13:13 +03:00
function (Export $export) use ($select_count) {
// Assert that the correct export is downloaded.
return $export->sheets()[0]->collection()->count() === $select_count;
}
);
}
public function testItShouldImportItems()
{
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->post(
route('items.import'),
[
'import' => UploadedFile::fake()->createWithContent(
'items.xlsx',
File::get(public_path('files/import/items.xlsx'))
),
]
)
->assertStatus(200);
2022-12-15 12:21:04 +03:00
Excel::assertImported('items.xlsx');
$this->assertFlashLevel('success');
}
2020-01-07 01:28:05 +03:00
public function getRequest()
{
2020-10-14 17:07:59 +03:00
return Item::factory()->enabled()->raw();
2020-01-07 01:28:05 +03:00
}
2019-04-02 18:51:06 +03:00
}