mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-16 06:17:56 +08:00
Merge pull request #10589 from andrewnester/10545-rest-controller-fix
#10545 - Fix for XMLResponseFormatter to format models in a proper way
This commit is contained in:
@@ -106,6 +106,7 @@ Yii Framework 2 Change Log
|
||||
- Enh #10359: Support wildcard category name in `yii/console/controllers/MessageController` (rmrevin)
|
||||
- Enh #10390: Added ability to disable outer tag for `\yii\helpers\BaseHtml::radiolist()`, `::checkboxList()` (TianJinRong, githubjeka, silverfire)
|
||||
- Enh #10535: Allow passing a `yii\db\Expression` to `Query::orderBy()` and `Query::groupBy()` (andrewnester, cebe)
|
||||
- Enh #10545: `yii\web\XMLResponseFormatter` changed to format models in a proper way (andrewnester)
|
||||
- Enh: Added last resort measure for `FileHelper::removeDirectory()` fail to unlink symlinks under Windows (samdark)
|
||||
- Chg #9369: `Yii::$app->user->can()` now returns `false` instead of erroring in case `authManager` component is not configured (creocoder)
|
||||
- Chg #9411: `DetailView` now automatically sets container tag ID in case it's not specified (samdark)
|
||||
|
||||
@@ -77,7 +77,9 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
|
||||
*/
|
||||
protected function buildXml($element, $data)
|
||||
{
|
||||
if (is_array($data) || $data instanceof \Traversable && $this->useTraversableAsArray) {
|
||||
if (is_array($data) ||
|
||||
($data instanceof \Traversable && $this->useTraversableAsArray && !$data instanceof Arrayable)
|
||||
) {
|
||||
foreach ($data as $name => $value) {
|
||||
if (is_int($name) && is_object($value)) {
|
||||
$this->buildXml($element, $value);
|
||||
|
||||
@@ -86,4 +86,16 @@ abstract class FormatterTest extends \yiiunit\TestCase
|
||||
$this->formatter->format($this->response);
|
||||
$this->assertEquals($json, $this->response->content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $data the data to be formatted
|
||||
* @param string $expectedResult the expected body
|
||||
* @dataProvider formatModelDataProvider
|
||||
*/
|
||||
public function testFormatModels($data, $expectedResult)
|
||||
{
|
||||
$this->response->data = $data;
|
||||
$this->formatter->format($this->response);
|
||||
$this->assertEquals($expectedResult, $this->response->content);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
namespace yiiunit\framework\web;
|
||||
|
||||
use yii\web\JsonResponseFormatter;
|
||||
use yiiunit\framework\web\stubs\ModelStub;
|
||||
|
||||
/**
|
||||
* @author Alexander Makarov <sam@rmcreative.ru>
|
||||
@@ -87,6 +88,13 @@ class JsonResponseFormatterTest extends FormatterTest
|
||||
];
|
||||
}
|
||||
|
||||
public function formatModelDataProvider()
|
||||
{
|
||||
return [
|
||||
[new ModelStub(['id' => 123, 'title' => 'abc', 'hidden' => 'hidden']), '{"id":123,"title":"abc"}']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $data the data to be formatted
|
||||
* @param string $json the expected JSON body
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
namespace yiiunit\framework\web;
|
||||
|
||||
use yii\web\XmlResponseFormatter;
|
||||
use yiiunit\framework\web\stubs\ModelStub;
|
||||
|
||||
/**
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
@@ -104,4 +105,14 @@ class XmlResponseFormatterTest extends FormatterTest
|
||||
], "<response><Post><id>123</id><title><></title></Post><a><Post><id>456</id><title>def</title></Post></a></response>\n"],
|
||||
]);
|
||||
}
|
||||
|
||||
public function formatModelDataProvider()
|
||||
{
|
||||
return $this->addXmlHead([
|
||||
[
|
||||
new ModelStub(['id' => 123, 'title' => 'abc', 'hidden' => 'hidden']),
|
||||
"<response><ModelStub><id>123</id><title>abc</title></ModelStub></response>\n"
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
17
tests/framework/web/stubs/ModelStub.php
Normal file
17
tests/framework/web/stubs/ModelStub.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace yiiunit\framework\web\stubs;
|
||||
|
||||
use yii\base\Model;
|
||||
|
||||
class ModelStub extends Model
|
||||
{
|
||||
public $id;
|
||||
public $title;
|
||||
public $hidden;
|
||||
|
||||
public function toArray(array $fields = [], array $expand = [], $recursive = true)
|
||||
{
|
||||
return ['id' => $this->id, 'title' => $this->title];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user