nhance-enrollment/tests/unit/SaveReminderMailConfigTest.php

174 lines
6.1 KiB
PHP

<?php
namespace Tests\Unit;
use App\Controllers\EmployeeRestController;
use App\Models\ClientPolicyModel;
use App\Models\ReminderMailConfigModel;
use CodeIgniter\Test\CIUnitTestCase;
use Tests\Support\Traits\ReminderMailControllerTestTrait;
class SaveReminderMailConfigTest extends CIUnitTestCase
{
use ReminderMailControllerTestTrait;
private function mockReminderMailConfigModel(array $methods): ReminderMailConfigModel
{
return $this->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' => '<p>Reminder body</p>',
'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) === '<p>Reminder body</p>'
&& ($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' => '<p>Reminder body</p>',
'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('<p>Reminder body</p>', $body['data']['email_body']);
}
}