mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Added XmlView.
This commit is contained in:
parent
e737845e95
commit
a6c1aafcc4
4 changed files with 155 additions and 0 deletions
86
lib/Cake/Test/Case/View/XmlViewTest.php
Normal file
86
lib/Cake/Test/Case/View/XmlViewTest.php
Normal file
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
/**
|
||||
* XmlViewTest file
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
||||
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice
|
||||
*
|
||||
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
||||
* @package Cake.Test.Case.View
|
||||
* @since CakePHP(tm) v 2.1.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
App::uses('Controller', 'Controller');
|
||||
App::uses('CakeRequest', 'Network');
|
||||
App::uses('CakeResponse', 'Network');
|
||||
App::uses('XmlView', 'View');
|
||||
|
||||
/**
|
||||
* XmlViewTest
|
||||
*
|
||||
* @package Cake.Test.Case.View
|
||||
*/
|
||||
class XmlViewTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* testRenderWithoutView method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRenderWithoutView() {
|
||||
$request = new CakeRequest();
|
||||
$response = new CakeResponse();
|
||||
$controller = new Controller($request, $response);
|
||||
$data = array('users' => array('user' => array('user1', 'user2')));
|
||||
$controller->set('serialize', $data);
|
||||
$view = new XmlView($controller);
|
||||
$output = $view->render(false);
|
||||
|
||||
$expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>';
|
||||
$this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output));
|
||||
$this->assertIdentical('application/xml', $response->type());
|
||||
}
|
||||
|
||||
/**
|
||||
* testRenderWithView method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRenderWithView() {
|
||||
App::build(array('View' => array(
|
||||
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Xml' . DS,
|
||||
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
|
||||
)));
|
||||
$request = new CakeRequest();
|
||||
$response = new CakeResponse();
|
||||
$controller = new Controller($request, $response);
|
||||
$data = array(
|
||||
array(
|
||||
'User' => array(
|
||||
'username' => 'user1'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'User' => array(
|
||||
'username' => 'user2'
|
||||
)
|
||||
)
|
||||
);
|
||||
$controller->set('users', $data);
|
||||
$view = new XmlView($controller);
|
||||
$output = $view->render('index', 'xml/xml_view');
|
||||
|
||||
$expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>';
|
||||
$this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output));
|
||||
$this->assertIdentical('application/xml', $response->type());
|
||||
$this->assertInstanceOf('HelperCollection', $view->Helpers);
|
||||
}
|
||||
|
||||
}
|
4
lib/Cake/Test/test_app/View/Layouts/xml/xml_view.ctp
Normal file
4
lib/Cake/Test/test_app/View/Layouts/xml/xml_view.ctp
Normal file
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<users>
|
||||
<?php echo $content_for_layout; ?>
|
||||
</users>
|
3
lib/Cake/Test/test_app/View/Xml/index.ctp
Normal file
3
lib/Cake/Test/test_app/View/Xml/index.ctp
Normal file
|
@ -0,0 +1,3 @@
|
|||
<?php foreach ($users as $user): ?>
|
||||
<user><?php echo $user['User']['username']; ?></user>
|
||||
<?php endforeach; ?>
|
62
lib/Cake/View/XmlView.php
Normal file
62
lib/Cake/View/XmlView.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
/**
|
||||
* A custom view class that is used for XML responses
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.View
|
||||
* @since CakePHP(tm) v 2.1.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
App::uses('View', 'View');
|
||||
App::uses('Xml', 'Utility');
|
||||
|
||||
/**
|
||||
* XmlView
|
||||
*
|
||||
* @package Cake.View
|
||||
*/
|
||||
class XmlView extends View {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Controller $controller
|
||||
*/
|
||||
public function __construct($controller) {
|
||||
parent::__construct($controller);
|
||||
|
||||
if (is_object($controller)) {
|
||||
$controller->response->type('xml');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a XML view.
|
||||
*
|
||||
* Uses the special 'serialize' parameter to convert a set of
|
||||
* view variables into a XML response. Makes generating simple
|
||||
* XML responses very easy. You can omit the 'serialize' parameter,
|
||||
* and use a normal view + layout as well.
|
||||
*
|
||||
* @param string $view The view being rendered.
|
||||
* @param string $layout The layout being rendered.
|
||||
* @return string The rendered view.
|
||||
*/
|
||||
public function render($view = null, $layout = null) {
|
||||
if (isset($this->viewVars['serialize']) && is_array($this->viewVars['serialize'])) {
|
||||
return $this->output = Xml::fromArray($this->viewVars['serialize'])->asXML();
|
||||
}
|
||||
return parent::render($view, $layout);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue