2024-09-06 21:59:58 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests;
|
|
|
|
|
|
|
|
|
|
use App\Models\Holding;
|
|
|
|
|
use App\Models\Portfolio;
|
|
|
|
|
use App\Models\Transaction;
|
2025-01-28 17:14:49 -06:00
|
|
|
use App\Models\User;
|
2024-09-06 21:59:58 -05:00
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
|
|
|
|
|
class DashboardTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
|
|
|
|
public function test_user_has_portfolios(): void
|
|
|
|
|
{
|
2024-09-09 19:39:38 -05:00
|
|
|
$this->actingAs($user = User::factory()->create());
|
2024-09-06 21:59:58 -05:00
|
|
|
|
|
|
|
|
Portfolio::factory(5)->create();
|
|
|
|
|
|
|
|
|
|
$this->assertCount(5, $user->portfolios);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_user_has_transactions(): void
|
|
|
|
|
{
|
2024-09-09 19:39:38 -05:00
|
|
|
$this->actingAs($user = User::factory()->create());
|
2024-09-06 21:59:58 -05:00
|
|
|
|
|
|
|
|
Transaction::factory(10)->create();
|
|
|
|
|
|
|
|
|
|
$this->assertCount(10, $user->transactions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_user_has_holdings(): void
|
|
|
|
|
{
|
2024-09-09 19:39:38 -05:00
|
|
|
$this->actingAs($user = User::factory()->create());
|
2024-09-06 21:59:58 -05:00
|
|
|
|
|
|
|
|
$portfolio = Portfolio::factory()->create();
|
|
|
|
|
|
|
|
|
|
Transaction::factory(5)->symbol('AAPL')->portfolio($portfolio->id)->create();
|
|
|
|
|
|
|
|
|
|
$this->assertCount(1, $user->holdings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_user_has_dashboard_metrics(): void
|
|
|
|
|
{
|
2024-09-09 19:39:38 -05:00
|
|
|
$this->actingAs($user = User::factory()->create());
|
2024-09-06 21:59:58 -05:00
|
|
|
|
|
|
|
|
$portfolio = Portfolio::factory()->create();
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2024-09-23 15:09:35 -05:00
|
|
|
Transaction::factory(5)->buy()->lastYear()->portfolio($portfolio->id)->symbol('AAPL')->create();
|
|
|
|
|
$transaction = Transaction::factory()->sell()->lastMonth()->portfolio($portfolio->id)->symbol('AAPL')->create();
|
2024-09-06 21:59:58 -05:00
|
|
|
|
|
|
|
|
$metrics = Holding::query()
|
2025-01-28 17:14:49 -06:00
|
|
|
->myHoldings()
|
|
|
|
|
->withPortfolioMetrics()
|
|
|
|
|
->first();
|
|
|
|
|
|
2024-09-06 21:59:58 -05:00
|
|
|
$this->assertEqualsWithDelta(
|
2024-09-23 15:09:35 -05:00
|
|
|
$transaction->sale_price - $transaction->cost_basis,
|
2024-09-06 21:59:58 -05:00
|
|
|
$metrics->realized_gain_dollars,
|
|
|
|
|
0.01
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|