ifnex/04_Laravel/tests/Feature/Services/PriceCalculatorServiceTest.php
Kazem Alghasi 285acdae89 docs(project): update project status, roadmap, and documentation
Update project documentation to reflect the completion of Phase 0 and Phase 1, and the commencement of Phase 2. This includes archiving obsolete documents, updating the README with a new architectural overview, and refining the project checklist and status reports.

- Archive obsolete PRD and Roadmap documents
- Update `IFNEX_Phase0_Checklist.md` with completed tasks and Phase 2 roadmap
- Update `STATUS.md` with recent development progress for August 2026
- Refactor `04_Laravel/README.md` to include detailed technical specifications and architecture diagrams
- Update root `README.md` with updated versioning and system architecture visualization
- Refine `PriceCalculatorServiceTest.php` to align with updated service output structures
2026-08-07 05:58:12 +03:30

108 lines
3.6 KiB
PHP
Raw Permalink 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
namespace Tests\Feature\Services;
use App\Enums\ShipmentType;
use App\Models\Country;
use App\Models\ShippingRate;
use App\Models\SystemSetting;
use App\Services\PriceCalculatorService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class PriceCalculatorServiceTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function it_calculates_price_correctly_for_standard_package()
{
// 1. تنظیم SystemSetting ها برای تست (جلوگیری از مقادیر پیش‌فرض بزرگ)
SystemSetting::create(['key' => 'profit_margin', 'value' => 1.0]); // بدون سود
SystemSetting::create(['key' => 'aed_to_irr', 'value' => 1.0]); // بدون تبدیل ارز
SystemSetting::create(['key' => 'vat_rate', 'value' => 0.0]); // بدون مالیات
SystemSetting::create(['key' => 'packing_cost_default', 'value' => 0]); // بدون هزینه بسته‌بندی
// 2. ایجاد کشور
$country = Country::factory()->create([
'iso_code' => 'US',
'name' => 'United States',
'export_zone_parcel' => 1,
'export_zone_doc' => 1,
'import_zone_parcel' => 1,
'import_zone_doc' => 1,
'is_active' => true,
]);
// 3. ایجاد نرخ‌های حمل‌ونقل
ShippingRate::create([
'direction' => 'export',
'type' => 'DOC_NORMAL',
'weight' => 1.0,
'zone_1' => 20.00,
'zone_2' => 0,
'zone_3' => 0,
'zone_4' => 0,
'zone_5' => 0,
'zone_6' => 0,
'zone_7' => 0,
'zone_8' => 0,
'zone_9' => 0,
'zone_10' => 0,
]);
ShippingRate::create([
'direction' => 'export',
'type' => 'DOC_NORMAL',
'weight' => 3.0,
'zone_1' => 40.00,
'zone_2' => 0,
'zone_3' => 0,
'zone_4' => 0,
'zone_5' => 0,
'zone_6' => 0,
'zone_7' => 0,
'zone_8' => 0,
'zone_9' => 0,
'zone_10' => 0,
]);
// 4. اجرای سرویس
$service = app(PriceCalculatorService::class);
$data = [
'direction' => 'Outbound', // ← توجه: در سرویس به 'export' تبدیل می‌شود
'type' => 'DOC_NORMAL',
'country_iso' => 'US',
'weight' => 2.5,
'volumetric_weight' => 3.0,
'extra_service' => 10.00,
'packing_cost' => 0, // ← اضافه کردن هزینه بسته‌بندی صفر
'domestic_pickup' => 0,
'domestic_delivery' => 0,
'warehousing_cost' => 0,
];
$result = $service->calculate($data);
// 5. بررسی ساختار خروجی
$this->assertIsArray($result);
$this->assertArrayHasKey('total_fee', $result); // ✅ تغییر به total_fee
$this->assertArrayHasKey('base_price', $result);
// 6. بررسی مقادیر
$this->assertEquals(1, $result['zone']);
$this->assertEquals(3.0, $result['chargeable_weight']);
$this->assertEquals(40.00, $result['base_price']);
// محاسبه دستی:
// base_price = 40
// net_rial = 40 × 1.0 (profit) × 1.0 (currency) = 40
// extra = 10
// subtotal = 50
// vat = 0
// total_fee = 50
$this->assertEquals(50.00, $result['total_fee']);
}
}