getMockBuilder(ReminderMailConfigModel::class) ->onlyMethods(array_keys($methods)) ->getMock(); } public function testMissingClientPolicyIdReturns400() { $controller = $this->buildJsonPostController( ['frequency' => 'daily'], $this->createMock(ClientPolicyModel::class), $this->mockReminderMailConfigModel([]) ); $body = $this->decodeResponse($controller->saveReminderMailConfig()); $this->assertFalse($body['status']); $this->assertSame(400, $body['code']); $this->assertSame('client_policy_id is required', $body['message']); } public function testMissingFrequencyReturns400() { $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $controller = $this->buildJsonPostController( ['client_policy_id' => 123], $clientPolicyModel, $this->mockReminderMailConfigModel([]) ); $body = $this->decodeResponse($controller->saveReminderMailConfig()); $this->assertFalse($body['status']); $this->assertSame(400, $body['code']); $this->assertSame('frequency is required', $body['message']); } public function testClientPolicyNotFoundReturns404() { $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $clientPolicyModel->method('find')->with(123)->willReturn(null); $controller = $this->buildJsonPostController( [ 'client_policy_id' => 123, 'frequency' => 'daily', ], $clientPolicyModel, $this->mockReminderMailConfigModel([]) ); $body = $this->decodeResponse($controller->saveReminderMailConfig()); $this->assertFalse($body['status']); $this->assertSame(404, $body['code']); $this->assertSame('Client policy not found', $body['message']); } public function testSaveConfigValidationErrorReturns400() { $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $clientPolicyModel->method('find')->with(123)->willReturn(['id' => 123]); $reminderMailConfigModel = $this->mockReminderMailConfigModel(['saveConfig' => null]); $reminderMailConfigModel->expects($this->once()) ->method('saveConfig') ->with( 123, 'weekly', '9', 1, $this->callback(function (array $options): bool { return !array_key_exists('email_subject', $options) && !array_key_exists('email_body', $options); }) ) ->willReturn([ 'status' => false, 'message' => 'Weekly reminder_days must be between 0 (Sunday) and 6 (Saturday).', ]); $controller = $this->buildJsonPostController( [ 'client_policy_id' => 123, 'frequency' => 'weekly', 'reminder_days' => '9', ], $clientPolicyModel, $reminderMailConfigModel ); $body = $this->decodeResponse($controller->saveReminderMailConfig()); $this->assertFalse($body['status']); $this->assertSame(400, $body['code']); $this->assertSame( 'Weekly reminder_days must be between 0 (Sunday) and 6 (Saturday).', $body['message'] ); } public function testSuccessfulSaveIncludesEmailTemplateFields() { $savedConfig = [ 'id' => 10, 'client_policy_id' => 123, 'frequency' => ReminderMailConfigModel::FREQUENCY_DAILY, 'email_subject' => 'Reminder subject', 'email_body' => '

Reminder body

', 'is_enabled' => 1, ]; $clientPolicyModel = $this->createMock(ClientPolicyModel::class); $clientPolicyModel->method('find')->with(123)->willReturn(['id' => 123]); $reminderMailConfigModel = $this->mockReminderMailConfigModel(['saveConfig' => null]); $reminderMailConfigModel->expects($this->once()) ->method('saveConfig') ->with( 123, 'daily', null, 1, $this->callback(function (array $options): bool { return ($options['email_subject'] ?? null) === 'Reminder subject' && ($options['email_body'] ?? null) === '

Reminder body

' && ($options['hr_id'] ?? null) === 45; }) ) ->willReturn([ 'status' => true, 'action' => 'updated', 'data' => $savedConfig, ]); $controller = $this->buildJsonPostController( [ 'client_policy_id' => 123, 'frequency' => 'daily', 'email_subject' => 'Reminder subject', 'email_body' => '

Reminder body

', 'hr_id' => 45, ], $clientPolicyModel, $reminderMailConfigModel ); $body = $this->decodeResponse($controller->saveReminderMailConfig()); $this->assertTrue($body['status']); $this->assertSame(200, $body['code']); $this->assertSame('updated', $body['action']); $this->assertSame('Reminder mail configuration saved successfully', $body['message']); $this->assertSame('Reminder subject', $body['data']['email_subject']); $this->assertSame('

Reminder body

', $body['data']['email_body']); } }