Files
investbrain/tests/DividendsTest.php
T

65 lines
1.9 KiB
PHP
Raw Normal View History

2024-09-09 17:56:57 -05:00
<?php
namespace Tests;
2024-09-12 19:34:20 -05:00
use App\Models\Dividend;
2024-09-09 17:56:57 -05:00
use Tests\TestCase;
2024-09-12 19:34:20 -05:00
use App\Models\User;
use App\Models\Split;
use App\Models\Holding;
use App\Models\Portfolio;
use App\Models\Transaction;
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());
$portfolio = Portfolio::factory()->create();
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolio->id)->symbol('ACME')->create();
$holding = Holding::query()->portfolio($portfolio->id)->symbol('ACME')->first();
$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());
$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();
$this->assertEquals(0, $holding->dividends_earned);
Dividend::refreshDividendData('ACME');
$transactions = Transaction::where(['reinvested_dividend' => true])->symbol('ACME')->portfolio($portfolio->id)->get();
$dividendsReinvested = $transactions->reduce(function ($carry, $transaction) {
return $carry + ($transaction->cost_basis * $transaction->quantity);
}, 0);
$this->assertCount(3, $transactions);
$this->assertEqualsWithDelta(4.95, $dividendsReinvested, 0.01);
}
2024-09-09 17:56:57 -05:00
}