|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DocbookCS\Tests\Integration\Runner; |
| 6 | + |
| 7 | +use DocbookCS\Runner\EntityExpansionMarker; |
| 8 | +use DocbookCS\Runner\EntityPreprocessor; |
| 9 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 10 | +use PHPUnit\Framework\Attributes\Test; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +#[ |
| 14 | + CoversClass(EntityExpansionMarker::class), |
| 15 | + CoversClass(EntityPreprocessor::class), |
| 16 | +] |
| 17 | +final class EntityExpansionMarkerTest extends TestCase |
| 18 | +{ |
| 19 | + #[Test] |
| 20 | + public function itMarksXmlExpansionsForParsingOnly(): void |
| 21 | + { |
| 22 | + $preprocessor = new EntityPreprocessor([ |
| 23 | + 'expanded' => '<para>Expanded</para>', |
| 24 | + ]); |
| 25 | + $source = '<root>&expanded;</root>'; |
| 26 | + $document = $this->parse($preprocessor->processForParsing($source)); |
| 27 | + $para = $document->getElementsByTagName('para')->item(0); |
| 28 | + $root = $document->documentElement; |
| 29 | + |
| 30 | + self::assertSame('<root><para>Expanded</para></root>', $preprocessor->process($source)); |
| 31 | + self::assertInstanceOf(\DOMElement::class, $para); |
| 32 | + self::assertInstanceOf(\DOMElement::class, $root); |
| 33 | + self::assertTrue(EntityExpansionMarker::contains($para)); |
| 34 | + self::assertFalse(EntityExpansionMarker::contains($root)); |
| 35 | + } |
| 36 | + |
| 37 | + #[Test] |
| 38 | + public function itRecognizesNestedExpansionMarkers(): void |
| 39 | + { |
| 40 | + $preprocessor = new EntityPreprocessor([ |
| 41 | + 'outer' => '<wrapper>&inner;<after/></wrapper>', |
| 42 | + 'inner' => '<para/>', |
| 43 | + ]); |
| 44 | + $document = $this->parse($preprocessor->processForParsing('<root>&outer;</root>')); |
| 45 | + |
| 46 | + foreach (['wrapper', 'para', 'after'] as $elementName) { |
| 47 | + $element = $document->getElementsByTagName($elementName)->item(0); |
| 48 | + self::assertInstanceOf(\DOMElement::class, $element); |
| 49 | + self::assertTrue(EntityExpansionMarker::contains($element)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + #[Test] |
| 54 | + public function itDoesNotExpandLiteralEntityText(): void |
| 55 | + { |
| 56 | + $preprocessor = new EntityPreprocessor(['value' => 'expanded']); |
| 57 | + $source = '<root><!-- &value; --><![CDATA[&value;]]><?test &value;?>&value;</root>'; |
| 58 | + |
| 59 | + self::assertSame( |
| 60 | + '<root><!-- &value; --><![CDATA[&value;]]><?test &value;?>expanded</root>', |
| 61 | + $preprocessor->process($source), |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + private function parse(string $xml): \DOMDocument |
| 66 | + { |
| 67 | + $document = new \DOMDocument(); |
| 68 | + $document->loadXML($xml); |
| 69 | + |
| 70 | + return $document; |
| 71 | + } |
| 72 | +} |
0 commit comments