2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* XmlTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-07-27 00:31:39 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.5432
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
App::import('Core', 'Xml');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* XmlTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-07-21 02:40:58 +00:00
|
|
|
class XmlTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
2010-07-27 00:31:39 +00:00
|
|
|
* testBuild method
|
2009-03-18 17:55:58 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-07-27 00:31:39 +00:00
|
|
|
function testBuild() {
|
|
|
|
$xml = '<tag>value</tag>';
|
|
|
|
$obj = Xml::build($xml);
|
|
|
|
$this->assertTrue($obj instanceof SimpleXMLElement);
|
|
|
|
$this->assertEqual((string)$obj->getName(), 'tag');
|
|
|
|
$this->assertEqual((string)$obj, 'value');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-27 00:31:39 +00:00
|
|
|
$xml = '<?xml version="1.0"?><tag>value</tag>';
|
|
|
|
$this->assertEqual($obj, Xml::build($xml));
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-27 02:16:23 +00:00
|
|
|
$xml = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'fixtures' . DS . 'sample.xml';
|
2010-07-27 00:31:39 +00:00
|
|
|
$obj = Xml::build($xml);
|
|
|
|
$this->assertEqual($obj->getName(), 'tags');
|
|
|
|
$this->assertEqual(count($obj), 2);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-27 00:31:39 +00:00
|
|
|
$this->assertEqual(Xml::build($xml), Xml::build(file_get_contents($xml)));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-27 00:31:39 +00:00
|
|
|
$xml = array('tag' => 'value');
|
|
|
|
$obj = Xml::build($xml);
|
|
|
|
$this->assertEqual($obj->getName(), 'tag');
|
|
|
|
$this->assertEqual((string)$obj, 'value');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-06-30 01:14:20 +00:00
|
|
|
|
2009-08-02 06:47:28 +00:00
|
|
|
/**
|
2010-08-23 03:19:54 +00:00
|
|
|
* data provider function for testBuildInvalidData
|
2010-07-27 02:25:55 +00:00
|
|
|
*
|
2010-08-23 21:23:53 +00:00
|
|
|
* @return array
|
2010-07-27 02:25:55 +00:00
|
|
|
*/
|
2010-08-23 03:19:54 +00:00
|
|
|
public static function invalidDataProvider() {
|
|
|
|
return array(
|
|
|
|
array(null),
|
|
|
|
array(false),
|
|
|
|
array(''),
|
|
|
|
array('<tag>')
|
|
|
|
);
|
2010-07-27 02:25:55 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-27 02:25:55 +00:00
|
|
|
/**
|
2010-08-23 03:19:54 +00:00
|
|
|
* testBuildInvalidData
|
2010-07-27 02:25:55 +00:00
|
|
|
*
|
2010-08-23 03:19:54 +00:00
|
|
|
* @dataProvider invalidDataProvider
|
2010-07-27 02:25:55 +00:00
|
|
|
* @expectedException Exception
|
|
|
|
* return void
|
|
|
|
*/
|
2010-08-23 03:19:54 +00:00
|
|
|
function testBuildInvalidData($value) {
|
|
|
|
Xml::build($value);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-07-27 00:31:39 +00:00
|
|
|
* testFromArray method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-07-27 00:31:39 +00:00
|
|
|
function testFromArray() {
|
|
|
|
$xml = array('tag' => 'value');
|
|
|
|
$obj = Xml::fromArray($xml);
|
|
|
|
$this->assertEqual($obj->getName(), 'tag');
|
|
|
|
$this->assertEqual((string)$obj, 'value');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-27 00:31:39 +00:00
|
|
|
$xml = array('tag' => null);
|
|
|
|
$obj = Xml::fromArray($xml);
|
|
|
|
$this->assertEqual($obj->getName(), 'tag');
|
|
|
|
$this->assertEqual((string)$obj, '');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-08-23 04:33:06 +00:00
|
|
|
$xml = array('tag' => array('@' => 'value'));
|
|
|
|
$obj = Xml::fromArray($xml);
|
|
|
|
$this->assertEqual($obj->getName(), 'tag');
|
|
|
|
$this->assertEqual((string)$obj, 'value');
|
|
|
|
|
2010-07-27 00:31:39 +00:00
|
|
|
$xml = array(
|
|
|
|
'tags' => array(
|
|
|
|
'tag' => array(
|
|
|
|
array(
|
|
|
|
'id' => '1',
|
|
|
|
'name' => 'defect'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => '2',
|
|
|
|
'name' => 'enhancement'
|
|
|
|
)
|
2008-05-30 11:40:08 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2010-07-27 00:31:39 +00:00
|
|
|
$obj = Xml::fromArray($xml);
|
|
|
|
$this->assertTrue($obj instanceof SimpleXMLElement);
|
|
|
|
$this->assertEqual($obj->getName(), 'tags');
|
|
|
|
$this->assertEqual(count($obj), 2);
|
|
|
|
$xmlText = '<' . '?xml version="1.0"?><tags><tag id="1" name="defect"/><tag id="2" name="enhancement"/></tags>';
|
|
|
|
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
|
|
|
|
|
|
|
|
$obj = Xml::fromArray($xml, 'tags');
|
|
|
|
$this->assertTrue($obj instanceof SimpleXMLElement);
|
|
|
|
$this->assertEqual($obj->getName(), 'tags');
|
|
|
|
$this->assertEqual(count($obj), 2);
|
|
|
|
$xmlText = '<' . '?xml version="1.0"?><tags><tag><id>1</id><name>defect</name></tag><tag><id>2</id><name>enhancement</name></tag></tags>';
|
|
|
|
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
|
|
|
|
|
|
|
|
$xml = array(
|
|
|
|
'tags' => array(
|
2008-05-30 11:40:08 +00:00
|
|
|
)
|
|
|
|
);
|
2010-07-27 00:31:39 +00:00
|
|
|
$obj = Xml::fromArray($xml);
|
|
|
|
$this->assertEqual($obj->getName(), 'tags');
|
|
|
|
$this->assertEqual((string)$obj, '');
|
|
|
|
|
|
|
|
$xml = array(
|
|
|
|
'tags' => array(
|
|
|
|
'bool' => true,
|
|
|
|
'int' => 1,
|
|
|
|
'float' => 10.2,
|
|
|
|
'string' => 'ok',
|
|
|
|
'null' => null,
|
|
|
|
'array' => array()
|
2008-05-30 11:40:08 +00:00
|
|
|
)
|
|
|
|
);
|
2010-07-27 00:31:39 +00:00
|
|
|
$obj = Xml::fromArray($xml, 'tags');
|
|
|
|
$this->assertEqual(count($obj), 6);
|
|
|
|
$this->assertIdentical((string)$obj->bool, '1');
|
|
|
|
$this->assertIdentical((string)$obj->int, '1');
|
|
|
|
$this->assertIdentical((string)$obj->float, '10.2');
|
|
|
|
$this->assertIdentical((string)$obj->string, 'ok');
|
|
|
|
$this->assertIdentical((string)$obj->null, '');
|
|
|
|
$this->assertIdentical((string)$obj->array, '');
|
2010-08-23 03:48:34 +00:00
|
|
|
|
|
|
|
$xml = array(
|
|
|
|
'tags' => array(
|
|
|
|
'tag' => array(
|
|
|
|
array(
|
|
|
|
'@id' => '1',
|
|
|
|
'name' => 'defect'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'@id' => '2',
|
|
|
|
'name' => 'enhancement'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$obj = Xml::fromArray($xml, 'tags');
|
|
|
|
$xmlText = '<' . '?xml version="1.0"?><tags><tag id="1"><name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
|
|
|
|
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
|
2010-08-23 04:33:06 +00:00
|
|
|
|
|
|
|
$xml = array(
|
|
|
|
'tags' => array(
|
|
|
|
'tag' => array(
|
|
|
|
array(
|
|
|
|
'@id' => '1',
|
|
|
|
'name' => 'defect',
|
|
|
|
'@' => 'Tag 1'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'@id' => '2',
|
|
|
|
'name' => 'enhancement'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'@' => 'All tags'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$obj = Xml::fromArray($xml, 'tags');
|
|
|
|
$xmlText = '<' . '?xml version="1.0"?><tags>All tags<tag id="1">Tag 1<name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
|
|
|
|
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
|
|
|
|
|
|
|
|
$xml = array(
|
|
|
|
'tags' => array(
|
|
|
|
'tag' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'@' => 'defect'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$obj = Xml::fromArray($xml);
|
|
|
|
$xmlText = '<' . '?xml version="1.0"?><tags><tag id="1">defect</tag></tags>';
|
|
|
|
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-08-23 03:19:54 +00:00
|
|
|
* data provider for fromArray() failures
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2010-08-23 21:23:53 +00:00
|
|
|
* @return array
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2010-08-23 03:19:54 +00:00
|
|
|
public static function invalidArrayDataProvider() {
|
|
|
|
return array(
|
|
|
|
array(''),
|
|
|
|
array(null),
|
|
|
|
array(false),
|
|
|
|
array(array()),
|
2010-08-23 21:23:53 +00:00
|
|
|
array(array('numeric key as root')),
|
|
|
|
array(array('item1' => '', 'item2' => '')),
|
|
|
|
array(array('items' => array('item1', 'item2'))),
|
2010-08-23 03:19:54 +00:00
|
|
|
array(array(
|
2010-07-27 00:31:39 +00:00
|
|
|
'tags' => array(
|
|
|
|
'tag' => array(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'string'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2009-07-01 18:58:05 +00:00
|
|
|
)
|
2010-08-23 03:19:54 +00:00
|
|
|
)),
|
2010-08-23 04:46:31 +00:00
|
|
|
array(array(
|
2010-08-23 03:48:34 +00:00
|
|
|
'tags' => array(
|
|
|
|
'@tag' => array(
|
|
|
|
array(
|
|
|
|
'@id' => '1',
|
|
|
|
'name' => 'defect'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'@id' => '2',
|
|
|
|
'name' => 'enhancement'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2010-08-23 04:46:31 +00:00
|
|
|
)),
|
2010-08-23 03:19:54 +00:00
|
|
|
array(new DateTime())
|
|
|
|
);
|
2010-07-27 02:25:55 +00:00
|
|
|
}
|
2008-07-31 23:50:32 +00:00
|
|
|
|
2010-07-27 02:25:55 +00:00
|
|
|
/**
|
2010-08-23 03:19:54 +00:00
|
|
|
* testFromArrayFail method
|
2010-07-27 02:25:55 +00:00
|
|
|
*
|
2010-08-23 03:19:54 +00:00
|
|
|
* @dataProvider invalidArrayDataProvider
|
2010-07-27 02:25:55 +00:00
|
|
|
* @expectedException Exception
|
|
|
|
*/
|
2010-08-23 03:19:54 +00:00
|
|
|
function testFromArrayFail($value) {
|
|
|
|
Xml::fromArray($value);
|
2010-07-27 02:25:55 +00:00
|
|
|
}
|
2008-07-31 23:50:32 +00:00
|
|
|
|
2010-07-27 02:25:55 +00:00
|
|
|
/**
|
|
|
|
* testToArray method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testToArray() {
|
2010-07-27 00:31:39 +00:00
|
|
|
$xml = '<tag>name</tag>';
|
|
|
|
$obj = Xml::build($xml);
|
|
|
|
$this->assertEqual(Xml::toArray($obj), array('tag' => 'name'));
|
2008-08-10 16:26:43 +00:00
|
|
|
|
2010-07-27 02:16:23 +00:00
|
|
|
$xml = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'fixtures' . DS . 'sample.xml';
|
2010-07-27 00:31:39 +00:00
|
|
|
$obj = Xml::build($xml);
|
2008-08-10 16:26:43 +00:00
|
|
|
$expected = array(
|
2010-08-23 03:24:56 +00:00
|
|
|
'tags' => array(
|
|
|
|
'tag' => array(
|
|
|
|
array(
|
|
|
|
'@id' => '1',
|
|
|
|
'name' => 'defect'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'@id' => '2',
|
|
|
|
'name' => 'enhancement'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEqual(Xml::toArray($obj), $expected);
|
|
|
|
|
|
|
|
$array = array(
|
2010-07-27 00:31:39 +00:00
|
|
|
'tags' => array(
|
|
|
|
'tag' => array(
|
2008-07-31 23:50:32 +00:00
|
|
|
array(
|
2010-07-27 00:31:39 +00:00
|
|
|
'id' => '1',
|
|
|
|
'name' => 'defect'
|
2008-07-31 23:50:32 +00:00
|
|
|
),
|
|
|
|
array(
|
2010-07-27 00:31:39 +00:00
|
|
|
'id' => '2',
|
|
|
|
'name' => 'enhancement'
|
2008-07-31 23:50:32 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2010-07-27 00:31:39 +00:00
|
|
|
);
|
2010-08-23 03:24:56 +00:00
|
|
|
$this->assertEqual(Xml::toArray(Xml::fromArray($array, 'tags')), $array);
|
2008-09-05 17:20:18 +00:00
|
|
|
|
2010-07-27 00:31:39 +00:00
|
|
|
$expected = array(
|
2010-08-23 03:24:56 +00:00
|
|
|
'tags' => array(
|
|
|
|
'tag' => array(
|
|
|
|
array(
|
|
|
|
'@id' => '1',
|
|
|
|
'@name' => 'defect'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'@id' => '2',
|
|
|
|
'@name' => 'enhancement'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEqual(Xml::toArray(Xml::fromArray($array)), $expected);
|
2010-08-23 03:48:34 +00:00
|
|
|
$this->assertEqual(Xml::toArray(Xml::fromArray($array, 'tags')), $array);
|
2010-08-23 03:24:56 +00:00
|
|
|
|
|
|
|
$array = array(
|
2010-07-27 00:31:39 +00:00
|
|
|
'tags' => array(
|
|
|
|
'tag' => array(
|
|
|
|
'id' => '1',
|
|
|
|
'posts' => array(
|
|
|
|
array('id' => '1'),
|
|
|
|
array('id' => '2')
|
2008-09-05 17:20:18 +00:00
|
|
|
)
|
|
|
|
),
|
2010-07-27 00:31:39 +00:00
|
|
|
'tagOther' => array(
|
|
|
|
'subtag' => array(
|
|
|
|
'id' => '1'
|
2008-09-05 17:20:18 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2009-09-22 20:47:11 +00:00
|
|
|
);
|
2010-08-23 03:24:56 +00:00
|
|
|
$expected = array(
|
|
|
|
'tags' => array(
|
|
|
|
'tag' => array(
|
|
|
|
'@id' => '1',
|
|
|
|
'posts' => array(
|
|
|
|
array('@id' => '1'),
|
|
|
|
array('@id' => '2')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'tagOther' => array(
|
|
|
|
'subtag' => array(
|
|
|
|
'@id' => '1'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEqual(Xml::toArray(Xml::fromArray($array)), $expected);
|
2010-03-26 08:59:09 +00:00
|
|
|
|
2010-07-28 21:53:36 +00:00
|
|
|
$xml = '<root>';
|
|
|
|
$xml .= '<tag id="1">defect</tag>';
|
|
|
|
$xml .= '</root>';
|
|
|
|
$obj = Xml::build($xml);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'root' => array(
|
|
|
|
'tag' => array(
|
2010-08-23 03:24:56 +00:00
|
|
|
'@id' => 1,
|
2010-08-23 04:33:06 +00:00
|
|
|
'@' => 'defect'
|
2010-07-28 21:53:36 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEqual(Xml::toArray($obj), $expected);
|
|
|
|
|
2010-07-27 00:31:39 +00:00
|
|
|
$xml = '<root>';
|
|
|
|
$xml .= '<table xmlns="http://www.w3.org/TR/html4/"><tr><td>Apples</td><td>Bananas</td></tr></table>';
|
|
|
|
$xml .= '<table xmlns="http://www.cakephp.org"><name>CakePHP</name><license>MIT</license></table>';
|
|
|
|
$xml .= '<table>The book is on the table.</table>';
|
|
|
|
$xml .= '</root>';
|
|
|
|
$obj = Xml::build($xml);
|
2010-03-26 08:59:09 +00:00
|
|
|
|
|
|
|
$expected = array(
|
2010-07-27 00:31:39 +00:00
|
|
|
'root' => array(
|
|
|
|
'table' => array(
|
|
|
|
array('tr' => array('td' => array('Apples', 'Bananas'))),
|
|
|
|
array('name' => 'CakePHP', 'license' => 'MIT'),
|
|
|
|
'The book is on the table.'
|
2010-03-26 08:59:09 +00:00
|
|
|
)
|
2010-07-27 00:31:39 +00:00
|
|
|
)
|
2010-03-26 08:59:09 +00:00
|
|
|
);
|
2010-07-27 00:31:39 +00:00
|
|
|
$this->assertEqual(Xml::toArray($obj), $expected);
|
2010-07-28 22:46:35 +00:00
|
|
|
|
|
|
|
$xml = '<root xmlns:cake="http://www.cakephp.org/">';
|
|
|
|
$xml .= '<tag>defect</tag>';
|
|
|
|
$xml .= '<cake:bug>1</cake:bug>';
|
|
|
|
$xml .= '</root>';
|
|
|
|
$obj = Xml::build($xml);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'root' => array(
|
|
|
|
'tag' => 'defect',
|
|
|
|
'bug' => 1
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEqual(Xml::toArray($obj), $expected);
|
2008-10-04 19:50:38 +00:00
|
|
|
}
|
2010-03-27 16:48:31 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|