-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX] Fix doc references containing anchors
- Loading branch information
Showing
8 changed files
with
263 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/guides/tests/unit/Interlink/InventoryGroupTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of phpDocumentor. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @link https://phpdoc.org | ||
*/ | ||
|
||
namespace phpDocumentor\Guides\Interlink; | ||
|
||
use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode; | ||
use phpDocumentor\Guides\ReferenceResolvers\Interlink\InventoryGroup; | ||
use phpDocumentor\Guides\ReferenceResolvers\Interlink\InventoryLink; | ||
use phpDocumentor\Guides\ReferenceResolvers\Messages; | ||
use phpDocumentor\Guides\ReferenceResolvers\NullAnchorNormalizer; | ||
use phpDocumentor\Guides\RenderContext; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class InventoryGroupTest extends TestCase | ||
{ | ||
private InventoryGroup $inventoryGroup; | ||
|
||
private RenderContext&MockObject $renderContext; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->inventoryGroup = new InventoryGroup(new NullAnchorNormalizer()); | ||
$this->renderContext = $this->createMock(RenderContext::class); | ||
} | ||
|
||
#[DataProvider('linkProvider')] | ||
public function testGetLinkFromInterlinkGroup(string $expected, string $input, string $path): void | ||
{ | ||
$this->inventoryGroup->addLink($path, new InventoryLink('', '', $path . '.html', '')); | ||
$messages = new Messages(); | ||
$link = $this->inventoryGroup->getLink( | ||
new DocReferenceNode($input, '', 'interlink'), | ||
$this->renderContext, | ||
$messages, | ||
); | ||
self::assertEmpty($messages->getWarnings()); | ||
self::assertEquals($expected, $link?->getPath()); | ||
} | ||
|
||
/** @return string[][] */ | ||
public static function linkProvider(): array | ||
{ | ||
return [ | ||
'plain' => [ | ||
'expected' => 'some-document.html', | ||
'input' => 'some-document', | ||
'path' => 'some-document', | ||
], | ||
'withAnchor' => [ | ||
'expected' => 'some-document.html#anchor', | ||
'input' => 'some-document#anchor', | ||
'path' => 'some-document', | ||
], | ||
]; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
packages/guides/tests/unit/ReferenceResolvers/DocReferenceResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of phpDocumentor. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @link https://phpdoc.org | ||
*/ | ||
|
||
namespace phpDocumentor\Guides\ReferenceResolvers; | ||
|
||
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode; | ||
use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode; | ||
use phpDocumentor\Guides\Nodes\ProjectNode; | ||
use phpDocumentor\Guides\Nodes\TitleNode; | ||
use phpDocumentor\Guides\RenderContext; | ||
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DocReferenceResolverTest extends TestCase | ||
{ | ||
private RenderContext&MockObject $renderContext; | ||
private ProjectNode $projectNode; | ||
private MockObject&UrlGeneratorInterface $urlGenerator; | ||
private MockObject&DocumentNameResolverInterface $documentNameResolver; | ||
private DocReferenceResolver $subject; | ||
|
||
protected function setUp(): void | ||
{ | ||
$documentEntry = new DocumentEntryNode('some-document', TitleNode::emptyNode()); | ||
$this->projectNode = new ProjectNode('some-name'); | ||
$this->projectNode->addDocumentEntry($documentEntry); | ||
$this->renderContext = $this->createMock(RenderContext::class); | ||
$this->renderContext->expects(self::once())->method('getProjectNode')->willReturn($this->projectNode); | ||
$this->documentNameResolver = self::createMock(DocumentNameResolverInterface::class); | ||
$this->urlGenerator = self::createMock(UrlGeneratorInterface::class); | ||
$this->subject = new DocReferenceResolver($this->urlGenerator, $this->documentNameResolver); | ||
} | ||
|
||
#[DataProvider('pathProvider')] | ||
public function testDocumentReducer(string $expected, string $input, string $path): void | ||
{ | ||
$this->documentNameResolver->expects(self::once())->method('canonicalUrl')->with('', $path)->willReturn($path); | ||
$input = new DocReferenceNode($input); | ||
$this->urlGenerator->expects(self::once())->method('generateCanonicalOutputUrl')->willReturn($path); | ||
$messages = new Messages(); | ||
self::assertTrue($this->subject->resolve($input, $this->renderContext, $messages)); | ||
self::assertEmpty($messages->getWarnings()); | ||
self::assertEquals($expected, $input->getUrl()); | ||
} | ||
|
||
/** @return string[][] */ | ||
public static function pathProvider(): array | ||
{ | ||
return [ | ||
'plain' => [ | ||
'expected' => 'some-document', | ||
'input' => 'some-document', | ||
'path' => 'some-document', | ||
], | ||
'withAnchor' => [ | ||
'expected' => 'some-document#anchor', | ||
'input' => 'some-document#anchor', | ||
'path' => 'some-document', | ||
], | ||
]; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/guides/tests/unit/ReferenceResolvers/InterlinkReferenceResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of phpDocumentor. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @link https://phpdoc.org | ||
*/ | ||
|
||
namespace phpDocumentor\Guides\ReferenceResolvers; | ||
|
||
use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode; | ||
use phpDocumentor\Guides\ReferenceResolvers\Interlink\Inventory; | ||
use phpDocumentor\Guides\ReferenceResolvers\Interlink\InventoryLink; | ||
use phpDocumentor\Guides\ReferenceResolvers\Interlink\InventoryRepository; | ||
use phpDocumentor\Guides\RenderContext; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class InterlinkReferenceResolverTest extends TestCase | ||
{ | ||
private RenderContext&MockObject $renderContext; | ||
private MockObject&InventoryRepository $inventoryRepository; | ||
private InterlinkReferenceResolver $subject; | ||
private AnchorNormalizer $anchorNormalizer; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->renderContext = $this->createMock(RenderContext::class); | ||
$this->inventoryRepository = $this->createMock(InventoryRepository::class); | ||
$this->anchorNormalizer = new NullAnchorNormalizer(); | ||
$this->subject = new InterlinkReferenceResolver($this->inventoryRepository); | ||
} | ||
|
||
#[DataProvider('pathProvider')] | ||
public function testDocumentReducer(string $expected, string $input, string $path): void | ||
{ | ||
$input = new DocReferenceNode($input, '', 'interlink-target'); | ||
$inventoryLink = new InventoryLink('project', '1.0', $path, ''); | ||
$inventory = new Inventory('base-url/', $this->anchorNormalizer); | ||
$this->inventoryRepository->expects(self::once())->method('getInventory')->willReturn($inventory); | ||
$this->inventoryRepository->expects(self::once())->method('getLink')->willReturn($inventoryLink); | ||
$messages = new Messages(); | ||
self::assertTrue($this->subject->resolve($input, $this->renderContext, $messages)); | ||
self::assertEmpty($messages->getWarnings()); | ||
self::assertEquals($expected, $input->getUrl()); | ||
} | ||
|
||
/** @return string[][] */ | ||
public static function pathProvider(): array | ||
{ | ||
return [ | ||
'plain' => [ | ||
'expected' => 'base-url/some-document.html', | ||
'input' => 'some-document', | ||
'path' => 'some-document.html', | ||
], | ||
'withAnchor' => [ | ||
'expected' => 'base-url/some-document.html#anchor', | ||
'input' => 'some-document#anchor', | ||
'path' => 'some-document.html#anchor', | ||
], | ||
]; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tests/Integration/tests/navigation/docref/expected/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
<!-- content start --> | ||
<div class="section" id="root"> | ||
<h1>Root</h1> | ||
|
||
|
||
<ul> | ||
<li><a href="/subfolder/index.html">Subfolder</a></li> | ||
<li><a href="/subfolder/index.html#subfolder-index">Subfolder</a></li> | ||
<li><a href="/subfolder/index.html#something">Subfolder</a></li> | ||
</ul> | ||
|
||
</div> | ||
<!-- content end --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
==== | ||
Root | ||
==== | ||
|
||
* :doc:`Subfolder <subfolder/index>` | ||
* :doc:`Subfolder <subfolder/index#subfolder-index>` | ||
* :doc:`Subfolder <subfolder/index#something>` |