FootnoteRendererTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Footnote;
  15. use League\CommonMark\Extension\Footnote\Renderer\FootnoteRenderer;
  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 FootnoteRendererTest extends TestCase
  21. {
  22. public function testDefaultAttributes(): void
  23. {
  24. $renderer = new FootnoteRenderer();
  25. $renderer->setConfiguration($this->createConfiguration());
  26. $fakeReference = new Reference('label', 'dest', 'title');
  27. $footnote = new Footnote($fakeReference);
  28. $output = $renderer->render($footnote, new FakeChildNodeRenderer());
  29. $this->assertSame('footnote', $output->getAttribute('class'));
  30. $this->assertSame('fn:label', $output->getAttribute('id'));
  31. $this->assertSame('doc-endnote', $output->getAttribute('role'));
  32. }
  33. public function testCustomClassAddedViaAST(): void
  34. {
  35. $renderer = new FootnoteRenderer();
  36. $renderer->setConfiguration($this->createConfiguration());
  37. $fakeReference = new Reference('label', 'dest', 'title');
  38. $footnote = new Footnote($fakeReference);
  39. $footnote->data->set('attributes/class', 'custom class');
  40. $output = $renderer->render($footnote, new FakeChildNodeRenderer());
  41. $this->assertSame('custom class footnote', $output->getAttribute('class'));
  42. }
  43. public function testClassConfiguration(): void
  44. {
  45. $renderer = new FootnoteRenderer();
  46. $renderer->setConfiguration($this->createConfiguration(['footnote' => ['footnote_class' => 'my-custom-class']]));
  47. $fakeReference = new Reference('label', 'dest', 'title');
  48. $footnote = new Footnote($fakeReference);
  49. $output = $renderer->render($footnote, new FakeChildNodeRenderer());
  50. $this->assertSame('my-custom-class', $output->getAttribute('class'));
  51. }
  52. public function testIdPrefixConfiguration(): void
  53. {
  54. $renderer = new FootnoteRenderer();
  55. $renderer->setConfiguration($this->createConfiguration(['footnote' => ['footnote_id_prefix' => 'custom-']]));
  56. $fakeReference = new Reference('label', 'dest', 'title');
  57. $footnote = new Footnote($fakeReference);
  58. $output = $renderer->render($footnote, new FakeChildNodeRenderer());
  59. $this->assertSame('custom-label', $output->getAttribute('id'));
  60. }
  61. /**
  62. * @param array<string, mixed> $values
  63. */
  64. private function createConfiguration(array $values = []): ConfigurationInterface
  65. {
  66. $config = Environment::createDefaultConfiguration();
  67. (new FootnoteExtension())->configureSchema($config);
  68. $config->merge($values);
  69. return $config->reader();
  70. }
  71. }