Merge pull request #1405 from jippi/feature/_serialize-in-json-and-xml-alias

Allow serialize in json and xml alias.
This commit is contained in:
Mark Story 2013-07-06 13:56:50 -07:00
commit 753200196f
4 changed files with 58 additions and 4 deletions

View file

@ -67,6 +67,25 @@ class JsonViewTest extends CakeTestCase {
$this->assertSame('application/json', $Response->type());
}
/**
* Test render with an array in _serialize and alias
*
* @return void
*/
public function testRenderWithoutViewMultipleAndAlias() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array('original_name' => 'my epic name', 'user' => 'fake', 'list' => array('item1', 'item2'));
$Controller->set($data);
$Controller->set('_serialize', array('new_name' => 'original_name', 'user'));
$View = new JsonView($Controller);
$output = $View->render(false);
$this->assertSame(json_encode(array('new_name' => $data['original_name'], 'user' => $data['user'])), $output);
$this->assertSame('application/json', $Response->type());
}
/**
* testJsonpResponse method
*

View file

@ -103,6 +103,35 @@ class XmlViewTest extends CakeTestCase {
$this->assertSame(Xml::build($expected)->asXML(), $output);
}
/**
* Test render with an array in _serialize and alias
*
* @return void
*/
public function testRenderWithoutViewMultipleAndAlias() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array('original_name' => 'my epic name', 'user' => 'fake', 'list' => array('item1', 'item2'));
$Controller->set($data);
$Controller->set('_serialize', array('new_name' => 'original_name', 'user'));
$View = new XmlView($Controller);
$this->assertSame('application/xml', $Response->type());
$output = $View->render(false);
$expected = array(
'response' => array('new_name' => $data['original_name'], 'user' => $data['user'])
);
$this->assertSame(Xml::build($expected)->asXML(), $output);
$Controller->set('_rootNode', 'custom_name');
$View = new XmlView($Controller);
$output = $View->render(false);
$expected = array(
'custom_name' => array('new_name' => $data['original_name'], 'user' => $data['user'])
);
$this->assertSame(Xml::build($expected)->asXML(), $output);
}
/**
* testRenderWithView method
*

View file

@ -119,8 +119,11 @@ class JsonView extends View {
protected function _serialize($serialize) {
if (is_array($serialize)) {
$data = array();
foreach ($serialize as $key) {
$data[$key] = $this->viewVars[$key];
foreach ($serialize as $alias => $key) {
if (is_numeric($alias)) {
$alias = $key;
}
$data[$alias] = $this->viewVars[$key];
}
} else {
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;

View file

@ -104,8 +104,11 @@ class XmlView extends View {
if (is_array($serialize)) {
$data = array($rootNode => array());
foreach ($serialize as $key) {
$data[$rootNode][$key] = $this->viewVars[$key];
foreach ($serialize as $alias => $key) {
if (is_numeric($alias)) {
$alias = $key;
}
$data[$rootNode][$alias] = $this->viewVars[$key];
}
} else {
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;