|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the CleverAge/ProcessBundle package. |
| 7 | + * |
| 8 | + * Copyright (c) Clever-Age |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CleverAge\ProcessBundle\Tests\Filesystem; |
| 15 | + |
| 16 | +use CleverAge\ProcessBundle\Filesystem\XmlFile; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +#[\PHPUnit\Framework\Attributes\CoversClass(XmlFile::class)] |
| 20 | +class XmlFileTest extends TestCase |
| 21 | +{ |
| 22 | + private string $path; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + $path = tempnam(sys_get_temp_dir(), 'xml_file_test_'); |
| 27 | + self::assertIsString($path); |
| 28 | + $this->path = $path; |
| 29 | + } |
| 30 | + |
| 31 | + protected function tearDown(): void |
| 32 | + { |
| 33 | + if (is_file($this->path)) { |
| 34 | + unlink($this->path); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public function testReadValidXml(): void |
| 39 | + { |
| 40 | + file_put_contents($this->path, '<root><a>1</a></root>'); |
| 41 | + |
| 42 | + $dom = (new XmlFile($this->path))->read(); |
| 43 | + |
| 44 | + self::assertNotNull($dom->documentElement); |
| 45 | + self::assertSame('root', $dom->documentElement->nodeName); |
| 46 | + self::assertSame('1', $dom->documentElement->textContent); |
| 47 | + } |
| 48 | + |
| 49 | + public function testReadEmptyFileThrows(): void |
| 50 | + { |
| 51 | + file_put_contents($this->path, ''); |
| 52 | + |
| 53 | + $this->expectException(\UnexpectedValueException::class); |
| 54 | + $this->expectExceptionMessage(\sprintf('XML file "%s" is empty', $this->path)); |
| 55 | + |
| 56 | + (new XmlFile($this->path))->read(); |
| 57 | + } |
| 58 | + |
| 59 | + public function testReadMalformedXmlThrowsWithLibxmlMessage(): void |
| 60 | + { |
| 61 | + file_put_contents($this->path, '<root><a>1</a>'); |
| 62 | + |
| 63 | + try { |
| 64 | + (new XmlFile($this->path))->read(); |
| 65 | + self::fail('An exception should have been thrown'); |
| 66 | + } catch (\UnexpectedValueException $e) { |
| 67 | + self::assertStringContainsString(\sprintf('Invalid XML in file "%s"', $this->path), $e->getMessage()); |
| 68 | + self::assertStringContainsString('Premature end of data in tag root', $e->getMessage()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public function testReadNotXmlThrows(): void |
| 73 | + { |
| 74 | + file_put_contents($this->path, 'not xml at all'); |
| 75 | + |
| 76 | + $this->expectException(\UnexpectedValueException::class); |
| 77 | + $this->expectExceptionMessage("Start tag expected, '<' not found"); |
| 78 | + |
| 79 | + (new XmlFile($this->path))->read(); |
| 80 | + } |
| 81 | + |
| 82 | + public function testReadUndefinedNamespacePrefixThrows(): void |
| 83 | + { |
| 84 | + file_put_contents($this->path, '<root><x:a/></root>'); |
| 85 | + |
| 86 | + $this->expectException(\UnexpectedValueException::class); |
| 87 | + $this->expectExceptionMessage('Namespace prefix x on a is not defined'); |
| 88 | + |
| 89 | + (new XmlFile($this->path))->read(); |
| 90 | + } |
| 91 | + |
| 92 | + public function testReadRestoresLibxmlErrorHandling(): void |
| 93 | + { |
| 94 | + file_put_contents($this->path, '<root>'); |
| 95 | + $previous = libxml_use_internal_errors(false); |
| 96 | + |
| 97 | + try { |
| 98 | + (new XmlFile($this->path))->read(); |
| 99 | + self::fail('An exception should have been thrown'); |
| 100 | + } catch (\UnexpectedValueException) { |
| 101 | + self::assertFalse(libxml_use_internal_errors()); |
| 102 | + self::assertSame([], libxml_get_errors()); |
| 103 | + } finally { |
| 104 | + libxml_use_internal_errors($previous); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public function testWriteThenRead(): void |
| 109 | + { |
| 110 | + $dom = new \DOMDocument(); |
| 111 | + $dom->loadXML('<root><a>ok</a></root>'); |
| 112 | + |
| 113 | + (new XmlFile($this->path, 'wb'))->write($dom); |
| 114 | + $read = (new XmlFile($this->path))->read(); |
| 115 | + |
| 116 | + self::assertSame('ok', $read->documentElement?->textContent); |
| 117 | + } |
| 118 | +} |
0 commit comments