Skip to content

Commit e672eee

Browse files
committed
Move Menu Entry Creation to TocNodeWithDocumentEntryTransformer
1 parent 6301e35 commit e672eee

File tree

6 files changed

+187
-7
lines changed

6 files changed

+187
-7
lines changed

packages/guides/src/Compiler/NodeTransformers/DocumentEntryRegistrationTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function leaveNode(Node $node, CompilerContext $compilerContext): Node|nu
3535
$this->logger->warning('Document has not title', $node->getLoggerInformation());
3636
}
3737

38-
$entry = new DocumentEntryNode($node->getFilePath(), $node->getTitle()??TitleNode::emptyNode());
38+
$entry = new DocumentEntryNode($node->getFilePath(), $node->getTitle() ?? TitleNode::emptyNode());
3939
$compilerContext->getProjectNode()->addDocumentEntry($entry);
4040

4141
return $node->setDocumentEntry($entry);

packages/guides/src/Nodes/DocumentNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public function getFootnoteTargetAnonymous(): FootnoteTarget|null
246246
return null;
247247
}
248248

249-
public function getDocumentEntry(): DocumentEntryNode
249+
public function getDocumentEntry(): DocumentEntryNode|null
250250
{
251251
if ($this->documentEntry === null) {
252252
throw new Exception('DocumentEntry may not be accessed before initialization');

packages/guides/tests/unit/Compiler/NodeTransformers/DocumentEntryRegistrationTransformerTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<?php
22
declare(strict_types=1);
33

4+
declare(strict_types=1);
5+
46
namespace phpDocumentor\Guides\Compiler\NodeTransformers;
57

6-
use Monolog\Logger;
78
use phpDocumentor\Guides\Compiler\CompilerContext;
8-
use phpDocumentor\Guides\Nodes\ClassNode;
99
use phpDocumentor\Guides\Nodes\DocumentNode;
1010
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
1111
use phpDocumentor\Guides\Nodes\ProjectNode;
1212
use phpDocumentor\Guides\Nodes\SectionNode;
1313
use phpDocumentor\Guides\Nodes\TitleNode;
14-
use PHPUnit\Framework\MockObject\Invocation;
1514
use PHPUnit\Framework\TestCase;
1615
use Psr\Log\LoggerInterface;
1716

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
toctrees do not work currently
1+
toctrees only support (and imply) :titlesonly: currently

tests/Integration/tests/toctree-titlesonly/input/skip

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)