XMLTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. }
  24. public function testArrayToXML()
  25. {
  26. $data = [
  27. 'tests' => [
  28. 'name' => 'test',
  29. 'value' => 1,
  30. ],
  31. ];
  32. $this->assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tests><name>test</name><value>1</value></tests>", XML::toXML($data));
  33. }
  34. public function testObjectToXML()
  35. {
  36. $obj = new tests();
  37. $obj->name = 'test';
  38. $obj->value = 1;
  39. $this->assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tests><name>test</name><value>1</value></tests>", XML::toXML($obj));
  40. }
  41. }
  42. class tests
  43. {
  44. public $name = '';
  45. public $value = 0;
  46. }