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-10-19 10:42:28 -05:00
|
|
|
use App\Models\Dividend;
|
2025-01-28 17:14:49 -06:00
|
|
|
use App\Models\Holding;
|
2024-10-19 10:42:28 -05:00
|
|
|
use App\Models\MarketData;
|
2025-01-28 17:14:49 -06:00
|
|
|
use App\Models\Portfolio;
|
2024-09-12 19:34:20 -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 DividendsTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
2024-09-12 19:34:20 -05:00
|
|
|
public function test_new_dividends_update_holding(): void
|
2024-09-09 17:56:57 -05:00
|
|
|
{
|
2024-09-12 19:34:20 -05:00
|
|
|
$this->actingAs($user = User::factory()->create());
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2024-09-12 19:34:20 -05:00
|
|
|
$portfolio = Portfolio::factory()->create();
|
|
|
|
|
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolio->id)->symbol('ACME')->create();
|
|
|
|
|
|
|
|
|
|
$holding = Holding::query()->portfolio($portfolio->id)->symbol('ACME')->first();
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2024-09-12 19:34:20 -05:00
|
|
|
$this->assertEquals(0, $holding->dividends_earned);
|
|
|
|
|
|
|
|
|
|
Dividend::refreshDividendData('ACME');
|
|
|
|
|
|
|
|
|
|
$holding->refresh();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(4.95, $holding->dividends_earned);
|
2024-09-09 17:56:57 -05:00
|
|
|
}
|
2024-10-18 20:46:22 -05:00
|
|
|
|
|
|
|
|
public function test_new_dividends_are_reinvested(): void
|
|
|
|
|
{
|
|
|
|
|
$this->actingAs($user = User::factory()->create());
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2024-10-18 20:46:22 -05:00
|
|
|
$portfolio = Portfolio::factory()->create();
|
|
|
|
|
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolio->id)->symbol('ACME')->create();
|
|
|
|
|
|
|
|
|
|
$holding = Holding::query()->portfolio($portfolio->id)->symbol('ACME')->first();
|
|
|
|
|
$holding->reinvest_dividends = true;
|
|
|
|
|
$holding->save();
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2024-10-18 20:46:22 -05:00
|
|
|
$this->assertEquals(0, $holding->dividends_earned);
|
|
|
|
|
|
|
|
|
|
Dividend::refreshDividendData('ACME');
|
|
|
|
|
|
|
|
|
|
$transactions = Transaction::where(['reinvested_dividend' => true])->symbol('ACME')->portfolio($portfolio->id)->get();
|
|
|
|
|
|
2024-10-19 10:42:28 -05:00
|
|
|
$market_data = MarketData::symbol('ACME')->first();
|
2025-01-28 17:14:49 -06:00
|
|
|
$dividendsReinvested = $transactions->sum('quantity');
|
2024-10-18 20:46:22 -05:00
|
|
|
|
|
|
|
|
$this->assertCount(3, $transactions);
|
2024-10-19 10:42:28 -05:00
|
|
|
$this->assertEqualsWithDelta(4.95, $dividendsReinvested * $market_data->market_value, 0.01);
|
2024-10-18 20:46:22 -05:00
|
|
|
}
|
2024-11-14 01:25:03 -06:00
|
|
|
|
2025-03-19 16:16:38 -05:00
|
|
|
public function test_cannot_insert_duplicate_dividends(): void
|
2024-11-14 01:25:03 -06:00
|
|
|
{
|
2025-03-19 16:16:38 -05:00
|
|
|
// first insert
|
|
|
|
|
Dividend::refreshDividendData('ACME');
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2025-03-19 16:16:38 -05:00
|
|
|
// try to duplicate
|
2024-11-14 01:25:03 -06:00
|
|
|
Dividend::refreshDividendData('ACME');
|
|
|
|
|
|
2025-03-19 16:16:38 -05:00
|
|
|
$dividend_count = Dividend::count();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(3, $dividend_count);
|
2024-11-14 01:25:03 -06:00
|
|
|
}
|
2025-08-22 16:37:33 -05:00
|
|
|
|
|
|
|
|
public function test_dividend_earnings_are_not_shared_between_portfolios(): void
|
|
|
|
|
{
|
|
|
|
|
$this->actingAs($user = User::factory()->create());
|
|
|
|
|
|
|
|
|
|
$portfolioOne = Portfolio::factory()->create();
|
|
|
|
|
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolioOne->id)->symbol('ACME')->create();
|
|
|
|
|
|
|
|
|
|
$portfolioTwo = Portfolio::factory()->create();
|
|
|
|
|
Transaction::factory(2)->buy()->sixMonthsAgo()->portfolio($portfolioTwo->id)->symbol('ACME')->create();
|
|
|
|
|
|
|
|
|
|
Dividend::refreshDividendData('ACME');
|
|
|
|
|
|
|
|
|
|
$holdingOne = Holding::query()->portfolio($portfolioOne->id)->symbol('ACME')->first();
|
|
|
|
|
$holdingTwo = Holding::query()->portfolio($portfolioTwo->id)->symbol('ACME')->first();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(4.95, $holdingOne->dividends_earned);
|
|
|
|
|
$this->assertEquals(8, $holdingTwo->dividends_earned);
|
|
|
|
|
}
|
2024-09-09 17:56:57 -05:00
|
|
|
}
|