clqms-be/tests/feature/Test/TestMapPatchTest.php
OpenCode Bot 9946978487 chore: refresh CLQMS backend baseline
Re-synced controllers, configs, libraries, seeds, and docs with the latest API expectations and response helpers.
2026-04-08 16:07:19 +07:00

122 lines
3.9 KiB
PHP
Executable File

<?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(),
], $data);
$response = $this->withHeaders($this->authHeaders())
->withBodyFormat('json')
->call('post', $this->endpoint, $payload);
$response->assertStatus(201);
$decoded = json_decode($response->getJSON(), true);
return $decoded['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']);
}
}