149 lines
4.2 KiB
PHP
149 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Calculator;
|
|
|
|
use App\Models\Test\TestDefCalModel;
|
|
use App\Models\Test\TestDefSiteModel;
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
|
use CodeIgniter\Test\FeatureTestTrait;
|
|
|
|
class CalculatorEndpointTest extends CIUnitTestCase
|
|
{
|
|
use FeatureTestTrait;
|
|
|
|
protected TestDefSiteModel $siteModel;
|
|
protected TestDefCalModel $calcModel;
|
|
protected ?int $siteId = null;
|
|
protected ?int $calcId = null;
|
|
protected string $calcName;
|
|
protected string $calcCode;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->siteModel = new TestDefSiteModel();
|
|
$this->calcModel = new TestDefCalModel();
|
|
$this->calcName = 'API Calc ' . uniqid();
|
|
$this->calcCode = $this->generateUniqueCalcCode();
|
|
|
|
$siteId = $this->siteModel->insert([
|
|
'SiteID' => 1,
|
|
'TestSiteCode' => $this->calcCode,
|
|
'TestSiteName' => $this->calcName,
|
|
'TestType' => 'CALC',
|
|
'ResultType' => 'NMRIC',
|
|
'RefType' => 'RANGE',
|
|
'Unit1' => 'mg/dL',
|
|
'VisibleScr' => 1,
|
|
'VisibleRpt' => 1,
|
|
'CountStat' => 0,
|
|
'CreateDate' => date('Y-m-d H:i:s'),
|
|
'StartDate' => date('Y-m-d H:i:s'),
|
|
]);
|
|
|
|
$this->assertNotFalse($siteId, 'Failed to insert testdefsite');
|
|
$this->siteId = $siteId;
|
|
|
|
$this->calcId = $this->calcModel->insert([
|
|
'TestSiteID' => $siteId,
|
|
'DisciplineID' => 1,
|
|
'DepartmentID' => 1,
|
|
'FormulaCode' => 'TBIL - DBIL',
|
|
'RefType' => 'RANGE',
|
|
'Unit1' => 'mg/dL',
|
|
'Factor' => 1,
|
|
'Decimal' => 2,
|
|
'CreateDate' => date('Y-m-d H:i:s'),
|
|
]);
|
|
|
|
$this->assertNotFalse($this->calcId, 'Failed to insert testdefcal');
|
|
|
|
$this->assertNotNull($this->calcModel->findActiveByCodeOrName($this->calcCode));
|
|
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if ($this->calcId) {
|
|
$this->calcModel->delete($this->calcId);
|
|
}
|
|
|
|
if ($this->siteId) {
|
|
$this->siteModel->delete($this->siteId);
|
|
}
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function testCalculateByCodeReturnsValue()
|
|
{
|
|
$response = $this->postCalc($this->calcCode, ['TBIL' => 5, 'DBIL' => 3]);
|
|
$response->assertStatus(200);
|
|
|
|
$data = $this->decodeResponse($response);
|
|
|
|
$this->assertArrayHasKey($this->calcCode, $data);
|
|
$this->assertEquals(2.0, $data[$this->calcCode]);
|
|
}
|
|
|
|
public function testCalculateByNameReturnsValue()
|
|
{
|
|
$response = $this->postCalc($this->calcName, ['TBIL' => 4, 'DBIL' => 1]);
|
|
$response->assertStatus(200);
|
|
|
|
$data = $this->decodeResponse($response);
|
|
|
|
$this->assertArrayHasKey($this->calcCode, $data);
|
|
$this->assertEquals(3.0, $data[$this->calcCode]);
|
|
}
|
|
|
|
public function testIncompletePayloadReturnsEmptyObject()
|
|
{
|
|
$response = $this->postCalc($this->calcCode, ['TBIL' => 5]);
|
|
$response->assertStatus(200);
|
|
|
|
$data = $this->decodeResponse($response);
|
|
$this->assertSame([], $data);
|
|
}
|
|
|
|
public function testUnknownCalculatorReturnsEmptyObject()
|
|
{
|
|
$response = $this->postCalc('UNKNOWN_CALC', ['TBIL' => 3, 'DBIL' => 1]);
|
|
$response->assertStatus(200);
|
|
|
|
$data = $this->decodeResponse($response);
|
|
$this->assertSame([], $data);
|
|
}
|
|
|
|
private function postCalc(string $identifier, array $payload)
|
|
{
|
|
return $this->withHeaders(['Content-Type' => 'application/json'])
|
|
->withBody(json_encode($payload))
|
|
->call('post', 'api/calc/' . rawurlencode($identifier));
|
|
}
|
|
|
|
private function decodeResponse($response): array
|
|
{
|
|
$json = $response->getJSON();
|
|
if (empty($json)) {
|
|
return [];
|
|
}
|
|
|
|
return json_decode($json, true) ?: [];
|
|
}
|
|
|
|
private function generateUniqueCalcCode(): string
|
|
{
|
|
$tries = 0;
|
|
do {
|
|
$code = 'TC' . strtoupper(bin2hex(random_bytes(2)));
|
|
$exists = $this->siteModel->where('TestSiteCode', $code)
|
|
->where('EndDate IS NULL')
|
|
->first();
|
|
} while ($exists && ++$tries < 20);
|
|
|
|
return $code;
|
|
}
|
|
}
|