-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from MRizki28/test2
Test2
- Loading branch information
Showing
5 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ jobs: | |
|
||
- name: Install dependencies | ||
run: composer install | ||
|
||
- name: Run tests | ||
run: vendor/bin/phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
vendor | ||
composer.lock | ||
composer.lock | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |