result = $result; } public function where(...$args) { return $this; } public function first(): ?array { return $this->result; } }; } private function buildController( array $getParams, $clientPolicyModel, $reminderMailConfigModel, ?object $notificationModel = null ): EmployeeRestController { $controller = new EmployeeRestController(); $request = $this->createMock(IncomingRequest::class); $request->method('getGet') ->willReturnCallback(function ($key = null) use ($getParams) { if ($key === null) { return $getParams; } return array_key_exists($key, $getParams) ? $getParams[$key] : null; }); $controller->initController($request, Services::response(), Services::logger()); $reflection = new ReflectionClass($controller); foreach ([ 'clientPolicyModel' => $clientPolicyModel, 'reminderMailConfigModel' => $reminderMailConfigModel, 'notificationModel' => $notificationModel, ] as $property => $value) { if ($value === null) { continue; } $prop = $reflection->getProperty($property); $prop->setAccessible(true); $prop->setValue($controller, $value); } return $controller; } private function decodeResponse($response): array { return json_decode($response->getBody(), true); } private function mockReminderMailConfigModel(array $methods): ReminderMailConfigModel { return $this->getMockBuilder(ReminderMailConfigModel::class) ->onlyMethods(array_keys($methods)) ->getMock(); } public function testMissingClientPolicyIdReturns400() { $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $reminderMailConfigModel = $this->mockReminderMailConfigModel([]); $controller = $this->buildController([], $clientPolicyModel, $reminderMailConfigModel); $body = $this->decodeResponse($controller->getReminderMailConfig()); $this->assertFalse($body['status']); $this->assertSame(400, $body['code']); $this->assertSame('client_policy_id is required', $body['message']); } public function testClientPolicyNotFoundReturns404() { $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $clientPolicyModel->method('find')->with(999)->willReturn(null); $reminderMailConfigModel = $this->mockReminderMailConfigModel([]); $controller = $this->buildController( ['client_policy_id' => '999'], $clientPolicyModel, $reminderMailConfigModel ); $body = $this->decodeResponse($controller->getReminderMailConfig()); $this->assertFalse($body['status']); $this->assertSame(404, $body['code']); $this->assertSame('Client policy not found', $body['message']); } public function testExistingConfigReturnsEnrichedDataAndWorkingDayOptions() { $config = [ 'id' => 10, 'client_policy_id' => 123, 'frequency' => ReminderMailConfigModel::FREQUENCY_WORKING_DAYS, 'reminder_days' => '1,3,5', 'email_subject' => 'Reminder', 'email_body' => '

Please enroll

', 'is_enabled' => 1, 'is_active' => 1, ]; $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $clientPolicyModel->method('find')->with(123)->willReturn([ 'id' => 123, 'client_id' => 42, ]); $reminderMailConfigModel = $this->mockReminderMailConfigModel([ 'getByClientPolicyId' => null, ]); $reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn($config); $controller = $this->buildController( ['client_policy_id' => '123'], $clientPolicyModel, $reminderMailConfigModel ); $body = $this->decodeResponse($controller->getReminderMailConfig()); $this->assertTrue($body['status']); $this->assertSame(200, $body['code']); $this->assertSame(123, $body['data']['client_policy_id']); $this->assertSame(['Mon', 'Wed', 'Fri'], $body['data']['working_day_labels']); $this->assertTrue($body['data']['has_custom_template']); $this->assertCount(7, $body['working_day_options']); $this->assertSame('Mon', $body['working_day_options'][0]['label']); } public function testLegacyFallbackWhenNoConfigButPolicyHasReminderDate() { $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $clientPolicyModel->method('find')->with(123)->willReturn([ 'id' => 123, 'client_id' => 42, 'reminder_date' => '1,15,28', ]); $reminderMailConfigModel = $this->mockReminderMailConfigModel([ 'getByClientPolicyId' => null, ]); $reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn(null); $controller = $this->buildController( ['client_policy_id' => '123'], $clientPolicyModel, $reminderMailConfigModel ); $body = $this->decodeResponse($controller->getReminderMailConfig()); $this->assertTrue($body['status']); $this->assertSame(200, $body['code']); $this->assertSame(123, $body['data']['client_policy_id']); $this->assertSame(ReminderMailConfigModel::FREQUENCY_CUSTOM, $body['data']['frequency']); $this->assertSame('1,15,28', $body['data']['reminder_days']); $this->assertSame('legacy_client_policy', $body['data']['source']); $this->assertArrayHasKey('working_day_options', $body); } public function testNoConfigAndNoLegacyReminderDateReturnsNullData() { $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $clientPolicyModel->method('find')->with(123)->willReturn([ 'id' => 123, 'client_id' => 42, ]); $reminderMailConfigModel = $this->mockReminderMailConfigModel([ 'getByClientPolicyId' => null, ]); $reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn(null); $controller = $this->buildController( ['client_policy_id' => '123'], $clientPolicyModel, $reminderMailConfigModel ); $body = $this->decodeResponse($controller->getReminderMailConfig()); $this->assertTrue($body['status']); $this->assertSame(200, $body['code']); $this->assertNull($body['data']); $this->assertCount(7, $body['working_day_options']); } public function testEmailTemplateModeReturnsConfigTemplate() { $config = [ 'client_policy_id' => 123, 'email_subject' => 'Custom subject', 'email_body' => '

Custom body

', ]; $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $clientPolicyModel->method('find')->with(123)->willReturn([ 'id' => 123, 'client_id' => 42, ]); $reminderMailConfigModel = $this->mockReminderMailConfigModel([ 'getByClientPolicyId' => null, ]); $reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn($config); $controller = $this->buildController( [ 'client_policy_id' => '123', 'is_email_template' => '1', ], $clientPolicyModel, $reminderMailConfigModel ); $body = $this->decodeResponse($controller->getReminderMailConfig()); $this->assertTrue($body['status']); $this->assertSame(200, $body['code']); $this->assertSame('Custom subject', $body['data']['email_subject']); $this->assertSame('

Custom body

', $body['data']['email_body']); $this->assertArrayNotHasKey('working_day_options', $body); } public function testEmailTemplateModeFallsBackToNotificationWhenNoConfig() { $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $clientPolicyModel->method('find')->with(123)->willReturn([ 'id' => 123, 'client_id' => 42, ]); $reminderMailConfigModel = $this->mockReminderMailConfigModel([ 'getByClientPolicyId' => null, ]); $reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn(null); $notificationModel = $this->createNotificationModelStub([ 'subject' => 'Default subject', 'mail_content' => '

Default body

', ]); $controller = $this->buildController( [ 'client_policy_id' => '123', 'is_email_template' => '1', ], $clientPolicyModel, $reminderMailConfigModel, $notificationModel ); $body = $this->decodeResponse($controller->getReminderMailConfig()); $this->assertTrue($body['status']); $this->assertSame(200, $body['code']); $this->assertSame('Default subject', $body['data']['email_subject']); $this->assertSame('

Default body

', $body['data']['email_body']); } public function testEmailTemplateModeReturns404WhenNotificationMissing() { $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $clientPolicyModel->method('find')->with(123)->willReturn([ 'id' => 123, 'client_id' => 42, ]); $reminderMailConfigModel = $this->mockReminderMailConfigModel([ 'getByClientPolicyId' => null, ]); $reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn(null); $notificationModel = $this->createNotificationModelStub(null); $controller = $this->buildController( [ 'client_policy_id' => '123', 'is_email_template' => '1', ], $clientPolicyModel, $reminderMailConfigModel, $notificationModel ); $body = $this->decodeResponse($controller->getReminderMailConfig()); $this->assertFalse($body['status']); $this->assertSame(404, $body['code']); $this->assertSame('Member reminder mail notification not found', $body['message']); } }