You've already forked Php-Template
All checks were successful
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 2m50s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 2m41s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 3m8s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 3m22s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 3m5s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 1m41s
Reviewed-on: #11 Co-authored-by: Ron Rise <ron@siteworxpro.com> Co-committed-by: Ron Rise <ron@siteworxpro.com>
51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Siteworxpro\Tests\Http;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Siteworxpro\App\Http\JsonResponseFactory;
|
|
use Siteworxpro\HttpStatus\CodesEnum;
|
|
|
|
class JsonResponseFactoryTest extends TestCase
|
|
{
|
|
/**
|
|
* @throws \JsonException
|
|
*/
|
|
public function testCreateJsonResponseReturnsValidResponse(): void
|
|
{
|
|
$data = ['key' => 'value'];
|
|
$statusCode = CodesEnum::OK;
|
|
|
|
$response = JsonResponseFactory::createJsonResponse($data, $statusCode);
|
|
|
|
$this->assertSame($statusCode->value, $response->getStatusCode());
|
|
$this->assertSame('application/json', $response->getHeaderLine('Content-Type'));
|
|
$this->assertSame(json_encode($data), (string) $response->getBody());
|
|
}
|
|
|
|
/**
|
|
* @throws \JsonException
|
|
*/
|
|
public function testCreateJsonResponseHandlesEmptyData(): void
|
|
{
|
|
$data = [];
|
|
$statusCode = CodesEnum::NO_CONTENT;
|
|
|
|
$response = JsonResponseFactory::createJsonResponse($data, $statusCode);
|
|
|
|
$this->assertSame($statusCode->value, $response->getStatusCode());
|
|
$this->assertSame('application/json', $response->getHeaderLine('Content-Type'));
|
|
$this->assertSame(json_encode($data), (string) $response->getBody());
|
|
}
|
|
|
|
public function testCreateJsonResponseThrowsExceptionOnInvalidData(): void
|
|
{
|
|
$this->expectException(\JsonException::class);
|
|
|
|
$data = ["invalid" => "\xB1\x31"];
|
|
JsonResponseFactory::createJsonResponse($data);
|
|
}
|
|
}
|