Skip to content

Commit

Permalink
Merge pull request #6 from MRizki28/test2
Browse files Browse the repository at this point in the history
Test2
  • Loading branch information
MRizki28 authored Aug 9, 2024
2 parents d05f9aa + 2bbe55c commit 18be473
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ jobs:

- name: Install dependencies
run: composer install

- name: Run tests
run: vendor/bin/phpunit
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
composer.lock
composer.lock
.phpunit.result.cache
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@
"MRizki28\\ApiResponse\\": "src/ApiResponse"
}
},
"autoload-dev": {
"psr-4": {
"MRizki28\\ApiResponse\\Tests\\": "tests"
}
},
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"php": "^8.2",
"symfony/http-foundation": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^11.3"
}
}
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
70 changes: 70 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace MRizki28\ApiResponse\Tests;

use MRizki28\ApiResponse\ApiResponse;
use PHPUnit\Framework\TestCase;

class ResponseTest extends TestCase
{
protected $apiResponse;

public function setUp(): void
{
$this->apiResponse = new ApiResponse();
}

public function test_success_response()
{
$response = $this->apiResponse->success([
'name' => 'Muhammad Rizki'
], 'Success', 200);

$this->assertJsonStringEqualsJsonString(
json_encode([
'status' => 'success',
'message' => 'Success',
'data' => ['name' => 'Muhammad Rizki']
]),
$response->getContent()
);

$this->assertEquals(200, $response->getStatusCode());
}

public function test_noFound_response()
{
$response = $this->apiResponse->notFound('Data not found', 404);

$this->assertJsonStringEqualsJsonString(
json_encode([
'status' => 'Data not found',
'message' => 'Data not found'
]),
$response->getContent()
);

$this->assertEquals(404, $response->getStatusCode());
}

public function test_error_response(){
$th = new \Exception('Error here');
$response = $this->apiResponse->error($th , 'Error', 500);

$this->assertJsonStringEqualsJsonString(
json_encode([
'status' => 'error',
'message' => 'Error',
'error' => [
'message' => 'Error here',
'file' => $th->getFile(),
'line' => $th->getLine(),
'trace' => $th->getTrace()
]
]),
$response->getContent()
);

$this->assertEquals(500, $response->getStatusCode());
}
}

0 comments on commit 18be473

Please sign in to comment.