XMLSerializerTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php declare(strict_types = 1);
  2. namespace TheSeer\Tokenizer;
  3. use PHPUnit\Framework\TestCase;
  4. /**
  5. * @covers \TheSeer\Tokenizer\XMLSerializer
  6. */
  7. class XMLSerializerTest extends TestCase {
  8. /** @var TokenCollection $tokens */
  9. private $tokens;
  10. protected function setUp() {
  11. $this->tokens = unserialize(
  12. file_get_contents(__DIR__ . '/_files/test.php.tokens'),
  13. [TokenCollection::class]
  14. );
  15. }
  16. public function testCanBeSerializedToXml() {
  17. $expected = file_get_contents(__DIR__ . '/_files/test.php.xml');
  18. $serializer = new XMLSerializer();
  19. $this->assertEquals($expected, $serializer->toXML($this->tokens));
  20. }
  21. public function testCanBeSerializedToDomDocument() {
  22. $serializer = new XMLSerializer();
  23. $result = $serializer->toDom($this->tokens);
  24. $this->assertInstanceOf(\DOMDocument::class, $result);
  25. $this->assertEquals('source', $result->documentElement->localName);
  26. }
  27. public function testCanBeSerializedToXmlWithCustomNamespace() {
  28. $expected = file_get_contents(__DIR__ . '/_files/customns.xml');
  29. $serializer = new XMLSerializer(new NamespaceUri('custom:xml:namespace'));
  30. $this->assertEquals($expected, $serializer->toXML($this->tokens));
  31. }
  32. public function testEmptyCollectionCreatesEmptyDocument() {
  33. $expected = file_get_contents(__DIR__ . '/_files/empty.xml');
  34. $serializer = new XMLSerializer();
  35. $this->assertEquals($expected, $serializer->toXML((new TokenCollection())));
  36. }
  37. }