ifnex/04_Laravel/tests/Feature/Api/PricingControllerTest.php
Kazem Alghasi abae820070 refactor(api): improve pricing logic and add test coverage
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`
2026-08-07 04:39:14 +03:30

58 lines
1.8 KiB
PHP

<?php
namespace Tests\Feature\Api;
use App\Services\PriceCalculatorService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class PricingControllerTest extends TestCase
{
#[Test]
public function it_can_calculate_pricing_with_valid_data()
{
// Mock کردن سرویس محاسبه قیمت
$this->mock(PriceCalculatorService::class, function ($mock) {
$mock->shouldReceive('calculate')
->once()
->andReturn([
'total_cost' => 150.00,
'currency' => 'USD',
]);
});
// ارسال درخواست معتبر (اصلاح آدرس به /api/calculate)
$response = $this->postJson('/api/calculate', [
'direction' => 'Outbound',
'type' => 'DOC_NORMAL',
'country_iso' => 'US',
'weight' => 2.5,
'volumetric_weight' => 3.0,
'extra_service' => 10.00,
]);
// بررسی وضعیت پاسخ و ساختار داده‌ها
$response->assertStatus(200)
->assertJson([
'total_cost' => 150.00,
'currency' => 'USD',
]);
}
#[Test]
public function it_returns_validation_errors_for_invalid_data()
{
// ارسال درخواست نامعتبر (اصلاح آدرس به /api/calculate)
$response = $this->postJson('/api/calculate', [
'direction' => 'InvalidDirection',
'type' => 'PARCEL',
]);
// بررسی دریافت خطای اعتبارسنجی
$response->assertStatus(422)
->assertJsonValidationErrors(['country_iso', 'weight', 'volumetric_weight', 'direction']);
}
}