From 0b83155b3f031c2dcb6e38988b6bc7ed1d143502 Mon Sep 17 00:00:00 2001 From: MRizki28 Date: Fri, 9 Aug 2024 17:15:19 +0800 Subject: [PATCH 1/5] add unit test --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 88e99d5..708b377 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor -composer.lock \ No newline at end of file +composer.lock +.phpunit.result.cache \ No newline at end of file From 47dda7b8a7bc54862fb364255bdf5ae06ca42251 Mon Sep 17 00:00:00 2001 From: MRizki28 Date: Fri, 9 Aug 2024 17:15:28 +0800 Subject: [PATCH 2/5] add unit test --- composer.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/composer.json b/composer.json index 13a2046..648c718 100644 --- a/composer.json +++ b/composer.json @@ -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" } } From d75c206e9fc5fff78ba8cebd3b3985695ac4d487 Mon Sep 17 00:00:00 2001 From: MRizki28 Date: Fri, 9 Aug 2024 17:15:33 +0800 Subject: [PATCH 3/5] add unit test --- phpunit.xml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 phpunit.xml diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..6810753 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,8 @@ + + + + + tests + + + \ No newline at end of file From ea697f796222a7401d4f84e1d07cdd57010cfb9a Mon Sep 17 00:00:00 2001 From: MRizki28 Date: Fri, 9 Aug 2024 17:15:39 +0800 Subject: [PATCH 4/5] add unit test --- .github/workflows/test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 93fbab9..cb65e33 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,3 +23,6 @@ jobs: - name: Install dependencies run: composer install + + - name: Run tests + run: vendor/bin/phpunit From 2bbe55c4b990b206e839d5be0ebde6149b4f6f93 Mon Sep 17 00:00:00 2001 From: MRizki28 Date: Fri, 9 Aug 2024 17:15:44 +0800 Subject: [PATCH 5/5] add unit test --- tests/ResponseTest.php | 70 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/ResponseTest.php diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php new file mode 100644 index 0000000..2cb05ab --- /dev/null +++ b/tests/ResponseTest.php @@ -0,0 +1,70 @@ +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()); + } +}