2121use phpDocumentor \Guides \Nodes \SectionNode ;
2222use phpDocumentor \Guides \Nodes \TitleNode ;
2323use PHPUnit \Framework \TestCase ;
24- use Symfony \Component \String \Slugger \AsciiSlugger ;
2524
2625final class ImplicitHyperlinkTargetPassTest extends TestCase
2726{
2827 public function testAllImplicitUniqueSections (): void
2928 {
3029 $ document = new DocumentNode ('1 ' , 'index ' );
31- $ expected = new DocumentNode ( ' 1 ' , 'index ' );
32- $ slugger = new AsciiSlugger ( );
33- foreach (['Document 1 ' , 'Section A ' , 'Section B ' ] as $ titles ) {
34- $ document ->addChildNode (
30+ $ documentSection = new SectionNode ( new TitleNode (InlineCompoundNode:: getPlainTextInlineNode ( ' Index ' ), 1 , 'index ' ) );
31+ $ document -> addChildNode ( $ documentSection );
32+ foreach (['Document 1 ' => ' document-1 ' , 'Section A ' => ' section-a ' , 'Section B ' => ' section-b ' ] as $ title => $ id ) {
33+ $ documentSection ->addChildNode (
3534 new SectionNode (
36- new TitleNode (InlineCompoundNode::getPlainTextInlineNode ($ titles ), 1 , $ slugger -> slug ( $ titles )-> lower ()-> toString () ),
35+ new TitleNode (InlineCompoundNode::getPlainTextInlineNode ($ title ), 1 , $ id ),
3736 ),
3837 );
39- $ expected ->addChildNode (
38+ }
39+
40+ $ expected = new DocumentNode ('1 ' , 'index ' );
41+ $ expectedSection = new SectionNode (new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Index ' ), 1 , 'index ' ));
42+ $ expected ->addChildNode ($ expectedSection );
43+ foreach (['Document 1 ' => 'document-1 ' , 'Section A ' => 'section-a ' , 'Section B ' => 'section-b ' ] as $ title => $ id ) {
44+ $ expectedSection ->addChildNode (
4045 new SectionNode (
41- new TitleNode (InlineCompoundNode::getPlainTextInlineNode ($ titles ), 1 , $ slugger -> slug ( $ titles )-> lower ()-> toString () ),
46+ new TitleNode (InlineCompoundNode::getPlainTextInlineNode ($ title ), 1 , $ id ),
4247 ),
4348 );
4449 }
4550
4651 $ pass = new ImplicitHyperlinkTargetPass ();
47- $ resultDocuments = $ pass ->run ([clone $ document ], new CompilerContext (new ProjectNode ()));
52+ $ resultDocuments = $ pass ->run ([$ document ], new CompilerContext (new ProjectNode ()));
4853
4954 self ::assertEquals ([$ expected ], $ resultDocuments );
5055 }
5156
5257 public function testImplicitWithConflict (): void
5358 {
5459 $ document = new DocumentNode ('1 ' , 'index ' );
55- $ expected = new DocumentNode ('1 ' , 'index ' );
56- $ slugger = new AsciiSlugger ();
57-
58- foreach (['Document 1 ' , 'Section A ' , 'Section A ' ] as $ titles ) {
59- $ document ->addChildNode (
60+ $ documentSection = new SectionNode (new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Index ' ), 1 , 'index ' ));
61+ $ document ->addChildNode ($ documentSection );
62+ foreach (['Document 1 ' => 'document-1 ' , 'Section A ' => 'section-a ' ] as $ title => $ id ) {
63+ $ documentSection ->addChildNode (
6064 new SectionNode (
61- new TitleNode (InlineCompoundNode::getPlainTextInlineNode ($ titles ), 1 , $ slugger ->slug ($ titles )->lower ()->toString ()),
62- ),
63- );
64- $ expected ->addChildNode (
65- new SectionNode (
66- new TitleNode (InlineCompoundNode::getPlainTextInlineNode ($ titles ), 1 , $ slugger ->slug ($ titles )->lower ()->toString ()),
65+ new TitleNode (InlineCompoundNode::getPlainTextInlineNode ($ title ), 1 , $ id ),
6766 ),
6867 );
6968 }
7069
71- $ pass = new ImplicitHyperlinkTargetPass ();
72- $ resultDocuments = $ pass ->run ([$ document ], new CompilerContext (new ProjectNode ()));
73-
74- $ section = $ expected ->getNodes ()[2 ];
75- self ::assertInstanceOf (SectionNode::class, $ section );
76- $ section ->getTitle ()->setId ('section-a-1 ' );
77-
78- self ::assertEquals ([$ expected ], $ resultDocuments );
79- }
70+ $ documentSection ->addChildNode (
71+ new SectionNode (
72+ new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Section A ' ), 1 , 'section-a ' ),
73+ ),
74+ );
8075
81- public function testExplicit (): void
82- {
83- $ document = new DocumentNode ('1 ' , 'index ' );
8476 $ expected = new DocumentNode ('1 ' , 'index ' );
77+ $ expectedSection = new SectionNode (new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Index ' ), 1 , 'index ' ));
78+ $ expected ->addChildNode ($ expectedSection );
79+ foreach (['Document 1 ' => 'document-1 ' , 'Section A ' => 'section-a ' ] as $ title => $ id ) {
80+ $ expectedSection ->addChildNode (
81+ new SectionNode (
82+ new TitleNode (InlineCompoundNode::getPlainTextInlineNode ($ title ), 1 , $ id ),
83+ ),
84+ );
85+ }
8586
86- $ document ->addChildNode (new SectionNode (
87- new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Document 1 ' ), 1 , 'document-1 ' ),
88- ));
89- $ expected ->addChildNode (new SectionNode (
90- new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Document 1 ' ), 1 , 'document-1 ' ),
91- ));
92-
93- $ document ->addChildNode (new AnchorNode ('custom-anchor ' ));
94- $ expected ->addChildNode (new AnchorNode ('removed ' ));
95- $ expected = $ expected ->removeNode (1 );
96-
97- $ document ->addChildNode (
98- new SectionNode (new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Section A ' ), 1 , 'section-a ' )),
87+ $ expectedSection ->addChildNode (
88+ new SectionNode (
89+ // conflict in ID, "-1" is added
90+ new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Section A ' ), 1 , 'section-a-1 ' ),
91+ ),
9992 );
100- $ expectedTitle = new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Section A ' ), 1 , 'section-a ' );
101- $ expectedTitle ->setId ('custom-anchor ' );
102- $ expected ->addChildNode (new SectionNode ($ expectedTitle ));
10393
10494 $ pass = new ImplicitHyperlinkTargetPass ();
10595 $ resultDocuments = $ pass ->run ([$ document ], new CompilerContext (new ProjectNode ()));
@@ -110,25 +100,27 @@ public function testExplicit(): void
110100 public function testExplicitHasPriorityOverImplicit (): void
111101 {
112102 $ document = new DocumentNode ('1 ' , 'index ' );
113- $ expected = new DocumentNode ( ' 1 ' , 'index ' );
114-
115- $ document ->addChildNode (
103+ $ documentSection = new SectionNode ( new TitleNode (InlineCompoundNode:: getPlainTextInlineNode ( ' Index ' ), 1 , 'index ' ) );
104+ $ document -> addChildNode ( $ documentSection );
105+ $ documentSection ->addChildNode (
116106 new SectionNode (new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Document 1 ' ), 1 , 'document-1 ' )),
117107 );
118- $ expectedTitle = new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Document 1 ' ), 1 , 'document-1 ' );
119- $ expectedTitle ->setId ('document-1-1 ' );
120- $ expected ->addChildNode (new SectionNode ($ expectedTitle ));
121-
122- $ document ->addChildNode (new AnchorNode ('document-1 ' ));
123- $ expected ->addChildNode (new AnchorNode ('removed ' ));
124- $ expected = $ expected ->removeNode (1 );
108+ $ documentSection ->addChildNode (new AnchorNode ('document-1 ' ));
109+ $ documentSection ->addChildNode (
110+ new SectionNode (new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Section A ' ), 1 , 'section-a ' )),
111+ );
125112
126- $ document ->addChildNode (
113+ $ expected = new DocumentNode ('1 ' , 'index ' );
114+ $ expectedSection = new SectionNode (new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Index ' ), 1 , 'index ' ));
115+ $ expected ->addChildNode ($ expectedSection );
116+ $ expectedSection ->addChildNode (
117+ // "document-1" is claimed by an explicit reference anchor, implicit reference gets the "-1" suffix
118+ new SectionNode (new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Document 1 ' ), 1 , 'document-1-1 ' )),
119+ );
120+ $ expectedSection ->addChildNode (new AnchorNode ('document-1 ' ));
121+ $ expectedSection ->addChildNode (
127122 new SectionNode (new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Section A ' ), 1 , 'section-a ' )),
128123 );
129- $ expectedTitle = new TitleNode (InlineCompoundNode::getPlainTextInlineNode ('Section A ' ), 1 , 'section-a ' );
130- $ expectedTitle ->setId ('document-1 ' );
131- $ expected ->addChildNode (new SectionNode ($ expectedTitle ));
132124
133125 $ pass = new ImplicitHyperlinkTargetPass ();
134126 $ resultDocuments = $ pass ->run ([$ document ], new CompilerContext (new ProjectNode ()));
0 commit comments