From a8fc1bb4d72fbaf6dc000005b9a67b5e7feb8ed3 Mon Sep 17 00:00:00 2001 From: Jaapio Date: Tue, 28 Nov 2023 21:02:00 +0100 Subject: [PATCH] [TASK] nextnode should not step when already set --- .../src/Renderer/DocumentListIterator.php | 8 +++++--- .../unit/Renderer/DocumentListIteratorTest.php | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/guides/src/Renderer/DocumentListIterator.php b/packages/guides/src/Renderer/DocumentListIterator.php index e8e46a67a..4d6f943e5 100644 --- a/packages/guides/src/Renderer/DocumentListIterator.php +++ b/packages/guides/src/Renderer/DocumentListIterator.php @@ -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(); diff --git a/packages/guides/tests/unit/Renderer/DocumentListIteratorTest.php b/packages/guides/tests/unit/Renderer/DocumentListIteratorTest.php index f1d3d08be..6a7010ae8 100644 --- a/packages/guides/tests/unit/Renderer/DocumentListIteratorTest.php +++ b/packages/guides/tests/unit/Renderer/DocumentListIteratorTest.php @@ -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(