clqms-be/tests/feature/Test/TestMapPatchTest.php

260 lines
9.2 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature\Test;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
use Firebase\JWT\JWT;
class TestMapPatchTest extends CIUnitTestCase
{
use FeatureTestTrait;
protected string $token;
protected string $endpoint = 'api/test/testmap';
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 createTestMap(array $data = []): array
{
$payload = array_merge([
'TestMapCode' => 'TM_' . uniqid(),
'TestMapName' => 'Test Map ' . uniqid(),
2026-04-08 16:54:32 +07:00
'HostType' => 'SITE',
'HostID' => 1,
'ClientType' => 'SITE',
'ClientID' => 1,
], $data);
$response = $this->withHeaders($this->authHeaders())
->withBodyFormat('json')
->call('post', $this->endpoint, $payload);
2026-04-08 16:54:32 +07:00
fwrite(STDERR, 'Create response: ' . $response->getStatusCode() . ' ' . $response->getBody() . PHP_EOL);
$response->assertStatus(201);
2026-04-08 16:54:32 +07:00
$created = json_decode($response->getJSON(), true);
$id = $created['data'];
$show = $this->withHeaders($this->authHeaders())->call('get', "{$this->endpoint}/{$id}");
$show->assertStatus(200);
return json_decode($show->getJSON(), true)['data'];
}
public function testPartialUpdateTestMapSuccess()
{
$testMap = $this->createTestMap();
$id = $testMap['TestMapID'];
$patch = $this->withHeaders($this->authHeaders())
->withBodyFormat('json')
->call('patch', "{$this->endpoint}/{$id}", ['TestMapName' => 'Updated TestMap']);
$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'];
$this->assertEquals('Updated TestMap', $showData['TestMapName']);
$this->assertEquals($testMap['TestMapCode'], $showData['TestMapCode']);
}
public function testPartialUpdateTestMapNotFound()
{
$patch = $this->withHeaders($this->authHeaders())
->withBodyFormat('json')
->call('patch', "{$this->endpoint}/999999", ['TestMapName' => 'Updated']);
$patch->assertStatus(404);
}
public function testPartialUpdateTestMapInvalidId()
{
$patch = $this->withHeaders($this->authHeaders())
->withBodyFormat('json')
->call('patch', "{$this->endpoint}/invalid", ['TestMapName' => 'Updated']);
$patch->assertStatus(400);
}
public function testPartialUpdateTestMapEmptyPayload()
{
$testMap = $this->createTestMap();
$id = $testMap['TestMapID'];
$patch = $this->withHeaders($this->authHeaders())
->withBodyFormat('json')
->call('patch', "{$this->endpoint}/{$id}", []);
$patch->assertStatus(400);
}
public function testPartialUpdateTestMapSingleField()
{
$testMap = $this->createTestMap();
$id = $testMap['TestMapID'];
$patch = $this->withHeaders($this->authHeaders())
->withBodyFormat('json')
->call('patch', "{$this->endpoint}/{$id}", ['TestMapCode' => 'NEW_' . uniqid()]);
$patch->assertStatus(200);
$showData = json_decode($this->withHeaders($this->authHeaders())
->call('get', "{$this->endpoint}/{$id}")
->getJSON(), true)['data'];
$this->assertNotEquals($testMap['TestMapCode'], $showData['TestMapCode']);
$this->assertEquals($testMap['TestMapName'], $showData['TestMapName']);
}
2026-04-08 16:54:32 +07:00
public function testCreateTestMapWithDetails()
{
$details = [
[
'HostTestCode' => 'HB_' . uniqid(),
'HostTestName' => 'Hemoglobin',
'ClientTestCode' => '2',
'ClientTestName' => 'Hemoglobin',
],
[
'HostTestCode' => 'HCT_' . uniqid(),
'HostTestName' => 'Hematocrit',
'ClientTestCode' => '3',
'ClientTestName' => 'Hematocrit',
],
];
$testMap = $this->createTestMap([
'HostType' => 'SITE',
'HostID' => 1,
'ClientType' => 'SITE',
'ClientID' => 2,
'details' => $details,
]);
$this->assertCount(2, $testMap['details']);
$this->assertEquals('2', $testMap['details'][0]['ClientTestCode']);
}
public function testPatchTestMapDetailOperations()
{
$initialDetails = [
[
'HostTestCode' => 'HB_' . uniqid(),
'HostTestName' => 'Hemoglobin',
'ClientTestCode' => '2',
'ClientTestName' => 'Hemoglobin',
],
[
'HostTestCode' => 'HCT_' . uniqid(),
'HostTestName' => 'Hematocrit',
'ClientTestCode' => '3',
'ClientTestName' => 'Hematocrit',
],
];
$testMap = $this->createTestMap([
'HostType' => 'SITE',
'HostID' => 1,
'ClientType' => 'SITE',
'ClientID' => 1,
'details' => $initialDetails,
]);
$existingDetails = $testMap['details'];
$editDetail = $existingDetails[0];
$deleteDetail = $existingDetails[1];
$patch = $this->withHeaders($this->authHeaders())
->withBodyFormat('json')
->call('patch', "{$this->endpoint}/{$testMap['TestMapID']}", [
'ClientType' => 'WST',
'details' => [
'edit' => [
[
'TestMapDetailID' => $editDetail['TestMapDetailID'],
'ClientTestName' => 'Hemoglobin Updated',
],
],
'new' => [
[
'HostTestCode' => 'MCV_' . uniqid(),
'HostTestName' => 'MCV',
'ClientTestCode' => '4',
'ClientTestName' => 'MCV',
],
],
'deleted' => [$deleteDetail['TestMapDetailID']],
],
]);
$patch->assertStatus(200);
$patchData = json_decode($patch->getJSON(), true);
$this->assertEquals('success', $patchData['status']);
$show = $this->withHeaders($this->authHeaders())->call('get', "{$this->endpoint}/{$testMap['TestMapID']}");
$show->assertStatus(200);
$showData = json_decode($show->getJSON(), true)['data'];
$this->assertEquals('WST', $showData['ClientType']);
$this->assertCount(2, $showData['details']);
$detailIds = array_column($showData['details'], 'TestMapDetailID');
$this->assertContains($editDetail['TestMapDetailID'], $detailIds);
$this->assertNotContains($deleteDetail['TestMapDetailID'], $detailIds);
$updatedDetails = array_values(array_filter($showData['details'], static fn ($row) => $row['TestMapDetailID'] === $editDetail['TestMapDetailID']));
$this->assertNotEmpty($updatedDetails);
$this->assertEquals('Hemoglobin Updated', $updatedDetails[0]['ClientTestName']);
}
public function testDeleteTestMapRemovesDetails()
{
$testMap = $this->createTestMap([
'HostType' => 'SITE',
'HostID' => 2,
'ClientType' => 'SITE',
'ClientID' => 3,
'details' => [
[
'HostTestCode' => 'PLT_' . uniqid(),
'HostTestName' => 'Platelet',
'ClientTestCode' => '5',
'ClientTestName' => 'Platelet',
],
],
]);
$delete = $this->withHeaders($this->authHeaders())
->withBodyFormat('json')
->call('delete', $this->endpoint, ['TestMapID' => $testMap['TestMapID']]);
$delete->assertStatus(200);
$details = $this->withHeaders($this->authHeaders())
->call('get', "{$this->endpoint}/detail/by-testmap/{$testMap['TestMapID']}");
$details->assertStatus(200);
$this->assertEquals([], json_decode($details->getJSON(), true)['data']);
}
}