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

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

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