SlugNormalizerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Normalizer;
  12. use League\CommonMark\Normalizer\SlugNormalizer;
  13. use League\Config\ConfigurationInterface;
  14. use PHPUnit\Framework\TestCase;
  15. final class SlugNormalizerTest extends TestCase
  16. {
  17. /**
  18. * @dataProvider dataProviderForTestNormalize
  19. */
  20. public function testNormalize(string $input, string $expectedOutput): void
  21. {
  22. $this->assertSame($expectedOutput, (new SlugNormalizer())->normalize($input));
  23. }
  24. /**
  25. * @return iterable<string[]>
  26. */
  27. public function dataProviderForTestNormalize(): iterable
  28. {
  29. yield ['', ''];
  30. yield ['hello world', 'hello-world'];
  31. yield ['hello-world', 'hello-world'];
  32. yield ['hello world', 'hello-world'];
  33. yield ['Hello World!', 'hello-world'];
  34. yield ['456*(&^3484389462342#$#$#$#$', '4563484389462342'];
  35. yield ['me&you', 'meyou'];
  36. yield ['special char ὐ here', 'special-char-ὐ-here'];
  37. yield ['ПРИСТАНЯМ СТРЕМЯТСЯ', 'пристаням-стремятся'];
  38. yield ['пристаням стремятся', 'пристаням-стремятся'];
  39. yield ['emoji 😂 example', 'emoji--example'];
  40. yield ['One ½ half', 'one--half'];
  41. yield ['Roman ↁ example', 'roman-ↁ-example'];
  42. yield ['Here\'s a DŽ digraph', 'heres-a-dž-digraph'];
  43. yield ['Here\'s another dž digraph', 'heres-another-dž-digraph'];
  44. yield ['Unicode x² superscript', 'unicode-x-superscript'];
  45. yield ['Equal = sign', 'equal--sign'];
  46. yield ['Tabs in here', 'tabs-in-here'];
  47. yield ['Tabs- -in- -here-too', 'tabs---in---here-too'];
  48. yield ['We-love---dashes even with -lots- of spaces', 'we-love---dashes-even-with--lots--of-spaces'];
  49. yield ['LOUD NOISES', 'loud-noises'];
  50. yield ['ťęŝŧ', 'ťęŝŧ'];
  51. yield ['ŤĘŜŦ', 'ťęŝŧ'];
  52. yield ["\nWho\nput\n\n newlines \nin here?!\n", 'who-put-newlines-in-here'];
  53. yield ['අත්හදා බලන මාතෘකාව', 'අත්හදා-බලන-මාතෘකාව'];
  54. yield ['අත්හදා බලන මාතෘකාව -', 'අත්හදා-බලන-මාතෘකාව--'];
  55. yield ['අත්හදා බලන මාතෘකාව - ', 'අත්හදා-බලන-මාතෘකාව--'];
  56. yield ['අත්හදා බලන මාතෘකාව - අ', 'අත්හදා-බලන-මාතෘකාව---අ'];
  57. yield ['测试标题', '测试标题'];
  58. yield ['测试 # 标题', '测试--标题'];
  59. yield ['测试 x² 标题', '测试-x-标题'];
  60. yield ['試験タイトル', '試験タイトル'];
  61. }
  62. /**
  63. * @dataProvider dataProviderForTestNormalizeWithMaxLength
  64. */
  65. public function testNormalizeWithMaxLength(string $input, int $maxLength, string $expectedOutput): void
  66. {
  67. $this->assertSame($expectedOutput, (new SlugNormalizer())->normalize($input, ['length' => $maxLength]));
  68. }
  69. /**
  70. * @return iterable<mixed>
  71. */
  72. public function dataProviderForTestNormalizeWithMaxLength(): iterable
  73. {
  74. yield ['Hello World', 8, 'hello-wo'];
  75. yield ['Hello World', 999, 'hello-world'];
  76. yield ['Hello World', 0, 'hello-world'];
  77. }
  78. public function testNormalizerWithDefaultMaxLength(): void
  79. {
  80. $config = $this->createMock(ConfigurationInterface::class);
  81. $config->expects($this->once())->method('get')->with('slug_normalizer/max_length')->willReturn(8);
  82. $normalizer = new SlugNormalizer();
  83. $normalizer->setConfiguration($config);
  84. $this->assertSame('hello-wo', $normalizer->normalize('Hello World'));
  85. }
  86. }