2011-10-29 20:51:44 +00:00
|
|
|
<?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() {
|
2011-11-08 03:40:39 +00:00
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
2011-10-29 20:51:44 +00:00
|
|
|
$data = array('users' => array('user' => array('user1', 'user2')));
|
2011-11-08 03:40:39 +00:00
|
|
|
$Controller->set(array('users' => $data, 'serialize' => 'users'));
|
|
|
|
$View = new XmlView($Controller);
|
|
|
|
$output = $View->render(false);
|
2011-10-29 20:51:44 +00:00
|
|
|
|
|
|
|
$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));
|
2011-11-08 03:40:39 +00:00
|
|
|
$this->assertIdentical('application/xml', $Response->type());
|
2011-11-18 03:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test render with an array in serialize
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderWithoutViewMultiple() {
|
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
|
|
|
$data = array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2'));
|
|
|
|
$Controller->set($data);
|
|
|
|
$Controller->set('serialize', array('no', 'user'));
|
|
|
|
$View = new XmlView($Controller);
|
|
|
|
$output = $View->render(false);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'response' => array('no' =>$data['no'], 'user' => $data['user'])
|
|
|
|
);
|
|
|
|
$this->assertIdentical(Xml::build($expected)->asXML(), $output);
|
|
|
|
$this->assertIdentical('application/xml', $Response->type());
|
2011-10-29 20:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testRenderWithView method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderWithView() {
|
|
|
|
App::build(array('View' => array(
|
|
|
|
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
|
|
|
|
)));
|
2011-11-08 03:40:39 +00:00
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
|
|
|
$Controller->name = $Controller->viewPath = 'Posts';
|
|
|
|
|
2011-10-29 20:51:44 +00:00
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'User' => array(
|
|
|
|
'username' => 'user1'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'User' => array(
|
|
|
|
'username' => 'user2'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-11-08 03:40:39 +00:00
|
|
|
$Controller->set('users', $data);
|
|
|
|
$View = new XmlView($Controller);
|
|
|
|
$output = $View->render('index', 'xml/xml_view');
|
2011-10-29 20:51:44 +00:00
|
|
|
|
|
|
|
$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));
|
2011-11-08 03:40:39 +00:00
|
|
|
$this->assertIdentical('application/xml', $Response->type());
|
|
|
|
$this->assertInstanceOf('HelperCollection', $View->Helpers);
|
2011-10-29 20:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|