- handle contact PATCH failures by checking model save result and returning HTTP 400 with the model error message - update ContactDetailModel nested updates to enforce active-detail checks and use model update() with explicit failure propagation - extend contact patch assertions and align test-create variants expectations to status=success for POST responses - refresh composer lock metadata/dependency constraints and include generated docs/data/test files updated during normalization - impact: API contract unchanged except clearer 400 error responses on invalid contact detail updates
122 lines
4.1 KiB
PHP
Executable File
122 lines
4.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Tests\Feature\Specimen;
|
|
|
|
use CodeIgniter\Test\FeatureTestTrait;
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
|
use Firebase\JWT\JWT;
|
|
|
|
class SpecimenPatchTest extends CIUnitTestCase
|
|
{
|
|
use FeatureTestTrait;
|
|
|
|
protected string $token;
|
|
protected string $endpoint = 'api/specimen';
|
|
|
|
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 createSpecimen(array $data = []): array
|
|
{
|
|
$payload = array_merge([
|
|
'SpecimenCode' => 'SPEC_' . uniqid(),
|
|
'SpecimenName' => 'Test Specimen ' . 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 testPartialUpdateSpecimenSuccess()
|
|
{
|
|
$specimen = $this->createSpecimen();
|
|
$id = $specimen['SpecimenID'];
|
|
|
|
$patch = $this->withHeaders($this->authHeaders())
|
|
->withBodyFormat('json')
|
|
->call('patch', "{$this->endpoint}/{$id}", ['SpecimenName' => 'Updated Specimen']);
|
|
|
|
$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 Specimen', $showData['SpecimenName']);
|
|
$this->assertEquals($specimen['SpecimenCode'], $showData['SpecimenCode']);
|
|
}
|
|
|
|
public function testPartialUpdateSpecimenNotFound()
|
|
{
|
|
$patch = $this->withHeaders($this->authHeaders())
|
|
->withBodyFormat('json')
|
|
->call('patch', "{$this->endpoint}/999999", ['SpecimenName' => 'Updated']);
|
|
|
|
$patch->assertStatus(404);
|
|
}
|
|
|
|
public function testPartialUpdateSpecimenInvalidId()
|
|
{
|
|
$patch = $this->withHeaders($this->authHeaders())
|
|
->withBodyFormat('json')
|
|
->call('patch', "{$this->endpoint}/invalid", ['SpecimenName' => 'Updated']);
|
|
|
|
$patch->assertStatus(400);
|
|
}
|
|
|
|
public function testPartialUpdateSpecimenEmptyPayload()
|
|
{
|
|
$specimen = $this->createSpecimen();
|
|
$id = $specimen['SpecimenID'];
|
|
|
|
$patch = $this->withHeaders($this->authHeaders())
|
|
->withBodyFormat('json')
|
|
->call('patch', "{$this->endpoint}/{$id}", []);
|
|
|
|
$patch->assertStatus(400);
|
|
}
|
|
|
|
public function testPartialUpdateSpecimenSingleField()
|
|
{
|
|
$specimen = $this->createSpecimen();
|
|
$id = $specimen['SpecimenID'];
|
|
|
|
$patch = $this->withHeaders($this->authHeaders())
|
|
->withBodyFormat('json')
|
|
->call('patch', "{$this->endpoint}/{$id}", ['SpecimenCode' => 'NEW_' . uniqid()]);
|
|
|
|
$patch->assertStatus(200);
|
|
$showData = json_decode($this->withHeaders($this->authHeaders())
|
|
->call('get', "{$this->endpoint}/{$id}")
|
|
->getJSON(), true)['data'];
|
|
|
|
$this->assertNotEquals($specimen['SpecimenCode'], $showData['SpecimenCode']);
|
|
$this->assertEquals($specimen['SpecimenName'], $showData['SpecimenName']);
|
|
}
|
|
}
|