|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace phpDocumentor\Guides\Compiler\NodeTransformers; |
| 6 | + |
| 7 | +use phpDocumentor\Guides\Compiler\CompilerContext; |
| 8 | +use phpDocumentor\Guides\Meta\DocumentReferenceEntry; |
| 9 | +use phpDocumentor\Guides\Nodes\ContentMenuNode; |
| 10 | +use phpDocumentor\Guides\Nodes\DocumentNode; |
| 11 | +use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode; |
| 12 | +use phpDocumentor\Guides\Nodes\DocumentTree\SectionEntryNode; |
| 13 | +use phpDocumentor\Guides\Nodes\InlineCompoundNode; |
| 14 | +use phpDocumentor\Guides\Nodes\Menu\MenuEntry as TocEntry; |
| 15 | +use phpDocumentor\Guides\Nodes\Menu\TocNode; |
| 16 | +use phpDocumentor\Guides\Nodes\ProjectNode; |
| 17 | +use phpDocumentor\Guides\Nodes\TitleNode; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +final class MenuNodeTransformerTest extends TestCase |
| 21 | +{ |
| 22 | + private static function getCompilerContext(string $path): CompilerContext |
| 23 | + { |
| 24 | + return (new CompilerContext(self::givenProjectNode()))->withShadowTree(new DocumentNode('123', $path)); |
| 25 | + } |
| 26 | + |
| 27 | + public function testSimpleFlatToc(): void |
| 28 | + { |
| 29 | + $node = (new TocNode(['index', 'page2']))->withOptions(['maxdepth' => 1]); |
| 30 | + $transformer = new MenuNodeTransformer(); |
| 31 | + |
| 32 | + $transformedNode = $transformer->enterNode($node, self::getCompilerContext('some/path')); |
| 33 | + |
| 34 | + self::assertEquals( |
| 35 | + [ |
| 36 | + new TocEntry( |
| 37 | + 'index', |
| 38 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1'), 1, 'title-1'), |
| 39 | + ), |
| 40 | + new TocEntry( |
| 41 | + 'page2', |
| 42 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 2'), 1, 'title-2'), |
| 43 | + ), |
| 44 | + ], |
| 45 | + $transformedNode->getMenuEntries(), |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function testTocEntryIsActive(): void |
| 50 | + { |
| 51 | + $node = (new TocNode(['index', 'page2']))->withOptions(['maxdepth' => 1]); |
| 52 | + $transformer = new MenuNodeTransformer(); |
| 53 | + |
| 54 | + $transformedNode = $transformer->enterNode($node, self::getCompilerContext('index')); |
| 55 | + |
| 56 | + self::assertEquals( |
| 57 | + [ |
| 58 | + (new TocEntry( |
| 59 | + 'index', |
| 60 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1'), 1, 'title-1'), |
| 61 | + ))->withOptions(['active' => 'true']), |
| 62 | + new TocEntry( |
| 63 | + 'page2', |
| 64 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 2'), 1, 'title-2'), |
| 65 | + ), |
| 66 | + ], |
| 67 | + $transformedNode->getMenuEntries(), |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function testSimpleContents(): void |
| 72 | + { |
| 73 | + $node = (new ContentMenuNode(['index']))->withOptions(['depth' => 1]); |
| 74 | + $transformer = new MenuNodeTransformer(); |
| 75 | + |
| 76 | + $transformedNode = $transformer->enterNode($node, self::getCompilerContext('some/path')); |
| 77 | + |
| 78 | + self::assertEquals( |
| 79 | + [ |
| 80 | + new TocEntry( |
| 81 | + 'index', |
| 82 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1'), 1, 'title-1'), |
| 83 | + ), |
| 84 | + ], |
| 85 | + $transformedNode->getMenuEntries(), |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + public function testTocWithChildNodes(): void |
| 90 | + { |
| 91 | + $node = (new TocNode(['index', 'page2']))->withOptions(['maxdepth' => 2]); |
| 92 | + $transformer = new MenuNodeTransformer(); |
| 93 | + |
| 94 | + $transformedNode = $transformer->enterNode($node, self::getCompilerContext('some/path')); |
| 95 | + |
| 96 | + $entry = new TocEntry( |
| 97 | + 'index', |
| 98 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1'), 1, 'title-1'), |
| 99 | + [ |
| 100 | + new TocEntry( |
| 101 | + 'index', |
| 102 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1.1'), 2, 'title-1-1'), |
| 103 | + ), |
| 104 | + new TocEntry( |
| 105 | + 'index', |
| 106 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1.2'), 2, 'title-1-2'), |
| 107 | + ), |
| 108 | + ], |
| 109 | + ); |
| 110 | + |
| 111 | + self::assertEquals( |
| 112 | + [ |
| 113 | + $entry, |
| 114 | + new TocEntry( |
| 115 | + 'page2', |
| 116 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 2'), 1, 'title-2'), |
| 117 | + ), |
| 118 | + ], |
| 119 | + $transformedNode->getMenuEntries(), |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + public function testTocWithDocumentReferences(): void |
| 124 | + { |
| 125 | + $node = (new TocNode(['page3']))->withOptions(['maxdepth' => 3]); |
| 126 | + $transformer = new MenuNodeTransformer(); |
| 127 | + |
| 128 | + $transformedNode = $transformer->enterNode($node, self::getCompilerContext('some/path')); |
| 129 | + |
| 130 | + $entry = new TocEntry( |
| 131 | + 'index', |
| 132 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1'), 1, 'title-1'), |
| 133 | + [ |
| 134 | + new TocEntry( |
| 135 | + 'index', |
| 136 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1.1'), 2, 'title-1-1'), |
| 137 | + ), |
| 138 | + new TocEntry( |
| 139 | + 'index', |
| 140 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1.2'), 2, 'title-1-2'), |
| 141 | + ), |
| 142 | + ], |
| 143 | + ); |
| 144 | + |
| 145 | + self::assertEquals( |
| 146 | + [ |
| 147 | + new TocEntry( |
| 148 | + 'page3', |
| 149 | + new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 3'), 1, 'title-3'), |
| 150 | + ), |
| 151 | + $entry, |
| 152 | + ], |
| 153 | + $transformedNode->getMenuEntries(), |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + private static function givenProjectNode(): ProjectNode |
| 158 | + { |
| 159 | + $indexDoc = new DocumentEntryNode('index', TitleNode::emptyNode()); |
| 160 | + $section = new SectionEntryNode(new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1'), 1, 'title-1')); |
| 161 | + $subSection = new SectionEntryNode(new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1.1'), 2, 'title-1-1')); |
| 162 | + $section->addChild($subSection); |
| 163 | + $section->addChild(new SectionEntryNode(new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 1.2'), 2, 'title-1-2'))); |
| 164 | + $indexDoc->addChild($section); |
| 165 | + |
| 166 | + $page2 = new DocumentEntryNode('page2', TitleNode::emptyNode()); |
| 167 | + $page2->addChild(new SectionEntryNode(new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 2'), 1, 'title-2'))); |
| 168 | + |
| 169 | + $page3 = new DocumentEntryNode('page3', TitleNode::emptyNode()); |
| 170 | + $page3->addChild(new SectionEntryNode(new TitleNode(InlineCompoundNode::getPlainTextInlineNode('Title 3'), 1, 'title-3'))); |
| 171 | + $page3->addChild(new DocumentReferenceEntry('index')); |
| 172 | + |
| 173 | + $projectNode = new ProjectNode(); |
| 174 | + $projectNode->setDocumentEntries([ |
| 175 | + 'index' => $indexDoc, |
| 176 | + 'page2' => $page2, |
| 177 | + 'page3' => $page3, |
| 178 | + ]); |
| 179 | + |
| 180 | + return $projectNode; |
| 181 | + } |
| 182 | +} |
0 commit comments