From 3f85503d976183d2bd07a67a86837f0bda26428f Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Mon, 22 Jun 2020 19:37:57 +0200 Subject: [PATCH 01/16] Removing getFormatter inside a new class --- app/Config/Format.php | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/app/Config/Format.php b/app/Config/Format.php index 5f64b702f424..5c3811058e45 100644 --- a/app/Config/Format.php +++ b/app/Config/Format.php @@ -1,4 +1,6 @@ - \CodeIgniter\Format\XMLFormatter::class, 'text/xml' => \CodeIgniter\Format\XMLFormatter::class, ]; - - //-------------------------------------------------------------------- - - /** - * A Factory method to return the appropriate formatter for the given mime type. - * - * @param string $mime - * - * @return \CodeIgniter\Format\FormatterInterface - */ - public function getFormatter(string $mime) - { - if (! array_key_exists($mime, $this->formatters)) - { - throw new \InvalidArgumentException('No Formatter defined for mime type: ' . $mime); - } - - $class = $this->formatters[$mime]; - - if (! class_exists($class)) - { - throw new \BadMethodCallException($class . ' is not a valid Formatter.'); - } - - return new $class(); - } - - //-------------------------------------------------------------------- - } From 7fe8f96c1b50be7b3b4343fe9c5df04cdd02ae56 Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Mon, 22 Jun 2020 19:43:02 +0200 Subject: [PATCH 02/16] new Format Class creating new class Format to handle getFormatter method --- system/Format/Format.php | 95 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 system/Format/Format.php diff --git a/system/Format/Format.php b/system/Format/Format.php new file mode 100644 index 000000000000..93f876b88b00 --- /dev/null +++ b/system/Format/Format.php @@ -0,0 +1,95 @@ +config = $config; + } + + //-------------------------------------------------------------------- + + /** + * A Factory method to return the appropriate formatter for the given mime type. + * + * @param string $mime + * + * @return CodeIgniter\Format\FormatterInterface + */ + public function getFormatter(string $mime): FormatterInterface + { + if (! array_key_exists($mime, $this->config->formatters)) + { + throw FormatException::forInvalidMime($mime); + } + + $class = $this->config->formatters[$mime]; + + if (! class_exists($class)) + { + throw FormatException::forInvalidFormatter($class); + } + + return new $class(); + } +} From 44472e54e3d30ba0480b7176e0714cad1bcfb334 Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Mon, 22 Jun 2020 19:45:37 +0200 Subject: [PATCH 03/16] New Exception methods new exception methods forInvalidFormatter & forInvalidMime using in getFormatter --- system/Format/Exceptions/FormatException.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/system/Format/Exceptions/FormatException.php b/system/Format/Exceptions/FormatException.php index 4529fd0fafa0..523c8346de85 100644 --- a/system/Format/Exceptions/FormatException.php +++ b/system/Format/Exceptions/FormatException.php @@ -1,14 +1,27 @@ - Date: Mon, 22 Jun 2020 19:47:19 +0200 Subject: [PATCH 04/16] added two translations adding invalidFormatter & invalidMime translation using in FormatException --- system/Language/en/Format.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/system/Language/en/Format.php b/system/Language/en/Format.php index c0070c8534b5..45afd33066e0 100644 --- a/system/Language/en/Format.php +++ b/system/Language/en/Format.php @@ -15,6 +15,8 @@ */ return [ + 'invalidFormatter' => '"{0}" is not a valid Formatter.', 'invalidJSON' => 'Failed to parse json string, error: "{0}".', + 'invalidMime' => 'No Formatter defined for mime type: "{0}".', 'missingExtension' => 'The SimpleXML extension is required to format XML.', ]; From 1a95f9fa31298954004526cbc7c5fba2232fb61b Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Mon, 22 Jun 2020 19:52:00 +0200 Subject: [PATCH 05/16] respect new Format Class adding some changes to current format codes to be compatible with \CodeIgniter\Format --- system/HTTP/Response.php | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/system/HTTP/Response.php b/system/HTTP/Response.php index e0127321bc3a..168241b26f60 100644 --- a/system/HTTP/Response.php +++ b/system/HTTP/Response.php @@ -211,6 +211,13 @@ class Response extends Message implements ResponseInterface */ protected $cookies = []; + /** + * Instance of Format class + * + * @var \CodeIgniter\Format\Format + */ + protected $format; + /** * If true, will not write output. Useful during testing. * @@ -249,6 +256,8 @@ public function __construct($config) $this->cookieSecure = $config->cookieSecure; $this->cookieHTTPOnly = $config->cookieHTTPOnly; + $this->format = new Format(new \Config\Format()); + // Default to an HTML Content-Type. Devs can override if needed. $this->setContentType('text/html'); } @@ -467,13 +476,7 @@ public function getJSON() if ($this->bodyFormat !== 'json') { - /** - * @var Format $config - */ - $config = config(Format::class); - $formatter = $config->getFormatter('application/json'); - - $body = $formatter->format($body); + $body = $this->format->getFormatter('application/json')->format($body); } return $body ?: null; @@ -509,13 +512,7 @@ public function getXML() if ($this->bodyFormat !== 'xml') { - /** - * @var Format $config - */ - $config = config(Format::class); - $formatter = $config->getFormatter('application/xml'); - - $body = $formatter->format($body); + $body = $this->format->getFormatter('application/xml')->format($body); } return $body; @@ -542,13 +539,7 @@ protected function formatBody($body, string $format) // Nothing much to do for a string... if (! is_string($body) || $format === 'json-unencoded') { - /** - * @var Format $config - */ - $config = config(Format::class); - $formatter = $config->getFormatter($mime); - - $body = $formatter->format($body); + $body = $this->format->getFormatter($mime)->format($body); } return $body; From affcecba29b6518f42c97de2aeaefef9e122c6ef Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Mon, 22 Jun 2020 19:55:01 +0200 Subject: [PATCH 06/16] respect new Format class adding some changes to current format codes to be compatible with \CodeIgniter\Format --- system/Test/FeatureResponse.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/system/Test/FeatureResponse.php b/system/Test/FeatureResponse.php index 6c919b7638eb..3fab78e9b123 100644 --- a/system/Test/FeatureResponse.php +++ b/system/Test/FeatureResponse.php @@ -414,9 +414,8 @@ public function assertJSONExact($test) if (is_array($test)) { - $config = new Format(); - $formatter = $config->getFormatter('application/json'); - $test = $formatter->format($test); + $format = new Format(new \Config\Format()); + $test = $format->getFormatter('application/json')->format($test); } $this->assertJsonStringEqualsJsonString($test, $json, 'Response does not contain matching JSON.'); From df4e1c42dcdba715fe3f879e8a7a4fa15e95e0b1 Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Mon, 22 Jun 2020 20:02:34 +0200 Subject: [PATCH 07/16] adding new service adding format as instance of factory for Formatters --- system/Config/Services.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/system/Config/Services.php b/system/Config/Services.php index e7e2aeb93ce3..4862270d1082 100644 --- a/system/Config/Services.php +++ b/system/Config/Services.php @@ -48,6 +48,7 @@ use CodeIgniter\Encryption\EncrypterInterface; use CodeIgniter\Encryption\Encryption; use CodeIgniter\Filters\Filters; +use CodeIgniter\Format\Format; use CodeIgniter\Honeypot\Honeypot; use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\CURLRequest; @@ -310,6 +311,31 @@ public static function filters($config = null, bool $getShared = true) //-------------------------------------------------------------------- + /** + * The Format class works as a factory for formatters. + * + * @param mixed $config + * @param boolean $getShared + * + * @return \CodeIgniter\Format\Format + */ + public static function format($config = null, bool $getShared = true) + { + if ($getShared) + { + return static::getSharedInstance('format', $config); + } + + if (is_null($config)) + { + $config = new \Config\Format(); + } + + return new Format($config); + } + + //-------------------------------------------------------------------- + /** * The Honeypot provides a secret input on forms that bots should NOT * fill in, providing an additional safeguard when accepting user input. From fae6f90d8309cb574ddac7df4f0457791d1caf08 Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Mon, 22 Jun 2020 20:09:46 +0200 Subject: [PATCH 08/16] using format() as service --- system/Test/FeatureResponse.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Test/FeatureResponse.php b/system/Test/FeatureResponse.php index 3fab78e9b123..2c79e7d0eeeb 100644 --- a/system/Test/FeatureResponse.php +++ b/system/Test/FeatureResponse.php @@ -40,7 +40,7 @@ use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\ResponseInterface; -use Config\Format; +use Config\Services; use PHPUnit\Framework\TestCase; /** @@ -414,7 +414,7 @@ public function assertJSONExact($test) if (is_array($test)) { - $format = new Format(new \Config\Format()); + $format = Services::format(); $test = $format->getFormatter('application/json')->format($test); } From d10f28d2ba781a7fc0837519641f68cbf715526d Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Mon, 22 Jun 2020 22:13:40 +0200 Subject: [PATCH 09/16] Update Response.php --- system/HTTP/Response.php | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/system/HTTP/Response.php b/system/HTTP/Response.php index 168241b26f60..dac9947f723b 100644 --- a/system/HTTP/Response.php +++ b/system/HTTP/Response.php @@ -43,7 +43,7 @@ use CodeIgniter\HTTP\Exceptions\HTTPException; use CodeIgniter\Pager\PagerInterface; use Config\App; -use Config\Format; +use Config\Services; /** * Representation of an outgoing, getServer-side response. @@ -211,13 +211,6 @@ class Response extends Message implements ResponseInterface */ protected $cookies = []; - /** - * Instance of Format class - * - * @var \CodeIgniter\Format\Format - */ - protected $format; - /** * If true, will not write output. Useful during testing. * @@ -256,8 +249,6 @@ public function __construct($config) $this->cookieSecure = $config->cookieSecure; $this->cookieHTTPOnly = $config->cookieHTTPOnly; - $this->format = new Format(new \Config\Format()); - // Default to an HTML Content-Type. Devs can override if needed. $this->setContentType('text/html'); } @@ -476,7 +467,8 @@ public function getJSON() if ($this->bodyFormat !== 'json') { - $body = $this->format->getFormatter('application/json')->format($body); + $format = Services::format(); + $body = $format->getFormatter('application/json')->format($body); } return $body ?: null; @@ -512,7 +504,8 @@ public function getXML() if ($this->bodyFormat !== 'xml') { - $body = $this->format->getFormatter('application/xml')->format($body); + $format = Services::format(); + $body = $format->getFormatter('application/xml')->format($body); } return $body; @@ -539,7 +532,8 @@ protected function formatBody($body, string $format) // Nothing much to do for a string... if (! is_string($body) || $format === 'json-unencoded') { - $body = $this->format->getFormatter($mime)->format($body); + $format = Services::format(); + $body = $format->getFormatter($mime)->format($body); } return $body; From 15a75f64914fff426a8e9d66c55f96af4e14e017 Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Mon, 22 Jun 2020 22:35:26 +0200 Subject: [PATCH 10/16] respect new Format Class adding some changes to current format codes to be compatible with \CodeIgniter\Format removing negotiate() methord 3rd argument its false by default removing useless property $this->formatter --- system/API/ResponseTrait.php | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/system/API/ResponseTrait.php b/system/API/ResponseTrait.php index 49248eb198ed..c268c48c88a3 100644 --- a/system/API/ResponseTrait.php +++ b/system/API/ResponseTrait.php @@ -41,6 +41,7 @@ use CodeIgniter\HTTP\Response; use Config\Format; +use Config\Services; /** * Response trait. @@ -386,32 +387,27 @@ protected function format($data = null) return $data; } - $config = new Format(); - $format = "application/$this->format"; + $mime = "application/$this->format"; // Determine correct response type through content negotiation if not explicitly declared if (empty($this->format) || ! in_array($this->format, ['json', 'xml'])) { - $format = $this->request->negotiate('media', $config->supportedResponseFormats, false); + $config = new Format(); + $mime = $this->request->negotiate('media', $config->supportedResponseFormats); } - $this->response->setContentType($format); + $this->response->setContentType($mime); - // if we don't have a formatter, make one - if (! isset($this->formatter)) - { - // if no formatter, use the default - $this->formatter = $config->getFormatter($format); - } - - if ($format !== 'application/json') + if ($mime !== 'application/json') { // Recursively convert objects into associative arrays // Conversion not required for JSONFormatter $data = json_decode(json_encode($data), true); } + + $format = Services::format(); - return $this->formatter->format($data); + return $format->getFormatter($mime)->format($data); } /** From 0ac8edee1b44db30ca1677fc8691a34705689476 Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Tue, 23 Jun 2020 18:07:54 +0200 Subject: [PATCH 11/16] adding getConfig method but shall I use (! empty($this->config) or just ! instanceof as I did --- system/Format/Format.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/system/Format/Format.php b/system/Format/Format.php index 93f876b88b00..ee137b96ce91 100644 --- a/system/Format/Format.php +++ b/system/Format/Format.php @@ -92,4 +92,19 @@ public function getFormatter(string $mime): FormatterInterface return new $class(); } + + /** + * Get instance of format configuration class + * + * @return \Config\Format + */ + public function getConfig(): object + { + if (! $this->config instanceof \Config\Format) + { + return $this->config; + } + + return new \Config\Format(); + } } From 2f35d65341b1dc685d3c76326d0d0b4c1b7ab9e3 Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Tue, 23 Jun 2020 18:23:53 +0200 Subject: [PATCH 12/16] improvements revert some changes and improve code style --- system/API/ResponseTrait.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/system/API/ResponseTrait.php b/system/API/ResponseTrait.php index c268c48c88a3..b57e899f62e4 100644 --- a/system/API/ResponseTrait.php +++ b/system/API/ResponseTrait.php @@ -40,7 +40,6 @@ namespace CodeIgniter\API; use CodeIgniter\HTTP\Response; -use Config\Format; use Config\Services; /** @@ -135,8 +134,7 @@ public function respond($data = null, int $status = null, string $message = '') $output = $this->format($data); } - return $this->response->setBody($output) - ->setStatusCode($status, $message); + return $this->response->setBody($output)->setStatusCode($status, $message); } //-------------------------------------------------------------------- @@ -388,12 +386,13 @@ protected function format($data = null) } $mime = "application/$this->format"; + + $format = Services::format(); // Determine correct response type through content negotiation if not explicitly declared if (empty($this->format) || ! in_array($this->format, ['json', 'xml'])) { - $config = new Format(); - $mime = $this->request->negotiate('media', $config->supportedResponseFormats); + $mime = $this->request->negotiate('media', $format->getConfig()->supportedResponseFormats); } $this->response->setContentType($mime); @@ -405,9 +404,14 @@ protected function format($data = null) $data = json_decode(json_encode($data), true); } - $format = Services::format(); + // if we don't have a formatter, make one + if (! isset($this->formatter)) + { + // if no formatter, use the default + $this->formatter = $format->getFormatter($mime); + } - return $format->getFormatter($mime)->format($data); + return $this->formatter->format($data); } /** From f513df002828c57356a11aa4c24a59308bece774 Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Tue, 23 Jun 2020 18:30:45 +0200 Subject: [PATCH 13/16] Reducing lines One line code --- system/HTTP/Response.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/system/HTTP/Response.php b/system/HTTP/Response.php index dac9947f723b..9a09b7970b2b 100644 --- a/system/HTTP/Response.php +++ b/system/HTTP/Response.php @@ -467,8 +467,7 @@ public function getJSON() if ($this->bodyFormat !== 'json') { - $format = Services::format(); - $body = $format->getFormatter('application/json')->format($body); + $body = Services::format()->getFormatter('application/json')->format($body); } return $body ?: null; @@ -504,8 +503,7 @@ public function getXML() if ($this->bodyFormat !== 'xml') { - $format = Services::format(); - $body = $format->getFormatter('application/xml')->format($body); + $body = Services::format()->getFormatter('application/xml')->format($body); } return $body; @@ -532,8 +530,7 @@ protected function formatBody($body, string $format) // Nothing much to do for a string... if (! is_string($body) || $format === 'json-unencoded') { - $format = Services::format(); - $body = $format->getFormatter($mime)->format($body); + $body = Services::format()->getFormatter($mime)->format($body); } return $body; From dc9bf9aa30b6b4655a7f9a3a364688967457b7ef Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Tue, 23 Jun 2020 18:46:38 +0200 Subject: [PATCH 14/16] use format() service --- tests/system/HTTP/ResponseTest.php | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/tests/system/HTTP/ResponseTest.php b/tests/system/HTTP/ResponseTest.php index b6a73e51ee76..5f68de82c800 100644 --- a/tests/system/HTTP/ResponseTest.php +++ b/tests/system/HTTP/ResponseTest.php @@ -5,7 +5,7 @@ use CodeIgniter\HTTP\Exceptions\HTTPException; use CodeIgniter\Test\Mock\MockResponse; use Config\App; -use Config\Format; +use Config\Services; use DateTime; use DateTimeZone; @@ -341,8 +341,6 @@ public function testSetCookieSuccessOnPrefix() public function testJSONWithArray() { $response = new Response(new App()); - $config = new Format(); - $formatter = $config->getFormatter('application/json'); $body = [ 'foo' => 'bar', @@ -352,19 +350,16 @@ public function testJSONWithArray() 3, ], ]; - $expected = $formatter->format($body); $response->setJSON($body); - $this->assertEquals($expected, $response->getJSON()); + $this->assertEquals(Services::format()->getFormatter('application/json')->format($body), $response->getJSON()); $this->assertTrue(strpos($response->getHeaderLine('content-type'), 'application/json') !== false); } public function testJSONGetFromNormalBody() { $response = new Response(new App()); - $config = new Format(); - $formatter = $config->getFormatter('application/json'); $body = [ 'foo' => 'bar', @@ -374,11 +369,10 @@ public function testJSONGetFromNormalBody() 3, ], ]; - $expected = $formatter->format($body); $response->setBody($body); - $this->assertEquals($expected, $response->getJSON()); + $this->assertEquals(Services::format()->getFormatter('application/json')->format($body), $response->getJSON()); } //-------------------------------------------------------------------- @@ -386,8 +380,6 @@ public function testJSONGetFromNormalBody() public function testXMLWithArray() { $response = new Response(new App()); - $config = new Format(); - $formatter = $config->getFormatter('application/xml'); $body = [ 'foo' => 'bar', @@ -397,19 +389,16 @@ public function testXMLWithArray() 3, ], ]; - $expected = $formatter->format($body); $response->setXML($body); - $this->assertEquals($expected, $response->getXML()); + $this->assertEquals(Services::format()->getFormatter('application/xml')->format($body), $response->getXML()); $this->assertTrue(strpos($response->getHeaderLine('content-type'), 'application/xml') !== false); } public function testXMLGetFromNormalBody() { $response = new Response(new App()); - $config = new Format(); - $formatter = $config->getFormatter('application/xml'); $body = [ 'foo' => 'bar', @@ -419,11 +408,10 @@ public function testXMLGetFromNormalBody() 3, ], ]; - $expected = $formatter->format($body); $response->setBody($body); - $this->assertEquals($expected, $response->getXML()); + $this->assertEquals(Services::format()->getFormatter('application/xml')->format($body), $response->getXML()); } //-------------------------------------------------------------------- From bea491b97334804d8cb7944e253c8b91e5464d86 Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Tue, 23 Jun 2020 20:41:56 +0200 Subject: [PATCH 15/16] improvements and some fixes replace with new format() service --- tests/system/Test/FeatureResponseTest.php | 31 ++++++++--------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/tests/system/Test/FeatureResponseTest.php b/tests/system/Test/FeatureResponseTest.php index 75e6aa9ae66a..a5f2d09b2a9a 100644 --- a/tests/system/Test/FeatureResponseTest.php +++ b/tests/system/Test/FeatureResponseTest.php @@ -2,9 +2,12 @@ use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\Response; +use CodeIgniter\Test\CIUnitTestCase use CodeIgniter\Test\FeatureResponse; +use Config\App; +use Config\Services; -class FeatureResponseTest extends \CodeIgniter\Test\CIUnitTestCase +class FeatureResponseTest extends CIUnitTestCase { /** @@ -115,7 +118,7 @@ public function testAssertRedirectFail() public function testAssertRedirectSuccess() { $this->getFeatureResponse('

Hello World

'); - $this->feature->response = new RedirectResponse(new Config\App()); + $this->feature->response = new RedirectResponse(new App()); $this->assertTrue($this->feature->response instanceof RedirectResponse); $this->assertTrue($this->feature->isRedirect()); @@ -125,7 +128,7 @@ public function testAssertRedirectSuccess() public function testGetRedirectUrlReturnsUrl() { $this->getFeatureResponse('

Hello World

'); - $this->feature->response = new RedirectResponse(new Config\App()); + $this->feature->response = new RedirectResponse(new App()); $this->feature->response->redirect('foo/bar'); $this->assertEquals('foo/bar', $this->feature->getRedirectUrl()); @@ -221,18 +224,14 @@ public function testAssertCookieExpired() public function testGetJSON() { $this->getFeatureResponse(['foo' => 'bar']); - $config = new \Config\Format(); - $formatter = $config->getFormatter('application/json'); - $this->assertEquals($formatter->format(['foo' => 'bar']), $this->feature->getJSON()); + $this->assertEquals(Services::format()->getFormatter('application/json')->format(['foo' => 'bar']), $this->feature->getJSON()); } public function testEmptyJSON() { $this->getFeatureResponse('

Hello World

'); $this->response->setJSON('', true); - $config = new \Config\Format(); - $formatter = $config->getFormatter('application/json'); // this should be "" - json_encode(''); $this->assertEquals('""', $this->feature->getJSON()); @@ -242,8 +241,6 @@ public function testFalseJSON() { $this->getFeatureResponse('

Hello World

'); $this->response->setJSON(false, true); - $config = new \Config\Format(); - $formatter = $config->getFormatter('application/json'); // this should be FALSE - json_encode(false) $this->assertEquals('false', $this->feature->getJSON()); @@ -253,8 +250,6 @@ public function testTrueJSON() { $this->getFeatureResponse('

Hello World

'); $this->response->setJSON(true, true); - $config = new \Config\Format(); - $formatter = $config->getFormatter('application/json'); // this should be TRUE - json_encode(true) $this->assertEquals('true', $this->feature->getJSON()); @@ -273,10 +268,8 @@ public function testInvalidJSON() public function testGetXML() { $this->getFeatureResponse(['foo' => 'bar']); - $config = new \Config\Format(); - $formatter = $config->getFormatter('application/xml'); - $this->assertEquals($formatter->format(['foo' => 'bar']), $this->feature->getXML()); + $this->assertEquals(Services::format()->getFormatter('application/xml')->format(['foo' => 'bar']), $this->feature->getXML()); } public function testJsonFragment() @@ -316,16 +309,12 @@ public function testJsonExactString() $this->getFeatureResponse($data); - $config = new \Config\Format(); - $formatter = $config->getFormatter('application/json'); - $expected = $formatter->format($data); - - $this->feature->assertJSONExact($expected); + $this->feature->assertJSONExact(Services::format()->getFormatter('application/json')->format($data)); } protected function getFeatureResponse($body = null, array $responseOptions = [], array $headers = []) { - $this->response = new Response(new \Config\App()); + $this->response = new Response(new App()); $this->response->setBody($body); if (count($responseOptions)) From 20906ac4e8ba677ca5517e33ec213a55dab413de Mon Sep 17 00:00:00 2001 From: Mostafa Khudair <59371810+mostafakhudair@users.noreply.github.com> Date: Tue, 23 Jun 2020 22:46:16 +0200 Subject: [PATCH 16/16] Update FeatureResponseTest.php --- tests/system/Test/FeatureResponseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/Test/FeatureResponseTest.php b/tests/system/Test/FeatureResponseTest.php index a5f2d09b2a9a..7ac3c22f255b 100644 --- a/tests/system/Test/FeatureResponseTest.php +++ b/tests/system/Test/FeatureResponseTest.php @@ -2,7 +2,7 @@ use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\Response; -use CodeIgniter\Test\CIUnitTestCase +use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\FeatureResponse; use Config\App; use Config\Services;