D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
ghanempharmacy.com
/
tests
/
Feature
/
Filename :
ShippingCostTest.php
back
Copy
<?php namespace Tests\Feature; use App\Models\DeliveryArea; use App\Models\DeliveryCity; use App\Models\DeliveryCountryCode; use App\Models\DeliveryState; use App\Models\ShippingAddress; use App\Utils\CartManager; use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class ShippingCostTest extends TestCase { use RefreshDatabase; public function test_hierarchical_shipping_cost_calculation() { // 1. Setup Hierarchy $country = DeliveryCountryCode::create(['country_code' => 'JO', 'shipping_cost' => 10]); $state = DeliveryState::create(['country_id' => $country->id, 'name' => 'Amman', 'cost' => 8, 'status' => 1]); $city = DeliveryCity::create(['state_id' => $state->id, 'name' => 'Amman City', 'cost' => 5, 'status' => 1]); $area = DeliveryArea::create(['city_id' => $city->id, 'name' => 'Tla Al-Ali', 'cost' => 3, 'status' => 1]); // 2. Test Area Level $address = ShippingAddress::create([ 'customer_id' => 1, 'country_id' => $country->id, 'state_id' => $state->id, 'city_id' => $city->id, 'area_id' => $area->id, 'address' => 'Test Address', 'contact_person_name' => 'John Doe', 'phone' => '12345678', 'address_type' => 'Home', ]); $this->assertEquals(3, CartManager::get_hierarchical_shipping_cost($address->id)); // 3. Test City Level Fallback (Disable Area status) $area->update(['status' => 0]); $this->assertEquals(5, CartManager::get_hierarchical_shipping_cost($address->id)); // 4. Test State Level Fallback (Disable City status) $city->update(['status' => 0]); $this->assertEquals(8, CartManager::get_hierarchical_shipping_cost($address->id)); // 5. Test Country Level Fallback (Disable State status) $state->update(['status' => 0]); $this->assertEquals(10, CartManager::get_hierarchical_shipping_cost($address->id)); // 6. Test Free Shipping (Cost = 0) $area->update(['status' => 1, 'cost' => 0]); $this->assertEquals(0, CartManager::get_hierarchical_shipping_cost($address->id)); // 7. Test Not Found $addressMiss = ShippingAddress::create([ 'customer_id' => 1, 'address' => 'Missing ID Address', 'contact_person_name' => 'Jane Doe', 'phone' => '87654321', 'address_type' => 'Work', ]); $this->assertNull(CartManager::get_hierarchical_shipping_cost($addressMiss->id)); } }