From 997782d58c559b4d7813d07677cd8d453a2622b9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 9 Apr 2020 16:13:54 +0700 Subject: [PATCH] add respondUpdated() method into API\ResponseTrait --- system/API/ResponseTrait.php | 14 ++++++++++++++ tests/system/API/ResponseTraitTest.php | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/system/API/ResponseTrait.php b/system/API/ResponseTrait.php index 395c0900e598..879c2aa993f5 100644 --- a/system/API/ResponseTrait.php +++ b/system/API/ResponseTrait.php @@ -66,6 +66,7 @@ trait ResponseTrait protected $codes = [ 'created' => 201, 'deleted' => 200, + 'updated' => 200, 'no_content' => 204, 'invalid_request' => 400, 'unsupported_response_type' => 400, @@ -196,6 +197,19 @@ public function respondDeleted($data = null, string $message = '') return $this->respond($data, $this->codes['deleted'], $message); } + /** + * Used after a resource has been successfully updated. + * + * @param mixed $data Data. + * @param string $message Message. + * + * @return mixed + */ + public function respondUpdated($data = null, string $message = '') + { + return $this->respond($data, $this->codes['updated'], $message); + } + //-------------------------------------------------------------------- /** diff --git a/tests/system/API/ResponseTraitTest.php b/tests/system/API/ResponseTraitTest.php index c081caf17481..beb905960091 100644 --- a/tests/system/API/ResponseTraitTest.php +++ b/tests/system/API/ResponseTraitTest.php @@ -242,6 +242,16 @@ public function testDeleted() $this->assertEquals($this->formatter->format(['id' => 3]), $this->response->getBody()); } + public function testUpdated() + { + $controller = $this->makeController(); + $controller->respondUpdated(['id' => 3], 'A Custom Reason'); + + $this->assertEquals('A Custom Reason', $this->response->getReason()); + $this->assertEquals(200, $this->response->getStatusCode()); + $this->assertEquals($this->formatter->format(['id' => 3]), $this->response->getBody()); + } + public function testUnauthorized() { $controller = $this->makeController();