2024-10-28 19:20:52 -05:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-28 17:33:54 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2024-10-28 19:20:52 -05:00
|
|
|
namespace Tests;
|
|
|
|
|
|
|
|
|
|
use App\Exports\BackupExport;
|
|
|
|
|
use App\Models\BackupImport as BackupImportModel;
|
2025-04-10 20:47:35 -05:00
|
|
|
use App\Models\Holding;
|
|
|
|
|
use App\Models\Portfolio;
|
2025-01-28 17:14:49 -06:00
|
|
|
use App\Models\Transaction;
|
|
|
|
|
use App\Models\User;
|
2024-10-28 19:20:52 -05:00
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
2025-01-28 17:14:49 -06:00
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
2024-10-28 19:20:52 -05:00
|
|
|
|
|
|
|
|
class ImportExportTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
public function test_can_create_exports(): void
|
|
|
|
|
{
|
|
|
|
|
Excel::fake();
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
$this->actingAs($user = User::factory()->create());
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
Transaction::factory(5)->buy()->lastYear()->symbol('AAPL')->create();
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
Excel::download(new BackupExport, now()->format('Y_m_d').'_investbrain_backup.xlsx');
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
Excel::assertDownloaded(now()->format('Y_m_d').'_investbrain_backup.xlsx', function (BackupExport $export) {
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
public function test_import_job_completes(): void
|
|
|
|
|
{
|
|
|
|
|
$this->actingAs($user = User::factory()->create());
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
$import_job = BackupImportModel::create([
|
|
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
|
'path' => __DIR__.'/0000_00_00_import_test.xlsx',
|
|
|
|
|
]);
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
$import_job->refresh();
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
$this->assertEquals('success', $import_job->status);
|
|
|
|
|
}
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
public function test_import_job_inserts_rows(): void
|
|
|
|
|
{
|
|
|
|
|
$this->actingAs($user = User::factory()->create());
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
BackupImportModel::create([
|
|
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
|
'path' => __DIR__.'/0000_00_00_import_test.xlsx',
|
|
|
|
|
]);
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
$this->assertEquals(3, $user->transactions->count());
|
|
|
|
|
}
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
public function test_import_job_calculates_correct_holding_data(): void
|
|
|
|
|
{
|
|
|
|
|
$this->actingAs($user = User::factory()->create());
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
BackupImportModel::create([
|
|
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
|
'path' => __DIR__.'/0000_00_00_import_test.xlsx',
|
|
|
|
|
]);
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
$holding = $user->holdings->first();
|
2024-10-28 19:20:52 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
$this->assertEquals('AAPL', $holding->symbol);
|
|
|
|
|
$this->assertEquals(6, $holding->quantity);
|
|
|
|
|
$this->assertEqualsWithDelta(233.33, $holding->average_cost_basis, 0.01);
|
|
|
|
|
}
|
2025-04-09 19:25:15 -05:00
|
|
|
|
2025-04-10 20:47:35 -05:00
|
|
|
public function test_configurations_are_set_on_import(): void
|
|
|
|
|
{
|
|
|
|
|
$this->actingAs($user = User::factory()->create());
|
|
|
|
|
|
|
|
|
|
Portfolio::create([
|
|
|
|
|
'id' => '9e792bb8-94e7-4ed3-b8cc-43b50d34c337',
|
|
|
|
|
'title' => 'Test Portfolio',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$holding = Holding::create([
|
|
|
|
|
'id' => '9cf8a662-7347-49fb-b9de-0cc1430a8d1f',
|
|
|
|
|
'portfolio_id' => '9e792bb8-94e7-4ed3-b8cc-43b50d34c337',
|
|
|
|
|
'symbol' => 'ACME',
|
|
|
|
|
'quantity' => 0,
|
|
|
|
|
'reinvest_dividends' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(false, $holding->reinvest_dividends);
|
|
|
|
|
|
|
|
|
|
BackupImportModel::create([
|
|
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
|
'path' => __DIR__.'/0000_00_00_import_configs_test.xlsx',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$holding->refresh();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(true, $holding->reinvest_dividends);
|
|
|
|
|
}
|
2024-10-28 19:20:52 -05:00
|
|
|
}
|