Files
portal/app/tests/Feature/Billing/PricingTierRepositoryTest.php
T
Дмитрий 1e0c0ab90a fix(billing): Plan 4 Task 2 code-review fixes (2 Important + 1 Minor)
- PricingTierResolver::resolveForCount — InvalidArgumentException на
  $leadOrdinal < 1 (closes I-1: defensive contract validation).
- PricingTierRepository::activeAt — explicit @var Collection<int,
  PricingTier> annotation для type narrowing (closes I-2; firstOrFail
  отвергнут — Stan ругается на Eloquent\Model return-type).
- PricingTierResolverTest — +1 unit test (8/8 PASS): throws на 0/-1.
- PricingTierRepositoryTest — +1 integration test (5/5 PASS): excludes
  inactive tiers (closes M-2 coverage gap).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 09:17:13 +03:00

61 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
use App\Models\PricingTier;
use App\Repositories\PricingTierRepository;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Carbon;
uses(DatabaseTransactions::class);
beforeEach(function () {
$this->repo = new PricingTierRepository;
// Clean baseline — удаляем seed-7-tiers, чтобы тесты могли создать свои контексты
// (PricingTierSeeder seed'ит 7 ступеней с effective_from='1970-01-01' через DatabaseSeeder).
PricingTier::query()->delete();
});
it('returns active tiers ordered by tier_no', function () {
PricingTier::factory()->create(['tier_no' => 2, 'effective_from' => '2024-01-01', 'is_active' => true]);
PricingTier::factory()->create(['tier_no' => 1, 'effective_from' => '2024-01-01', 'is_active' => true]);
$tiers = $this->repo->activeAt(Carbon::parse('2024-06-01'));
expect($tiers->pluck('tier_no')->all())->toBe([1, 2]);
});
it('returns max effective_from <= today (newer overrides older)', function () {
PricingTier::factory()->create(['tier_no' => 1, 'effective_from' => '2024-01-01', 'price_per_lead_kopecks' => 50000]);
PricingTier::factory()->create(['tier_no' => 1, 'effective_from' => '2024-06-01', 'price_per_lead_kopecks' => 30000]);
$tiers = $this->repo->activeAt(Carbon::parse('2024-07-01'));
expect($tiers->first()->price_per_lead_kopecks)->toBe(30000);
});
it('ignores future effective_from', function () {
PricingTier::factory()->create(['tier_no' => 1, 'effective_from' => '2024-01-01', 'price_per_lead_kopecks' => 50000]);
PricingTier::factory()->create(['tier_no' => 1, 'effective_from' => '2099-01-01', 'price_per_lead_kopecks' => 99999]);
$tiers = $this->repo->activeAt(Carbon::parse('2024-06-01'));
expect($tiers->first()->price_per_lead_kopecks)->toBe(50000);
});
it('returns empty collection when no active tiers exist', function () {
$tiers = $this->repo->activeAt(Carbon::parse('2024-06-01'));
expect($tiers)->toBeEmpty();
});
it('excludes inactive tiers', function () {
PricingTier::factory()->create(['tier_no' => 1, 'effective_from' => '2024-01-01', 'is_active' => false]);
PricingTier::factory()->create(['tier_no' => 2, 'effective_from' => '2024-01-01', 'is_active' => true]);
$tiers = $this->repo->activeAt(Carbon::parse('2024-06-01'));
expect($tiers->pluck('tier_no')->all())->toBe([2]);
});