akaunting/tests/Feature/Banking/TransfersTest.php

163 lines
4.7 KiB
PHP
Raw Normal View History

2018-11-07 11:42:25 +03:00
<?php
namespace Tests\Feature\Banking;
2020-10-01 18:20:32 +03:00
use App\Exports\Banking\Transfers as Export;
use App\Jobs\Banking\CreateTransfer;
2020-01-07 09:29:45 +03:00
use App\Models\Banking\Account;
2020-10-01 18:20:32 +03:00
use App\Models\Banking\Transfer;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
2018-11-07 11:42:25 +03:00
use Tests\Feature\FeatureTestCase;
class TransfersTest extends FeatureTestCase
{
public function testItShouldSeeTransferListPage()
{
$this->loginAs()
->get(route('transfers.index'))
->assertStatus(200)
->assertSeeText(trans_choice('general.transfers', 2));
}
public function testItShouldSeeTransferCreatePage()
{
$this->loginAs()
->get(route('transfers.create'))
->assertStatus(200)
->assertSeeText(trans('general.title.new', ['type' => trans_choice('general.transfers', 1)]));
}
public function testItShouldCreateTransfer()
{
$this->loginAs()
2020-01-06 16:37:32 +03:00
->post(route('transfers.store'), $this->getRequest())
2019-11-16 10:21:14 +03:00
->assertStatus(200);
2018-11-07 11:42:25 +03:00
$this->assertFlashLevel('success');
}
public function testItShouldSeeTransferUpdatePage()
{
2020-01-07 09:29:45 +03:00
$transfer = $this->dispatch(new CreateTransfer($this->getRequest()));
2018-11-07 11:42:25 +03:00
$this->loginAs()
2021-07-06 21:39:09 +03:00
->get(route('transfers.show', $transfer->id))
2018-11-07 11:42:25 +03:00
->assertStatus(200)
2019-11-18 10:54:26 +03:00
->assertSee($transfer->description);
2018-11-07 11:42:25 +03:00
}
2019-11-16 10:21:14 +03:00
public function testItShouldUpdateTransfer()
2018-11-07 11:42:25 +03:00
{
2020-01-06 16:37:32 +03:00
$request = $this->getRequest();
2018-11-07 11:42:25 +03:00
2020-01-07 09:29:45 +03:00
$transfer = $this->dispatch(new CreateTransfer($request));
2019-11-16 10:21:14 +03:00
2020-01-14 01:05:32 +03:00
$request['description'] = $this->faker->text(15);
2018-11-07 11:42:25 +03:00
$this->loginAs()
2020-01-13 10:55:19 +03:00
->patch(route('transfers.update', $transfer->id), $request)
2019-11-16 10:21:14 +03:00
->assertStatus(200);
2018-11-07 11:42:25 +03:00
$this->assertFlashLevel('success');
}
2019-11-16 10:21:14 +03:00
public function testItShouldDeleteTransfer()
2018-11-07 11:42:25 +03:00
{
2020-01-07 09:29:45 +03:00
$transfer = $this->dispatch(new CreateTransfer($this->getRequest()));
2018-11-07 11:42:25 +03:00
$this->loginAs()
2020-01-13 10:55:19 +03:00
->delete(route('transfers.destroy', $transfer->id))
2019-11-16 10:21:14 +03:00
->assertStatus(200);
2018-11-07 11:42:25 +03:00
$this->assertFlashLevel('success');
}
2020-10-01 18:20:32 +03:00
public function testItShouldExportTransfers()
{
$count = 5;
2020-10-14 17:07:59 +03:00
Transfer::factory()->count($count)->create();
2020-10-01 18:20:32 +03:00
\Excel::fake();
$this->loginAs()
2021-02-12 11:20:10 +03:00
->get(route('transfers.export'))
->assertStatus(200);
2020-10-01 18:20:32 +03:00
2021-05-23 17:13:13 +03:00
\Excel::matchByRegex();
2020-10-01 18:20:32 +03:00
\Excel::assertDownloaded(
2021-05-23 17:13:13 +03:00
'/' . \Str::filename(trans_choice('general.transfers', 2)) . '-\d{10}\.xlsx/',
2020-10-01 18:20:32 +03:00
function (Export $export) use ($count) {
// Assert that the correct export is downloaded.
return $export->collection()->count() === $count;
}
);
}
public function testItShouldExportSelectedTransfers()
{
2021-05-23 17:13:13 +03:00
$create_count = 5;
$select_count = 3;
$transfers = Transfer::factory()->count($create_count)->create();
2020-10-01 18:20:32 +03:00
\Excel::fake();
$this->loginAs()
2021-02-12 11:20:10 +03:00
->post(
route('bulk-actions.action', ['group' => 'banking', 'type' => 'transfers']),
2021-05-23 17:13:13 +03:00
['handle' => 'export', 'selected' => $transfers->take($select_count)->pluck('id')->toArray()]
2021-02-12 11:20:10 +03:00
)
2020-10-01 18:20:32 +03:00
->assertStatus(200);
2021-05-23 17:13:13 +03:00
\Excel::matchByRegex();
2020-10-01 18:20:32 +03:00
\Excel::assertDownloaded(
2021-05-23 17:13:13 +03:00
'/' . \Str::filename(trans_choice('general.transfers', 2)) . '-\d{10}\.xlsx/',
function (Export $export) use ($select_count) {
return $export->collection()->count() === $select_count;
2020-10-01 18:20:32 +03:00
}
);
}
public function testItShouldImportTransfers()
{
\Excel::fake();
$this->loginAs()
2021-02-12 11:20:10 +03:00
->post(
route('transfers.import'),
[
'import' => UploadedFile::fake()->createWithContent(
'transfers.xlsx',
File::get(public_path('files/import/transfers.xlsx'))
),
]
)
->assertStatus(200);
2020-10-01 18:20:32 +03:00
\Excel::assertImported('transfers.xlsx');
$this->assertFlashLevel('success');
}
2020-01-07 01:28:05 +03:00
public function getRequest()
2018-11-07 11:42:25 +03:00
{
2020-10-14 17:07:59 +03:00
$from_account = Account::factory()->enabled()->default_currency()->create();
2020-01-06 16:37:32 +03:00
2020-10-14 17:07:59 +03:00
$to_account = Account::factory()->enabled()->default_currency()->create();
2020-01-06 16:37:32 +03:00
2018-11-07 11:42:25 +03:00
return [
'company_id' => $this->company->id,
2020-01-07 09:29:45 +03:00
'from_account_id' => $from_account->id,
'to_account_id' => $to_account->id,
2020-01-14 01:05:32 +03:00
'amount' => $this->faker->randomFloat(2, 1, 1000),
2018-11-07 11:42:25 +03:00
'transferred_at' => $this->faker->date(),
2020-01-14 01:05:32 +03:00
'description'=> $this->faker->text(20),
2019-11-16 10:21:14 +03:00
'payment_method' => setting('default.payment_method'),
2020-01-14 01:05:32 +03:00
'reference' => $this->faker->text(20),
2018-11-07 11:42:25 +03:00
];
}
2020-01-07 09:45:40 +03:00
}