Refactor the pricing calculation service and shipping rate import logic to improve consistency and reliability. This includes updating direction naming conventions, enhancing model casting, and adding comprehensive test suites. - Update `ShippingRatesImport` to use 'outbound' and 'inbound' instead of 'export' and 'import' - Refactor `PriceCalculatorService` to use a more modular calculation structure - Update `ShippingRate` model to use explicit property casting for zones - Add `HasFactory` trait to `Country` and `ShippingRate` models - Add new database factories for `Country` and `ShippingRate` - Implement new feature tests for API endpoints and service logic - Add new service tests for `PriceCalculatorService`
69 lines
2.4 KiB
PHP
69 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Services;
|
|
|
|
use App\Models\Country;
|
|
use App\Models\ShippingRate;
|
|
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()
|
|
{
|
|
// ایجاد رکورد کشور مورد نیاز برای تست در دیتابیس
|
|
$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,
|
|
]);
|
|
|
|
// ایجاد نرخهای حملونقل مورد نیاز برای تست
|
|
ShippingRate::factory()->create([
|
|
'direction' => 'export', // اصلاح شد: تطبیق با مقادیر مجاز دیتابیس
|
|
'type' => 'DOC_NORMAL',
|
|
'weight' => 1.0,
|
|
'zone_1' => 20.00,
|
|
]);
|
|
|
|
ShippingRate::factory()->create([
|
|
'direction' => 'export', // اصلاح شد: تطبیق با مقادیر مجاز دیتابیس
|
|
'type' => 'DOC_NORMAL',
|
|
'weight' => 3.0,
|
|
'zone_1' => 40.00,
|
|
]);
|
|
|
|
$service = app(PriceCalculatorService::class);
|
|
|
|
$data = [
|
|
'direction' => 'export',
|
|
'type' => 'DOC_NORMAL', // اصلاح شد: تطبیق با مقادیر مجاز ShipmentType Enum
|
|
'country_iso' => 'US',
|
|
'weight' => 2.5,
|
|
'volumetric_weight' => 3.0,
|
|
'extra_service' => 10.00,
|
|
];
|
|
|
|
$result = $service->calculate($data);
|
|
|
|
$this->assertIsArray($result);
|
|
$this->assertArrayHasKey('total_cost', $result);
|
|
$this->assertArrayHasKey('currency', $result);
|
|
|
|
// بررسی مقدار محاسبه شده
|
|
// وزن حجمی (3.0) بیشتر از وزن واقعی (2.5) است، بنابراین باید از وزن حجمی استفاده شود
|
|
// نزدیکترین نرخ برای وزن 3.0، نرخ 40.00 است
|
|
// هزینه کل = نرخ حمل (40.00) + خدمات اضافی (10.00) = 50.00
|
|
$this->assertEquals(50.00, $result['total_cost']);
|
|
}
|
|
}
|