210 lines
7.8 KiB
PHP
210 lines
7.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Unit\Http\Controllers\Api\V1;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Api\V1\BaseController;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class BaseControllerTest extends TestCase
|
||
|
|
{
|
||
|
|
protected BaseController $controller;
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
parent::setUp();
|
||
|
|
$this->controller = new BaseController();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_send_response_returns_success_json_response(): void
|
||
|
|
{
|
||
|
|
$data = ['test' => 'data'];
|
||
|
|
$message = 'Test message';
|
||
|
|
$code = 200;
|
||
|
|
|
||
|
|
$response = $this->controller->sendResponse($data, $message, $code);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$this->assertEquals($code, $response->getStatusCode());
|
||
|
|
|
||
|
|
$responseData = json_decode($response->getContent(), true);
|
||
|
|
$this->assertTrue($responseData['success']);
|
||
|
|
$this->assertEquals($data, $responseData['data']);
|
||
|
|
$this->assertEquals($message, $responseData['message']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_send_response_with_default_parameters(): void
|
||
|
|
{
|
||
|
|
$data = ['test' => 'data'];
|
||
|
|
|
||
|
|
$response = $this->controller->sendResponse($data);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$this->assertEquals(200, $response->getStatusCode());
|
||
|
|
|
||
|
|
$responseData = json_decode($response->getContent(), true);
|
||
|
|
$this->assertTrue($responseData['success']);
|
||
|
|
$this->assertEquals($data, $responseData['data']);
|
||
|
|
$this->assertEquals('Success', $responseData['message']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_send_response_with_null_data(): void
|
||
|
|
{
|
||
|
|
$response = $this->controller->sendResponse(null, 'Test message');
|
||
|
|
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$this->assertEquals(200, $response->getStatusCode());
|
||
|
|
|
||
|
|
$responseData = json_decode($response->getContent(), true);
|
||
|
|
$this->assertTrue($responseData['success']);
|
||
|
|
$this->assertNull($responseData['data']);
|
||
|
|
$this->assertEquals('Test message', $responseData['message']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_send_error_returns_error_json_response(): void
|
||
|
|
{
|
||
|
|
$error = 'Test error';
|
||
|
|
$errorMessages = ['field' => ['error message']];
|
||
|
|
$code = 400;
|
||
|
|
|
||
|
|
$response = $this->controller->sendError($error, $errorMessages, $code);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$this->assertEquals($code, $response->getStatusCode());
|
||
|
|
|
||
|
|
$responseData = json_decode($response->getContent(), true);
|
||
|
|
$this->assertFalse($responseData['success']);
|
||
|
|
$this->assertEquals($error, $responseData['message']);
|
||
|
|
$this->assertEquals($errorMessages, $responseData['errors']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_send_error_without_error_messages(): void
|
||
|
|
{
|
||
|
|
$error = 'Test error';
|
||
|
|
|
||
|
|
$response = $this->controller->sendError($error);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$this->assertEquals(400, $response->getStatusCode());
|
||
|
|
|
||
|
|
$responseData = json_decode($response->getContent(), true);
|
||
|
|
$this->assertFalse($responseData['success']);
|
||
|
|
$this->assertEquals($error, $responseData['message']);
|
||
|
|
$this->assertArrayNotHasKey('errors', $responseData);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_send_error_with_empty_error_messages(): void
|
||
|
|
{
|
||
|
|
$error = 'Test error';
|
||
|
|
$errorMessages = [];
|
||
|
|
|
||
|
|
$response = $this->controller->sendError($error, $errorMessages);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$this->assertEquals(400, $response->getStatusCode());
|
||
|
|
|
||
|
|
$responseData = json_decode($response->getContent(), true);
|
||
|
|
$this->assertFalse($responseData['success']);
|
||
|
|
$this->assertEquals($error, $responseData['message']);
|
||
|
|
$this->assertArrayNotHasKey('errors', $responseData);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_send_validation_error_returns_422_status(): void
|
||
|
|
{
|
||
|
|
$errors = [
|
||
|
|
'email' => ['Email is required'],
|
||
|
|
'password' => ['Password must be at least 8 characters']
|
||
|
|
];
|
||
|
|
|
||
|
|
$response = $this->controller->sendValidationError($errors);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$this->assertEquals(422, $response->getStatusCode());
|
||
|
|
|
||
|
|
$responseData = json_decode($response->getContent(), true);
|
||
|
|
$this->assertFalse($responseData['success']);
|
||
|
|
$this->assertEquals('Validation failed', $responseData['message']);
|
||
|
|
$this->assertEquals($errors, $responseData['errors']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_send_not_found_returns_404_status(): void
|
||
|
|
{
|
||
|
|
$message = 'Custom not found message';
|
||
|
|
|
||
|
|
$response = $this->controller->sendNotFound($message);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$this->assertEquals(404, $response->getStatusCode());
|
||
|
|
|
||
|
|
$responseData = json_decode($response->getContent(), true);
|
||
|
|
$this->assertFalse($responseData['success']);
|
||
|
|
$this->assertEquals($message, $responseData['message']);
|
||
|
|
$this->assertArrayNotHasKey('errors', $responseData);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_send_not_found_with_default_message(): void
|
||
|
|
{
|
||
|
|
$response = $this->controller->sendNotFound();
|
||
|
|
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$this->assertEquals(404, $response->getStatusCode());
|
||
|
|
|
||
|
|
$responseData = json_decode($response->getContent(), true);
|
||
|
|
$this->assertFalse($responseData['success']);
|
||
|
|
$this->assertEquals('Resource not found', $responseData['message']);
|
||
|
|
$this->assertArrayNotHasKey('errors', $responseData);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_send_unauthorized_returns_401_status(): void
|
||
|
|
{
|
||
|
|
$message = 'Custom unauthorized message';
|
||
|
|
|
||
|
|
$response = $this->controller->sendUnauthorized($message);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$this->assertEquals(401, $response->getStatusCode());
|
||
|
|
|
||
|
|
$responseData = json_decode($response->getContent(), true);
|
||
|
|
$this->assertFalse($responseData['success']);
|
||
|
|
$this->assertEquals($message, $responseData['message']);
|
||
|
|
$this->assertArrayNotHasKey('errors', $responseData);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_send_unauthorized_with_default_message(): void
|
||
|
|
{
|
||
|
|
$response = $this->controller->sendUnauthorized();
|
||
|
|
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$this->assertEquals(401, $response->getStatusCode());
|
||
|
|
|
||
|
|
$responseData = json_decode($response->getContent(), true);
|
||
|
|
$this->assertFalse($responseData['success']);
|
||
|
|
$this->assertEquals('Unauthorized', $responseData['message']);
|
||
|
|
$this->assertArrayNotHasKey('errors', $responseData);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_response_structure_consistency(): void
|
||
|
|
{
|
||
|
|
// Test that all response methods return consistent JSON structure
|
||
|
|
$successResponse = $this->controller->sendResponse(['data'], 'Success');
|
||
|
|
$errorResponse = $this->controller->sendError('Error');
|
||
|
|
$validationResponse = $this->controller->sendValidationError(['field' => ['error']]);
|
||
|
|
$notFoundResponse = $this->controller->sendNotFound();
|
||
|
|
$unauthorizedResponse = $this->controller->sendUnauthorized();
|
||
|
|
|
||
|
|
$responses = [
|
||
|
|
json_decode($successResponse->getContent(), true),
|
||
|
|
json_decode($errorResponse->getContent(), true),
|
||
|
|
json_decode($validationResponse->getContent(), true),
|
||
|
|
json_decode($notFoundResponse->getContent(), true),
|
||
|
|
json_decode($unauthorizedResponse->getContent(), true),
|
||
|
|
];
|
||
|
|
|
||
|
|
foreach ($responses as $response) {
|
||
|
|
$this->assertArrayHasKey('success', $response);
|
||
|
|
$this->assertArrayHasKey('message', $response);
|
||
|
|
$this->assertIsBool($response['success']);
|
||
|
|
$this->assertIsString($response['message']);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|