Skip to content

Commit 85da04a

Browse files
committed
Fix tests to pass
1 parent ca869ec commit 85da04a

File tree

9 files changed

+80
-14
lines changed

9 files changed

+80
-14
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use SplStack;
1515
use Webmozart\Assert\Assert;
1616

17+
use function assert;
18+
1719
/** @implements NodeTransformer<DocumentNode|AnchorNode> */
1820
final class CollectLinkTargetsTransformer implements NodeTransformer
1921
{
@@ -36,12 +38,16 @@ public function enterNode(Node $node, CompilerContext $compilerContext): Node
3638
$this->documentStack->push($node);
3739
} elseif ($node instanceof AnchorNode) {
3840
$currentDocument = $compilerContext->getDocumentNode();
39-
$parentSection = $compilerContext->getShadowTree()->getParent()->getNode();
41+
$parentSection = $compilerContext->getShadowTree()->getParent()?->getNode();
4042
assert($parentSection instanceof SectionNode);
4143

4244
$compilerContext->getProjectNode()->addLinkTarget(
4345
$node->toString(),
44-
new InternalTarget($currentDocument->getFilePath(), $node->toString()),
46+
new InternalTarget(
47+
$currentDocument->getFilePath(),
48+
$node->toString(),
49+
$parentSection->getTitle()->toString(),
50+
),
4551
);
4652
} elseif ($node instanceof SectionNode) {
4753
$currentDocument = $this->documentStack->top();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function leaveNode(Node $node, CompilerContext $compilerContext): Node|nu
3535

3636
$this->seen[spl_object_hash($node)] = spl_object_hash($node);
3737
$parent = $compilerContext->getShadowTree()->getParent();
38+
if ($parent === null) {
39+
throw new LogicException('Node not found in shadow tree');
40+
}
41+
3842
$position = $parent->findPosition($node);
3943
if ($position === null) {
4044
throw new LogicException('Node not found in shadow tree');

packages/guides/src/Nodes/CompoundNode.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function addChildNode(Node $node): void
4040
$this->value[] = $node;
4141
}
4242

43+
/** @param TValue $node */
4344
public function pushChildNode(Node $node): void
4445
{
4546
array_unshift($this->value, $node);

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ protected function setUp(): void
2424
/** @return iterable<MoveAnchorTransformer> */
2525
public function getTransformers(): iterable
2626
{
27+
//phpstan:ignore-next-line
2728
yield new MoveAnchorTransformer();
2829
}
2930

@@ -98,12 +99,11 @@ public function testAnchorShouldNotBeMovedTwice(): void
9899

99100
self::assertCount(1, $context->getDocumentNode()->getChildren());
100101
$updatedSection = $context->getDocumentNode()->getChildren()[0];
101-
102-
self::assertCount(2, $updatedSection->getChildren());
103-
self::assertEquals([$node1, $subSection], $context->getDocumentNode()->getChildren()[0]->getChildren());
104-
102+
self::assertInstanceOf(SectionNode::class, $updatedSection);
103+
self::assertEquals([$node1, $subSection], $updatedSection->getChildren());
105104
$updatedSubSection = $updatedSection->getChildren()[1];
106-
self::assertCount(1, $updatedSubSection->getChildren());
105+
self::assertInstanceOf(SectionNode::class, $updatedSubSection);
106+
self::assertEquals([new AnchorNode('bar')], $updatedSubSection->getChildren());
107107
}
108108

109109
public function testNoMoveWhenAnchorIsOnlyChild(): void
@@ -140,8 +140,11 @@ public function testMoveAnchorsAtTheEndOfSectionToNextSection(): void
140140
$this->documentNodeTraverser->traverse($document, $context);
141141

142142
self::assertCount(2, $context->getDocumentNode()->getChildren());
143-
self::assertCount(0, $context->getDocumentNode()->getChildren()[0]->getChildren());
144-
self::assertCount(2, $context->getDocumentNode()->getChildren()[1]->getChildren());
143+
[$firstChild, $secondChild] = $context->getDocumentNode()->getChildren();
144+
self::assertInstanceOf(SectionNode::class, $firstChild);
145+
self::assertInstanceOf(SectionNode::class, $secondChild);
146+
self::assertCount(0, $firstChild->getChildren());
147+
self::assertCount(2, $secondChild->getChildren());
145148
}
146149

147150
public function testMoveAnchorsAtTheEndOfSectionToNextParentNeighbourSection(): void
@@ -165,7 +168,10 @@ public function testMoveAnchorsAtTheEndOfSectionToNextParentNeighbourSection():
165168
$this->documentNodeTraverser->traverse($document, $context);
166169

167170
self::assertCount(2, $context->getDocumentNode()->getChildren());
168-
self::assertCount(1, $context->getDocumentNode()->getChildren()[0]->getChildren());
169-
self::assertCount(2, $context->getDocumentNode()->getChildren()[1]->getChildren());
171+
[$firstChild, $secondChild] = $context->getDocumentNode()->getChildren();
172+
self::assertInstanceOf(SectionNode::class, $firstChild);
173+
self::assertInstanceOf(SectionNode::class, $secondChild);
174+
self::assertCount(1, $firstChild->getChildren());
175+
self::assertCount(2, $secondChild->getChildren());
170176
}
171177
}

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ parameters:
2424
- packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/TextRoleRule.php
2525
- packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/VariableInlineRule.php
2626

27+
-
28+
message: '#^Return type \(iterable<phpDocumentor\\Guides\\Compiler\\NodeTransformers\\MoveAnchorTransformer>\) of method class@anonymous\/guides\/tests\/unit\/Compiler\/.*#'
29+
paths:
30+
- packages/guides/tests/unit/Compiler/NodeTransformers/MoveAnchorTransformerTest.php
2731
paths:
2832
- packages/guides/src
2933
- packages/guides-markdown/src
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Overview</title>
5+
</head>
6+
<body>
7+
<div class="section" id="overview">
8+
<h1>Overview</h1>
9+
<div class="section" id="overview-1">
10+
<h2>Overview 1</h2>
11+
<a id="rst-overview"></a>
12+
<p>RST Overview content</p>
13+
</div>
14+
<div class="section" id="overview-2">
15+
<h2>Overview 2</h2>
16+
<a id="sphinx-overview"></a>
17+
<p>Sphinx Overview content</p>
18+
</div>
19+
</div>
20+
</body>
21+
</html>

tests/Integration/tests/anchor-to-page/expected/objects.inv.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
"index.html#page",
2727
"Page"
2828
],
29+
"overview": [
30+
null,
31+
null,
32+
"sphinx-overview.html#overview",
33+
"Overview"
34+
],
2935
"rst-overview": [
3036
null,
3137
null,
@@ -39,4 +45,4 @@
3945
"Overview"
4046
]
4147
}
42-
}
48+
}

tests/Integration/tests/inventory_ref_json/expected/objects.inv.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,24 @@
3838
]
3939
},
4040
"std:label": {
41+
"document-title": [
42+
"My Project",
43+
"3.1.4",
44+
"index.html#document-title",
45+
"Document Title"
46+
],
4147
"my_index": [
4248
"My Project",
4349
"3.1.4",
4450
"index.html#my_index",
4551
"Document Title"
4652
],
53+
"page-1": [
54+
"My Project",
55+
"3.1.4",
56+
"page1.html#page-1",
57+
"Page 1"
58+
],
4759
"my_cool_page": [
4860
"My Project",
4961
"3.1.4",
@@ -62,6 +74,12 @@
6274
"subfolder\/index.html#subfolder-index",
6375
"Subfolder Index"
6476
],
77+
"subpage-1": [
78+
"My Project",
79+
"3.1.4",
80+
"subfolder\/subpage1.html#subpage-1",
81+
"Subpage 1"
82+
],
6583
"another_cool_page": [
6684
"My Project",
6785
"3.1.4",
@@ -75,4 +93,4 @@
7593
"Subpage 2"
7694
]
7795
}
78-
}
96+
}

tests/Integration/tests/references/expected/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h1>Overview</h1>
1414
<a id="Other Overview"></a>
1515
<p>Other Overview content</p>
1616
<p>This is a link to the RST Overview: <a href="/index.html#RST Overview">alternative</a></p>
17-
<p>This is a link to the Other Overview: <a href="/index.html#Other Overview">Other Overview</a></p>
17+
<p>This is a link to the Other Overview: <a href="/index.html#Other Overview">Overview</a></p>
1818
<p>This is a link to <a href="/page.html">Page 1</a>
1919
This is a link to with alternative text <a href="/page.html">alternative</a></p>
2020
</div>

0 commit comments

Comments
 (0)