XMLTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace AlibabaCloud\Tea\XML\Tests;
  3. use AlibabaCloud\Tea\XML\XML;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * @internal
  7. * @coversNothing
  8. */
  9. class RpcUtilsTest extends TestCase
  10. {
  11. private $xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
  12. "<tests>\n" .
  13. " <name>test</name>\n" .
  14. " <value>1</value>\n" .
  15. "</tests>\n";
  16. public function testParseXml()
  17. {
  18. $res = XML::parseXml($this->xmlStr, new tests());
  19. $name = $res['name'];
  20. $value = $res['value'];
  21. $this->assertEquals('test', $name);
  22. $this->assertEquals('1', $value);
  23. $res = XML::parseXml($this->xmlStr, null);
  24. $name = $res['name'];
  25. $value = $res['value'];
  26. $this->assertEquals('test', $name);
  27. $this->assertEquals('1', $value);
  28. }
  29. public function testArrayToXML()
  30. {
  31. $data = [
  32. 'tests' => [
  33. 'name' => 'test',
  34. 'value' => 1,
  35. ],
  36. ];
  37. $this->assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tests><name>test</name><value>1</value></tests>", XML::toXML($data));
  38. }
  39. public function testObjectToXML()
  40. {
  41. $obj = new tests();
  42. $obj->name = 'test';
  43. $obj->value = 1;
  44. $this->assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tests><name>test</name><value>1</value></tests>", XML::toXML($obj));
  45. }
  46. }
  47. class tests
  48. {
  49. public $name = '';
  50. public $value = 0;
  51. }