212 lines
6.8 KiB
PHP
212 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Bucket;
|
|
use App\Models\Scenario;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class ProjectionPreviewTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private Scenario $scenario;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->scenario = Scenario::factory()->create();
|
|
}
|
|
|
|
public function test_preview_returns_correct_allocations(): void
|
|
{
|
|
Bucket::factory()->need()->fixedLimit(50000)->create([
|
|
'scenario_id' => $this->scenario->id,
|
|
'name' => 'Rent',
|
|
'priority' => 1,
|
|
'starting_amount' => 0,
|
|
]);
|
|
|
|
Bucket::factory()->want()->fixedLimit(30000)->create([
|
|
'scenario_id' => $this->scenario->id,
|
|
'name' => 'Fun',
|
|
'priority' => 2,
|
|
'starting_amount' => 0,
|
|
]);
|
|
|
|
// Send 70000 cents = $700
|
|
$response = $this->postJson(
|
|
"/scenarios/{$this->scenario->uuid}/projections/preview",
|
|
['amount' => 70000]
|
|
);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonCount(2, 'allocations');
|
|
$response->assertJsonPath('allocations.0.bucket_name', 'Rent');
|
|
$response->assertJsonPath('allocations.0.allocated_amount', 50000);
|
|
$response->assertJsonPath('allocations.1.bucket_name', 'Fun');
|
|
$response->assertJsonPath('allocations.1.allocated_amount', 20000);
|
|
$response->assertJsonPath('total_allocated', 70000);
|
|
$response->assertJsonPath('unallocated', 0);
|
|
}
|
|
|
|
public function test_preview_returns_remaining_capacity(): void
|
|
{
|
|
Bucket::factory()->need()->fixedLimit(50000)->create([
|
|
'scenario_id' => $this->scenario->id,
|
|
'name' => 'Savings',
|
|
'priority' => 1,
|
|
'starting_amount' => 0,
|
|
]);
|
|
|
|
// Send 30000 cents = $300
|
|
$response = $this->postJson(
|
|
"/scenarios/{$this->scenario->uuid}/projections/preview",
|
|
['amount' => 30000]
|
|
);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('allocations.0.allocated_amount', 30000);
|
|
$response->assertJsonPath('allocations.0.remaining_capacity', 20000);
|
|
}
|
|
|
|
public function test_preview_returns_uuids_not_integer_ids(): void
|
|
{
|
|
$bucket = Bucket::factory()->need()->fixedLimit(50000)->create([
|
|
'scenario_id' => $this->scenario->id,
|
|
'priority' => 1,
|
|
'starting_amount' => 0,
|
|
]);
|
|
|
|
$response = $this->postJson(
|
|
"/scenarios/{$this->scenario->uuid}/projections/preview",
|
|
['amount' => 10000]
|
|
);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('allocations.0.bucket_id', $bucket->uuid);
|
|
}
|
|
|
|
public function test_preview_includes_bucket_type(): void
|
|
{
|
|
Bucket::factory()->need()->fixedLimit(50000)->create([
|
|
'scenario_id' => $this->scenario->id,
|
|
'priority' => 1,
|
|
'starting_amount' => 0,
|
|
]);
|
|
|
|
$response = $this->postJson(
|
|
"/scenarios/{$this->scenario->uuid}/projections/preview",
|
|
['amount' => 10000]
|
|
);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('allocations.0.bucket_type', 'need');
|
|
}
|
|
|
|
public function test_preview_rejects_missing_amount(): void
|
|
{
|
|
$response = $this->postJson(
|
|
"/scenarios/{$this->scenario->uuid}/projections/preview",
|
|
[]
|
|
);
|
|
|
|
$response->assertUnprocessable();
|
|
$response->assertJsonValidationErrors('amount');
|
|
}
|
|
|
|
public function test_preview_rejects_zero_amount(): void
|
|
{
|
|
$response = $this->postJson(
|
|
"/scenarios/{$this->scenario->uuid}/projections/preview",
|
|
['amount' => 0]
|
|
);
|
|
|
|
$response->assertUnprocessable();
|
|
$response->assertJsonValidationErrors('amount');
|
|
}
|
|
|
|
public function test_preview_rejects_negative_amount(): void
|
|
{
|
|
$response = $this->postJson(
|
|
"/scenarios/{$this->scenario->uuid}/projections/preview",
|
|
['amount' => -50]
|
|
);
|
|
|
|
$response->assertUnprocessable();
|
|
$response->assertJsonValidationErrors('amount');
|
|
}
|
|
|
|
public function test_preview_with_no_buckets_returns_empty_allocations(): void
|
|
{
|
|
$response = $this->postJson(
|
|
"/scenarios/{$this->scenario->uuid}/projections/preview",
|
|
['amount' => 100000]
|
|
);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('allocations', []);
|
|
$response->assertJsonPath('total_allocated', 0);
|
|
$response->assertJsonPath('unallocated', 100000);
|
|
}
|
|
|
|
public function test_preview_respects_priority_ordering(): void
|
|
{
|
|
Bucket::factory()->want()->fixedLimit(20000)->create([
|
|
'scenario_id' => $this->scenario->id,
|
|
'name' => 'Low Priority',
|
|
'priority' => 3,
|
|
'starting_amount' => 0,
|
|
]);
|
|
|
|
Bucket::factory()->need()->fixedLimit(30000)->create([
|
|
'scenario_id' => $this->scenario->id,
|
|
'name' => 'High Priority',
|
|
'priority' => 1,
|
|
'starting_amount' => 0,
|
|
]);
|
|
|
|
$response = $this->postJson(
|
|
"/scenarios/{$this->scenario->uuid}/projections/preview",
|
|
['amount' => 40000]
|
|
);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('allocations.0.bucket_name', 'High Priority');
|
|
$response->assertJsonPath('allocations.0.allocated_amount', 30000);
|
|
$response->assertJsonPath('allocations.1.bucket_name', 'Low Priority');
|
|
$response->assertJsonPath('allocations.1.allocated_amount', 10000);
|
|
}
|
|
|
|
public function test_preview_with_overflow_bucket_captures_remainder(): void
|
|
{
|
|
Bucket::factory()->need()->fixedLimit(20000)->create([
|
|
'scenario_id' => $this->scenario->id,
|
|
'name' => 'Rent',
|
|
'priority' => 1,
|
|
'starting_amount' => 0,
|
|
]);
|
|
|
|
Bucket::factory()->overflow()->create([
|
|
'scenario_id' => $this->scenario->id,
|
|
'name' => 'Overflow',
|
|
'priority' => 2,
|
|
'starting_amount' => 0,
|
|
]);
|
|
|
|
$response = $this->postJson(
|
|
"/scenarios/{$this->scenario->uuid}/projections/preview",
|
|
['amount' => 50000]
|
|
);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('allocations.0.bucket_name', 'Rent');
|
|
$response->assertJsonPath('allocations.0.allocated_amount', 20000);
|
|
$response->assertJsonPath('allocations.1.bucket_name', 'Overflow');
|
|
$response->assertJsonPath('allocations.1.allocated_amount', 30000);
|
|
$response->assertJsonPath('allocations.1.remaining_capacity', null);
|
|
$response->assertJsonPath('total_allocated', 50000);
|
|
$response->assertJsonPath('unallocated', 0);
|
|
}
|
|
}
|