xml.test.php

来自「Cake Framwork , Excellent」· PHP 代码 · 共 520 行 · 第 1/2 页

PHP
520
字号
	// 				array('id' => 17, 'name' => 'Web'),	// 				array('id' => 6, 'name' => 'CD-ROM')	// 			)	// 		)	// 	);	// 	// 	$xml = new Xml($input, array('format' => 'tags', 'tags' => array(	// 		'MediaType'	=> array('value' => 'id', 'children' => false),	// 		'JobType'	=> array('children' => array()),	// 		'Industry'	=> array('children' => array('name')),	// 		'show'		=> false	// 	)));	// 	// 	$result = $xml->toString(array('header' => false, 'cdata' => false));	// 	$expected = '<project><id>1</id><title /><client_id>1</client_id><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><name>Financial</name></industry><business_solution><id>6</id><name>Convert Sales</name></business_solution><media_type>15</media_type><media_type>7</media_type><media_type>6</media_type></project><project><id>2</id><title /><client_id>2</client_id><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><name>Education</name></industry><business_solution><id>4</id><name>Build Relationship</name></business_solution><business_solution><id>6</id><name>Convert Sales</name></business_solution><media_type>17</media_type><media_type>6</media_type></project>';	// 	$this->assertEqual($expected, $result);	// }	/*	 * Broken due to a Set class issue	 */	// function testMixedArray() {	// 	$input = array('OptionGroup' => array(	// 		array('name' => 'OptA', 'id' => 12, 'OptA 1', 'OptA 2', 'OptA 3', 'OptA 4', 'OptA 5', 'OptA 6'),	// 		array('name' => 'OptB', 'id' => 12, 'OptB 1', 'OptB 2', 'OptB 3', 'OptB 4', 'OptB 5', 'OptB 6')	// 	));	// 	$expected = '<option_group><name>OptA</name><id>12</id><option_group>OptA 1</option_group><option_group>OptA 2</option_group><option_group>OptA 3</option_group><option_group>OptA 4</option_group><option_group>OptA 5</option_group><option_group>OptA 6</option_group></option_group><option_group><name>OptB</name><id>12</id><option_group>OptB 1</option_group><option_group>OptB 2</option_group><option_group>OptB 3</option_group><option_group>OptB 4</option_group><option_group>OptB 5</option_group><option_group>OptB 6</option_group></option_group>';	// 	$xml = new Xml($input, array('format' => 'tags'));	// 	$result = $xml->toString(array('header' => false, 'cdata' => false));	// 	$this->assertEqual($expected, $result);	// }	// function testMixedNestedArray() {	// 	$input = array(	// 		'OptionA' =>  array(	// 			'name' => 'OptA',	// 			'id' => 12,	// 			'opt' => array('OptA 1', 'OptA 2', 'OptA 3', 'OptA 4', 'OptA 5', 'OptA 6')	// 		),	// 		'OptionB' 	=> array(	// 			'name' => 'OptB',	// 			'id' => 12,	// 			'opt' => array('OptB 1', 'OptB 2', 'OptB 3', 'OptB 4', 'OptB 5', 'OptB 6')	// 		)	// 	);	// 	$expected = '<option_a><name>OptA</name><id>12</id><opt>OptA 1</opt><opt>OptA 2</opt><opt>OptA 3</opt><opt>OptA 4</opt><opt>OptA 5</opt><opt>OptA 6</opt></option_a><option_b><name>OptB</name><id>12</id><opt>OptB 1</opt><opt>OptB 2</opt><opt>OptB 3</opt><opt>OptB 4</opt><opt>OptB 5</opt><opt>OptB 6</opt></option_b>';	// 	$xml = new Xml($input, array('format' => 'tags'));	// 	$result = $xml->toString(array('header' => false, 'cdata' => false));	// 	$this->assertEqual($expected, $result);	// }	// function testMixedArrayAttributes() {	// 	$input = array('OptionGroup' => array(	// 		array(	// 			'name' => 'OptA',	// 			'id' => 12,	// 			array('opt' => 'OptA 1'),	// 			array('opt' => 'OptA 2'),	// 			array('opt' => 'OptA 3'),	// 			array('opt' => 'OptA 4'),	// 			array('opt' => 'OptA 5'),	// 			array('opt' => 'OptA 6')	// 		),	// 		array(	// 			'name' => 'OptB',	// 			'id' => 12,	// 			array('opt' => 'OptB 1'),	// 			array('opt' => 'OptB 2'),	// 			array('opt' => 'OptB 3'),	// 			array('opt' => 'OptB 4'),	// 			array('opt' => 'OptB 5'),	// 			array('opt' => 'OptB 6')	// 		)	// 	));	// 	$expected = '<option_group name="OptA" id="12"><opt>OptA 1</opt><opt>OptA 2</opt><opt>OptA 3</opt><opt>OptA 4</opt><opt>OptA 5</opt><opt>OptA 6</opt></option_group><option_group name="OptB" id="12"><opt>OptB 1</opt><opt>OptB 2</opt><opt>OptB 3</opt><opt>OptB 4</opt><opt>OptB 5</opt><opt>OptB 6</opt></option_group>';	// 		// 	$options = array('tags' => array('option_group' => array('attributes' => array('id', 'name'))));	// 	$xml = new Xml($input, $options);	// 	$result = $xml->toString(false);	// 	// 	$this->assertEqual($expected, $result);	// }	/*	 * Not implemented yet	 */	// function testTagMap() {	// 	$input = array(	// 		array(	// 			'Project' => array('id' => 1, 'title' => null, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),	// 			'Style' => array('id' => null, 'name' => null),	// 			'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),	// 			'Industry' => array('id' => 1, 'name' => 'Financial')	// 		),	// 		array(	// 			'Project' => array('id' => 2, 'title' => null, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),	// 			'Style' => array('id' => null, 'name' => null),	// 			'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),	// 			'Industry' => array('id' => 2, 'name' => 'Education'),	// 		)	// 	);	// 	$expected = '<project id="1"><title /><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style id=""><name /></style><jobtype id="1">Touch Screen Kiosk</jobtype><industry id="1"><name>Financial</name></industry></project><project id="2"><title /><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style id=""><name /></style><jobtype id="2">Awareness Campaign</jobtype><industry id="2"><name>Education</name></industry></project>';	// 	// 	$xml = new Xml($input, array('tags' => array(	// 		'Project'	=> array('attributes' => array('id')), 	// 		'style'		=> array('attributes' => array('id')), 	// 		'JobType'	=> array('name' => 'jobtype', 'attributes' => array('id'), 'value' => 'name'), 	// 		'Industry'	=> array('attributes' => array('id'))	// 	)));	// 	$result = $xml->toString(array('header' => false, 'cdata' => false));	// 	$this->assertEqual($expected, $result);	// }/** * testAllCData method *  * @access public * @return void */	function testAllCData() {		$input = array(			array(				'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '1.89', 'industry_id' => '1.56', 'modified' => null, 'created' => null),				'Style' => array('id' => null, 'name' => null),				'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),				'Industry' => array('id' => 1, 'name' => 'Financial')			),			array(				'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '2.2', 'industry_id' => 2.2, 'modified' => '2007-11-26 14:48:36', 'created' => null),				'Style' => array('id' => null, 'name' => null),				'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),				'Industry' => array('id' => 2, 'name' => 'Education'),			)		);		$expected = '<project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1.89</job_type_id><industry_id>1.56</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name><![CDATA[Touch Screen Kiosk]]></name></job_type><industry><id>1</id><name><![CDATA[Financial]]></name></industry></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2.2</job_type_id><industry_id>2.2</industry_id><modified><![CDATA[2007-11-26 14:48:36]]></modified><created /><style><id /><name /></style><job_type><id>2</id><name><![CDATA[Awareness Campaign]]></name></job_type><industry><id>2</id><name><![CDATA[Education]]></name></industry></project>';		$xml = new Xml($input, array('format' => 'tags'));		$result = $xml->toString(array('header' => false, 'cdata' => true));		$this->assertEqual($expected, $result);	}	/*	 * PHP-native Unicode support pending	 */	// function testConvertEntities() {	// 	$input = array('project' => '&eacute;c&icirc;t');	// 	$xml = new Xml($input);	// 	// 	$result = $xml->toString(array('header' => false, 'cdata' => false, 'convertEntities' => true));	// 	$expected = '<project>&#233;c&#238;t</project>';	// 	$this->assertEqual($result, $expected);	// }/** * testWhitespace method *  * @access public * @return void */	function testWhitespace() {		$input = array(			array(				'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),				'Style' => array('id' => null, 'name' => null),				'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),				'Industry' => array('id' => 1, 'name' => 'Financial')			),			array(				'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),				'Style' => array('id' => null, 'name' => null),				'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),				'Industry' => array('id' => 2, 'name' => 'Education'),			)		);		$expected = "\n\t<project>\n\t\t<id>\n\t\t\t1\n\t\t</id>\n\t\t<title />\n\t\t<client_id>\n\t\t\t1\n\t\t</client_id>\n\t\t<show>\n\t\t\t1\n\t\t</show>\n\t\t<is_spotlight />\n\t\t<style_id>\n\t\t\t0\n\t\t</style_id>\n\t\t<job_type_id>\n\t\t\t1\n\t\t</job_type_id>\n\t\t<industry_id>\n\t\t\t1\n\t\t</industry_id>\n\t\t<modified />\n\t\t<created />\n\t\t<style>\n\t\t\t<id />\n\t\t\t<name />\n\t\t</style>\n\t\t<job_type>\n\t\t\t<id>\n\t\t\t\t1\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tTouch Screen Kiosk\n\t\t\t</name>\n\t\t</job_type>\n\t\t<industry>\n\t\t\t<id>\n\t\t\t\t1\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tFinancial\n\t\t\t</name>\n\t\t</industry>\n\t</project>\n\t<project>\n\t\t<id>\n\t\t\t2\n\t\t</id>\n\t\t<title />\n\t\t<client_id>\n\t\t\t2\n\t\t</client_id>\n\t\t<show>\n\t\t\t1\n\t\t</show>\n\t\t<is_spotlight />\n\t\t<style_id>\n\t\t\t0\n\t\t</style_id>\n\t\t<job_type_id>\n\t\t\t2\n\t\t</job_type_id>\n\t\t<industry_id>\n\t\t\t2\n\t\t</industry_id>\n\t\t<modified>\n\t\t\t2007-11-26 14:48:36\n\t\t</modified>\n\t\t<created />\n\t\t<style>\n\t\t\t<id />\n\t\t\t<name />\n\t\t</style>\n\t\t<job_type>\n\t\t\t<id>\n\t\t\t\t2\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tAwareness Campaign\n\t\t\t</name>\n\t\t</job_type>\n\t\t<industry>\n\t\t\t<id>\n\t\t\t\t2\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tEducation\n\t\t\t</name>\n\t\t</industry>\n\t</project>\n";			$xml = new Xml($input, array('format' => 'tags'));		$result = $xml->toString(array('header' => false, 'cdata' => false, 'whitespace' => true));		$this->assertEqual($expected, $result);	}/** * testSetSerialization method *  * @access public * @return void */	function testSetSerialization() {		$input = array(			array(				'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),				'Style' => array('id' => null, 'name' => null),				'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),				'Industry' => array('id' => 1, 'name' => 'Financial')			),			array(				'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),				'Style' => array('id' => null, 'name' => null),				'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),				'Industry' => array('id' => 2, 'name' => 'Education'),			)		);		$expected = '<project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><id>1</id><name>Financial</name></industry></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><id>2</id><name>Education</name></industry></project>';			$xml = new Xml(Set::map($input), array('format' => 'tags'));		$result = $xml->toString(array('header' => false, 'cdata' => false));		$this->assertEqual($expected, $result);	}	function testSimpleParsing() {		$source = '<response><hello><![CDATA[happy world]]></hello><goodbye><![CDATA[cruel world]]></goodbye></response>';		$xml = new Xml($source);		$result = $xml->toString();		$this->assertEqual($source, $result);	}	function testMixedParsing() {		$source = '<response><body><hello><![CDATA[happy world]]></hello><![CDATA[in between]]><goodbye><![CDATA[cruel world]]></goodbye></body></response>';		$xml = new Xml($source);		$result = $xml->toString();		$this->assertEqual($source, $result);	}	function testComplexParsing() {		$source = '<projects><project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><id>1</id><name>Financial</name></industry></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><id>2</id><name>Education</name></industry></project></projects>';		$xml = new Xml($source);		$result = $xml->toString(array('cdata' => false));		$this->assertEqual($source, $result);	}	function testNamespaceParsing() {		$source = '<a:container xmlns:a="http://example.com/a" xmlns:b="http://example.com/b" xmlns:c="http://example.com/c" xmlns:d="http://example.com/d" xmlns:e="http://example.com/e"><b:rule test=""><c:result>value</c:result></b:rule><d:rule test=""><e:result>value</e:result></d:rule></a:container>';		$xml = new Xml($source);		$result = $xml->toString(array('cdata' => false));		$this->assertEqual($source, $result);		$children = $xml->children('container');		$this->assertEqual($children[0]->namespace, 'a');			$children = $children[0]->children('rule');		$this->assertEqual($children[0]->namespace, 'b');	}	function testNamespaces() {		$source = '<a:container xmlns:a="http://example.com/a" xmlns:b="http://example.com/b" xmlns:c="http://example.com/c" xmlns:d="http://example.com/d" xmlns:e="http://example.com/e"><b:rule test=""><c:result>value</c:result></b:rule><d:rule test=""><e:result>value</e:result></d:rule></a:container>';		$xml = new Xml($source);		$expects = '<a:container xmlns:a="http://example.com/a" xmlns:b="http://example.com/b" xmlns:c="http://example.com/c" xmlns:d="http://example.com/d" xmlns:e="http://example.com/e" xmlns:f="http://example.com/f"><b:rule test=""><c:result>value</c:result></b:rule><d:rule test=""><e:result>value</e:result></d:rule></a:container>';		$_xml = XmlManager::getInstance();		$xml->addNamespace('f', 'http://example.com/f');		$result = $xml->toString(array('cdata' => false));		$this->assertEqual($expects, $result);	}		/*	 * @todo Add test for default namespaces	 */}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?