From 00aafc82ef2f48d161353dce33bb9ddb6ea084d3 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 21 Feb 2015 02:32:09 +0300 Subject: [PATCH] Fixes #7258: Response was sending HTML content type when formatter was set to JSON or XML, nulls were handled wrong --- framework/CHANGELOG.md | 1 + tests/unit/framework/web/FormatterTest.php | 77 +++++++++++++++++ .../web/JsonResponseFormatterTest.php | 67 +-------------- tests/unit/framework/web/Post.php | 16 ++++ .../web/XmlResponseFormatterTest.php | 86 ++++--------------- 5 files changed, 115 insertions(+), 132 deletions(-) create mode 100644 tests/unit/framework/web/FormatterTest.php create mode 100644 tests/unit/framework/web/Post.php diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 0bf778155e..db0c3a4ecd 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -15,6 +15,7 @@ Yii Framework 2 Change Log - Bug #7226: `yii\web\Request::getEtag()` should strip off `-gzip` which may be added by Apache (mcd-php) - Bug #7227: Query builder should respect column alias setting when `yii\db\Expression` is being selected (mdmunir, qiangxue) - Bug #7271: `ActiveRecord::populateRecord()` should be called in late binding approach (jlorente) +- Bug #7258: Response was sending HTML content type when formatter was set to JSON or XML, nulls were handled wrong (slavcodev, samdark) - Bug #7358: Fix trimming PHPDoc prefix with TAB indent in `yii\console\Controller::parseDocCommentSummary()` (gugglegum) - Enh #3168: Improved the performance of `yii\rbac\DbManager::checkAccess()` by caching mechanism (qiangxue) - Enh #4710: Added `yii\web\AssetManager::appendTimestamp` to support cache busting for assets (qiangxue) diff --git a/tests/unit/framework/web/FormatterTest.php b/tests/unit/framework/web/FormatterTest.php new file mode 100644 index 0000000000..1c998793f9 --- /dev/null +++ b/tests/unit/framework/web/FormatterTest.php @@ -0,0 +1,77 @@ +mockApplication(); + $this->response = new Response; + $this->formatter = $this->getFormatterInstance(); + } + + /** + * @return ResponseFormatterInterface + */ + abstract protected function getFormatterInstance(); + + /** + * Formatter should not format null + */ + public function testFormatNull() + { + $this->response->data = null; + $this->formatter->format($this->response); + $this->assertEquals(null, $this->response->content); + } + + /** + * @param mixed $data the data to be formatted + * @param string $json the expected JSON body + * @dataProvider formatScalarDataProvider + */ + public function testFormatScalar($data, $json) + { + $this->response->data = $data; + $this->formatter->format($this->response); + $this->assertEquals($json, $this->response->content); + } + + /** + * @param mixed $data the data to be formatted + * @param string $json the expected JSON body + * @dataProvider formatArrayDataProvider + */ + public function testFormatArrays($data, $json) + { + $this->response->data = $data; + $this->formatter->format($this->response); + $this->assertEquals($json, $this->response->content); + } + + /** + * @param mixed $data the data to be formatted + * @param string $json the expected JSON body + * @dataProvider formatObjectDataProvider + */ + public function testFormatObjects($data, $json) + { + $this->response->data = $data; + $this->formatter->format($this->response); + $this->assertEquals($json, $this->response->content); + } +} \ No newline at end of file diff --git a/tests/unit/framework/web/JsonResponseFormatterTest.php b/tests/unit/framework/web/JsonResponseFormatterTest.php index ee43de27b4..ca49f48499 100644 --- a/tests/unit/framework/web/JsonResponseFormatterTest.php +++ b/tests/unit/framework/web/JsonResponseFormatterTest.php @@ -7,21 +7,7 @@ namespace yiiunit\framework\web; -use yii\base\Object; use yii\web\JsonResponseFormatter; -use yii\web\Response; - -class Post extends Object -{ - public $id; - public $title; - - public function __construct($id, $title) - { - $this->id = $id; - $this->title = $title; - } -} /** * @author Alexander Makarov @@ -29,40 +15,19 @@ class Post extends Object * * @group web */ -class JsonResponseFormatterTest extends \yiiunit\TestCase +class JsonResponseFormatterTest extends FormatterTest { /** - * @var Response + * @return JsonResponseFormatter */ - public $response; - /** - * @var JsonResponseFormatter - */ - public $formatter; - - protected function setUp() + protected function getFormatterInstance() { - $this->mockApplication(); - $this->response = new Response; - $this->formatter = new JsonResponseFormatter; - } - - /** - * @param mixed $data the data to be formatted - * @param string $json the expected JSON body - * @dataProvider formatScalarDataProvider - */ - public function testFormatScalar($data, $json) - { - $this->response->data = $data; - $this->formatter->format($this->response); - $this->assertEquals($json, $this->response->content); + return new JsonResponseFormatter(); } public function formatScalarDataProvider() { return [ - [null, 'null'], [1, 1], ['abc', '"abc"'], [true, 'true'], @@ -70,18 +35,6 @@ class JsonResponseFormatterTest extends \yiiunit\TestCase ]; } - /** - * @param mixed $data the data to be formatted - * @param string $json the expected JSON body - * @dataProvider formatArrayDataProvider - */ - public function testFormatArrays($data, $json) - { - $this->response->data = $data; - $this->formatter->format($this->response); - $this->assertEquals($json, $this->response->content); - } - public function formatArrayDataProvider() { return [ @@ -106,18 +59,6 @@ class JsonResponseFormatterTest extends \yiiunit\TestCase ]; } - /** - * @param mixed $data the data to be formatted - * @param string $json the expected JSON body - * @dataProvider formatObjectDataProvider - */ - public function testFormatObjects($data, $json) - { - $this->response->data = $data; - $this->formatter->format($this->response); - $this->assertEquals($json, $this->response->content); - } - public function formatObjectDataProvider() { return [ diff --git a/tests/unit/framework/web/Post.php b/tests/unit/framework/web/Post.php new file mode 100644 index 0000000000..d6ebc68525 --- /dev/null +++ b/tests/unit/framework/web/Post.php @@ -0,0 +1,16 @@ +id = $id; + $this->title = $title; + } +} \ No newline at end of file diff --git a/tests/unit/framework/web/XmlResponseFormatterTest.php b/tests/unit/framework/web/XmlResponseFormatterTest.php index efb26f85bd..d98da3dd7c 100644 --- a/tests/unit/framework/web/XmlResponseFormatterTest.php +++ b/tests/unit/framework/web/XmlResponseFormatterTest.php @@ -7,86 +7,47 @@ namespace yiiunit\framework\web; -use yii\base\Object; -use yii\web\Response; use yii\web\XmlResponseFormatter; -class Post extends Object -{ - public $id; - public $title; - - public function __construct($id, $title) - { - $this->id = $id; - $this->title = $title; - } -} - /** * @author Qiang Xue * @since 2.0 * * @group web */ -class XmlResponseFormatterTest extends \yiiunit\TestCase +class XmlResponseFormatterTest extends FormatterTest { /** - * @var Response + * @return XmlResponseFormatter */ - public $response; - /** - * @var XmlResponseFormatter - */ - public $formatter; - - protected function setUp() + protected function getFormatterInstance() { - $this->mockApplication(); - $this->response = new Response; - $this->formatter = new XmlResponseFormatter; + return new XmlResponseFormatter(); } - /** - * @param mixed $data the data to be formatted - * @param string $xml the expected XML body - * @dataProvider formatScalarDataProvider - */ - public function testFormatScalar($data, $xml) + private $xmlHead = "\n"; + + private function addXmlHead(array $data) { - $head = "\n"; - $this->response->data = $data; - $this->formatter->format($this->response); - $this->assertEquals($head . $xml, $this->response->content); + foreach ($data as &$item) { + $item[1] = $this->xmlHead . $item[1]; + } + return $data; } public function formatScalarDataProvider() { - return [ - [null, "\n"], + return $this->addXmlHead([ [1, "1\n"], ['abc', "abc\n"], [true, "1\n"], ["<>", "<>\n"], - ]; - } - - /** - * @param mixed $data the data to be formatted - * @param string $xml the expected XML body - * @dataProvider formatArrayDataProvider - */ - public function testFormatArrays($data, $xml) - { - $head = "\n"; - $this->response->data = $data; - $this->formatter->format($this->response); - $this->assertEquals($head . $xml, $this->response->content); + ]); } public function formatArrayDataProvider() { - return [ + return $this->addXmlHead([ [[], "\n"], [[1, 'abc'], "1abc\n"], [[ @@ -105,25 +66,12 @@ class XmlResponseFormatterTest extends \yiiunit\TestCase 'c' => [2, '<>'], true, ], "1abc2<>1\n"], - ]; - } - - /** - * @param mixed $data the data to be formatted - * @param string $xml the expected XML body - * @dataProvider formatObjectDataProvider - */ - public function testFormatObjects($data, $xml) - { - $head = "\n"; - $this->response->data = $data; - $this->formatter->format($this->response); - $this->assertEquals($head . $xml, $this->response->content); + ]); } public function formatObjectDataProvider() { - return [ + return $this->addXmlHead([ [new Post(123, 'abc'), "123abc\n"], [[ new Post(123, 'abc'), @@ -133,6 +81,6 @@ class XmlResponseFormatterTest extends \yiiunit\TestCase new Post(123, '<>'), 'a' => new Post(456, 'def'), ], "123<>456def\n"], - ]; + ]); } }