PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Test/Case/Utility/XmlTest.php

https://github.com/Bancha/cakephp
PHP | 855 lines | 629 code | 74 blank | 152 comment | 0 complexity | 0eef86b3b7d3d1aaa5f637df3e5396c8 MD5 | raw file
  1. <?php
  2. /**
  3. * XmlTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.libs
  16. * @since CakePHP(tm) v 1.2.0.5432
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Xml', 'Utility');
  20. App::uses('CakeTestModel', 'TestSuite/Fixture');
  21. /**
  22. * Article class
  23. *
  24. * @package cake.tests.cases.libs
  25. */
  26. class XmlArticle extends CakeTestModel {
  27. /**
  28. * name property
  29. *
  30. * @var string 'Article'
  31. */
  32. public $name = 'Article';
  33. /**
  34. * belongsTo property
  35. *
  36. * @var array
  37. */
  38. public $belongsTo = array(
  39. 'XmlUser' => array(
  40. 'className' => 'XmlArticle',
  41. 'foreignKey' => 'user_id'
  42. )
  43. );
  44. }
  45. /**
  46. * User class
  47. *
  48. * @package cake.tests.cases.libs
  49. */
  50. class XmlUser extends CakeTestModel {
  51. /**
  52. * name property
  53. *
  54. * @var string 'User'
  55. */
  56. public $name = 'User';
  57. /**
  58. * hasMany property
  59. *
  60. * @var array
  61. */
  62. public $hasMany = array('Article');
  63. }
  64. /**
  65. * XmlTest class
  66. *
  67. * @package cake.tests.cases.libs
  68. */
  69. class XmlTest extends CakeTestCase {
  70. /**
  71. * autoFixtures property
  72. *
  73. * @var bool false
  74. */
  75. public $autoFixtures = false;
  76. /**
  77. * fixtures property
  78. * @var array
  79. */
  80. public $fixtures = array(
  81. 'core.article', 'core.user'
  82. );
  83. /**
  84. * setup method
  85. *
  86. * @access public
  87. * @return void
  88. */
  89. function setUp() {
  90. parent::setup();
  91. $this->_appEncoding = Configure::read('App.encoding');
  92. Configure::write('App.encoding', 'UTF-8');
  93. }
  94. /**
  95. * tearDown method
  96. *
  97. * @access public
  98. * @return void
  99. */
  100. function tearDown() {
  101. parent::tearDown();
  102. Configure::write('App.encoding', $this->_appEncoding);
  103. }
  104. /**
  105. * testBuild method
  106. *
  107. * @return void
  108. */
  109. public function testBuild() {
  110. $xml = '<tag>value</tag>';
  111. $obj = Xml::build($xml);
  112. $this->assertTrue($obj instanceof SimpleXMLElement);
  113. $this->assertEqual((string)$obj->getName(), 'tag');
  114. $this->assertEqual((string)$obj, 'value');
  115. $xml = '<?xml version="1.0" encoding="UTF-8"?><tag>value</tag>';
  116. $this->assertEqual($obj, Xml::build($xml));
  117. $obj = Xml::build($xml, array('return' => 'domdocument'));
  118. $this->assertTrue($obj instanceof DOMDocument);
  119. $this->assertEqual($obj->firstChild->nodeName, 'tag');
  120. $this->assertEqual($obj->firstChild->nodeValue, 'value');
  121. $xml = CAKE . 'Test' . DS . 'Fixture' . DS . 'sample.xml';
  122. $obj = Xml::build($xml);
  123. $this->assertEqual($obj->getName(), 'tags');
  124. $this->assertEqual(count($obj), 2);
  125. $this->assertEqual(Xml::build($xml), Xml::build(file_get_contents($xml)));
  126. $obj = Xml::build($xml, array('return' => 'domdocument'));
  127. $this->assertEqual($obj->firstChild->nodeName, 'tags');
  128. $this->assertEqual(Xml::build($xml, array('return' => 'domdocument')), Xml::build(file_get_contents($xml), array('return' => 'domdocument')));
  129. $this->assertEqual(Xml::build($xml, array('return' => 'simplexml')), Xml::build($xml, 'simplexml'));
  130. $xml = array('tag' => 'value');
  131. $obj = Xml::build($xml);
  132. $this->assertEqual($obj->getName(), 'tag');
  133. $this->assertEqual((string)$obj, 'value');
  134. $obj = Xml::build($xml, array('return' => 'domdocument'));
  135. $this->assertEqual($obj->firstChild->nodeName, 'tag');
  136. $this->assertEqual($obj->firstChild->nodeValue, 'value');
  137. $obj = Xml::build($xml, array('return' => 'domdocument', 'encoding' => null));
  138. $this->assertNoPattern('/encoding/', $obj->saveXML());
  139. }
  140. /**
  141. * data provider function for testBuildInvalidData
  142. *
  143. * @return array
  144. */
  145. public static function invalidDataProvider() {
  146. return array(
  147. array(null),
  148. array(false),
  149. array(''),
  150. array('<tag>')
  151. );
  152. }
  153. /**
  154. * testBuildInvalidData
  155. *
  156. * @dataProvider invalidDataProvider
  157. * @expectedException Exception
  158. * return void
  159. */
  160. public function testBuildInvalidData($value) {
  161. Xml::build($value);
  162. }
  163. /**
  164. * testFromArray method
  165. *
  166. * @return void
  167. */
  168. public function testFromArray() {
  169. $xml = array('tag' => 'value');
  170. $obj = Xml::fromArray($xml);
  171. $this->assertEqual($obj->getName(), 'tag');
  172. $this->assertEqual((string)$obj, 'value');
  173. $xml = array('tag' => null);
  174. $obj = Xml::fromArray($xml);
  175. $this->assertEqual($obj->getName(), 'tag');
  176. $this->assertEqual((string)$obj, '');
  177. $xml = array('tag' => array('@' => 'value'));
  178. $obj = Xml::fromArray($xml);
  179. $this->assertEqual($obj->getName(), 'tag');
  180. $this->assertEqual((string)$obj, 'value');
  181. $xml = array(
  182. 'tags' => array(
  183. 'tag' => array(
  184. array(
  185. 'id' => '1',
  186. 'name' => 'defect'
  187. ),
  188. array(
  189. 'id' => '2',
  190. 'name' => 'enhancement'
  191. )
  192. )
  193. )
  194. );
  195. $obj = Xml::fromArray($xml, 'attributes');
  196. $this->assertTrue($obj instanceof SimpleXMLElement);
  197. $this->assertEqual($obj->getName(), 'tags');
  198. $this->assertEqual(count($obj), 2);
  199. $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1" name="defect"/><tag id="2" name="enhancement"/></tags>';
  200. $this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
  201. $obj = Xml::fromArray($xml);
  202. $this->assertTrue($obj instanceof SimpleXMLElement);
  203. $this->assertEqual($obj->getName(), 'tags');
  204. $this->assertEqual(count($obj), 2);
  205. $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag><id>1</id><name>defect</name></tag><tag><id>2</id><name>enhancement</name></tag></tags>';
  206. $this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
  207. $xml = array(
  208. 'tags' => array(
  209. )
  210. );
  211. $obj = Xml::fromArray($xml);
  212. $this->assertEqual($obj->getName(), 'tags');
  213. $this->assertEqual((string)$obj, '');
  214. $xml = array(
  215. 'tags' => array(
  216. 'bool' => true,
  217. 'int' => 1,
  218. 'float' => 10.2,
  219. 'string' => 'ok',
  220. 'null' => null,
  221. 'array' => array()
  222. )
  223. );
  224. $obj = Xml::fromArray($xml, 'tags');
  225. $this->assertEqual(count($obj), 6);
  226. $this->assertIdentical((string)$obj->bool, '1');
  227. $this->assertIdentical((string)$obj->int, '1');
  228. $this->assertIdentical((string)$obj->float, '10.2');
  229. $this->assertIdentical((string)$obj->string, 'ok');
  230. $this->assertIdentical((string)$obj->null, '');
  231. $this->assertIdentical((string)$obj->array, '');
  232. $xml = array(
  233. 'tags' => array(
  234. 'tag' => array(
  235. array(
  236. '@id' => '1',
  237. 'name' => 'defect'
  238. ),
  239. array(
  240. '@id' => '2',
  241. 'name' => 'enhancement'
  242. )
  243. )
  244. )
  245. );
  246. $obj = Xml::fromArray($xml, 'tags');
  247. $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1"><name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
  248. $this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
  249. $xml = array(
  250. 'tags' => array(
  251. 'tag' => array(
  252. array(
  253. '@id' => '1',
  254. 'name' => 'defect',
  255. '@' => 'Tag 1'
  256. ),
  257. array(
  258. '@id' => '2',
  259. 'name' => 'enhancement'
  260. ),
  261. ),
  262. '@' => 'All tags'
  263. )
  264. );
  265. $obj = Xml::fromArray($xml, 'tags');
  266. $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags>All tags<tag id="1">Tag 1<name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
  267. $this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
  268. $xml = array(
  269. 'tags' => array(
  270. 'tag' => array(
  271. 'id' => 1,
  272. '@' => 'defect'
  273. )
  274. )
  275. );
  276. $obj = Xml::fromArray($xml, 'attributes');
  277. $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1">defect</tag></tags>';
  278. $this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
  279. }
  280. /**
  281. * data provider for fromArray() failures
  282. *
  283. * @return array
  284. */
  285. public static function invalidArrayDataProvider() {
  286. return array(
  287. array(''),
  288. array(null),
  289. array(false),
  290. array(array()),
  291. array(array('numeric key as root')),
  292. array(array('item1' => '', 'item2' => '')),
  293. array(array('items' => array('item1', 'item2'))),
  294. array(array(
  295. 'tags' => array(
  296. 'tag' => array(
  297. array(
  298. array(
  299. 'string'
  300. )
  301. )
  302. )
  303. )
  304. )),
  305. array(array(
  306. 'tags' => array(
  307. '@tag' => array(
  308. array(
  309. '@id' => '1',
  310. 'name' => 'defect'
  311. ),
  312. array(
  313. '@id' => '2',
  314. 'name' => 'enhancement'
  315. )
  316. )
  317. )
  318. )),
  319. array(new DateTime())
  320. );
  321. }
  322. /**
  323. * testFromArrayFail method
  324. *
  325. * @dataProvider invalidArrayDataProvider
  326. * @expectedException Exception
  327. */
  328. public function testFromArrayFail($value) {
  329. Xml::fromArray($value);
  330. }
  331. /**
  332. * testToArray method
  333. *
  334. * @return void
  335. */
  336. public function testToArray() {
  337. $xml = '<tag>name</tag>';
  338. $obj = Xml::build($xml);
  339. $this->assertEqual(Xml::toArray($obj), array('tag' => 'name'));
  340. $xml = CAKE . 'Test' . DS . 'Fixture' . DS . 'sample.xml';
  341. $obj = Xml::build($xml);
  342. $expected = array(
  343. 'tags' => array(
  344. 'tag' => array(
  345. array(
  346. '@id' => '1',
  347. 'name' => 'defect'
  348. ),
  349. array(
  350. '@id' => '2',
  351. 'name' => 'enhancement'
  352. )
  353. )
  354. )
  355. );
  356. $this->assertEqual(Xml::toArray($obj), $expected);
  357. $array = array(
  358. 'tags' => array(
  359. 'tag' => array(
  360. array(
  361. 'id' => '1',
  362. 'name' => 'defect'
  363. ),
  364. array(
  365. 'id' => '2',
  366. 'name' => 'enhancement'
  367. )
  368. )
  369. )
  370. );
  371. $this->assertEqual(Xml::toArray(Xml::fromArray($array, 'tags')), $array);
  372. $expected = array(
  373. 'tags' => array(
  374. 'tag' => array(
  375. array(
  376. '@id' => '1',
  377. '@name' => 'defect'
  378. ),
  379. array(
  380. '@id' => '2',
  381. '@name' => 'enhancement'
  382. )
  383. )
  384. )
  385. );
  386. $this->assertEqual(Xml::toArray(Xml::fromArray($array, 'attributes')), $expected);
  387. $this->assertEqual(Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument', 'format' => 'attributes'))), $expected);
  388. $this->assertEqual(Xml::toArray(Xml::fromArray($array)), $array);
  389. $this->assertEqual(Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument'))), $array);
  390. $array = array(
  391. 'tags' => array(
  392. 'tag' => array(
  393. 'id' => '1',
  394. 'posts' => array(
  395. array('id' => '1'),
  396. array('id' => '2')
  397. )
  398. ),
  399. 'tagOther' => array(
  400. 'subtag' => array(
  401. 'id' => '1'
  402. )
  403. )
  404. )
  405. );
  406. $expected = array(
  407. 'tags' => array(
  408. 'tag' => array(
  409. '@id' => '1',
  410. 'posts' => array(
  411. array('@id' => '1'),
  412. array('@id' => '2')
  413. )
  414. ),
  415. 'tagOther' => array(
  416. 'subtag' => array(
  417. '@id' => '1'
  418. )
  419. )
  420. )
  421. );
  422. $this->assertEqual(Xml::toArray(Xml::fromArray($array, 'attributes')), $expected);
  423. $this->assertEqual(Xml::toArray(Xml::fromArray($array, array('format' => 'attributes', 'return' => 'domdocument'))), $expected);
  424. $xml = '<root>';
  425. $xml .= '<tag id="1">defect</tag>';
  426. $xml .= '</root>';
  427. $obj = Xml::build($xml);
  428. $expected = array(
  429. 'root' => array(
  430. 'tag' => array(
  431. '@id' => 1,
  432. '@' => 'defect'
  433. )
  434. )
  435. );
  436. $this->assertEqual(Xml::toArray($obj), $expected);
  437. $xml = '<root>';
  438. $xml .= '<table xmlns="http://www.w3.org/TR/html4/"><tr><td>Apples</td><td>Bananas</td></tr></table>';
  439. $xml .= '<table xmlns="http://www.cakephp.org"><name>CakePHP</name><license>MIT</license></table>';
  440. $xml .= '<table>The book is on the table.</table>';
  441. $xml .= '</root>';
  442. $obj = Xml::build($xml);
  443. $expected = array(
  444. 'root' => array(
  445. 'table' => array(
  446. array('tr' => array('td' => array('Apples', 'Bananas'))),
  447. array('name' => 'CakePHP', 'license' => 'MIT'),
  448. 'The book is on the table.'
  449. )
  450. )
  451. );
  452. $this->assertEqual(Xml::toArray($obj), $expected);
  453. $xml = '<root xmlns:cake="http://www.cakephp.org/">';
  454. $xml .= '<tag>defect</tag>';
  455. $xml .= '<cake:bug>1</cake:bug>';
  456. $xml .= '</root>';
  457. $obj = Xml::build($xml);
  458. $expected = array(
  459. 'root' => array(
  460. 'tag' => 'defect',
  461. 'cake:bug' => 1
  462. )
  463. );
  464. $this->assertEqual(Xml::toArray($obj), $expected);
  465. }
  466. /**
  467. * testRss
  468. *
  469. * @return void
  470. */
  471. public function testRss() {
  472. $rss = file_get_contents(CAKE . 'Test' . DS . 'Fixture' . DS . 'rss.xml');
  473. $rssAsArray = Xml::toArray(Xml::build($rss));
  474. $this->assertEqual($rssAsArray['rss']['@version'], '2.0');
  475. $this->assertEqual(count($rssAsArray['rss']['channel']['item']), 2);
  476. $atomLink = array('@href' => 'http://bakery.cakephp.org/articles/rss', '@rel' => 'self', '@type' => 'application/rss+xml');
  477. $this->assertEqual($rssAsArray['rss']['channel']['atom:link'], $atomLink);
  478. $this->assertEqual($rssAsArray['rss']['channel']['link'], 'http://bakery.cakephp.org/');
  479. $expected = array(
  480. 'title' => 'Alertpay automated sales via IPN',
  481. 'link' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn',
  482. 'description' => 'I\'m going to show you how I implemented a payment module via the Alertpay payment processor.',
  483. 'pubDate' => 'Tue, 31 Aug 2010 01:42:00 -0500',
  484. 'guid' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn'
  485. );
  486. $this->assertIdentical($rssAsArray['rss']['channel']['item'][1], $expected);
  487. $rss = array(
  488. 'rss' => array(
  489. 'xmlns:atom' => 'http://www.w3.org/2005/Atom',
  490. '@version' => '2.0',
  491. 'channel' => array(
  492. 'atom:link' => array(
  493. '@href' => 'http://bakery.cakephp.org/articles/rss',
  494. '@rel' => 'self',
  495. '@type' => 'application/rss+xml'
  496. ),
  497. 'title' => 'The Bakery: ',
  498. 'link' => 'http://bakery.cakephp.org/',
  499. 'description' => 'Recent Articles at The Bakery.',
  500. 'pubDate' => 'Sun, 12 Sep 2010 04:18:26 -0500',
  501. 'item' => array(
  502. array(
  503. 'title' => 'CakePHP 1.3.4 released',
  504. 'link' => 'http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released'
  505. ),
  506. array(
  507. 'title' => 'Wizard Component 1.2 Tutorial',
  508. 'link' => 'http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial'
  509. )
  510. )
  511. )
  512. )
  513. );
  514. $rssAsSimpleXML = Xml::fromArray($rss);
  515. $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?>';
  516. $xmlText .= '<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">';
  517. $xmlText .= '<channel>';
  518. $xmlText .= '<atom:link href="http://bakery.cakephp.org/articles/rss" rel="self" type="application/rss+xml"/>';
  519. $xmlText .= '<title>The Bakery: </title>';
  520. $xmlText .= '<link>http://bakery.cakephp.org/</link>';
  521. $xmlText .= '<description>Recent Articles at The Bakery.</description>';
  522. $xmlText .= '<pubDate>Sun, 12 Sep 2010 04:18:26 -0500</pubDate>';
  523. $xmlText .= '<item><title>CakePHP 1.3.4 released</title><link>http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released</link></item>';
  524. $xmlText .= '<item><title>Wizard Component 1.2 Tutorial</title><link>http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial</link></item>';
  525. $xmlText .= '</channel></rss>';
  526. $this->assertEqual(str_replace(array("\r", "\n"), '', $rssAsSimpleXML->asXML()), $xmlText);
  527. }
  528. /**
  529. * testXmlRpc
  530. *
  531. * @return void
  532. */
  533. public function testXmlRpc() {
  534. $xml = Xml::build('<methodCall><methodName>test</methodName><params /></methodCall>');
  535. $expected = array(
  536. 'methodCall' => array(
  537. 'methodName' => 'test',
  538. 'params' => ''
  539. )
  540. );
  541. $this->assertIdentical(Xml::toArray($xml), $expected);
  542. $xml = Xml::build('<methodCall><methodName>test</methodName><params><param><value><array><data><value><int>12</int></value><value><string>Egypt</string></value><value><boolean>0</boolean></value><value><int>-31</int></value></data></array></value></param></params></methodCall>');
  543. $expected = array(
  544. 'methodCall' => array(
  545. 'methodName' => 'test',
  546. 'params' => array(
  547. 'param' => array(
  548. 'value' => array(
  549. 'array' => array(
  550. 'data' => array(
  551. 'value' => array(
  552. array('int' => '12'),
  553. array('string' => 'Egypt'),
  554. array('boolean' => '0'),
  555. array('int' => '-31')
  556. )
  557. )
  558. )
  559. )
  560. )
  561. )
  562. )
  563. );
  564. $this->assertIdentical(Xml::toArray($xml), $expected);
  565. $xmlText = '<?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value><array><data><value><int>1</int></value><value><string>testing</string></value></data></array></value></param></params></methodResponse>';
  566. $xml = Xml::build($xmlText);
  567. $expected = array(
  568. 'methodResponse' => array(
  569. 'params' => array(
  570. 'param' => array(
  571. 'value' => array(
  572. 'array' => array(
  573. 'data' => array(
  574. 'value' => array(
  575. array('int' => '1'),
  576. array('string' => 'testing')
  577. )
  578. )
  579. )
  580. )
  581. )
  582. )
  583. )
  584. );
  585. $this->assertIdentical(Xml::toArray($xml), $expected);
  586. $xml = Xml::fromArray($expected, 'tags');
  587. $this->assertEqual(str_replace(array("\r", "\n"), '', $xml->asXML()), $xmlText);
  588. }
  589. /**
  590. * testSoap
  591. *
  592. * @return void
  593. */
  594. public function testSoap() {
  595. $xmlRequest = Xml::build(CAKE . 'Test' . DS . 'Fixture' . DS . 'soap_request.xml');
  596. $expected = array(
  597. 'Envelope' => array(
  598. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  599. 'soap:Body' => array(
  600. 'm:GetStockPrice' => array(
  601. 'm:StockName' => 'IBM'
  602. )
  603. )
  604. )
  605. );
  606. $this->assertEqual(Xml::toArray($xmlRequest), $expected);
  607. $xmlResponse = Xml::build(CAKE . 'Test' . DS . 'Fixture' . DS . 'soap_response.xml');
  608. $expected = array(
  609. 'Envelope' => array(
  610. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  611. 'soap:Body' => array(
  612. 'm:GetStockPriceResponse' => array(
  613. 'm:Price' => '34.5'
  614. )
  615. )
  616. )
  617. );
  618. $this->assertEqual(Xml::toArray($xmlResponse), $expected);
  619. $xml = array(
  620. 'soap:Envelope' => array(
  621. 'xmlns:soap' => 'http://www.w3.org/2001/12/soap-envelope',
  622. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  623. 'soap:Body' => array(
  624. 'xmlns:m' => 'http://www.example.org/stock',
  625. 'm:GetStockPrice' => array(
  626. 'm:StockName' => 'IBM'
  627. )
  628. )
  629. )
  630. );
  631. $xmlRequest = Xml::fromArray($xml, array('encoding' => null));
  632. $xmlText = '<' . '?xml version="1.0"?>';
  633. $xmlText .= '<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">';
  634. $xmlText .= '<soap:Body xmlns:m="http://www.example.org/stock">';
  635. $xmlText .= '<m:GetStockPrice><m:StockName>IBM</m:StockName></m:GetStockPrice>';
  636. $xmlText .= '</soap:Body></soap:Envelope>';
  637. $this->assertEqual(str_replace(array("\r", "\n"), '', $xmlRequest->asXML()), $xmlText);
  638. }
  639. /**
  640. * testNamespace
  641. *
  642. * @retun void
  643. */
  644. public function testNamespace() {
  645. $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:tag id="1"><child>good</child><otherchild>bad</otherchild></ns:tag><tag>Tag without ns</tag></root>');
  646. $expected = array(
  647. 'root' => array(
  648. 'ns:tag' => array(
  649. '@id' => '1',
  650. 'child' => 'good',
  651. 'otherchild' => 'bad'
  652. ),
  653. 'tag' => 'Tag without ns'
  654. )
  655. );
  656. $this->assertEqual(Xml::toArray($xmlResponse), $expected);
  657. $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:tag id="1" /><tag><id>1</id></tag></root>');
  658. $expected = array(
  659. 'root' => array(
  660. 'ns:tag' => array(
  661. '@id' => '1'
  662. ),
  663. 'tag' => array(
  664. 'id' => '1'
  665. )
  666. )
  667. );
  668. $this->assertEqual(Xml::toArray($xmlResponse), $expected);
  669. $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:attr>1</ns:attr></root>');
  670. $expected = array(
  671. 'root' => array(
  672. 'ns:attr' => '1'
  673. )
  674. );
  675. $this->assertEqual(Xml::toArray($xmlResponse), $expected);
  676. $xmlResponse = Xml::build('<root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>');
  677. $this->assertEqual(Xml::toArray($xmlResponse), $expected);
  678. $xml = array(
  679. 'root' => array(
  680. 'ns:attr' => array(
  681. 'xmlns:ns' => 'http://cakephp.org',
  682. '@' => 1
  683. )
  684. )
  685. );
  686. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>';
  687. $xmlResponse = Xml::fromArray($xml);
  688. $this->assertEqual(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
  689. $xml = array(
  690. 'root' => array(
  691. 'tag' => array(
  692. 'xmlns:pref' => 'http://cakephp.org',
  693. 'pref:item' => array(
  694. 'item 1',
  695. 'item 2'
  696. )
  697. )
  698. )
  699. );
  700. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns:pref="http://cakephp.org"><pref:item>item 1</pref:item><pref:item>item 2</pref:item></tag></root>';
  701. $xmlResponse = Xml::fromArray($xml);
  702. $this->assertEqual(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
  703. $xml = array(
  704. 'root' => array(
  705. 'tag' => array(
  706. 'xmlns:' => 'http://cakephp.org'
  707. )
  708. )
  709. );
  710. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns="http://cakephp.org"/></root>';
  711. $xmlResponse = Xml::fromArray($xml);
  712. $this->assertEqual(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
  713. $xml = array(
  714. 'root' => array(
  715. 'xmlns:' => 'http://cakephp.org'
  716. )
  717. );
  718. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns="http://cakephp.org"/>';
  719. $xmlResponse = Xml::fromArray($xml);
  720. $this->assertEqual(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
  721. $xml = array(
  722. 'root' => array(
  723. 'xmlns:ns' => 'http://cakephp.org'
  724. )
  725. );
  726. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns:ns="http://cakephp.org"/>';
  727. $xmlResponse = Xml::fromArray($xml);
  728. $this->assertEqual(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
  729. }
  730. /**
  731. * test that CDATA blocks don't get screwed up by SimpleXml
  732. *
  733. * @return void
  734. */
  735. function testCdata() {
  736. $xml = '<' . '?xml version="1.0" encoding="UTF-8"?>' .
  737. '<people><name><![CDATA[ Mark ]]></name></people>';
  738. $result = Xml::build($xml);
  739. $this->assertEquals(' Mark ', (string)$result->name);
  740. }
  741. /**
  742. * data provider for toArray() failures
  743. *
  744. * @return array
  745. */
  746. public static function invalidToArrayDataProvider() {
  747. return array(
  748. array(new DateTime()),
  749. array(array())
  750. );
  751. }
  752. /**
  753. * testToArrayFail method
  754. *
  755. * @dataProvider invalidToArrayDataProvider
  756. * @expectedException Exception
  757. */
  758. public function testToArrayFail($value) {
  759. Xml::toArray($value);
  760. }
  761. /**
  762. * testWithModel method
  763. *
  764. * @return void
  765. */
  766. public function testWithModel() {
  767. $this->loadFixtures('User', 'Article');
  768. $user = new XmlUser();
  769. $data = $user->read(null, 1);
  770. $obj = Xml::build(compact('data'));
  771. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><data>';
  772. $expected .= '<User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>';
  773. $expected .= '<created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>';
  774. $expected .= '<Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>';
  775. $expected .= '<published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>';
  776. $expected .= '<Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>';
  777. $expected .= '<published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>';
  778. $expected .= '</data>';
  779. $this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $expected);
  780. }
  781. }