SlugifyTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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->assertInstanceOf(
  47. 'Cocur\Slugify\Slugify',
  48. $this->slugify->addRule('X', 'y')
  49. );
  50. $this->assertEquals('y', $this->slugify->slugify('X'));
  51. }
  52. /**
  53. * @test
  54. * @covers Cocur\Slugify\Slugify::addRules()
  55. * @covers Cocur\Slugify\Slugify::slugify()
  56. */
  57. public function addRulesAddsMultipleRules()
  58. {
  59. $this->assertInstanceOf(
  60. 'Cocur\Slugify\Slugify',
  61. $this->slugify->addRules(array('x' => 'y', 'a' => 'b'))
  62. );
  63. $this->assertEquals('yb', $this->slugify->slugify('xa'));
  64. }
  65. /**
  66. * @test
  67. * @covers Cocur\Slugify\Slugify::activateRuleset()
  68. */
  69. public function activateRulesetActivatesTheGivenRuleset()
  70. {
  71. $this->assertInstanceOf(
  72. 'Cocur\Slugify\Slugify',
  73. $this->slugify->activateRuleset('esperanto')
  74. );
  75. $this->assertEquals(
  76. 'sercxi-mangxi-hxirurgio-jxurnalo-sxuo-malgraux',
  77. $this->slugify->slugify('serĉi manĝi ĥirurgio ĵurnalo ŝuo malgraŭ')
  78. );
  79. }
  80. /**
  81. * @test
  82. * @covers Cocur\Slugify\Slugify::activateRuleset()
  83. * @expectedException \InvalidArgumentException
  84. */
  85. public function activateRulesetThrowsExceptionIfInvalidName()
  86. {
  87. $this->slugify->activateRuleset('invalid');
  88. }
  89. /**
  90. * @test
  91. * @covers Cocur\Slugify\Slugify::addRuleset()
  92. * @covers Cocur\Slugify\Slugify::getRulesets()
  93. */
  94. public function addRulesetGetRulesets()
  95. {
  96. $this->assertInstanceOf(
  97. 'Cocur\Slugify\Slugify',
  98. $this->slugify->addRuleset('foo', array('k' => 'key'))
  99. );
  100. $this->assertCount(2, $this->slugify->getRulesets());
  101. }
  102. /**
  103. * @test
  104. * @covers Cocur\Slugify\Slugify::create()
  105. */
  106. public function createReturnsAnInstance()
  107. {
  108. $this->assertInstanceOf('Cocur\\Slugify\\SlugifyInterface', Slugify::create());
  109. }
  110. /**
  111. * @test
  112. * @covers Cocur\Slugify\Slugify::setRegExp()
  113. */
  114. public function otherRegExpsProduceOtherResults()
  115. {
  116. $actual = 'File Name.tar.gz';
  117. $expected = 'file-name.tar.gz';
  118. $this->assertNotEquals($expected, $this->slugify->slugify($actual));
  119. $this->assertInstanceOf(
  120. 'Cocur\Slugify\Slugify',
  121. $this->slugify->setRegExp('/([^a-z0-9.]|-)+/')
  122. );
  123. $this->assertEquals($expected, $this->slugify->slugify($actual));
  124. }
  125. /**
  126. * @test
  127. * @covers Cocur\Slugify\Slugify::__construct()
  128. */
  129. public function constructWithOtherRegexp()
  130. {
  131. $actual = 'File Name.tar.gz';
  132. $expected = 'file-name.tar.gz';
  133. $this->slugify = new Slugify('/([^a-z0-9.]|-)+/');
  134. $this->assertEquals($expected, $this->slugify->slugify($actual));
  135. }
  136. /**
  137. * @test
  138. * @covers Cocur\Slugify\Slugify::__construct()
  139. * @covers Cocur\Slugify\Slugify::slugify()
  140. */
  141. public function doNotConvertToLowercase()
  142. {
  143. $actual = 'File Name';
  144. $expected = 'File-Name';
  145. $this->slugify = new Slugify(null, array('lowercase' => false));
  146. $this->assertEquals($expected, $this->slugify->slugify($actual));
  147. }
  148. /**
  149. * @test
  150. * @covers Cocur\Slugify\Slugify::setOptions()
  151. */
  152. public function setOptionsSetsOptions()
  153. {
  154. $actual = 'File Name';
  155. $expected = 'File-Name';
  156. $this->slugify = new Slugify();
  157. $this->slugify->setOptions(array('lowercase' => false));
  158. $this->assertEquals($expected, $this->slugify->slugify($actual));
  159. }
  160. public function provider()
  161. {
  162. return array(
  163. array(' a b ', 'a-b'),
  164. array('Hello', 'hello'),
  165. array('Hello World', 'hello-world'),
  166. array('Привет мир', 'privet-mir'),
  167. array('Привіт світ', 'privit-svit'),
  168. array('Hello: World', 'hello-world'),
  169. array('H+e#l1l--o/W§o r.l:d)', 'h-e-l1l-o-w-o-r-l-d'),
  170. array(': World', 'world'),
  171. array('Hello World!', 'hello-world'),
  172. array('Ä ä Ö ö Ü ü ß', 'ae-ae-oe-oe-ue-ue-ss'),
  173. array('Á À á à É È é è Ó Ò ó ò Ñ ñ Ú Ù ú ù', 'a-a-a-a-e-e-e-e-o-o-o-o-n-n-u-u-u-u'),
  174. array('Â â Ê ê Ô ô Û û', 'a-a-e-e-o-o-u-u'),
  175. array('Â â Ê ê Ô ô Û 1', 'a-a-e-e-o-o-u-1'),
  176. array('°¹²³⁴⁵⁶⁷⁸⁹@₀₁₂₃₄₅₆₇₈₉', '0123456789at0123456789'),
  177. array('Mórë thån wørds', 'more-thaan-woerds'),
  178. array('Блоґ їжачка', 'blog-jizhachka'),
  179. array('фильм', 'film'),
  180. array('драма', 'drama'),
  181. array('Ύπαρξη Αυτής η Σκουληκομυρμηγκότρυπα', 'iparxi-autis-i-skoulikomirmigkotripa'),
  182. array('C’est du français !', 'c-est-du-francais'),
  183. array('هذه هي اللغة العربية', 'hthh-hy-llgh-laarby'),
  184. array('مرحبا العالم', 'mrhb-laa-lm'),
  185. array('Één jaar', 'een-jaar'),
  186. array('tiếng việt rất khó', 'tieng-viet-rat-kho'),
  187. array('Nguyễn Đăng Khoa', 'nguyen-dang-khoa'),
  188. array('နှစ်သစ်ကူးတွင် သတ္တဝါတွေ စိတ်ချမ်းသာ ကိုယ်ကျန်းမာ၍ ကောင်းခြင်း အနန္တနှင့် ပြည့်စုံကြပါစေ', 'nhitthitkutwin-thttwatwe-seikkhyaantha-koekyaanmaywae-kaungkhyin-anntnhin-pyisonkypase'),
  189. array('Zażółć żółcią gęślą jaźń', 'zazolc-zolcia-gesla-jazn'),
  190. array('Mężny bądź chroń pułk twój i sześć flag', 'mezny-badz-chron-pulk-twoj-i-szesc-flag'),
  191. array('ერთი ორი სამი ოთხი ხუთი', 'erti-ori-sami-otkhi-khuti'),
  192. array(str_repeat('Übergrößenträger', 1000), str_repeat('uebergroessentraeger', 1000)),
  193. array(str_repeat('my️🎉', 5000), substr(str_repeat('my-', 5000), 0, -1)),
  194. array(str_repeat('hi🇦🇹', 5000), substr(str_repeat('hi-', 5000), 0, -1)),
  195. );
  196. }
  197. }