MentionTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace League\CommonMark\Tests\Unit\Extension\Mention;
  12. use League\CommonMark\Extension\Mention\Mention;
  13. use League\CommonMark\Node\Inline\Text;
  14. use PHPUnit\Framework\TestCase;
  15. final class MentionTest extends TestCase
  16. {
  17. public function testConstructorAndGetters(): void
  18. {
  19. $mention = new Mention('test', '@', 'colinodell');
  20. $this->assertSame('test', $mention->getName());
  21. $this->assertSame('@', $mention->getPrefix());
  22. $this->assertSame('colinodell', $mention->getIdentifier());
  23. $this->assertSame('@colinodell', $mention->getLabel());
  24. $this->assertSame('', $mention->getUrl());
  25. $this->assertCount(1, $mention->children());
  26. $this->assertInstanceOf(Text::class, $child = $mention->firstChild());
  27. \assert($child instanceof Text);
  28. $this->assertSame('@colinodell', $child->getLiteral());
  29. }
  30. public function testConstructorAndGettersWithCustomLabel(): void
  31. {
  32. $mention = new Mention('test', '#', '123', 'Issue #123');
  33. $this->assertSame('#', $mention->getPrefix());
  34. $this->assertSame('123', $mention->getIdentifier());
  35. $this->assertSame('Issue #123', $mention->getLabel());
  36. $this->assertSame('', $mention->getUrl());
  37. $this->assertCount(1, $mention->children());
  38. $this->assertInstanceOf(Text::class, $child = $mention->firstChild());
  39. \assert($child instanceof Text);
  40. $this->assertSame('Issue #123', $child->getLiteral());
  41. }
  42. public function testSetUrl(): void
  43. {
  44. $mention = new Mention('test', '@', 'colinodell');
  45. $mention->setUrl('https://www.twitter.com/colinodell');
  46. $this->assertSame('https://www.twitter.com/colinodell', $mention->getUrl());
  47. }
  48. public function testSetLabel(): void
  49. {
  50. $mention = new Mention('test', '@', 'colinodell');
  51. $this->assertSame('@colinodell', $mention->getLabel());
  52. $this->assertCount(1, $mention->children());
  53. $this->assertInstanceOf(Text::class, $child = $mention->firstChild());
  54. \assert($child instanceof Text);
  55. $this->assertSame('@colinodell', $child->getLiteral());
  56. $mention->setLabel('Colin O\'Dell');
  57. $this->assertSame('Colin O\'Dell', $mention->getLabel());
  58. $this->assertCount(1, $mention->children());
  59. $this->assertInstanceOf(Text::class, $child2 = $mention->firstChild());
  60. \assert($child2 instanceof Text);
  61. $this->assertSame('Colin O\'Dell', $child2->getLiteral());
  62. $this->assertSame($child, $child2);
  63. }
  64. public function testLabelFunctionsWhenLabelDetached(): void
  65. {
  66. $mention = new Mention('test', '@', 'colinodell');
  67. $this->assertSame('@colinodell', $mention->getLabel());
  68. $mention->detachChildren();
  69. $this->assertCount(0, $mention->children());
  70. $this->assertNull($mention->getLabel());
  71. $mention->setLabel('Colin O\'Dell');
  72. $this->assertSame('Colin O\'Dell', $mention->getLabel());
  73. $this->assertCount(1, $mention->children());
  74. $this->assertInstanceOf(Text::class, $child = $mention->firstChild());
  75. \assert($child instanceof Text);
  76. $this->assertSame('Colin O\'Dell', $child->getLiteral());
  77. }
  78. }