builder->recreate(); self::assertSame($this->builder->getKernel(), $builder->getKernel()); self::assertSame($this->builder->getConfiguration(), $builder->getConfiguration()); } /** * Tests that the build produced the excepted documents */ public function testBuild(): void { self::assertTrue(is_dir($this->targetFile())); self::assertTrue(file_exists($this->targetFile('index.html'))); self::assertTrue(file_exists($this->targetFile('introduction.html'))); self::assertTrue(file_exists($this->targetFile('subdirective.html'))); self::assertTrue(file_exists($this->targetFile('magic-link.html'))); self::assertTrue(file_exists($this->targetFile('subdir/test.html'))); self::assertTrue(file_exists($this->targetFile('subdir/file.html'))); } public function testCachedMetas(): void { // check that metas were cached self::assertTrue(file_exists($this->targetFile('metas.php'))); $cachedContents = (string) file_get_contents($this->targetFile('metas.php')); /** @var MetaEntry[] $metaEntries */ $metaEntries = unserialize($cachedContents); self::assertArrayHasKey('index', $metaEntries); self::assertSame('Summary', $metaEntries['index']->getTitle()); // look at all the other documents this document depends // on, like :doc: and :ref: self::assertSame([ 'index', 'toc-glob', 'subdir/index', ], array_values(array_unique($metaEntries['introduction']->getDepends()))); // assert the self-refs don't mess up dependencies self::assertSame([ 'subdir/index', 'index', 'subdir/file', ], array_values(array_unique($metaEntries['subdir/index']->getDepends()))); // update meta cache to see that it was used // Summary is the main header in "index.rst" // we reference it in link-to-index.rst // it should cause link-to-index.rst to re-render with the new // title as the link file_put_contents( $this->targetFile('metas.php'), str_replace('Summary', 'Sumario', $cachedContents) ); // also we need to trigger the link-to-index.rst as looking updated sleep(1); $contents = file_get_contents(__DIR__ . '/input/link-to-index.rst'); file_put_contents( __DIR__ . '/input/link-to-index.rst', $contents . ' ' ); // change it back file_put_contents( __DIR__ . '/input/link-to-index.rst', $contents ); // new builder, which will use cached metas $builder = new Builder(); $builder->build($this->sourceFile(), $this->targetFile()); $contents = $this->getFileContents($this->targetFile('link-to-index.html')); self::assertStringContainsString('Sumario', $contents); } /** * Tests the ..url :: directive */ public function testUrl(): void { $contents = $this->getFileContents($this->targetFile('index.html')); self::assertStringContainsString('"magic-link.html', $contents); self::assertStringContainsString('Another page', $contents); } /** * Tests the links */ public function testLinks(): void { $contents = $this->getFileContents($this->targetFile('subdir/test.html')); self::assertStringContainsString('"../to/resource"', $contents); self::assertStringContainsString('"http://absolute/"', $contents); self::assertStringContainsString('"http://google.com"', $contents); self::assertStringContainsString('"http://yahoo.com"', $contents); self::assertSame(2, substr_count($contents, 'http://something.com')); } public function testAnchor(): void { $contents = $this->getFileContents($this->targetFile('subdir/test.html')); self::assertStringContainsString('

This is a test anchor

', $contents); self::assertStringContainsString('', $contents); } /** * Tests that the index toctree worked */ public function testToctree(): void { $contents = $this->getFileContents($this->targetFile('index.html')); self::assertStringContainsString('"introduction.html', $contents); self::assertStringContainsString('Introduction page', $contents); self::assertStringContainsString('"subdirective.html', $contents); self::assertStringContainsString('"subdir/test.html', $contents); self::assertStringContainsString('"subdir/file.html', $contents); } public function testToctreeGlob(): void { $contents = $this->getFileContents($this->targetFile('toc-glob.html')); // links to first

tag of other pages must not contain a url fragment self::assertStringContainsString('magic-link.html"', $contents); self::assertStringContainsString('introduction.html"', $contents); self::assertStringContainsString('subdirective.html"', $contents); self::assertStringContainsString('subdir/file.html"', $contents); // links to other

tags should contain a url fragment self::assertStringContainsString('index.html#another-h1', $contents); // links to other headings contain a url fragment self::assertStringContainsString('subdir/test.html#subdirectory', $contents); self::assertStringContainsString('subdir/file.html#heading-2', $contents); // link to

inside the same page contains a url fragment self::assertStringContainsString('toc-glob.html#toc-glob', $contents); } public function testToctreeGlobOrder(): void { $contents = $this->getFileContents($this->targetFile('toc-glob.html')); // assert `index` is first since it is defined first in toc-glob.rst self::assertStringContainsString('
  • Summary
  • ', $contents); // assert `introduction` is at the end after the glob since it is defined last in toc-glob.rst self::assertStringContainsString('Introduction page
    ', $contents); // assert the glob part is alphabetical $crawler = new Crawler($contents); $expectedLinks = [ 'index.html', // a second "h1" (a rare thing) is included as a top-level headline 'index.html#another-h1', // this is another.rst - it has a custom url 'magic-link.html', 'introduction.html', 'link-to-index.html', // the subdir handling is actually different than Sphinx, which // does not look into subdirs with a normal * glob 'subdir/file.html', 'subdir/test.html', 'subdir/toc.html', 'subdirective.html', 'toc-glob-reversed.html', // only here because we explicitly include it, "self file" is normally ignored 'toc-glob.html#toc-glob', // this is manually included again 'introduction.html', ]; $actualLinks = array_map(static function ($linkElement): string { return $linkElement->attributes->getNamedItem('href')->nodeValue; }, iterator_to_array($crawler->filter('.toc > ul > li > a'))); self::assertSame($expectedLinks, $actualLinks); } public function testToctreeGlobReversedOrder(): void { $contents = $this->getFileContents($this->targetFile('toc-glob-reversed.html')); $crawler = new Crawler($contents); // see previous test for why they are in this order (now reversed) $expectedLinks = [ 'introduction.html', 'toc-glob.html', 'subdirective.html', 'subdir/toc.html', 'subdir/test.html', 'subdir/file.html', 'link-to-index.html', 'introduction.html', 'magic-link.html', 'index.html', // having the other h1 anchor AFTER index.html is what Sphinx does too 'index.html#another-h1', ]; $actualLinks = array_map(static function ($linkElement): string { return $linkElement->attributes->getNamedItem('href')->nodeValue; }, iterator_to_array($crawler->filter('.toc > ul > li > a'))); self::assertSame($expectedLinks, $actualLinks); } public function testToctreeInSubdirectory(): void { $contents = $this->getFileContents($this->targetFile('subdir/toc.html')); self::assertStringContainsString('../introduction.html"', $contents); self::assertStringContainsString('../subdirective.html"', $contents); self::assertStringContainsString('../magic-link.html"', $contents); self::assertStringContainsString('"test.html"', $contents); self::assertStringContainsString('file.html"', $contents); } public function testAnchors(): void { $contents = $this->getFileContents($this->targetFile('index.html')); self::assertStringContainsString('', $contents); $contents = $this->getFileContents($this->targetFile('introduction.html')); self::assertStringContainsString('

    Reference to the Summary Reference

    ', $contents); } /** * Testing references to other documents */ public function testReferences(): void { $contents = $this->getFileContents($this->targetFile('introduction.html')); self::assertStringContainsString('Index, paragraph toc', $contents); self::assertStringContainsString('Index', $contents); self::assertStringContainsString('Summary', $contents); self::assertStringContainsString('Link index absolutely', $contents); self::assertStringContainsString('Test Reference', $contents); self::assertStringContainsString('Camel Case Reference', $contents); $contents = $this->getFileContents($this->targetFile('subdir/test.html')); self::assertStringContainsString('"../index.html"', $contents); self::assertStringContainsString('the subdir same doc reference', $contents); self::assertStringContainsString('Reference absolute to index', $contents); self::assertStringContainsString('Reference absolute to file', $contents); self::assertStringContainsString('Reference relative to file', $contents); $contents = $this->getFileContents($this->targetFile('index.html')); self::assertStringContainsString('Link to the same doc reference', $contents); self::assertStringContainsString('Link to the same doc reference with ticks', $contents); self::assertStringContainsString('Link to Subdirectory', $contents); self::assertStringContainsString('Link to Subdirectory Test', $contents); self::assertStringContainsString('Link to Subdirectory Child', $contents); self::assertStringContainsString('Link to Subdirectory Child Test', $contents); self::assertStringContainsString('Link to Subdirectory Child Level 2', $contents); self::assertStringContainsString('Link to Subdirectory Child Level 2 Test', $contents); self::assertStringContainsString('Link to Subdirectory Child Level 3', $contents); self::assertStringContainsString('Link to Subdirectory Child Level 3 Test', $contents); } public function testSubdirReferences(): void { $contents = $this->getFileContents($this->targetFile('subdir/test.html')); self::assertStringContainsString('

    This is a test anchor

    ', $contents); self::assertStringContainsString('

    This is a test subdir reference with anchor

    ', $contents); } public function testFileInclude(): void { $contents = $this->getFileContents($this->targetFile('subdir/test.html')); self::assertSame(2, substr_count($contents, 'This file is included')); } /** * Testing wrapping sub directive */ public function testSubDirective(): void { $contents = $this->getFileContents($this->targetFile('subdirective.html')); self::assertSame(2, substr_count($contents, '
    ')); self::assertSame(2, substr_count($contents, '
  • ')); self::assertStringContainsString('
  • ', $contents); self::assertSame(2, substr_count($contents, '')); self::assertSame(1, substr_count($contents, '')); self::assertStringContainsString('

    This is a simple note!

    ', $contents); self::assertStringContainsString('

    There is a title here

    ', $contents); } public function testReferenceInDirective(): void { $contents = $this->getFileContents($this->targetFile('index.html')); self::assertStringContainsString( '

    Reference in directory

    ', $contents ); } public function testTitleLinks(): void { $contents = $this->getFileContents($this->targetFile('magic-link.html')); self::assertStringContainsString( '

    see See also

    ', $contents ); self::assertStringContainsString( '

    see Another page

    ', $contents ); self::assertStringContainsString( '

    see test

    ', $contents ); self::assertStringContainsString( '

    see title with ampersand &

    ', $contents ); self::assertStringContainsString( '

    see A title with ticks

    ', $contents ); } public function testHeadings(): void { $contents = $this->getFileContents($this->targetFile('subdir/file.html')); foreach (range(1, 6) as $index) { self::assertStringContainsString( sprintf( 'Heading %d', $index, $index, $index ), $contents ); } } public function testReferenceToTitleWith2CharactersLong(): void { $contents = $this->getFileContents($this->targetFile('subdir/test.html')); self::assertStringContainsString( 'em', $contents ); } protected function getFixturesDirectory(): string { return 'Builder'; } }