RDFParserTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * This file is part of the PHPExiftool package.
  4. *
  5. * (c) Alchemy <support@alchemy.fr>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace PHPExiftool\Test;
  11. use PHPExiftool\RDFParser;
  12. class RDFParserTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var RDFParser
  16. */
  17. protected $object;
  18. protected function setUp()
  19. {
  20. $this->object = new RDFParser;
  21. }
  22. /**
  23. * @covers PHPExiftool\RDFParser::open
  24. */
  25. public function testOpen()
  26. {
  27. $this->object->open(file_get_contents(__DIR__ . '/../../../files/simplefile.xml'));
  28. }
  29. /**
  30. * @covers PHPExiftool\RDFParser::close
  31. */
  32. public function testClose()
  33. {
  34. $this->object->close();
  35. }
  36. /**
  37. * @covers PHPExiftool\RDFParser::ParseEntities
  38. * @covers PHPExiftool\RDFParser::getDom
  39. * @covers PHPExiftool\RDFParser::getDomXpath
  40. * @covers PHPExiftool\RDFParser::getNamespacesFromXml
  41. */
  42. public function testParseEntities()
  43. {
  44. $entities = $this->object
  45. ->open(file_get_contents(__DIR__ . '/../../../files/simplefile.xml'))
  46. ->parseEntities();
  47. $this->assertInstanceOf('\\Doctrine\\Common\\Collections\\ArrayCollection', $entities);
  48. $this->assertEquals(1, count($entities));
  49. $this->assertInstanceOf('\\PHPExiftool\\FileEntity', $entities->first());
  50. }
  51. /**
  52. * @covers PHPExiftool\RDFParser::ParseEntities
  53. * @covers PHPExiftool\RDFParser::getDom
  54. * @covers PHPExiftool\RDFParser::getDomXpath
  55. * @covers \PHPExiftool\Exception\LogicException
  56. * @expectedException \PHPExiftool\Exception\LogicException
  57. */
  58. public function testParseEntitiesWithoutDom()
  59. {
  60. $this->object->parseEntities();
  61. }
  62. /**
  63. * @covers PHPExiftool\RDFParser::ParseEntities
  64. * @covers PHPExiftool\RDFParser::getDom
  65. * @covers PHPExiftool\RDFParser::getDomXpath
  66. * @covers \PHPExiftool\Exception\ParseError
  67. * @covers \PHPExiftool\Exception\RuntimeException
  68. * @expectedException \PHPExiftool\Exception\RuntimeException
  69. */
  70. public function testParseEntitiesWrongDom()
  71. {
  72. $this->object->open('wrong xml')->parseEntities();
  73. }
  74. /**
  75. * @covers PHPExiftool\RDFParser::ParseMetadatas
  76. * @covers PHPExiftool\RDFParser::getDom
  77. * @covers PHPExiftool\RDFParser::getDomXpath
  78. */
  79. public function testParseMetadatas()
  80. {
  81. $metadatas = $this->object
  82. ->open(file_get_contents(__DIR__ . '/../../../files/ExifTool.xml'))
  83. ->ParseMetadatas();
  84. $this->assertInstanceOf('\\PHPExiftool\\Driver\\Metadata\\MetadataBag', $metadatas);
  85. $this->assertEquals(349, count($metadatas));
  86. }
  87. /**
  88. * @covers PHPExiftool\RDFParser::Query
  89. * @covers PHPExiftool\RDFParser::readNodeValue
  90. */
  91. public function testQuery()
  92. {
  93. $xml = "<?xml version='1.0' encoding='UTF-8'?>
  94. <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
  95. <rdf:Description xmlns:NeutronSpace='http://ns.exiftool.ca/NeutronSpace/1.0/'>
  96. <NeutronSpace:SpecialRomain>Hello World !</NeutronSpace:SpecialRomain>
  97. <NeutronSpace:SpecialRomainbase64 rdf:datatype='http://www.w3.org/2001/XMLSchema#base64Binary'>SGVsbG8gYmFzZTY0ICE=</NeutronSpace:SpecialRomainbase64>
  98. <NeutronSpace:Multi>
  99. <rdf:Bag>
  100. <rdf:li>romain</rdf:li>
  101. <rdf:li>neutron</rdf:li>
  102. </rdf:Bag>
  103. </NeutronSpace:Multi>
  104. </rdf:Description>
  105. </rdf:RDF>";
  106. $this->object->open($xml);
  107. $metadata_simple = $this->object->Query('NeutronSpace:SpecialRomain');
  108. $metadata_base64 = $this->object->Query('NeutronSpace:SpecialRomainbase64');
  109. $metadata_multi = $this->object->Query('NeutronSpace:Multi');
  110. $null_datas = $this->object->Query('NeutronSpace:NoData');
  111. $null_datas_2 = $this->object->Query('NamespaceUnknown:NoData');
  112. $this->assertNull($null_datas);
  113. $this->assertNull($null_datas_2);
  114. $this->assertInstanceOf('\\PHPExiftool\\Driver\\Value\\Mono', $metadata_simple);
  115. $this->assertInstanceOf('\\PHPExiftool\\Driver\\Value\\Binary', $metadata_base64);
  116. $this->assertInstanceOf('\\PHPExiftool\\Driver\\Value\\Multi', $metadata_multi);
  117. $this->assertEquals('Hello World !', $metadata_simple->asString());
  118. $this->assertEquals('Hello base64 !', $metadata_base64->asString());
  119. $this->assertEquals(array('romain', 'neutron'), $metadata_multi->asArray());
  120. }
  121. }