Skip to content

Commit

Permalink
Merge pull request #726 from phpDocumentor/fix/next-node
Browse files Browse the repository at this point in the history
[TASK] nextnode should not step when already set
  • Loading branch information
jaapio authored Nov 30, 2023
2 parents 7b48fa6 + a8fc1bb commit 44d0cbd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/guides/src/Renderer/DocumentListIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ public function valid(): bool

public function nextNode(): DocumentNode|null
{
$this->innerIterator->next();
if ($this->nextDocument === null) {
$this->innerIterator->next();

if ($this->innerIterator->valid()) {
$this->nextDocument = WeakReference::create($this->current());
if ($this->innerIterator->valid()) {
$this->nextDocument = WeakReference::create($this->current());
}
}

return $this->nextDocument?->get();
Expand Down
18 changes: 18 additions & 0 deletions packages/guides/tests/unit/Renderer/DocumentListIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ public function testPreviousStepsBackToDeepestLevelInPreviousNode(): void
self::assertSame('1.1.2', $iterator->previousNode()?->getTitle()?->toString());
}

public function testNextNode(): void
{
$iterator = new DocumentListIterator(
new DocumentTreeIterator([$this->entry1, $this->entry2], $this->randomOrderedDocuments),
$this->randomOrderedDocuments,
);

$iterator->next(); // 1
$iterator->next(); // 1.1
$iterator->next(); // 1.1.1
self::assertSame('2', $iterator->nextNode()?->getTitle()?->toString());
self::assertSame('2', $iterator->nextNode()->getTitle()->toString());
$iterator->next();
self::assertSame('2', $iterator->current()->getTitle()?->toString());
$iterator->next();
self::assertSame('2.1', $iterator->current()->getTitle()?->toString());
}

public function testPreviousReturnsNullWhenNoPrevious(): void
{
$iterator = new DocumentListIterator(
Expand Down

0 comments on commit 44d0cbd

Please sign in to comment.