2024-09-09 17:56:57 -05:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-28 17:33:54 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2024-09-09 17:56:57 -05:00
|
|
|
namespace Tests;
|
|
|
|
|
|
2024-09-09 19:39:38 -05:00
|
|
|
use App\Models\Holding;
|
|
|
|
|
use App\Models\Portfolio;
|
2025-01-28 17:14:49 -06:00
|
|
|
use App\Models\Split;
|
2024-09-09 19:39:38 -05:00
|
|
|
use App\Models\Transaction;
|
2025-01-28 17:14:49 -06:00
|
|
|
use App\Models\User;
|
2024-09-09 17:56:57 -05:00
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
|
|
|
|
|
class SplitsTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
2024-09-09 19:39:38 -05:00
|
|
|
public function test_splits_create_new_transaction(): void
|
2024-09-09 17:56:57 -05:00
|
|
|
{
|
2024-09-09 19:39:38 -05:00
|
|
|
$this->actingAs($user = User::factory()->create());
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2024-09-09 19:39:38 -05:00
|
|
|
$portfolio = Portfolio::factory()->create();
|
|
|
|
|
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolio->id)->symbol('ACME')->create();
|
|
|
|
|
|
|
|
|
|
// manually reset the split last sync date (which is set when the holding is created)
|
|
|
|
|
Holding::query()->portfolio($portfolio->id)->symbol('ACME')->update([
|
2025-01-28 17:14:49 -06:00
|
|
|
'splits_synced_at' => null,
|
2024-09-09 19:39:38 -05:00
|
|
|
]);
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2024-09-09 19:39:38 -05:00
|
|
|
Split::refreshSplitData('ACME');
|
|
|
|
|
|
|
|
|
|
$transactions = Transaction::query()->symbol('ACME')->portfolio($portfolio->id)->get();
|
|
|
|
|
|
|
|
|
|
$this->assertCount(2, $transactions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_splits_do_not_create_new_transaction_if_already_synced(): void
|
|
|
|
|
{
|
|
|
|
|
$this->actingAs($user = User::factory()->create());
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2024-09-09 19:39:38 -05:00
|
|
|
$portfolio = Portfolio::factory()->create();
|
|
|
|
|
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolio->id)->symbol('ACME')->create();
|
|
|
|
|
|
|
|
|
|
Split::refreshSplitData('ACME');
|
|
|
|
|
|
|
|
|
|
$transactions = Transaction::query()->symbol('ACME')->portfolio($portfolio->id)->get();
|
|
|
|
|
|
|
|
|
|
$this->assertCount(1, $transactions);
|
2024-09-09 17:56:57 -05:00
|
|
|
}
|
|
|
|
|
}
|