2018-07-14 13:26:29 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Common;
|
|
|
|
|
2021-02-12 11:54:34 +03:00
|
|
|
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;
|
2021-02-12 11:54:34 +03:00
|
|
|
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
|
|
|
|
2021-02-12 11:54:34 +03:00
|
|
|
public function testItShouldExportItems()
|
|
|
|
{
|
|
|
|
$count = 5;
|
|
|
|
Item::factory()->count($count)->create();
|
|
|
|
|
2022-12-15 12:21:04 +03:00
|
|
|
Excel::fake();
|
2021-02-12 11:54:34 +03:00
|
|
|
|
|
|
|
$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(
|
2022-12-16 10:36:57 +03:00
|
|
|
'/' . str()->filename(trans_choice('general.items', 2)) . '-\d{10}\.xlsx/',
|
2021-02-12 11:54:34 +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 11:54:34 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldExportSelectedItems()
|
|
|
|
{
|
2021-05-23 17:13:13 +03:00
|
|
|
$create_count = 5;
|
|
|
|
$select_count = 3;
|
|
|
|
|
|
|
|
$items = Item::factory()->count($create_count)->create();
|
2021-02-12 11:54:34 +03:00
|
|
|
|
2022-12-15 12:21:04 +03:00
|
|
|
Excel::fake();
|
2021-02-12 11:54:34 +03:00
|
|
|
|
|
|
|
$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()]
|
2021-02-12 11:54:34 +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.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;
|
2021-02-12 11:54:34 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldImportItems()
|
|
|
|
{
|
2022-12-15 12:21:04 +03:00
|
|
|
Excel::fake();
|
2021-02-12 11:54:34 +03:00
|
|
|
|
|
|
|
$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');
|
2021-02-12 11:54:34 +03:00
|
|
|
|
|
|
|
$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
|
|
|
}
|