From e067f7cb9f1cd682e2b572c87048015d7834aa56 Mon Sep 17 00:00:00 2001 From: Arif RH Date: Fri, 6 Mar 2020 20:40:30 +0700 Subject: [PATCH 1/2] Fix ResourceController Issue, including Test Unit --- system/API/ResponseTrait.php | 12 +++- system/RESTful/ResourceController.php | 6 -- .../system/RESTful/ResourceControllerTest.php | 59 ++++++++++++++++++- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/system/API/ResponseTrait.php b/system/API/ResponseTrait.php index f16b2846d55e..60c6f0b9a1b4 100644 --- a/system/API/ResponseTrait.php +++ b/system/API/ResponseTrait.php @@ -93,6 +93,12 @@ trait ResponseTrait 'not_implemented' => 501, ]; + /** + * + * @var string the representation format to return resource data in (json/xml) + */ + protected $format = 'json'; + //-------------------------------------------------------------------- /** @@ -366,8 +372,12 @@ protected function format($data = null) // Determine correct response type through content negotiation $config = new Format(); - $format = $this->request->negotiate('media', $config->supportedResponseFormats, false); + if (!in_array($this->format, ['json', 'xml'])) + $format = $this->request->negotiate('media', $config->supportedResponseFormats, false); + else + $format = "application/$this->format"; + $this->response->setContentType($format); // if we don't have a formatter, make one diff --git a/system/RESTful/ResourceController.php b/system/RESTful/ResourceController.php index 4aaec7c823b2..eabbfa6aff4a 100644 --- a/system/RESTful/ResourceController.php +++ b/system/RESTful/ResourceController.php @@ -66,12 +66,6 @@ class ResourceController extends Controller */ protected $model = null; - /** - * - * @var string the representation format to return resource data in (json/xml) - */ - protected $format = 'json'; - //-------------------------------------------------------------------- public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) diff --git a/tests/system/RESTful/ResourceControllerTest.php b/tests/system/RESTful/ResourceControllerTest.php index 2c0472e78b0e..2dccbc25491a 100644 --- a/tests/system/RESTful/ResourceControllerTest.php +++ b/tests/system/RESTful/ResourceControllerTest.php @@ -4,7 +4,6 @@ use CodeIgniter\Config\Services; use Config\App; use CodeIgniter\Test\Mock\MockCodeIgniter; - /** * Exercise our ResourceController class. * We know the resource routing works, from RouterTest, @@ -251,4 +250,62 @@ public function testFormat() $this->assertEquals('xml', $resource->getFormat()); } + //-------------------------------------------------------------------- + public function testJSONFormatOutput() + { + $resource = new \CodeIgniter\Test\Mock\MockResourceController(); + + $config = new \Config\App; + $uri = new \CodeIgniter\HTTP\URI; + $agent = new \CodeIgniter\HTTP\UserAgent; + + $request = new \CodeIgniter\HTTP\IncomingRequest($config, $uri, '', $agent); + $response = new \CodeIgniter\HTTP\Response($config); + $logger = new \Psr\Log\NullLogger; + + $resource->initController($request, $response, $logger); + $resource->setFormat('json'); + + $data = [ + 'foo' => 'bar', + ]; + + $the_response = $resource->respond($data); + $result = $the_response->getBody(); + + $JSONFormatter = new \CodeIgniter\Format\JSONFormatter; + $expected = $JSONFormatter->format($data); + + $this->assertEquals($expected, $result); + } + + //-------------------------------------------------------------------- + public function testXMLFormatOutput() + { + $resource = new \CodeIgniter\Test\Mock\MockResourceController(); + + $config = new \Config\App; + $uri = new \CodeIgniter\HTTP\URI; + $agent = new \CodeIgniter\HTTP\UserAgent; + + $request = new \CodeIgniter\HTTP\IncomingRequest($config, $uri, '', $agent); + $response = new \CodeIgniter\HTTP\Response($config); + $logger = new \Psr\Log\NullLogger; + + $resource->initController($request, $response, $logger); + $resource->setFormat('xml'); + + $data = [ + 'foo' => 'bar', + ]; + + $the_response = $resource->respond($data); + $result = $the_response->getBody(); + + $XMLFormatter = new \CodeIgniter\Format\XMLFormatter; + $expected = $XMLFormatter->format($data); + + $this->assertEquals($expected, $result); + } + } From 8d25689775f0a00434fffaaadb02e52a33b95a7e Mon Sep 17 00:00:00 2001 From: Arif RH Date: Fri, 6 Mar 2020 23:34:03 +0700 Subject: [PATCH 2/2] following style guide --- system/API/ResponseTrait.php | 10 ++++++--- .../system/RESTful/ResourceControllerTest.php | 21 ++++++++++--------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/system/API/ResponseTrait.php b/system/API/ResponseTrait.php index 60c6f0b9a1b4..395c0900e598 100644 --- a/system/API/ResponseTrait.php +++ b/system/API/ResponseTrait.php @@ -373,11 +373,15 @@ protected function format($data = null) // Determine correct response type through content negotiation $config = new Format(); - if (!in_array($this->format, ['json', 'xml'])) + if (! in_array($this->format, ['json', 'xml'])) + { $format = $this->request->negotiate('media', $config->supportedResponseFormats, false); - else + } + else + { $format = "application/$this->format"; - + } + $this->response->setContentType($format); // if we don't have a formatter, make one diff --git a/tests/system/RESTful/ResourceControllerTest.php b/tests/system/RESTful/ResourceControllerTest.php index 2dccbc25491a..4636069ef43d 100644 --- a/tests/system/RESTful/ResourceControllerTest.php +++ b/tests/system/RESTful/ResourceControllerTest.php @@ -4,6 +4,7 @@ use CodeIgniter\Config\Services; use Config\App; use CodeIgniter\Test\Mock\MockCodeIgniter; + /** * Exercise our ResourceController class. * We know the resource routing works, from RouterTest, @@ -254,14 +255,14 @@ public function testFormat() public function testJSONFormatOutput() { $resource = new \CodeIgniter\Test\Mock\MockResourceController(); - + $config = new \Config\App; $uri = new \CodeIgniter\HTTP\URI; $agent = new \CodeIgniter\HTTP\UserAgent; - $request = new \CodeIgniter\HTTP\IncomingRequest($config, $uri, '', $agent); + $request = new \CodeIgniter\HTTP\IncomingRequest($config, $uri, '', $agent); $response = new \CodeIgniter\HTTP\Response($config); - $logger = new \Psr\Log\NullLogger; + $logger = new \Psr\Log\NullLogger; $resource->initController($request, $response, $logger); $resource->setFormat('json'); @@ -271,10 +272,10 @@ public function testJSONFormatOutput() ]; $the_response = $resource->respond($data); - $result = $the_response->getBody(); + $result = $the_response->getBody(); $JSONFormatter = new \CodeIgniter\Format\JSONFormatter; - $expected = $JSONFormatter->format($data); + $expected = $JSONFormatter->format($data); $this->assertEquals($expected, $result); } @@ -283,14 +284,14 @@ public function testJSONFormatOutput() public function testXMLFormatOutput() { $resource = new \CodeIgniter\Test\Mock\MockResourceController(); - + $config = new \Config\App; $uri = new \CodeIgniter\HTTP\URI; $agent = new \CodeIgniter\HTTP\UserAgent; - $request = new \CodeIgniter\HTTP\IncomingRequest($config, $uri, '', $agent); + $request = new \CodeIgniter\HTTP\IncomingRequest($config, $uri, '', $agent); $response = new \CodeIgniter\HTTP\Response($config); - $logger = new \Psr\Log\NullLogger; + $logger = new \Psr\Log\NullLogger; $resource->initController($request, $response, $logger); $resource->setFormat('xml'); @@ -300,10 +301,10 @@ public function testXMLFormatOutput() ]; $the_response = $resource->respond($data); - $result = $the_response->getBody(); + $result = $the_response->getBody(); $XMLFormatter = new \CodeIgniter\Format\XMLFormatter; - $expected = $XMLFormatter->format($data); + $expected = $XMLFormatter->format($data); $this->assertEquals($expected, $result); }