2026-04-08 06:54:50 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Feature\Test;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\Test\FeatureTestTrait;
|
|
|
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
|
|
|
|
use Firebase\JWT\JWT;
|
|
|
|
|
|
|
|
|
|
class TestMapDetailPatchTest extends CIUnitTestCase
|
|
|
|
|
{
|
|
|
|
|
use FeatureTestTrait;
|
|
|
|
|
|
|
|
|
|
protected string $token;
|
|
|
|
|
protected string $endpoint = 'api/test/testmap/detail';
|
2026-04-09 09:02:50 +07:00
|
|
|
protected string $mapEndpoint = 'api/test/testmap';
|
2026-04-08 06:54:50 +07:00
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
$key = getenv('JWT_SECRET') ?: 'my-secret-key';
|
|
|
|
|
$payload = [
|
|
|
|
|
'iss' => 'localhost',
|
|
|
|
|
'aud' => 'localhost',
|
|
|
|
|
'iat' => time(),
|
|
|
|
|
'nbf' => time(),
|
|
|
|
|
'exp' => time() + 3600,
|
|
|
|
|
'uid' => 1,
|
|
|
|
|
'email' => 'admin@admin.com',
|
|
|
|
|
];
|
|
|
|
|
$this->token = JWT::encode($payload, $key, 'HS256');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function authHeaders(): array
|
|
|
|
|
{
|
|
|
|
|
return ['Cookie' => 'token=' . $this->token];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function createTestMapDetail(array $data = []): array
|
|
|
|
|
{
|
2026-04-09 09:02:50 +07:00
|
|
|
$mapResponse = $this->withHeaders($this->authHeaders())
|
|
|
|
|
->withBodyFormat('json')
|
|
|
|
|
->call('post', $this->mapEndpoint, [
|
|
|
|
|
'HostType' => 'SITE',
|
|
|
|
|
'HostID' => 1,
|
|
|
|
|
'ClientType' => 'SITE',
|
|
|
|
|
'ClientID' => 1,
|
|
|
|
|
]);
|
|
|
|
|
$mapResponse->assertStatus(201);
|
|
|
|
|
$mapID = json_decode($mapResponse->getJSON(), true)['data'];
|
|
|
|
|
|
2026-04-08 06:54:50 +07:00
|
|
|
$payload = array_merge([
|
2026-04-09 09:02:50 +07:00
|
|
|
'TestMapID' => $mapID,
|
|
|
|
|
'HostTestCode' => 'HB',
|
|
|
|
|
'HostTestName' => 'Hemoglobin',
|
|
|
|
|
'ClientTestCode' => '2',
|
|
|
|
|
'ClientTestName' => 'Hemoglobin',
|
2026-04-08 06:54:50 +07:00
|
|
|
], $data);
|
|
|
|
|
|
|
|
|
|
$response = $this->withHeaders($this->authHeaders())
|
|
|
|
|
->withBodyFormat('json')
|
|
|
|
|
->call('post', $this->endpoint, $payload);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(201);
|
|
|
|
|
$decoded = json_decode($response->getJSON(), true);
|
2026-04-09 09:02:50 +07:00
|
|
|
$detailID = $decoded['data'];
|
|
|
|
|
|
|
|
|
|
$show = $this->withHeaders($this->authHeaders())->call('get', "{$this->endpoint}/{$detailID}");
|
|
|
|
|
$show->assertStatus(200);
|
|
|
|
|
return json_decode($show->getJSON(), true)['data'];
|
2026-04-08 06:54:50 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPartialUpdateTestMapDetailSuccess()
|
|
|
|
|
{
|
|
|
|
|
$detail = $this->createTestMapDetail();
|
|
|
|
|
$id = $detail['TestMapDetailID'];
|
|
|
|
|
|
|
|
|
|
$patch = $this->withHeaders($this->authHeaders())
|
|
|
|
|
->withBodyFormat('json')
|
2026-04-09 09:02:50 +07:00
|
|
|
->call('patch', "{$this->endpoint}/{$id}", ['ClientTestName' => 'Updated Detail']);
|
2026-04-08 06:54:50 +07:00
|
|
|
|
|
|
|
|
$patch->assertStatus(200);
|
|
|
|
|
$patchData = json_decode($patch->getJSON(), true);
|
|
|
|
|
$this->assertEquals('success', $patchData['status']);
|
|
|
|
|
|
|
|
|
|
$show = $this->withHeaders($this->authHeaders())->call('get', "{$this->endpoint}/{$id}");
|
|
|
|
|
$show->assertStatus(200);
|
|
|
|
|
$showData = json_decode($show->getJSON(), true)['data'];
|
|
|
|
|
|
2026-04-09 09:02:50 +07:00
|
|
|
$this->assertEquals('Updated Detail', $showData['ClientTestName']);
|
|
|
|
|
$this->assertEquals($detail['HostTestCode'], $showData['HostTestCode']);
|
2026-04-08 06:54:50 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPartialUpdateTestMapDetailNotFound()
|
|
|
|
|
{
|
|
|
|
|
$patch = $this->withHeaders($this->authHeaders())
|
|
|
|
|
->withBodyFormat('json')
|
2026-04-09 09:02:50 +07:00
|
|
|
->call('patch', "{$this->endpoint}/999999", ['ClientTestName' => 'Updated']);
|
2026-04-08 06:54:50 +07:00
|
|
|
|
|
|
|
|
$patch->assertStatus(404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPartialUpdateTestMapDetailInvalidId()
|
|
|
|
|
{
|
|
|
|
|
$patch = $this->withHeaders($this->authHeaders())
|
|
|
|
|
->withBodyFormat('json')
|
2026-04-09 09:02:50 +07:00
|
|
|
->call('patch', "{$this->endpoint}/invalid", ['ClientTestName' => 'Updated']);
|
2026-04-08 06:54:50 +07:00
|
|
|
|
|
|
|
|
$patch->assertStatus(400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPartialUpdateTestMapDetailEmptyPayload()
|
|
|
|
|
{
|
|
|
|
|
$detail = $this->createTestMapDetail();
|
|
|
|
|
$id = $detail['TestMapDetailID'];
|
|
|
|
|
|
|
|
|
|
$patch = $this->withHeaders($this->authHeaders())
|
|
|
|
|
->withBodyFormat('json')
|
|
|
|
|
->call('patch', "{$this->endpoint}/{$id}", []);
|
|
|
|
|
|
|
|
|
|
$patch->assertStatus(400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPartialUpdateTestMapDetailSingleField()
|
|
|
|
|
{
|
|
|
|
|
$detail = $this->createTestMapDetail();
|
|
|
|
|
$id = $detail['TestMapDetailID'];
|
|
|
|
|
|
|
|
|
|
$patch = $this->withHeaders($this->authHeaders())
|
|
|
|
|
->withBodyFormat('json')
|
2026-04-09 09:02:50 +07:00
|
|
|
->call('patch', "{$this->endpoint}/{$id}", ['HostTestCode' => 'HBA1C']);
|
2026-04-08 06:54:50 +07:00
|
|
|
|
|
|
|
|
$patch->assertStatus(200);
|
|
|
|
|
$showData = json_decode($this->withHeaders($this->authHeaders())
|
|
|
|
|
->call('get', "{$this->endpoint}/{$id}")
|
|
|
|
|
->getJSON(), true)['data'];
|
|
|
|
|
|
2026-04-09 09:02:50 +07:00
|
|
|
$this->assertNotEquals($detail['HostTestCode'], $showData['HostTestCode']);
|
|
|
|
|
$this->assertEquals($detail['ClientTestName'], $showData['ClientTestName']);
|
2026-04-08 06:54:50 +07:00
|
|
|
}
|
|
|
|
|
}
|