allow serialize in json and xml alias

This commit is contained in:
Christian Winther 2013-07-06 16:57:46 +00:00
parent fd2c096712
commit cbb4f7db90
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()); $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 * testJsonpResponse method
* *

View file

@ -103,6 +103,35 @@ class XmlViewTest extends CakeTestCase {
$this->assertSame(Xml::build($expected)->asXML(), $output); $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 * testRenderWithView method
* *

View file

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

View file

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