FootnoteBackrefRendererTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the league/commonmark package.
  4. *
  5. * (c) Colin O'Dell <colinodell@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace League\CommonMark\Tests\Unit\Extension\Footnote\Renderer;
  12. use League\CommonMark\Environment\Environment;
  13. use League\CommonMark\Extension\Footnote\FootnoteExtension;
  14. use League\CommonMark\Extension\Footnote\Node\FootnoteBackref;
  15. use League\CommonMark\Extension\Footnote\Renderer\FootnoteBackrefRenderer;
  16. use League\CommonMark\Reference\Reference;
  17. use League\CommonMark\Tests\Unit\Renderer\FakeChildNodeRenderer;
  18. use League\Config\ConfigurationInterface;
  19. use PHPUnit\Framework\TestCase;
  20. final class FootnoteBackrefRendererTest extends TestCase
  21. {
  22. public function testDefaultAttributes(): void
  23. {
  24. $renderer = new FootnoteBackrefRenderer();
  25. $renderer->setConfiguration($this->createConfiguration());
  26. $fakeReference = new Reference('label', 'dest', 'title');
  27. $footnoteBackref = new FootnoteBackref($fakeReference);
  28. $output = $renderer->render($footnoteBackref, new FakeChildNodeRenderer());
  29. $this->assertStringContainsString('class="footnote-backref"', $output);
  30. $this->assertStringContainsString('rev="footnote"', $output);
  31. $this->assertStringContainsString('role="doc-backlink"', $output);
  32. }
  33. public function testCustomClassAddedViaAST(): void
  34. {
  35. $renderer = new FootnoteBackrefRenderer();
  36. $renderer->setConfiguration($this->createConfiguration());
  37. $fakeReference = new Reference('label', 'dest', 'title');
  38. $footnoteBackref = new FootnoteBackref($fakeReference);
  39. $footnoteBackref->data->set('attributes/class', 'custom class');
  40. $output = $renderer->render($footnoteBackref, new FakeChildNodeRenderer());
  41. $this->assertStringContainsString('class="custom class footnote-backref"', $output);
  42. }
  43. public function testClassConfiguration(): void
  44. {
  45. $renderer = new FootnoteBackrefRenderer();
  46. $renderer->setConfiguration($this->createConfiguration(['footnote' => ['backref_class' => 'my-custom-class']]));
  47. $fakeReference = new Reference('label', 'dest', 'title');
  48. $footnoteBackref = new FootnoteBackref($fakeReference);
  49. $output = $renderer->render($footnoteBackref, new FakeChildNodeRenderer());
  50. $this->assertStringContainsString('class="my-custom-class"', $output);
  51. }
  52. /**
  53. * @param array<string, mixed> $values
  54. */
  55. private function createConfiguration(array $values = []): ConfigurationInterface
  56. {
  57. $config = Environment::createDefaultConfiguration();
  58. (new FootnoteExtension())->configureSchema($config);
  59. $config->merge($values);
  60. return $config->reader();
  61. }
  62. }