2026-03-16 07:24:50 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\Test\FeatureTestTrait;
|
|
|
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
|
|
|
|
use Firebase\JWT\JWT;
|
|
|
|
|
|
|
|
|
|
class ContactControllerTest extends CIUnitTestCase
|
|
|
|
|
{
|
|
|
|
|
use FeatureTestTrait;
|
|
|
|
|
|
|
|
|
|
protected $token;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
// Generate JWT Token
|
|
|
|
|
$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');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function callProtected($method, $path, $params = [])
|
|
|
|
|
{
|
|
|
|
|
return $this->withHeaders(['Cookie' => 'token=' . $this->token])
|
|
|
|
|
->call($method, $path, $params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIndexReturnsSuccess()
|
|
|
|
|
{
|
|
|
|
|
$result = $this->callProtected('get', 'api/contact');
|
|
|
|
|
|
|
|
|
|
$result->assertStatus(200);
|
|
|
|
|
$json = $result->getJSON();
|
|
|
|
|
$data = json_decode($json, true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('success', $data['status']);
|
|
|
|
|
$this->assertIsArray($data['data']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testShowReturnsDataIfFound()
|
|
|
|
|
{
|
|
|
|
|
$indexResult = $this->callProtected('get', 'api/contact');
|
|
|
|
|
$indexData = json_decode($indexResult->getJSON(), true);
|
|
|
|
|
|
|
|
|
|
if (empty($indexData['data'])) {
|
|
|
|
|
$this->markTestSkipped('No contacts found in database to test show.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$id = $indexData['data'][0]['ContactID'];
|
|
|
|
|
$result = $this->callProtected('get', "api/contact/$id");
|
|
|
|
|
|
|
|
|
|
$result->assertStatus(200);
|
|
|
|
|
$json = $result->getJSON();
|
|
|
|
|
$data = json_decode($json, true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('success', $data['status']);
|
|
|
|
|
$this->assertIsArray($data['data']);
|
|
|
|
|
$this->assertEquals($id, $data['data']['ContactID']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCreateContact()
|
|
|
|
|
{
|
|
|
|
|
$contactData = [
|
|
|
|
|
'NameFirst' => 'TestContact' . time(),
|
|
|
|
|
'NameLast' => 'LastName',
|
|
|
|
|
'Specialty' => 'GP',
|
|
|
|
|
'Occupation' => 'MD'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$result = $this->withHeaders(['Cookie' => 'token=' . $this->token])
|
|
|
|
|
->withBody(json_encode($contactData))
|
|
|
|
|
->call('post', 'api/contact');
|
|
|
|
|
|
|
|
|
|
$result->assertStatus(201);
|
|
|
|
|
$json = $result->getJSON();
|
|
|
|
|
$data = json_decode($json, true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('success', $data['status']);
|
|
|
|
|
$this->assertIsArray($data['data']);
|
|
|
|
|
$this->assertEquals('success', $data['data']['status']);
|
|
|
|
|
$this->assertIsInt($data['data']['ContactID']);
|
|
|
|
|
}
|
|
|
|
|
}
|