/** * @dataProvider tocSubProvider * * @param string $input * @param string $expected */ public function testTocSub($input, $expected) { $document = new Document($input); $toc = new TOC(); $toc->prepare($document); $call = Call::extractFirstCall($document); $toc->tocSub($call); $this->assertSame($expected, $document->getContent()); }
public function testWildcard_WhenFrontmatterIsUnknown_MustNotBeReplaced() { $document = new Document('@foo@'); $frontmatter = new Frontmatter(); $call = Call::extractFirstCall($document); $frontmatter->wildcard($call); $this->assertFalse($call->isReplaced()); $this->assertSame('@foo@', $document->getContent()); }
public function testGetMethodsForCall_SpecificPlaceholdersHavePriorityOverPotentialFrontmatter() { // Frontmatter must not accidentally replace a @toc@ call even though it *theoretically* would be frontmatter. $collection = new PlaceholderCollection([new TOC(), new Frontmatter()]); $document = new Document("---\ntoc: foo\n---\n@toc@"); $call = Call::extractFirstCall($document, Placeholder::PRIORITY_FIRST); $methods = $collection->getMethodsForCall($call); $this->assertCount(0, $methods); $tocCall = Call::extractFirstCall($document, Placeholder::PRIORITY_LAST); $methods = $collection->getMethodsForCall($tocCall); $this->assertCount(1, $methods); $this->assertSame(TOC::class, get_class($methods[0]->getPlaceholder())); }
/** * applyPlaceholdersAtPriority by calling only the placeholders that are relevant at a certain priority. * * @param Document $document The document to modify. * @param int $priority The priority stage to consider. * Placeholders are only called when they've * listed that priority. See * `Placeholder::getCallPriorities()` * @param \nochso\WriteMe\Placeholder\PlaceholderCollection $placeholders */ private function applyPlaceholdersAtPriority(Document $document, $priority, PlaceholderCollection $placeholders) { $offset = 0; $call = Call::extractFirstCall($document, $priority, $offset); while ($call !== null) { $methods = $placeholders->getMethodsForCall($call); $isReplaced = false; foreach ($methods as $method) { $method->call($call); if ($call->isReplaced()) { $isReplaced = true; break; } } if ($isReplaced) { // Anything could have changed in content. Start from the beginning. $offset = 0; } else { // The call was skipped by all placeholders. Ignore it at this priority. $offset = $call->getEndPositionOfRawCall(); } $call = Call::extractFirstCall($document, $priority, $offset); } }
public function testGetEndPositionOfRawCall() { $document = new Document('@test@ @test@'); $call = Call::extractFirstCall($document, Placeholder::PRIORITY_FIRST); $this->assertSame(6, $call->getEndPositionOfRawCall()); $secondCall = Call::extractFirstCall($document, Placeholder::PRIORITY_FIRST, $call->getEndPositionOfRawCall()); $this->assertSame(13, $secondCall->getEndPositionOfRawCall(), 'End position must be absolute and not be influenced by an offset'); }