121 lines
3.5 KiB
PHP
121 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Support\Traits;
|
|
|
|
use App\Controllers\EmployeeRestController;
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
|
use Config\Services;
|
|
use ReflectionClass;
|
|
|
|
trait ReminderMailControllerTestTrait
|
|
{
|
|
private function createNotificationModelStub(?array $result): object
|
|
{
|
|
return new class($result) {
|
|
private ?array $result;
|
|
|
|
public function __construct(?array $result)
|
|
{
|
|
$this->result = $result;
|
|
}
|
|
|
|
public function where(...$args)
|
|
{
|
|
return $this;
|
|
}
|
|
|
|
public function first(): ?array
|
|
{
|
|
return $this->result;
|
|
}
|
|
};
|
|
}
|
|
|
|
private function buildGetController(
|
|
array $getParams,
|
|
$clientPolicyModel,
|
|
$reminderMailConfigModel,
|
|
?object $notificationModel = null
|
|
): 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;
|
|
});
|
|
|
|
return $this->injectReminderMailControllerDependencies(
|
|
$request,
|
|
$clientPolicyModel,
|
|
$reminderMailConfigModel,
|
|
$notificationModel
|
|
);
|
|
}
|
|
|
|
private function buildJsonPostController(
|
|
array $jsonData,
|
|
$clientPolicyModel,
|
|
$reminderMailConfigModel,
|
|
bool $isPost = true,
|
|
?object $notificationModel = null,
|
|
?EmployeeRestController $controller = null
|
|
): EmployeeRestController {
|
|
$request = $this->createMock(IncomingRequest::class);
|
|
$request->method('is')
|
|
->with('post')
|
|
->willReturn($isPost);
|
|
$request->method('getJSON')
|
|
->with(true)
|
|
->willReturn($jsonData);
|
|
$request->method('getVar')
|
|
->willReturn(null);
|
|
|
|
$controller = $controller ?? new EmployeeRestController();
|
|
|
|
return $this->injectReminderMailControllerDependencies(
|
|
$request,
|
|
$clientPolicyModel,
|
|
$reminderMailConfigModel,
|
|
$notificationModel,
|
|
$controller
|
|
);
|
|
}
|
|
|
|
private function injectReminderMailControllerDependencies(
|
|
IncomingRequest $request,
|
|
$clientPolicyModel,
|
|
$reminderMailConfigModel,
|
|
?object $notificationModel = null,
|
|
?EmployeeRestController $controller = null
|
|
): EmployeeRestController {
|
|
$controller = $controller ?? new EmployeeRestController();
|
|
$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);
|
|
}
|
|
}
|