akaunting/tests/Feature/Purchases/PaymentsTest.php

173 lines
4.7 KiB
PHP
Raw Normal View History

2018-10-09 14:18:12 +03:00
<?php
2019-12-31 15:49:09 +03:00
namespace Tests\Feature\Purchases;
2018-10-09 14:18:12 +03:00
use App\Exports\Purchases\Payments as Export;
2019-11-17 15:06:00 +03:00
use App\Jobs\Banking\CreateTransaction;
2019-12-16 13:19:09 +03:00
use App\Models\Banking\Transaction;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
2018-10-09 14:18:12 +03:00
use Tests\Feature\FeatureTestCase;
class PaymentsTest extends FeatureTestCase
{
public function testItShouldSeePaymentListPage()
{
$this->loginAs()
2019-11-16 10:21:14 +03:00
->get(route('payments.index'))
2018-10-09 14:18:12 +03:00
->assertStatus(200)
->assertSeeText(trans_choice('general.payments', 2));
}
2021-06-30 12:56:46 +03:00
public function testItShouldSeePaymentShowPage()
{
$request = $this->getRequest();
$payment = $this->dispatch(new CreateTransaction($request));
$this->loginAs()
->get(route('payments.show', $payment->id))
->assertStatus(200)
->assertSee($payment->contact->email);
}
2018-10-09 14:18:12 +03:00
public function testItShouldSeePaymentCreatePage()
{
$this->loginAs()
2019-11-16 10:21:14 +03:00
->get(route('payments.create'))
2018-10-09 14:18:12 +03:00
->assertStatus(200)
->assertSeeText(trans('general.title.new', ['type' => trans_choice('general.payments', 1)]));
}
public function testItShouldCreatePayment()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest();
2018-10-09 14:18:12 +03:00
$this->loginAs()
2020-07-25 17:22:50 +03:00
->post(route('payments.store'), $request)
2019-11-16 10:21:14 +03:00
->assertStatus(200);
2018-10-09 14:18:12 +03:00
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
$this->assertDatabaseHas('transactions', $request);
2018-10-09 14:18:12 +03:00
}
2020-01-13 10:55:19 +03:00
public function testItShouldSeePaymentUpdatePage()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest();
$payment = $this->dispatch(new CreateTransaction($request));
2020-01-13 10:55:19 +03:00
$this->loginAs()
->get(route('payments.edit', $payment->id))
->assertStatus(200)
->assertSee($payment->amount);
}
2018-10-09 14:18:12 +03:00
public function testItShouldUpdatePayment()
{
2020-01-07 01:28:05 +03:00
$request = $this->getRequest();
2018-10-09 14:18:12 +03:00
2019-11-17 15:06:00 +03:00
$payment = $this->dispatch(new CreateTransaction($request));
2018-10-09 14:18:12 +03:00
2020-01-13 17:20:28 +03:00
$request['amount'] = $this->faker->randomFloat(2, 1, 1000);
2018-10-09 14:18:12 +03:00
$this->loginAs()
2019-11-16 10:21:14 +03:00
->patch(route('payments.update', $payment->id), $request)
2020-01-13 17:20:28 +03:00
->assertStatus(200)
->assertSee($request['amount']);
2018-10-09 14:18:12 +03:00
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
$this->assertDatabaseHas('transactions', $request);
2018-10-09 14:18:12 +03:00
}
public function testItShouldDeletePayment()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest();
$payment = $this->dispatch(new CreateTransaction($request));
2018-10-09 14:18:12 +03:00
$this->loginAs()
2019-11-16 10:21:14 +03:00
->delete(route('payments.destroy', $payment->id))
->assertStatus(200);
2018-10-09 14:18:12 +03:00
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
$this->assertSoftDeleted('transactions', $request);
2018-10-09 14:18:12 +03:00
}
2020-01-07 01:28:05 +03:00
public function testItShouldExportPayments()
{
$count = 5;
Transaction::factory()->expense()->count($count)->create();
\Excel::fake();
$this->loginAs()
->get(route('payments.export'))
->assertStatus(200);
2021-05-23 17:13:13 +03:00
\Excel::matchByRegex();
\Excel::assertDownloaded(
2021-05-23 17:13:13 +03:00
'/' . \Str::filename(trans_choice('general.payments', 2)) . '-\d{10}\.xlsx/',
function (Export $export) use ($count) {
// Assert that the correct export is downloaded.
return $export->collection()->count() === $count;
}
);
}
public function testItShouldExportSelectedPayments()
{
2021-05-23 17:13:13 +03:00
$create_count = 5;
$select_count = 3;
$payments = Transaction::factory()->expense()->count($create_count)->create();
\Excel::fake();
$this->loginAs()
->post(
route('bulk-actions.action', ['group' => 'purchases', 'type' => 'payments']),
2021-05-23 17:13:13 +03:00
['handle' => 'export', 'selected' => $payments->take($select_count)->pluck('id')->toArray()]
)
->assertStatus(200);
2021-05-23 17:13:13 +03:00
\Excel::matchByRegex();
\Excel::assertDownloaded(
2021-05-23 17:13:13 +03:00
'/' . \Str::filename(trans_choice('general.payments', 2)) . '-\d{10}\.xlsx/',
function (Export $export) use ($select_count) {
return $export->collection()->count() === $select_count;
}
);
}
public function testItShouldImportPayments()
{
\Excel::fake();
$this->loginAs()
->post(
route('payments.import'),
[
'import' => UploadedFile::fake()->createWithContent(
'payments.xlsx',
File::get(public_path('files/import/payments.xlsx'))
),
]
)
->assertStatus(200);
\Excel::assertImported('payments.xlsx');
$this->assertFlashLevel('success');
}
2020-01-07 01:28:05 +03:00
public function getRequest()
{
2020-10-14 17:07:59 +03:00
return Transaction::factory()->expense()->raw();
2020-01-07 01:28:05 +03:00
}
2019-04-02 18:51:06 +03:00
}