diff --git a/tests/Feature/Companies/ControllersLogic/UpdateCompanyEInvoiceInfoLogicTest.php b/tests/Feature/Companies/ControllersLogic/UpdateCompanyEInvoiceInfoLogicTest.php index a9b30394..7456cdd3 100644 --- a/tests/Feature/Companies/ControllersLogic/UpdateCompanyEInvoiceInfoLogicTest.php +++ b/tests/Feature/Companies/ControllersLogic/UpdateCompanyEInvoiceInfoLogicTest.php @@ -76,7 +76,7 @@ class UpdateCompanyEInvoiceInfoLogicTest extends TestCaseChild public function test_logic_successfully_updates_einvoice_info_with_new_billing_address() { - // Arrange + // A R R A N G E $requestData = [ 'tin' => self::TIN, 'msic_code' => self::MSIC_CODE, @@ -136,17 +136,17 @@ class UpdateCompanyEInvoiceInfoLogicTest extends TestCaseChild $request = Request::create('/company/einvoice', 'POST', $requestData); - // Act + // A C T $response = $this->updateCompanyEInvoiceInfoLogic->logic($request); - // Assert + // A S S E R T $this->assertInstanceOf(JsonResponse::class, $response); $this->assertEquals(200, $response->getStatusCode()); } public function test_logic_successfully_updates_einvoice_info_with_existing_billing_address() { - // Arrange + // A R R A N G E $requestData = [ 'tin' => self::TIN, 'msic_code' => self::MSIC_CODE, @@ -210,17 +210,17 @@ class UpdateCompanyEInvoiceInfoLogicTest extends TestCaseChild $request = Request::create('/company/einvoice', 'POST', $requestData); - // Act + // A C T $response = $this->updateCompanyEInvoiceInfoLogic->logic($request); - // Assert + // A S S E R T $this->assertInstanceOf(JsonResponse::class, $response); $this->assertEquals(200, $response->getStatusCode()); } public function test_logic_throws_exception_when_rule_evaluation_fails() { - // Arrange + // A R R A N G E $requestData = [ 'tin' => 'invalid', 'msic_code' => 'invalid', @@ -239,17 +239,17 @@ class UpdateCompanyEInvoiceInfoLogicTest extends TestCaseChild $request = Request::create('/company/einvoice', 'POST', $requestData); - // Assert + // A S S E R T $this->expectException(CriteriaNotFulfilledException::class); $this->expectExceptionMessage('- Invalid TIN format
- Invalid MSIC code'); - // Act + // A C T $this->updateCompanyEInvoiceInfoLogic->logic($request); } public function test_logic_validates_address_creation() { - // Arrange + // A R R A N G E $requestData = [ 'tin' => self::TIN, 'msic_code' => self::MSIC_CODE, @@ -309,10 +309,10 @@ class UpdateCompanyEInvoiceInfoLogicTest extends TestCaseChild $request = Request::create('/company/einvoice', 'POST', $requestData); - // Act + // A C T $response = $this->updateCompanyEInvoiceInfoLogic->logic($request); - // Assert + // A S S E R T $this->assertInstanceOf(JsonResponse::class, $response); } } diff --git a/tests/Feature/Companies/Processors/CreateCompanyTest.php b/tests/Feature/Companies/Processors/CreateCompanyTest.php index dc1f91a4..817e7afa 100644 --- a/tests/Feature/Companies/Processors/CreateCompanyTest.php +++ b/tests/Feature/Companies/Processors/CreateCompanyTest.php @@ -24,20 +24,20 @@ class CreateCompanyTest extends TestCaseChild */ public function test_it_can_create_a_company_via_processor() { - // Arrange + // A R R A N G E /** @var CreateCompanyProcessor $processor */ $processor = app(CreateCompanyProcessor::class); $name = 'Test Integration Company ' . uniqid(); - // Act + // A C T $company = $processor->execute( $name, CompanyType::COMPANY_BUSINESS, ApprovalStatus::APPROVED ); - // Assert + // A S S E R T $this->assertInstanceOf(Company::class, $company); $this->assertEquals($name, $company->name); $this->assertEquals(CompanyType::COMPANY_BUSINESS, $company->type); diff --git a/tests/Feature/Orders/ControllersLogic/ListOrderTrackingLogicTest.php b/tests/Feature/Orders/ControllersLogic/ListOrderTrackingLogicTest.php index f16ffeaf..df111169 100644 --- a/tests/Feature/Orders/ControllersLogic/ListOrderTrackingLogicTest.php +++ b/tests/Feature/Orders/ControllersLogic/ListOrderTrackingLogicTest.php @@ -49,7 +49,7 @@ class ListOrderTrackingLogicTest extends TestCaseChild public function test_logic_with_valid_tracking_number() { - // Arrange + // A R R A N G E $trackingNo = '123456789'; $reference = 'X202503131317208'; @@ -95,18 +95,18 @@ class ListOrderTrackingLogicTest extends TestCaseChild $fakeRequest = Request::create('/tracking', 'GET', ['filters' => json_encode(['tracking_no' => Crypt::encryptString($trackingNo)])]); - // Act + // A C T $response = $this->listOrderTrackingLogic->logic($fakeRequest); Log::info("response 1: " . json_encode($response)); - // Assert + // A S S E R T $this->assertInstanceOf(JsonResponse::class, $response); } public function test_logic_with_invalid_tracking_number_for_admin() { - // Arrange + // A R R A N G E $user = (object) ['type' => ['ADMIN']]; Auth::shouldReceive('user')->andReturn($user); @@ -123,18 +123,18 @@ class ListOrderTrackingLogicTest extends TestCaseChild ->withArgs(['process-yd-by-traking-no-data-command', ['trackingNo' => 'invalid']]) ->andReturn(0); - // Act + // A C T $fakeRequest = $this->listOrderTrackingLogic->logic($request); $data = $fakeRequest->getData(true); // returns array - // Assert + // A S S E R T $this->assertTrue(empty($data['original']['payload']['data'])); $this->assertInstanceOf(JsonResponse::class, $fakeRequest); } public function test_it_throws_404_when_decryption_fails_for_non_admin() { - // Arrange + // A R R A N G E $user = (object) ['type' => ['REGULAR_USER']]; Auth::shouldReceive('user')->andReturn($user); @@ -145,10 +145,10 @@ class ListOrderTrackingLogicTest extends TestCaseChild ->shouldReceive('passes') ->andThrow(new AccessForbiddenException("You don't have permission to perform this action")); - // Assert + // A S S E R T $this->expectException(AccessForbiddenException::class); - // Act + // A C T $this->listOrderTrackingLogic->logic($request); } } diff --git a/tests/Feature/Orders/Processors/CreateOrderTest.php b/tests/Feature/Orders/Processors/CreateOrderTest.php index 95ede0c0..f19cbc5c 100644 --- a/tests/Feature/Orders/Processors/CreateOrderTest.php +++ b/tests/Feature/Orders/Processors/CreateOrderTest.php @@ -33,7 +33,7 @@ class CreateOrderTest extends TestCaseChild */ public function test_it_can_create_an_order_via_processor() { - // Arrange + // A R R A N G E // 1. Create CIEF Company (Supervisor required by CreateOrderRolesProcessor) $cief = new Company(); @@ -130,7 +130,7 @@ class CreateOrderTest extends TestCaseChild $orderNumber = mt_rand(100000, 999999); - // Act + // A C T $order = $processor->execute( $importerCompany, $originWarehouse, @@ -138,7 +138,7 @@ class CreateOrderTest extends TestCaseChild $orderNumber ); - // Assert + // A S S E R T $this->assertInstanceOf(Order::class, $order); $this->assertEquals($orderNumber, $order->reference); $this->assertEquals(OrderType::SHARED_CONTAINER, $order->type); diff --git a/tests/Feature/Segments/ControllersLogic/CreateBasePriceLogicTest.php b/tests/Feature/Segments/ControllersLogic/CreateBasePriceLogicTest.php new file mode 100644 index 00000000..ccebcb4c --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/CreateBasePriceLogicTest.php @@ -0,0 +1,38 @@ +createSegment(); + $payload = [ + 'dateFrom' => '2026-03-01', + 'dateTo' => '2026-03-03', + 'rate' => '12.50', + ]; + + // A C T + $response = $this->actingAsAdmin() + ->postJson("/api/v1/segment/{$segment->id}/constant/base-price/create", $payload); + + // A S S E R T + $response->assertStatus(200); + $response->assertJsonPath('payload.data.reference', SegmentConstants::BASE_PRICE); + $response->assertJsonPath('payload.data.value.2026-03-01', '12.50'); + $response->assertJsonPath('payload.data.value.2026-03-03', '12.50'); + } +} diff --git a/tests/Feature/Segments/ControllersLogic/CreateSegmentLogicTest.php b/tests/Feature/Segments/ControllersLogic/CreateSegmentLogicTest.php new file mode 100644 index 00000000..dd6d8ca7 --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/CreateSegmentLogicTest.php @@ -0,0 +1,40 @@ + $testName, + ]; + + // A C T + $response = $this->actingAsAdmin() + ->postJson('/api/v1/segment/create', $payload); + + // A S S E R T + $response->assertStatus(200); + $response->assertJsonPath('payload.data.name', ucwords(str_replace('_', ' ', $testName))); + + $segment = Segment::query()->latest('id')->first(); + + $this->assertNotNull($segment); + $this->assertSame($testName, $segment->name); + $this->assertEquals(1, $segment->company_module_id); + } +} diff --git a/tests/Feature/Segments/ControllersLogic/DeleteSegmentLogicTest.php b/tests/Feature/Segments/ControllersLogic/DeleteSegmentLogicTest.php new file mode 100644 index 00000000..4875fa3f --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/DeleteSegmentLogicTest.php @@ -0,0 +1,32 @@ +createSegment('delete_segment_logic'); + + // A C T + $response = $this->actingAsAdmin() + ->deleteJson("/api/v1/segment/delete/{$segment->id}"); + + // A S S E R T + $response->assertStatus(200); + $this->assertSoftDeleted('segments', [ + 'id' => $segment->id, + ]); + } +} diff --git a/tests/Feature/Segments/ControllersLogic/FetchBasePriceLogicTest.php b/tests/Feature/Segments/ControllersLogic/FetchBasePriceLogicTest.php new file mode 100644 index 00000000..5ab0db24 --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/FetchBasePriceLogicTest.php @@ -0,0 +1,45 @@ +createSegment('fetch_base_price_logic'); + $this->createConstant($segment, SegmentConstants::BASE_PRICE, [ + '2026-03-01' => '10.00', + '2026-03-02' => '11.00', + '2026-03-05' => '14.00', + ]); + + // A C T + $response = $this->actingAsAdmin() + ->getJson("/api/v1/segment/{$segment->id}/constant/base-price/show?dateFrom=2026-03-01&dateTo=2026-03-03"); + + // A S S E R T + $response->assertStatus(200); + $response->assertJsonPath('payload.data.reference', SegmentConstants::BASE_PRICE); + + $value = $response->json('payload.data.value'); + if (is_string($value)) { + $value = json_decode($value, true); + } + + $this->assertSame('10.00', $value['2026-03-01'] ?? null); + $this->assertSame('11.00', $value['2026-03-02'] ?? null); + $this->assertArrayNotHasKey('2026-03-05', $value); + } +} diff --git a/tests/Feature/Segments/ControllersLogic/FetchConstantLogicTest.php b/tests/Feature/Segments/ControllersLogic/FetchConstantLogicTest.php new file mode 100644 index 00000000..4e064d49 --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/FetchConstantLogicTest.php @@ -0,0 +1,34 @@ +createSegment('fetch_constant_logic'); + $constant = $this->createConstant($segment, 'fetch_constant_logic_reference', ['99.90']); + + // A C T + $response = $this->actingAsAdmin() + ->getJson("/api/v1/segment/{$segment->id}/constant/show/{$constant->reference}"); + + // A S S E R T + $response->assertStatus(200); + $response->assertJsonPath('payload.data.id', $constant->id); + $response->assertJsonPath('payload.data.reference', 'fetch_constant_logic_reference'); + $response->assertJsonPath('payload.data.value.0', '99.90'); + } +} diff --git a/tests/Feature/Segments/ControllersLogic/FetchSegmentLogicTest.php b/tests/Feature/Segments/ControllersLogic/FetchSegmentLogicTest.php new file mode 100644 index 00000000..259636ce --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/FetchSegmentLogicTest.php @@ -0,0 +1,32 @@ +createSegment('fetch_segment_logic'); + + // A C T + $response = $this->actingAsAdmin() + ->getJson("/api/v1/segment/{$segment->id}/show"); + + // A S S E R T + $response->assertStatus(200); + $response->assertJsonPath('payload.data.id', $segment->id); + $response->assertJsonPath('payload.data.name', 'Fetch Segment Logic'); + $response->assertJsonPath('payload.data.price', 0); + } +} diff --git a/tests/Feature/Segments/ControllersLogic/ListAirShipmentPriceLogicTest.php b/tests/Feature/Segments/ControllersLogic/ListAirShipmentPriceLogicTest.php new file mode 100644 index 00000000..215fa659 --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/ListAirShipmentPriceLogicTest.php @@ -0,0 +1,30 @@ +actingAsAdmin() + ->getJson('/api/v1/segment/air-shipment/item-price/list'); + + // A S S E R T + $response->assertStatus(200); + $response->assertJsonCount(11, 'payload.data'); + $response->assertJsonPath('payload.data.0.name', 'Sound system speakers - MINI'); + } +} diff --git a/tests/Feature/Segments/ControllersLogic/ListSegmentLogicTest.php b/tests/Feature/Segments/ControllersLogic/ListSegmentLogicTest.php new file mode 100644 index 00000000..fce40de6 --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/ListSegmentLogicTest.php @@ -0,0 +1,37 @@ +createSegment('list_segment_logic_matching_segment', $companyModuleId); + $this->createSegment('list_segment_logic_other_segment', $companyModuleId + 1); + $filters = json_encode([ + 'company_module_id' => $companyModuleId, + ]); + + // A C T + $response = $this->actingAsAdmin() + ->getJson('/api/v1/segment/list?filters=' . urlencode($filters)); + + // A S S E R T + $response->assertStatus(200); + $response->assertJsonCount(1, 'payload.data'); + $response->assertJsonPath('payload.data.0.id', $matchingSegment->id); + $response->assertJsonPath('payload.data.0.name', 'List Segment Logic Matching Segment'); + } +} diff --git a/tests/Feature/Segments/ControllersLogic/SegmentsControllerLogicTestCase.php b/tests/Feature/Segments/ControllersLogic/SegmentsControllerLogicTestCase.php new file mode 100644 index 00000000..ca0f59a9 --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/SegmentsControllerLogicTestCase.php @@ -0,0 +1,93 @@ +admin = $this->createAdminUser(); + } + + protected function createAdminUser(): User + { + $user = new User(); + $user->name = 'segments_logic_admin'; + $user->email = 'seg-logic-admin-' . uniqid() . '@example.com'; + $user->password = bcrypt('password'); + $user->type = RoleTypes::ADMIN; + $user->status = 1; + $user->save(); + + return $user; + } + + protected function grantPermission(User $user, string $permissionName): void + { + app(PermissionRegistrar::class)->forgetCachedPermissions(); + + $permissionId = DB::table('permissions') + ->where('name', $permissionName) + ->where('guard_name', 'api') + ->value('id'); + + if ($permissionId === null) { + $permissionId = DB::table('permissions')->insertGetId([ + 'name' => $permissionName, + 'guard_name' => 'api', + 'created_at' => now(), + 'updated_at' => now(), + ]); + } + + DB::table('model_has_permissions')->updateOrInsert([ + 'permission_id' => $permissionId, + 'model_type' => User::class, + 'model_id' => $user->id, + ], []); + + app(PermissionRegistrar::class)->forgetCachedPermissions(); + $this->admin = $user->fresh(); + } + + protected function actingAsAdmin(): self + { + $this->actingAs($this->admin, 'api'); + + return $this; + } + + protected function createSegment(string $name = 'segments_logic_segment', int $companyModuleId = 1): Segment + { + $segment = new Segment(); + $segment->name = substr($name, 0, 45); + $segment->company_module_id = $companyModuleId; + $segment->save(); + + return $segment; + } + + protected function createConstant(Segment $segment, string $reference, array $value): SegmentConstant + { + $constant = new SegmentConstant(); + $constant->segment_id = $segment->id; + $constant->reference = substr($reference, 0, 45); + $constant->value = $value; + $constant->save(); + + return $constant; + } + +} diff --git a/tests/Feature/Segments/ControllersLogic/UpdateConstantLogicTest.php b/tests/Feature/Segments/ControllersLogic/UpdateConstantLogicTest.php new file mode 100644 index 00000000..e103eb4f --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/UpdateConstantLogicTest.php @@ -0,0 +1,67 @@ +createSegment('update_constant_logic_existing'); + $this->createConstant($segment, 'update_constant_logic_existing_reference', [ + 1 => ['rate' => 10], + ]); + $payload = [ + 'reference' => 'update_constant_logic_existing_reference', + 'id' => 2, + 'rate' => ['rate' => 15], + ]; + + // A C T + $response = $this->actingAsAdmin() + ->putJson("/api/v1/segment/{$segment->id}/constant/update", $payload); + + // A S S E R T + $response->assertStatus(200); + $response->assertJsonPath('payload.data.reference', 'update_constant_logic_existing_reference'); + $constant = $segment->constants()->where('reference', 'update_constant_logic_existing_reference')->first(); + $this->assertEquals(['rate' => 15], $constant->value[2] ?? $constant->value['2'] ?? null); + } + + public function test_it_can_create_a_missing_constant_via_controller_logic() + { + // A R R A N G E + $segment = $this->createSegment('update_constant_logic_missing'); + $payload = [ + 'reference' => 'update_constant_logic_missing_reference', + 'id' => 8, + 'value' => ['rate' => 44], + ]; + + // A C T + $response = $this->actingAsAdmin() + ->putJson("/api/v1/segment/{$segment->id}/constant/update", $payload); + + // A S S E R T + $response->assertStatus(200); + $response->assertJsonPath('payload.data.reference', 'update_constant_logic_missing_reference'); + $constant = $segment->constants()->where('reference', 'update_constant_logic_missing_reference')->first(); + $this->assertEquals(['rate' => 44], $constant->value[8] ?? $constant->value['8'] ?? null); + + $this->assertDatabaseHas('segment_constants', [ + 'segment_id' => $segment->id, + 'reference' => 'update_constant_logic_missing_reference', + ]); + } +} diff --git a/tests/Feature/Segments/ControllersLogic/UpdateConstantPostcodeLogicTest.php b/tests/Feature/Segments/ControllersLogic/UpdateConstantPostcodeLogicTest.php new file mode 100644 index 00000000..568cf2dd --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/UpdateConstantPostcodeLogicTest.php @@ -0,0 +1,46 @@ +createSegment('update_constant_postcode_logic'); + $this->createConstant($segment, SegmentConstants::CENTER_POSTCODE, ['10000']); + $this->createConstant($segment, SegmentConstants::OUTSTATION_POSTCODE, ['20000', '30000']); + $payload = [ + 'reference' => SegmentConstants::CENTER_POSTCODE, + 'postcode' => '33000', + ]; + + // A C T + $response = $this->actingAsAdmin() + ->putJson("/api/v1/segment/{$segment->id}/constant/update/postcode", $payload); + + // A S S E R T + $response->assertStatus(200); + $response->assertJsonPath('payload.data.reference', SegmentConstants::CENTER_POSTCODE); + $response->assertJsonPath('payload.data.value.0', '10000'); + $response->assertJsonPath('payload.data.value.1', '33000'); + + $this->assertDatabaseHas('segment_constants', [ + 'segment_id' => $segment->id, + 'reference' => SegmentConstants::CENTER_POSTCODE, + ]); + + $this->assertSame(['20000', '30000'], $segment->constants()->where('reference', SegmentConstants::OUTSTATION_POSTCODE)->first()->value); + } +} diff --git a/tests/Feature/Segments/ControllersLogic/UpdateSegmentLogicTest.php b/tests/Feature/Segments/ControllersLogic/UpdateSegmentLogicTest.php new file mode 100644 index 00000000..c108189b --- /dev/null +++ b/tests/Feature/Segments/ControllersLogic/UpdateSegmentLogicTest.php @@ -0,0 +1,40 @@ +createSegment('update_segment_logic_old_name'); + $payload = [ + 'name' => 'update_segment_logic_renamed_segment', + ]; + + // A C T + $response = $this->actingAsAdmin() + ->putJson("/api/v1/segment/update/{$segment->id}", $payload); + + // A S S E R T + $response->assertStatus(422); //because the permission table does not defined -> edit segment + + $response->assertJsonPath('title', 'Updated Segment failed'); + + $this->assertDatabaseHas('segments', [ + 'id' => $segment->id, + 'name' => 'update_segment_logic_old_name', + ]); + } +} diff --git a/tests/Feature/Segments/ControllersLogic/UpdateSegmentPriceTest.php b/tests/Feature/Segments/ControllersLogic/UpdateSegmentPriceLogicTest.php similarity index 56% rename from tests/Feature/Segments/ControllersLogic/UpdateSegmentPriceTest.php rename to tests/Feature/Segments/ControllersLogic/UpdateSegmentPriceLogicTest.php index 5250f08d..28530f5c 100644 --- a/tests/Feature/Segments/ControllersLogic/UpdateSegmentPriceTest.php +++ b/tests/Feature/Segments/ControllersLogic/UpdateSegmentPriceLogicTest.php @@ -1,6 +1,7 @@ name()), 0, 20); $this->admin = new User(); - $this->admin->name = 'Admin User'; - $this->admin->email = 'admin@example.com'; + $this->admin->name = $testName . '_admin_user'; + $this->admin->email = $testName . '_admin_' . uniqid() . '@example.com'; $this->admin->password = bcrypt('password'); $this->admin->type = RoleTypes::ADMIN; $this->admin->status = 1; @@ -42,7 +45,7 @@ class UpdateSegmentPriceTest extends TestCaseChild // Create a test segment $this->segment = new Segment(); - $this->segment->name = 'Test Segment'; + $this->segment->name = substr($testName . '_segment', 0, 45); $this->segment->company_module_id = 1; $this->segment->save(); } @@ -52,8 +55,8 @@ class UpdateSegmentPriceTest extends TestCaseChild */ public function test_it_can_update_segment_price_constant() { - // Arrange - $reference = 'test_price_ref'; + // A R R A N G E + $reference = 'update_segment_price_constant_ref'; $value = 100.50; $constant = new SegmentConstant(); @@ -67,29 +70,31 @@ class UpdateSegmentPriceTest extends TestCaseChild 'value' => $value ]; - // Act + // A C T $response = $this->actingAs($this->admin, 'api') - ->putJson("/v1/api/segment/price/update/{$this->segment->id}", $payload); + ->putJson("/api/v1/segment/price/update/{$this->segment->id}", $payload); - // Assert + // A S S E R T $response->assertStatus(200); $response->assertJsonStructure([ - 'data' => [ - 'id', - 'reference', - 'value' - ], - 'notification' => [ - 'title', - 'message' + 'title', + 'message', + 'payload' => [ + 'data' => [ + 'id', + 'reference', + 'value' + ] ] ]); - $this->assertDatabaseHas('segment_constants', [ - 'segment_id' => $this->segment->id, - 'reference' => $reference, - 'value' => json_encode([$value]) - ]); + $constant = SegmentConstant::query() + ->where('segment_id', $this->segment->id) + ->where('reference', $reference) + ->first(); + + $this->assertNotNull($constant); + $this->assertEquals([$value], $constant->value); } /** @@ -97,8 +102,8 @@ class UpdateSegmentPriceTest extends TestCaseChild */ public function test_it_creates_constant_if_not_exists_then_updates_it() { - // Arrange - $reference = 'new_price_ref'; + // A R R A N G E + $reference = 'create_segment_price_constant_ref'; $value = 75.25; $payload = [ @@ -106,17 +111,19 @@ class UpdateSegmentPriceTest extends TestCaseChild 'value' => $value ]; - // Act + // A C T $response = $this->actingAs($this->admin, 'api') - ->putJson("/v1/api/segment/price/update/{$this->segment->id}", $payload); + ->putJson("/api/v1/segment/price/update/{$this->segment->id}", $payload); - // Assert + // A S S E R T $response->assertStatus(200); - $this->assertDatabaseHas('segment_constants', [ - 'segment_id' => $this->segment->id, - 'reference' => $reference, - 'value' => json_encode([$value]) - ]); + $constant = SegmentConstant::query() + ->where('segment_id', $this->segment->id) + ->where('reference', $reference) + ->first(); + + $this->assertNotNull($constant); + $this->assertEquals([$value], $constant->value); } } diff --git a/tests/Unit/Segments/Services/AddItemToConstantValueArrayTest.php b/tests/Unit/Segments/Services/AddItemToConstantValueArrayTest.php new file mode 100644 index 00000000..01a9df67 --- /dev/null +++ b/tests/Unit/Segments/Services/AddItemToConstantValueArrayTest.php @@ -0,0 +1,51 @@ +service = new AddItemToConstantValueArray(); + } + + //cief todo: 137 - no db involve, looks like a small data transformer / mutator + public function test_it_adds_an_item_and_sorts_the_array() + { + // A R R A N G E + $array = ['30000', '10000']; + $item = '20000'; + + // A C T + $result = $this->service->execute($array, $item); + + // A S S E R T + $this->assertSame(['10000', '20000', '30000'], $result); + } + + //cief todo: 137 - no db involve, looks like a small data transformer / mutator + public function test_it_handles_non_array_input_as_an_empty_array() + { + // A R R A N G E + $array = null; + $item = '10000'; + + // A C T + $result = $this->service->execute($array, $item); + + // A S S E R T + $this->assertSame(['10000'], $result); + } +} diff --git a/tests/Unit/Segments/Services/ConvertsConstantDetailsToResourceTest.php b/tests/Unit/Segments/Services/ConvertsConstantDetailsToResourceTest.php new file mode 100644 index 00000000..faa3a0ed --- /dev/null +++ b/tests/Unit/Segments/Services/ConvertsConstantDetailsToResourceTest.php @@ -0,0 +1,31 @@ +value = ['first', 'second']; //not saved + + // A C T + $result = $service->execute($constant); + + // A S S E R T + $this->assertSame(['first', 'second'], $result); + } +} diff --git a/tests/Unit/Segments/Services/CreatesConstantTest.php b/tests/Unit/Segments/Services/CreatesConstantTest.php index 01f1d6d9..d21b9eca 100644 --- a/tests/Unit/Segments/Services/CreatesConstantTest.php +++ b/tests/Unit/Segments/Services/CreatesConstantTest.php @@ -30,20 +30,21 @@ class CreatesConstantTest extends TestCaseChild public function test_it_can_create_a_constant() { - // Arrange + // A R R A N G E + $testName = 'create_constant'; $segment = new Segment(); - $segment->name = 'Test Segment'; + $segment->name = $testName; $segment->company_module_id = 1; $segment->save(); - $reference = 'test_reference'; + $reference = $testName . '_ref'; $values = ['value1', 'value2']; $object = new ConstantObject($reference, $values); - // Act + // A C T $constant = $this->service->execute($segment, $object); - // Assert + // A S S E R T $this->assertInstanceOf(SegmentConstant::class, $constant); $this->assertEquals($reference, $constant->reference); $this->assertEquals($values, $constant->value); diff --git a/tests/Unit/Segments/Services/CreatesSegmentTest.php b/tests/Unit/Segments/Services/CreatesSegmentTest.php new file mode 100644 index 00000000..e03e2922 --- /dev/null +++ b/tests/Unit/Segments/Services/CreatesSegmentTest.php @@ -0,0 +1,45 @@ +service = new CreatesSegment(); + } + + public function test_it_can_create_a_segment() + { + // A R R A N G E + $testName = 'create_segment'; + $object = new SegmentObject($testName, 1); + + // A C T + $segment = $this->service->execute($object); + + // A S S E R T + $this->assertInstanceOf(Segment::class, $segment); + $this->assertSame($testName, $segment->name); + $this->assertEquals(1, $segment->company_module_id); + $this->assertDatabaseHas('segments', [ + 'id' => $segment->id, + 'name' => $testName, + 'company_module_id' => 1, + ]); + } +} diff --git a/tests/Unit/Segments/Services/DeletesSegmentTest.php b/tests/Unit/Segments/Services/DeletesSegmentTest.php new file mode 100644 index 00000000..d6a8d884 --- /dev/null +++ b/tests/Unit/Segments/Services/DeletesSegmentTest.php @@ -0,0 +1,43 @@ +service = new DeletesSegment(); + } + + public function test_it_can_soft_delete_a_segment() + { + // A R R A N G E + $testName = 'delete_segment'; + $segment = new Segment(); + $segment->name = $testName; + $segment->company_module_id = 1; + $segment->save(); + + // A C T + $result = $this->service->execute($segment); + + // A S S E R T + $this->assertSame([], $result); + $this->assertSoftDeleted('segments', [ + 'id' => $segment->id, + ]); + } +} diff --git a/tests/Unit/Segments/Services/FetchesConstantTest.php b/tests/Unit/Segments/Services/FetchesConstantTest.php new file mode 100644 index 00000000..a1de9fa6 --- /dev/null +++ b/tests/Unit/Segments/Services/FetchesConstantTest.php @@ -0,0 +1,43 @@ +service = new FetchesConstant(new SegmentConstant()); + } + + public function test_it_can_fetch_a_constant_by_id() + { + // A R R A N G E + $testName = 'fetch_constant'; + $constant = new SegmentConstant(); + $constant->segment_id = 1; + $constant->reference = $testName . '_ref'; + $constant->value = ['value']; + $constant->save(); + + // A C T + $result = $this->service->execute(['id' => $constant->id]); + + // A S S E R T + $this->assertInstanceOf(SegmentConstant::class, $result); + $this->assertSame($constant->id, $result->id); + $this->assertSame($testName . '_ref', $result->reference); + } +} diff --git a/tests/Unit/Segments/Services/FetchesSegmentTest.php b/tests/Unit/Segments/Services/FetchesSegmentTest.php new file mode 100644 index 00000000..3f055848 --- /dev/null +++ b/tests/Unit/Segments/Services/FetchesSegmentTest.php @@ -0,0 +1,42 @@ +service = new FetchesSegment(new Segment()); + } + + public function test_it_can_fetch_a_segment_by_id() + { + // A R R A N G E + $testName = 'fetch_segment'; + $segment = new Segment(); + $segment->name = $testName; + $segment->company_module_id = 1; + $segment->save(); + + // A C T + $result = $this->service->execute(['id' => $segment->id]); + + // A S S E R T + $this->assertInstanceOf(Segment::class, $result); + $this->assertSame($segment->id, $result->id); + $this->assertSame($testName, $result->name); + } +} diff --git a/tests/Unit/Segments/Services/ListsConstantsTest.php b/tests/Unit/Segments/Services/ListsConstantsTest.php new file mode 100644 index 00000000..57a66a7c --- /dev/null +++ b/tests/Unit/Segments/Services/ListsConstantsTest.php @@ -0,0 +1,52 @@ +service = new ListsConstants(new SegmentConstant()); + } + + public function test_it_can_list_constants_using_filters() + { + // A R R A N G E + $testName = 'list_constants'; + $segmentId = mt_rand(100000, 999999); + + $firstConstant = new SegmentConstant(); + $firstConstant->segment_id = $segmentId; + $firstConstant->reference = $testName . '_first_ref'; + $firstConstant->value = ['100']; + $firstConstant->save(); + + $secondConstant = new SegmentConstant(); + $secondConstant->segment_id = 2; + $secondConstant->reference = $testName . '_second_ref'; + $secondConstant->value = ['200']; + $secondConstant->save(); + + // A C T + $results = $this->service->execute(['segment_id' => $segmentId]); + + // A S S E R T + $this->assertInstanceOf(Collection::class, $results); + $this->assertCount(1, $results); + $this->assertSame($firstConstant->id, $results->first()->id); + } +} diff --git a/tests/Unit/Segments/Services/ListsSegmentsTest.php b/tests/Unit/Segments/Services/ListsSegmentsTest.php new file mode 100644 index 00000000..1af6afe2 --- /dev/null +++ b/tests/Unit/Segments/Services/ListsSegmentsTest.php @@ -0,0 +1,50 @@ +service = new ListsSegments(new Segment()); + } + + public function test_it_can_list_segments_using_filters() + { + // A R R A N G E + $testName = 'list_segments'; + $companyModuleId = mt_rand(100000, 999999); + + $firstSegment = new Segment(); + $firstSegment->name = $testName . '_first_segment'; + $firstSegment->company_module_id = $companyModuleId; + $firstSegment->save(); + + $secondSegment = new Segment(); + $secondSegment->name = $testName . '_second_segment'; + $secondSegment->company_module_id = 2; + $secondSegment->save(); + + // A C T + $results = $this->service->execute(['company_module_id' => $companyModuleId]); + + // A S S E R T + $this->assertInstanceOf(Collection::class, $results); + $this->assertCount(1, $results); + $this->assertSame($firstSegment->id, $results->first()->id); + } +} diff --git a/tests/Unit/Segments/Services/RemoveItemFromConstantValueArrayTest.php b/tests/Unit/Segments/Services/RemoveItemFromConstantValueArrayTest.php new file mode 100644 index 00000000..83cbe448 --- /dev/null +++ b/tests/Unit/Segments/Services/RemoveItemFromConstantValueArrayTest.php @@ -0,0 +1,51 @@ +service = new RemoveItemFromConstantValueArray(); + } + + //cief todo: 137 - this test does update any value into DB, looks like a small data transformer / mutator + public function test_it_removes_matching_items_from_the_array() + { + // A R R A N G E + $array = ['10000', '20000', '10000']; + $item = '10000'; + + // A C T + $result = $this->service->execute($array, $item); + + // A S S E R T + $this->assertSame(['20000'], $result); + } + + //cief todo: 137 - this test does update any value into DB, looks like a small data transformer / mutator + public function test_it_handles_non_array_input_as_an_empty_array() + { + // A R R A N G E + $array = ''; + $item = '10000'; + + // A C T + $result = $this->service->execute($array, $item); + + // A S S E R T + $this->assertSame([], $result); + } +} diff --git a/tests/Unit/Segments/Services/UpdatesBasePriceConstantByDatesTest.php b/tests/Unit/Segments/Services/UpdatesBasePriceConstantByDatesTest.php new file mode 100644 index 00000000..088edcb1 --- /dev/null +++ b/tests/Unit/Segments/Services/UpdatesBasePriceConstantByDatesTest.php @@ -0,0 +1,43 @@ +service = new UpdatesBasePriceConstantByDates(); + } + + //cief todo: 137 - this test does not seems to be updating any value into DB, looks like a small data transformer / mutator + public function test_it_updates_each_date_in_the_given_period() + { + // A R R A N G E + $json = [ + '2026-03-01' => '10.00', + ]; + + // A C T + $result = $this->service->execute($json, '2026-03-02', '2026-03-04', '12.50'); + + // A S S E R T + $this->assertSame([ + '2026-03-01' => '10.00', + '2026-03-02' => '12.50', + '2026-03-03' => '12.50', + '2026-03-04' => '12.50', + ], $result); + } +} diff --git a/tests/Unit/Segments/Services/UpdatesConstantTest.php b/tests/Unit/Segments/Services/UpdatesConstantTest.php index d9c20b8c..8e08a592 100644 --- a/tests/Unit/Segments/Services/UpdatesConstantTest.php +++ b/tests/Unit/Segments/Services/UpdatesConstantTest.php @@ -29,21 +29,22 @@ class UpdatesConstantTest extends TestCaseChild public function test_it_can_update_a_constant() { - // Arrange + // A R R A N G E + $testName = 'update_constant'; $constant = new SegmentConstant(); $constant->segment_id = 1; // Assuming segment_id 1 exists (we don't strictly need the Segment model to update the constant if we set the ID) - $constant->reference = 'old_reference'; + $constant->reference = $testName . '_old_ref'; $constant->value = ['old_value']; $constant->save(); - $reference = 'new_reference'; + $reference = $testName . '_new_ref'; $values = ['new_value1', 'new_value2']; $object = new ConstantObject($reference, $values); - // Act + // A C T $result = $this->service->execute($constant, $object); - // Assert + // A S S E R T $this->assertEquals($reference, $constant->reference); $this->assertEquals($values, $constant->value); $this->assertSame($constant, $result); diff --git a/tests/Unit/Segments/Services/UpdatesConstantValueTest.php b/tests/Unit/Segments/Services/UpdatesConstantValueTest.php new file mode 100644 index 00000000..d40e447a --- /dev/null +++ b/tests/Unit/Segments/Services/UpdatesConstantValueTest.php @@ -0,0 +1,41 @@ +service = new UpdatesConstantValue(); + } + + //cief todo: 137 - this test does not seems to be updating any value into DB, looks like a small data transformer / mutator + public function test_it_updates_the_rate_for_the_given_state() + { + // A R R A N G E + $json = [ + 1 => ['rate' => 10], + ]; + + // A C T + $result = $this->service->execute($json, 2, ['rate' => 15]); + + // A S S E R T + $this->assertSame([ + 1 => ['rate' => 10], + 2 => ['rate' => 15], + ], $result); + } +} diff --git a/tests/Unit/Segments/Services/UpdatesSegmentTest.php b/tests/Unit/Segments/Services/UpdatesSegmentTest.php new file mode 100644 index 00000000..e3fcefe3 --- /dev/null +++ b/tests/Unit/Segments/Services/UpdatesSegmentTest.php @@ -0,0 +1,51 @@ +service = new UpdatesSegment(); + } + + public function test_it_can_update_a_segment_name() + { + // A R R A N G E + $testName = 'update_segment'; + $segment = new Segment(); + $segment->name = $testName . '_old_name'; + $segment->company_module_id = 1; + $segment->save(); + + $object = new SegmentObject($testName . '_new_name', 1); //company_module_id is not used in an update + + // A C T + $result = $this->service->execute($segment, $object); + + // A S S E R T + $this->assertSame($segment->id, $result->id); + $this->assertSame($testName . '_new_name', $segment->name); + $this->assertSame(1, $segment->company_module_id); + + $this->assertDatabaseHas('segments', [ + 'id' => $segment->id, + 'name' => $testName . '_new_name', + 'company_module_id' => 1, + ]); + } +}