SlugifyTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. use Mockery;
  13. /**
  14. * SlugifyTest
  15. *
  16. * @category test
  17. * @package org.cocur.slugify
  18. * @author Florian Eckerstorfer <florian@eckerstorfer.co>
  19. * @author Ivo Bathke <ivo.bathke@gmail.com>
  20. * @author Marchenko Alexandr
  21. * @copyright 2012-2014 Florian Eckerstorfer
  22. * @license http://www.opensource.org/licenses/MIT The MIT License
  23. */
  24. class SlugifyTest extends \PHPUnit_Framework_TestCase
  25. {
  26. /**
  27. * @var Slugify
  28. */
  29. private $slugify;
  30. /**
  31. * @var \Cocur\Slugify\RuleProvider\RuleProviderInterface|\Mockery\MockInterface
  32. */
  33. private $provider;
  34. protected function setUp()
  35. {
  36. $this->provider = Mockery::mock('\Cocur\Slugify\RuleProvider\RuleProviderInterface');
  37. $this->provider->shouldReceive('getRules')->andReturn([]);
  38. $this->slugify = new Slugify([], $this->provider);
  39. }
  40. /**
  41. * @test
  42. * @dataProvider defaultRuleProvider
  43. * @covers Cocur\Slugify\Slugify::slugify()
  44. */
  45. public function slugifyReturnsSlugifiedStringUsingDefaultProvider($string, $result)
  46. {
  47. $slugify = new Slugify();
  48. $this->assertEquals($result, $slugify->slugify($string));
  49. }
  50. /**
  51. * @test
  52. * @covers Cocur\Slugify\Slugify::addRule()
  53. * @covers Cocur\Slugify\Slugify::slugify()
  54. */
  55. public function addRuleAddsRule()
  56. {
  57. $this->assertInstanceOf(
  58. 'Cocur\Slugify\Slugify',
  59. $this->slugify->addRule('X', 'y')
  60. );
  61. $this->assertEquals('y', $this->slugify->slugify('X'));
  62. }
  63. /**
  64. * @test
  65. * @covers Cocur\Slugify\Slugify::addRules()
  66. * @covers Cocur\Slugify\Slugify::slugify()
  67. */
  68. public function addRulesAddsMultipleRules()
  69. {
  70. $this->assertInstanceOf(
  71. 'Cocur\Slugify\Slugify',
  72. $this->slugify->addRules(['x' => 'y', 'a' => 'b'])
  73. );
  74. $this->assertEquals('yb', $this->slugify->slugify('xa'));
  75. }
  76. /**
  77. * @test
  78. * @covers Cocur\Slugify\Slugify::activateRuleset()
  79. */
  80. public function activateRulesetActivatesTheGivenRuleset()
  81. {
  82. $provider = Mockery::mock('\Cocur\Slugify\RuleProvider\RuleProviderInterface');
  83. $provider->shouldReceive('getRules')->with('esperanto')->once()->andReturn(['ĉ' => 'cx']);
  84. $slugify = new Slugify(['rulesets' => []], $provider);
  85. $this->assertInstanceOf(
  86. 'Cocur\Slugify\Slugify',
  87. $slugify->activateRuleset('esperanto')
  88. );
  89. $this->assertEquals('sercxi', $slugify->slugify('serĉi'));
  90. }
  91. /**
  92. * @test
  93. * @covers Cocur\Slugify\Slugify::create()
  94. */
  95. public function createReturnsAnInstance()
  96. {
  97. $this->assertInstanceOf('Cocur\\Slugify\\SlugifyInterface', Slugify::create());
  98. }
  99. /**
  100. * @test
  101. * @covers Cocur\Slugify\Slugify::__construct()
  102. */
  103. public function constructWithOtherRegexp()
  104. {
  105. $this->slugify = new Slugify(['regexp' => '/([^a-z0-9.]|-)+/']);
  106. $this->assertEquals('file-name.tar.gz', $this->slugify->slugify('File Name.tar.gz'));
  107. }
  108. /**
  109. * @test
  110. * @covers Cocur\Slugify\Slugify::__construct()
  111. * @covers Cocur\Slugify\Slugify::slugify()
  112. */
  113. public function doNotConvertToLowercase()
  114. {
  115. $actual = 'File Name';
  116. $expected = 'File-Name';
  117. $this->slugify = new Slugify(['lowercase' => false]);
  118. $this->assertEquals($expected, $this->slugify->slugify($actual));
  119. }
  120. /**
  121. * @test
  122. * @dataProvider customRulesProvider
  123. */
  124. public function customRules($rule, $string, $result)
  125. {
  126. $slugify = new Slugify();
  127. $slugify->activateRuleSet($rule);
  128. $this->assertSame($result, $slugify->slugify($string));
  129. }
  130. public function customRulesProvider()
  131. {
  132. return [
  133. ['azerbaijani', 'əöüğşçı', 'eougsci'],
  134. ['azerbaijani', 'Fərhad Səfərov', 'ferhad-seferov'],
  135. ['croatian', 'Č Ć Ž Š Đ č ć ž š đ', 'c-c-z-s-dj-c-c-z-s-dj'],
  136. ['danish', 'Æ æ Ø ø Å å É é', 'ae-ae-oe-oe-aa-aa-e-e'],
  137. ['romanian', 'ă î â ş ș ţ ț Ă Î Â Ş Ș Ţ Ț', 'a-i-a-s-s-t-t-a-i-a-s-s-t-t'],
  138. ['serbian', 'А Б В Г Д Ђ Е Ж З И Ј К Л Љ М Н Њ О П Р С Т Ћ У Ф Х Ц Ч Џ Ш а б в г д ђ е ж з и ј к л љ м н њ о п р с т ћ у ф х ц ч џ ш Š Đ Ž Ć Č š đ ž ć č', 'a-b-v-g-d-dj-e-z-z-i-j-k-l-lj-m-n-nj-o-p-r-s-t-c-u-f-h-c-c-dz-s-a-b-v-g-d-dj-e-z-z-i-j-k-l-lj-m-n-nj-o-p-r-s-t-c-u-f-h-c-c-dz-s-s-dj-z-c-c-s-dj-z-c-c'],
  139. ['lithuanian', 'Ą Č Ę Ė Į Š Ų Ū Ž ą č ę ė į š ų ū ž', 'a-c-e-e-i-s-u-u-z-a-c-e-e-i-s-u-u-z'],
  140. ['estonian', 'Š Ž Õ Ä Ö Ü š ž õ ä ö ü', 's-z-o-a-o-u-s-z-o-a-o-u'],
  141. ];
  142. }
  143. /**
  144. * @test
  145. * @covers Cocur\Slugify\Slugify::__construct()
  146. * @covers Cocur\Slugify\Slugify::slugify()
  147. */
  148. public function slugifyDefaultsToSeparatorOption()
  149. {
  150. $actual = 'file name';
  151. $expected = 'file__name';
  152. $this->slugify = new Slugify(['separator' => '__']);
  153. $this->assertEquals($expected, $this->slugify->slugify($actual));
  154. }
  155. /**
  156. * @test
  157. * @covers Cocur\Slugify\Slugify::__construct()
  158. * @covers Cocur\Slugify\Slugify::slugify()
  159. */
  160. public function slugifyHonorsSeparatorArgument()
  161. {
  162. $actual = 'file name';
  163. $expected = 'file__name';
  164. $this->slugify = new Slugify(['separator' => 'dummy']);
  165. $this->assertEquals($expected, $this->slugify->slugify($actual, '__'));
  166. }
  167. /**
  168. * @test
  169. * @covers Cocur\Slugify\Slugify::slugify()
  170. */
  171. public function slugifyOptionsArray()
  172. {
  173. $this->assertEquals('file-name', $this->slugify->slugify('file name'));
  174. $this->assertEquals('file+name', $this->slugify->slugify('file name', ['separator' => '+']));
  175. $this->assertEquals('name-1', $this->slugify->slugify('name(1)'));
  176. $this->assertEquals('name(1)', $this->slugify->slugify('name(1)', ['regexp' => '/([^a-z0-9.()]|-)+/']));
  177. $this->assertEquals('file-name', $this->slugify->slugify('FILE NAME'));
  178. $this->assertEquals('FILE-NAME', $this->slugify->slugify('FILE NAME', ['lowercase' => false]));
  179. }
  180. /**
  181. * @test
  182. * @covers Cocur\Slugify\Slugify::slugify()
  183. */
  184. public function slugifyCustomRuleSet()
  185. {
  186. $slugify = new Slugify();
  187. $this->assertSame('fur', $slugify->slugify('für', ['ruleset' => 'turkish']));
  188. $this->assertSame('fuer', $slugify->slugify('für'));
  189. }
  190. public function defaultRuleProvider()
  191. {
  192. return [
  193. [' a b ', 'a-b'],
  194. ['Hello', 'hello'],
  195. ['Hello World', 'hello-world'],
  196. ['Привет мир', 'privet-mir'],
  197. ['Привіт світ', 'privit-svit'],
  198. ['Hello: World', 'hello-world'],
  199. ['H+e#l1l--o/W§o r.l:d)', 'h-e-l1l-o-w-o-r-l-d'],
  200. [': World', 'world'],
  201. ['Hello World!', 'hello-world'],
  202. ['Ä ä Ö ö Ü ü ß', 'ae-ae-oe-oe-ue-ue-ss'],
  203. ['Á À á à É È é è Ó Ò ó ò Ñ ñ Ú Ù ú ù', 'a-a-a-a-e-e-e-e-o-o-o-o-n-n-u-u-u-u'],
  204. ['Â â Ê ê Ô ô Û û', 'a-a-e-e-o-o-u-u'],
  205. ['Â â Ê ê Ô ô Û 1', 'a-a-e-e-o-o-u-1'],
  206. ['°¹²³⁴⁵⁶⁷⁸⁹@₀₁₂₃₄₅₆₇₈₉', '0123456789at0123456789'],
  207. ['Mórë thån wørds', 'more-thaan-woerds'],
  208. ['Блоґ їжачка', 'blog-jizhachka'],
  209. ['фильм', 'film'],
  210. ['драма', 'drama'],
  211. ['Ύπαρξη Αυτής η Σκουληκομυρμηγκότρυπα', 'iparxi-autis-i-skoulikomirmigkotripa'],
  212. ['Français Œuf où à', 'francais-oeuf-ou-a'],
  213. ['هذه هي اللغة العربية', 'hthh-hy-llgh-laarby'],
  214. ['مرحبا العالم', 'mrhb-laa-lm'],
  215. ['Één jaar', 'een-jaar'],
  216. ['tiếng việt rất khó', 'tieng-viet-rat-kho'],
  217. ['Nguyễn Đăng Khoa', 'nguyen-dang-khoa'],
  218. ['နှစ်သစ်ကူးတွင် သတ္တဝါတွေ စိတ်ချမ်းသာ ကိုယ်ကျန်းမာ၍ ကောင်းခြင်း အနန္တနှင့် ပြည့်စုံကြပါစေ', 'nhitthitkutwin-thttwatwe-seikkhyaantha-koekyaanmaywae-kaungkhyin-anntnhin-pyisonkypase'],
  219. ['Zażółć żółcią gęślą jaźń', 'zazolc-zolcia-gesla-jazn'],
  220. ['Mężny bądź chroń pułk twój i sześć flag', 'mezny-badz-chron-pulk-twoj-i-szesc-flag'],
  221. ['ერთი ორი სამი ოთხი ხუთი', 'erti-ori-sami-otkhi-khuti'],
  222. ['अ ऒ न द', 'a-oii-na-tha'],
  223. ['Æ Ø Å æ ø å', 'ae-oe-aa-ae-oe-aa'],
  224. [str_repeat('Übergrößenträger', 1000), str_repeat('uebergroessentraeger', 1000)],
  225. [str_repeat('my🎉', 5000), substr(str_repeat('my-', 5000), 0, -1)],
  226. [str_repeat('hi🇦🇹', 5000), substr(str_repeat('hi-', 5000), 0, -1)],
  227. ['Č Ć Ž Š Đ č ć ž š đ', 'c-c-z-s-d-c-c-z-s-d'],
  228. ['Ą Č Ę Ė Į Š Ų Ū Ž ą č ę ė į š ų ū ž', 'a-c-e-e-i-s-u-u-z-a-c-e-e-i-s-u-u-z'],
  229. ];
  230. }
  231. }