Fixes #7258: Response was sending HTML content type when formatter was set to JSON or XML, nulls were handled wrong

This commit is contained in:
Alexander Makarov
2015-02-21 02:32:09 +03:00
parent 9028bc5aa7
commit 00aafc82ef
5 changed files with 115 additions and 132 deletions

View File

@@ -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)

View File

@@ -0,0 +1,77 @@
<?php
namespace yiiunit\framework\web;
use yii\web\Response;
use yii\web\ResponseFormatterInterface;
abstract class FormatterTest extends \yiiunit\TestCase
{
/**
* @var Response
*/
public $response;
/**
* @var ResponseFormatterInterface
*/
public $formatter;
protected function setUp()
{
$this->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);
}
}

View File

@@ -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 <sam@rmcreative.ru>
@@ -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 [

View File

@@ -0,0 +1,16 @@
<?php
namespace yiiunit\framework\web;
use yii\base\Object;
class Post extends Object
{
public $id;
public $title;
public function __construct($id, $title)
{
$this->id = $id;
$this->title = $title;
}
}

View File

@@ -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 <qiang.xue@gmail.com>
* @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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
private function addXmlHead(array $data)
{
$head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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, "<response></response>\n"],
return $this->addXmlHead([
[1, "<response>1</response>\n"],
['abc', "<response>abc</response>\n"],
[true, "<response>1</response>\n"],
["<>", "<response>&lt;&gt;</response>\n"],
];
}
/**
* @param mixed $data the data to be formatted
* @param string $xml the expected XML body
* @dataProvider formatArrayDataProvider
*/
public function testFormatArrays($data, $xml)
{
$head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$this->response->data = $data;
$this->formatter->format($this->response);
$this->assertEquals($head . $xml, $this->response->content);
]);
}
public function formatArrayDataProvider()
{
return [
return $this->addXmlHead([
[[], "<response/>\n"],
[[1, 'abc'], "<response><item>1</item><item>abc</item></response>\n"],
[[
@@ -105,25 +66,12 @@ class XmlResponseFormatterTest extends \yiiunit\TestCase
'c' => [2, '<>'],
true,
], "<response><a>1</a><b>abc</b><c><item>2</item><item>&lt;&gt;</item></c><item>1</item></response>\n"],
];
}
/**
* @param mixed $data the data to be formatted
* @param string $xml the expected XML body
* @dataProvider formatObjectDataProvider
*/
public function testFormatObjects($data, $xml)
{
$head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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'), "<response><Post><id>123</id><title>abc</title></Post></response>\n"],
[[
new Post(123, 'abc'),
@@ -133,6 +81,6 @@ class XmlResponseFormatterTest extends \yiiunit\TestCase
new Post(123, '<>'),
'a' => new Post(456, 'def'),
], "<response><Post><id>123</id><title>&lt;&gt;</title></Post><a><Post><id>456</id><title>def</title></Post></a></response>\n"],
];
]);
}
}