Merge pull request #2828 from EnesSacid-Buker/tests

Transfer test refactoring
This commit is contained in:
Cüneyt Şentürk 2022-12-16 14:07:31 +03:00 committed by GitHub
commit 4cb869d451
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 65 deletions

View File

@ -4,15 +4,10 @@ namespace Database\Factories;
use App\Abstracts\Factory; use App\Abstracts\Factory;
use App\Models\Banking\Account; use App\Models\Banking\Account;
use App\Models\Banking\Transaction;
use App\Models\Banking\Transfer as Model; use App\Models\Banking\Transfer as Model;
use App\Models\Setting\Category;
use App\Traits\Categories;
class Transfer extends Factory class Transfer extends Factory
{ {
use Categories;
/** /**
* The name of the factory's corresponding model. * The name of the factory's corresponding model.
* *
@ -27,42 +22,20 @@ class Transfer extends Factory
*/ */
public function definition() public function definition()
{ {
$accounts = Account::enabled()->get(); $from_account = Account::factory()->enabled()->default_currency()->create();
if ($accounts->count() >= 2) { $to_account = Account::factory()->enabled()->default_currency()->create();
$random = $accounts->random(2);
$expense_account = $random->first();
$income_account = $random->last();
} else {
$expense_account = $accounts->first();
$income_account = Account::factory()->enabled()->default_currency()->create();
}
$request = [
'amount' => $this->faker->randomFloat(2, 1, 1000),
'paid_at' => $this->faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d'),
'category_id' => $this->getTransferCategoryId(),
'description' => $this->faker->text(20),
'reference' => $this->faker->text(20),
'created_from' => 'core::factory',
];
$expense_transaction = Transaction::factory()->create(array_merge($request, [
'type' => Transaction::EXPENSE_TRANSFER_TYPE,
'account_id' => $expense_account->id,
]));
$income_transaction = Transaction::factory()->create(array_merge($request, [
'type' => Transaction::INCOME_TRANSFER_TYPE,
'account_id' => $income_account->id,
]));
return [ return [
'company_id' => $this->company->id, 'company_id' => $this->company->id,
'expense_transaction_id' => $expense_transaction->id, 'from_account_id' => $from_account->id,
'income_transaction_id' => $income_transaction->id, 'to_account_id' => $to_account->id,
'amount' => $this->faker->randomFloat(2, 1, 1000),
'transferred_at' => $this->faker->date(),
'description'=> $this->faker->text(20),
'payment_method' => setting('default.payment_method'),
'reference' => $this->faker->text(20),
'created_from' => 'core::factory',
]; ];
} }
} }

View File

@ -4,10 +4,10 @@ namespace Tests\Feature\Banking;
use App\Exports\Banking\Transfers as Export; use App\Exports\Banking\Transfers as Export;
use App\Jobs\Banking\CreateTransfer; use App\Jobs\Banking\CreateTransfer;
use App\Models\Banking\Account;
use App\Models\Banking\Transfer; use App\Models\Banking\Transfer;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Maatwebsite\Excel\Facades\Excel;
use Tests\Feature\FeatureTestCase; use Tests\Feature\FeatureTestCase;
class TransfersTest extends FeatureTestCase class TransfersTest extends FeatureTestCase
@ -76,18 +76,20 @@ class TransfersTest extends FeatureTestCase
public function testItShouldExportTransfers() public function testItShouldExportTransfers()
{ {
$count = 5; $count = 5;
Transfer::factory()->count($count)->create(); foreach (Transfer::factory()->count($count)->raw() as $request) {
$this->dispatch(new CreateTransfer($request));
}
\Excel::fake(); Excel::fake();
$this->loginAs() $this->loginAs()
->get(route('transfers.export')) ->get(route('transfers.export'))
->assertStatus(200); ->assertStatus(200);
\Excel::matchByRegex(); Excel::matchByRegex();
\Excel::assertDownloaded( Excel::assertDownloaded(
'/' . \Str::filename(trans_choice('general.transfers', 2)) . '-\d{10}\.xlsx/', '/' . str()->filename(trans_choice('general.transfers', 2)) . '-\d{10}\.xlsx/',
function (Export $export) use ($count) { function (Export $export) use ($count) {
// Assert that the correct export is downloaded. // Assert that the correct export is downloaded.
return $export->collection()->count() === $count; return $export->collection()->count() === $count;
@ -100,21 +102,23 @@ class TransfersTest extends FeatureTestCase
$create_count = 5; $create_count = 5;
$select_count = 3; $select_count = 3;
$transfers = Transfer::factory()->count($create_count)->create(); foreach (Transfer::factory()->count($create_count)->raw() as $request) {
$responses[] = $this->dispatch(new CreateTransfer($request));
}
\Excel::fake(); Excel::fake();
$this->loginAs() $this->loginAs()
->post( ->post(
route('bulk-actions.action', ['group' => 'banking', 'type' => 'transfers']), route('bulk-actions.action', ['group' => 'banking', 'type' => 'transfers']),
['handle' => 'export', 'selected' => $transfers->take($select_count)->pluck('id')->toArray()] ['handle' => 'export', 'selected' => collect($responses)->take($select_count)->pluck('id')->toArray()]
) )
->assertStatus(200); ->assertStatus(200);
\Excel::matchByRegex(); Excel::matchByRegex();
\Excel::assertDownloaded( Excel::assertDownloaded(
'/' . \Str::filename(trans_choice('general.transfers', 2)) . '-\d{10}\.xlsx/', '/' . str()->filename(trans_choice('general.transfers', 2)) . '-\d{10}\.xlsx/',
function (Export $export) use ($select_count) { function (Export $export) use ($select_count) {
return $export->collection()->count() === $select_count; return $export->collection()->count() === $select_count;
} }
@ -123,7 +127,7 @@ class TransfersTest extends FeatureTestCase
public function testItShouldImportTransfers() public function testItShouldImportTransfers()
{ {
\Excel::fake(); Excel::fake();
$this->loginAs() $this->loginAs()
->post( ->post(
@ -137,26 +141,13 @@ class TransfersTest extends FeatureTestCase
) )
->assertStatus(200); ->assertStatus(200);
\Excel::assertImported('transfers.xlsx'); Excel::assertImported('transfers.xlsx');
$this->assertFlashLevel('success'); $this->assertFlashLevel('success');
} }
public function getRequest() public function getRequest()
{ {
$from_account = Account::factory()->enabled()->default_currency()->create(); return Transfer::factory()->raw();
$to_account = Account::factory()->enabled()->default_currency()->create();
return [
'company_id' => $this->company->id,
'from_account_id' => $from_account->id,
'to_account_id' => $to_account->id,
'amount' => $this->faker->randomFloat(2, 1, 1000),
'transferred_at' => $this->faker->date(),
'description'=> $this->faker->text(20),
'payment_method' => setting('default.payment_method'),
'reference' => $this->faker->text(20),
];
} }
} }