182 lines
6.1 KiB
PHP
182 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 Config\Services;
|
|
use Tests\Support\Traits\ReminderMailControllerTestTrait;
|
|
|
|
class SendReminderMailTest extends CIUnitTestCase
|
|
{
|
|
use ReminderMailControllerTestTrait;
|
|
|
|
private function mockReminderMailConfigModel(array $methods): ReminderMailConfigModel
|
|
{
|
|
return $this->getMockBuilder(ReminderMailConfigModel::class)
|
|
->onlyMethods(array_keys($methods))
|
|
->getMock();
|
|
}
|
|
|
|
public function testNonPostRequestReturns405()
|
|
{
|
|
$controller = $this->buildJsonPostController(
|
|
['client_policy_id' => 123],
|
|
$this->createMock(ClientPolicyModel::class),
|
|
$this->mockReminderMailConfigModel([]),
|
|
false
|
|
);
|
|
|
|
$body = $this->decodeResponse($controller->sendReminderMail());
|
|
|
|
$this->assertFalse($body['status']);
|
|
$this->assertSame(405, $body['code']);
|
|
$this->assertSame('Only POST method is allowed', $body['message']);
|
|
}
|
|
|
|
public function testEmptyJsonBodyReturns400()
|
|
{
|
|
$controller = $this->buildJsonPostController(
|
|
[],
|
|
$this->createMock(ClientPolicyModel::class),
|
|
$this->mockReminderMailConfigModel([])
|
|
);
|
|
|
|
$body = $this->decodeResponse($controller->sendReminderMail());
|
|
|
|
$this->assertFalse($body['status']);
|
|
$this->assertSame(400, $body['code']);
|
|
$this->assertSame('JSON request body is required', $body['message']);
|
|
}
|
|
|
|
public function testMissingClientPolicyIdReturns400()
|
|
{
|
|
$controller = $this->buildJsonPostController(
|
|
['email_subject' => 'Reminder'],
|
|
$this->createMock(ClientPolicyModel::class),
|
|
$this->mockReminderMailConfigModel([])
|
|
);
|
|
|
|
$body = $this->decodeResponse($controller->sendReminderMail());
|
|
|
|
$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(123)->willReturn(null);
|
|
|
|
$controller = $this->buildJsonPostController(
|
|
['client_policy_id' => 123],
|
|
$clientPolicyModel,
|
|
$this->mockReminderMailConfigModel([])
|
|
);
|
|
|
|
$body = $this->decodeResponse($controller->sendReminderMail());
|
|
|
|
$this->assertFalse($body['status']);
|
|
$this->assertSame(404, $body['code']);
|
|
$this->assertSame('Client policy not found', $body['message']);
|
|
}
|
|
|
|
public function testSavesEmailTemplateBeforeSendingMail()
|
|
{
|
|
$clientPolicyModel = $this->createMock(ClientPolicyModel::class);
|
|
$clientPolicyModel->method('find')->with(123)->willReturn([
|
|
'id' => 123,
|
|
'client_id' => 42,
|
|
'client_branch_id' => 7,
|
|
]);
|
|
|
|
$reminderMailConfigModel = $this->mockReminderMailConfigModel(['saveEmailTemplate' => null]);
|
|
$reminderMailConfigModel->expects($this->once())
|
|
->method('saveEmailTemplate')
|
|
->with(
|
|
123,
|
|
'Enrollment reminder',
|
|
'<p>Hi [[member_name]]</p>',
|
|
45
|
|
);
|
|
|
|
$controller = $this->getMockBuilder(EmployeeRestController::class)
|
|
->onlyMethods(['dispatchManualReminder'])
|
|
->getMock();
|
|
$controller->expects($this->once())
|
|
->method('dispatchManualReminder')
|
|
->with(42, 7, 123)
|
|
->willReturn(
|
|
Services::response()->setJSON([
|
|
'status' => true,
|
|
'code' => 200,
|
|
'message' => 'Mail sent successfully',
|
|
])->setStatusCode(200)
|
|
);
|
|
|
|
$controller = $this->buildJsonPostController(
|
|
[
|
|
'client_policy_id' => 123,
|
|
'email_subject' => 'Enrollment reminder',
|
|
'email_body' => '<p>Hi [[member_name]]</p>',
|
|
'hr_id' => 45,
|
|
],
|
|
$clientPolicyModel,
|
|
$reminderMailConfigModel,
|
|
true,
|
|
null,
|
|
$controller
|
|
);
|
|
|
|
$body = $this->decodeResponse($controller->sendReminderMail());
|
|
|
|
$this->assertTrue($body['status']);
|
|
$this->assertSame(200, $body['code']);
|
|
$this->assertSame('Mail sent successfully', $body['message']);
|
|
}
|
|
|
|
public function testSkipsTemplateSaveWhenEmailFieldsNotProvided()
|
|
{
|
|
$clientPolicyModel = $this->createMock(ClientPolicyModel::class);
|
|
$clientPolicyModel->method('find')->with(123)->willReturn([
|
|
'id' => 123,
|
|
'client_id' => 42,
|
|
'client_branch_id' => 7,
|
|
]);
|
|
|
|
$reminderMailConfigModel = $this->mockReminderMailConfigModel(['saveEmailTemplate' => null]);
|
|
$reminderMailConfigModel->expects($this->never())->method('saveEmailTemplate');
|
|
|
|
$controller = $this->getMockBuilder(EmployeeRestController::class)
|
|
->onlyMethods(['dispatchManualReminder'])
|
|
->getMock();
|
|
$controller->expects($this->once())
|
|
->method('dispatchManualReminder')
|
|
->with(42, 7, 123)
|
|
->willReturn(
|
|
Services::response()->setJSON([
|
|
'status' => false,
|
|
'code' => 200,
|
|
'message' => 'There is no data to send',
|
|
])->setStatusCode(200)
|
|
);
|
|
|
|
$controller = $this->buildJsonPostController(
|
|
['client_policy_id' => 123],
|
|
$clientPolicyModel,
|
|
$reminderMailConfigModel,
|
|
true,
|
|
null,
|
|
$controller
|
|
);
|
|
|
|
$body = $this->decodeResponse($controller->sendReminderMail());
|
|
|
|
$this->assertFalse($body['status']);
|
|
$this->assertSame('There is no data to send', $body['message']);
|
|
}
|
|
}
|