Files
portal/app/tests/Feature/Models/PricingTierTest.php
T
Дмитрий 71e38ee0a9 feat(models): add PricingTier model with kopecks→rubles accessor + current() snapshot
- app/Models/PricingTier.php — fillable + casts (date, boolean, integer)
  + accessor priceRubles (kopecks → float rubles)
  + scopeActive (is_active=true AND effective_from <= today)
  + static current() — keyed by tier_no Collection<int, PricingTier>
- database/factories/PricingTierFactory.php — реалистичные ступени (300/700/1000/.../null)
- tests/Feature/Models/PricingTierTest.php — 4 теста: factory, accessor,
  scopeActive, current() snapshot всех 7 ступеней

ide-helper:models -W -M -N перегенерил docblocks (WebhookDedupKey synced
после schema v8.16).

Pest: 455 / 453 passed / 2 skipped (449 + 4 новых = 453).
Larastan: 0 errors. Pint auto-fix.

Spec: §7.2
Plan: Task 7

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 16:48:00 +03:00

60 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\PricingTier;
use Illuminate\Foundation\Testing\DatabaseTransactions;
uses(DatabaseTransactions::class);
test('PricingTier can be created via factory', function () {
$t = PricingTier::factory()->create();
expect($t->tier_no)->toBeInt()->toBeBetween(1, 7);
expect($t->price_per_lead_kopecks)->toBeInt()->toBeGreaterThan(0);
});
test('priceRubles accessor converts kopecks to rubles', function () {
$t = PricingTier::factory()->create([
'tier_no' => 1,
'price_per_lead_kopecks' => 6000,
]);
expect($t->price_rubles)->toBe(60.0);
});
test('scopeActive returns only is_active=true with effective_from <= today', function () {
PricingTier::factory()->create([
'is_active' => true,
'effective_from' => now()->subDay(),
'tier_no' => 1,
]);
PricingTier::factory()->create([
'is_active' => false,
'effective_from' => now()->subDay(),
'tier_no' => 2,
]);
PricingTier::factory()->create([
'is_active' => true,
'effective_from' => now()->addDay(),
'tier_no' => 3,
]);
expect(PricingTier::active()->count())->toBe(1);
});
test('current() returns today active tier set keyed by tier_no', function () {
foreach (range(1, 7) as $n) {
PricingTier::factory()->create([
'tier_no' => $n,
'is_active' => true,
'effective_from' => now()->subDay(),
'leads_in_tier' => $n === 7 ? null : 100,
'price_per_lead_kopecks' => 7000 - ($n * 500),
]);
}
$current = PricingTier::current();
expect($current)->toHaveCount(7);
expect($current[1]->price_per_lead_kopecks)->toBe(6500);
expect($current[7]->leads_in_tier)->toBeNull();
});