SlugifyTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * This file is part of cocur/slugify.
  4. *
  5. * (c) Florian Eckerstorfer <florian@eckerstorfer.co>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Cocur\Slugify\Tests;
  11. use Cocur\Slugify\Slugify;
  12. /**
  13. * SlugifyTest
  14. *
  15. * @category test
  16. * @package org.cocur.slugify
  17. * @author Florian Eckerstorfer <florian@eckerstorfer.co>
  18. * @author Ivo Bathke <ivo.bathke@gmail.com>
  19. * @author Marchenko Alexandr
  20. * @copyright 2012-2014 Florian Eckerstorfer
  21. * @license http://www.opensource.org/licenses/MIT The MIT License
  22. */
  23. class SlugifyTest extends \PHPUnit_Framework_TestCase
  24. {
  25. private $slugify;
  26. public function setUp()
  27. {
  28. $this->slugify = new Slugify();
  29. }
  30. /**
  31. * @test
  32. * @dataProvider provider
  33. * @covers Cocur\Slugify\Slugify::slugify()
  34. */
  35. public function slugifyReturnsSlugifiedString($string, $result)
  36. {
  37. $this->assertEquals($result, $this->slugify->slugify($string));
  38. }
  39. /**
  40. * @test
  41. * @covers Cocur\Slugify\Slugify::addRule()
  42. * @covers Cocur\Slugify\Slugify::slugify()
  43. */
  44. public function addRuleAddsRule()
  45. {
  46. $this->slugify->addRule('X', 'y');
  47. $this->assertEquals('y', $this->slugify->slugify('X'));
  48. }
  49. /**
  50. * @test
  51. * @covers Cocur\Slugify\Slugify::addRules()
  52. * @covers Cocur\Slugify\Slugify::slugify()
  53. */
  54. public function addRulesAddsMultipleRules()
  55. {
  56. $this->slugify->addRules(array('x' => 'y', 'a' => 'b'));
  57. $this->assertEquals('yb', $this->slugify->slugify('xa'));
  58. }
  59. /**
  60. * @test
  61. * @covers Cocur\Slugify\Slugify::activateRuleset()
  62. */
  63. public function activateRulesetActivatesTheGivenRuleset()
  64. {
  65. $this->slugify->activateRuleset('esperanto');
  66. $this->assertEquals(
  67. 'sercxi-mangxi-hxirurgio-jxurnalo-sxuo-malgraux',
  68. $this->slugify->slugify('serĉi manĝi ĥirurgio ĵurnalo ŝuo malgraŭ')
  69. );
  70. }
  71. /**
  72. * @test
  73. * @covers Cocur\Slugify\Slugify::activateRuleset()
  74. * @expectedException \InvalidArgumentException
  75. */
  76. public function activateRulesetThrowsExceptionIfInvalidName()
  77. {
  78. $this->slugify->activateRuleset('invalid');
  79. }
  80. /**
  81. * @test
  82. * @covers Cocur\Slugify\Slugify::addRuleset()
  83. * @covers Cocur\Slugify\Slugify::getRulesets()
  84. */
  85. public function addRulesetGetRulesets()
  86. {
  87. $this->slugify->addRuleset('foo', array('k' => 'key'));
  88. $this->assertCount(2, $this->slugify->getRulesets());
  89. }
  90. /**
  91. * @test
  92. * @covers Cocur\Slugify\Slugify::create()
  93. */
  94. public function createReturnsAnInstance()
  95. {
  96. $this->assertInstanceOf('Cocur\\Slugify\\SlugifyInterface', Slugify::create());
  97. }
  98. /**
  99. * @test
  100. * @covers Cocur\Slugify\Slugify::setRegExp()
  101. */
  102. public function otherRegExpsProduceOtherResults()
  103. {
  104. $actual = 'File Name.tar.gz';
  105. $expected = 'file-name.tar.gz';
  106. $this->assertNotEquals($expected, $this->slugify->slugify($actual));
  107. $this->slugify->setRegExp('/([^a-z0-9.]|-)+/');
  108. $this->assertEquals($expected, $this->slugify->slugify($actual));
  109. }
  110. /**
  111. * @test
  112. * @covers Cocur\Slugify\Slugify::__construct()
  113. */
  114. public function constructWithOtherRegexp()
  115. {
  116. $actual = 'File Name.tar.gz';
  117. $expected = 'file-name.tar.gz';
  118. $this->slugify = new Slugify('/([^a-z0-9.]|-)+/');
  119. $this->assertEquals($expected, $this->slugify->slugify($actual));
  120. }
  121. public function provider()
  122. {
  123. return array(
  124. array(' a b ', 'a-b'),
  125. array('Hello', 'hello'),
  126. array('Hello World', 'hello-world'),
  127. array('Привет мир', 'privet-mir'),
  128. array('Привіт світ', 'privit-svit'),
  129. array('Hello: World', 'hello-world'),
  130. array('H+e#l1l--o/W§o r.l:d)', 'h-e-l1l-o-w-o-r-l-d'),
  131. array(': World', 'world'),
  132. array('Hello World!', 'hello-world'),
  133. array('Ä ä Ö ö Ü ü ß', 'ae-ae-oe-oe-ue-ue-ss'),
  134. array('Á À á à É È é è Ó Ò ó ò Ñ ñ Ú Ù ú ù', 'a-a-a-a-e-e-e-e-o-o-o-o-n-n-u-u-u-u'),
  135. array('Â â Ê ê Ô ô Û û', 'a-a-e-e-o-o-u-u'),
  136. array('Â â Ê ê Ô ô Û 1', 'a-a-e-e-o-o-u-1'),
  137. array('°¹²³@', '0123at'),
  138. array('Mórë thån wørds', 'more-than-words'),
  139. array('Блоґ їжачка', 'blog-jizhachka'),
  140. array('фильм', 'film'),
  141. array('драма', 'drama'),
  142. array('ελληνικά', 'ellenika'),
  143. array('C’est du français !', 'c-est-du-francais'),
  144. array('هذه هي اللغة العربية', 'hthh-hy-llgh-laarby'),
  145. array('مرحبا العالم', 'mrhb-laa-lm'),
  146. array('Één jaar', 'een-jaar'),
  147. array('tiếng việt rất khó', 'tieng-viet-rat-kho')
  148. );
  149. }
  150. }