InflectorTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://book.cakephp.org/3.0/en/development/testing.html
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Utility;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\Utility\Inflector;
  18. /**
  19. * Short description for class.
  20. */
  21. class InflectorTest extends TestCase
  22. {
  23. /**
  24. * A list of chars to test transliteration.
  25. *
  26. * @var array
  27. */
  28. public static $maps = [
  29. 'de' => [ /* German */
  30. 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss',
  31. 'ẞ' => 'SS'
  32. ],
  33. 'latin' => [
  34. 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Å' => 'A', 'Ă' => 'A', 'Æ' => 'AE', 'Ç' =>
  35. 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
  36. 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ő' => 'O', 'Ø' => 'O',
  37. 'Ș' => 'S', 'Ț' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ű' => 'U',
  38. 'Ý' => 'Y', 'Þ' => 'TH', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a',
  39. 'å' => 'a', 'ă' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
  40. 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' =>
  41. 'o', 'ô' => 'o', 'õ' => 'o', 'ő' => 'o', 'ø' => 'o', 'ș' => 's', 'ț' => 't', 'ù' => 'u', 'ú' => 'u',
  42. 'û' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y'
  43. ],
  44. 'tr' => [ /* Turkish */
  45. 'ş' => 's', 'Ş' => 'S', 'ı' => 'i', 'İ' => 'I', 'ç' => 'c', 'Ç' => 'C', 'ğ' => 'g', 'Ğ' => 'G'
  46. ],
  47. 'uk' => [ /* Ukrainian */
  48. 'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g'
  49. ],
  50. 'cs' => [ /* Czech */
  51. 'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
  52. 'ž' => 'z', 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T',
  53. 'Ů' => 'U', 'Ž' => 'Z'
  54. ],
  55. 'pl' => [ /* Polish */
  56. 'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ś' => 's', 'ź' => 'z',
  57. 'ż' => 'z', 'Ą' => 'A', 'Ć' => 'C', 'Ł' => 'L', 'Ń' => 'N', 'Ó' => 'O', 'Ś' => 'S',
  58. 'Ź' => 'Z', 'Ż' => 'Z'
  59. ],
  60. 'ro' => [ /* Romanian */
  61. 'ă' => 'a', 'â' => 'a', 'î' => 'i', 'ș' => 's', 'ț' => 't', 'Ţ' => 'T', 'ţ' => 't'
  62. ],
  63. 'lv' => [ /* Latvian */
  64. 'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n',
  65. 'š' => 's', 'ū' => 'u', 'ž' => 'z', 'Ā' => 'A', 'Č' => 'C', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'I',
  66. 'Ķ' => 'K', 'Ļ' => 'L', 'Ņ' => 'N', 'Š' => 'S', 'Ū' => 'U', 'Ž' => 'Z'
  67. ],
  68. 'lt' => [ /* Lithuanian */
  69. 'ą' => 'a', 'č' => 'c', 'ę' => 'e', 'ė' => 'e', 'į' => 'i', 'š' => 's', 'ų' => 'u', 'ū' => 'u', 'ž' => 'z',
  70. 'Ą' => 'A', 'Č' => 'C', 'Ę' => 'E', 'Ė' => 'E', 'Į' => 'I', 'Š' => 'S', 'Ų' => 'U', 'Ū' => 'U', 'Ž' => 'Z'
  71. ]
  72. ];
  73. /**
  74. * tearDown
  75. *
  76. * @return void
  77. */
  78. public function tearDown()
  79. {
  80. parent::tearDown();
  81. Inflector::reset();
  82. }
  83. /**
  84. * testInflectingSingulars method
  85. *
  86. * @dataProvider singularizeProvider
  87. * @return void
  88. */
  89. public function testInflectingSingulars($singular, $plural)
  90. {
  91. $this->assertEquals($singular, Inflector::singularize($plural));
  92. }
  93. /**
  94. * Data provider for testing singularize()
  95. *
  96. * @return array
  97. */
  98. public function singularizeProvider()
  99. {
  100. return [
  101. ['categoria', 'categorias'],
  102. ['menu', 'menus'],
  103. ['news', 'news'],
  104. ['food_menu', 'food_menus'],
  105. ['Menu', 'Menus'],
  106. ['FoodMenu', 'FoodMenus'],
  107. ['house', 'houses'],
  108. ['powerhouse', 'powerhouses'],
  109. ['quiz', 'quizzes'],
  110. ['Bus', 'Buses'],
  111. ['bus', 'buses'],
  112. ['matrix_row', 'matrix_rows'],
  113. ['matrix', 'matrices'],
  114. ['vertex', 'vertices'],
  115. ['index', 'indices'],
  116. ['Alias', 'Aliases'],
  117. ['Alias', 'Alias'],
  118. ['Media', 'Media'],
  119. ['NodeMedia', 'NodeMedia'],
  120. ['alumnus', 'alumni'],
  121. ['bacillus', 'bacilli'],
  122. ['cactus', 'cacti'],
  123. ['focus', 'foci'],
  124. ['fungus', 'fungi'],
  125. ['nucleus', 'nuclei'],
  126. ['octopus', 'octopuses'],
  127. ['radius', 'radii'],
  128. ['stimulus', 'stimuli'],
  129. ['syllabus', 'syllabi'],
  130. ['terminus', 'termini'],
  131. ['virus', 'viruses'],
  132. ['person', 'people'],
  133. ['glove', 'gloves'],
  134. ['dove', 'doves'],
  135. ['life', 'lives'],
  136. ['knife', 'knives'],
  137. ['wolf', 'wolves'],
  138. ['slave', 'slaves'],
  139. ['shelf', 'shelves'],
  140. ['taxi', 'taxis'],
  141. ['tax', 'taxes'],
  142. ['Tax', 'Taxes'],
  143. ['AwesomeTax', 'AwesomeTaxes'],
  144. ['fax', 'faxes'],
  145. ['wax', 'waxes'],
  146. ['niche', 'niches'],
  147. ['cave', 'caves'],
  148. ['grave', 'graves'],
  149. ['wave', 'waves'],
  150. ['bureau', 'bureaus'],
  151. ['genetic_analysis', 'genetic_analyses'],
  152. ['doctor_diagnosis', 'doctor_diagnoses'],
  153. ['paranthesis', 'parantheses'],
  154. ['Cause', 'Causes'],
  155. ['colossus', 'colossuses'],
  156. ['diagnosis', 'diagnoses'],
  157. ['basis', 'bases'],
  158. ['analysis', 'analyses'],
  159. ['curve', 'curves'],
  160. ['cafe', 'cafes'],
  161. ['roof', 'roofs'],
  162. ['foe', 'foes'],
  163. ['database', 'databases'],
  164. ['cookie', 'cookies'],
  165. ['thief', 'thieves'],
  166. ['potato', 'potatoes'],
  167. ['hero', 'heroes'],
  168. ['buffalo', 'buffaloes'],
  169. ['baby', 'babies'],
  170. ['tooth', 'teeth'],
  171. ['goose', 'geese'],
  172. ['foot', 'feet'],
  173. ['objective', 'objectives'],
  174. ['archive', 'archives'],
  175. ['brief', 'briefs'],
  176. ['quota', 'quotas'],
  177. ['curve', 'curves'],
  178. ['body_curve', 'body_curves'],
  179. ['metadata', 'metadata'],
  180. ['files_metadata', 'files_metadata'],
  181. ['address', 'addresses'],
  182. ['sieve', 'sieves'],
  183. ['blue_octopus', 'blue_octopuses'],
  184. ['chef', 'chefs'],
  185. ['', ''],
  186. ];
  187. }
  188. /**
  189. * Test that overlapping irregulars don't collide.
  190. *
  191. * @return void
  192. */
  193. public function testSingularizeMultiWordIrregular()
  194. {
  195. Inflector::rules('irregular', [
  196. 'pregunta_frecuente' => 'preguntas_frecuentes',
  197. 'categoria_pregunta_frecuente' => 'categorias_preguntas_frecuentes',
  198. ]);
  199. $this->assertEquals('pregunta_frecuente', Inflector::singularize('preguntas_frecuentes'));
  200. $this->assertEquals(
  201. 'categoria_pregunta_frecuente',
  202. Inflector::singularize('categorias_preguntas_frecuentes')
  203. );
  204. $this->assertEquals(
  205. 'faq_categoria_pregunta_frecuente',
  206. Inflector::singularize('faq_categorias_preguntas_frecuentes')
  207. );
  208. }
  209. /**
  210. * testInflectingPlurals method
  211. *
  212. * @dataProvider pluralizeProvider
  213. * @return void
  214. */
  215. public function testInflectingPlurals($plural, $singular)
  216. {
  217. $this->assertEquals($plural, Inflector::pluralize($singular));
  218. }
  219. /**
  220. * Data provider for testing pluralize()
  221. *
  222. * @return array
  223. */
  224. public function pluralizeProvider()
  225. {
  226. return [
  227. ['axmen', 'axman'],
  228. ['men', 'man'],
  229. ['women', 'woman'],
  230. ['humans', 'human'],
  231. ['axmen', 'axman'],
  232. ['men', 'man'],
  233. ['women', 'woman'],
  234. ['humans', 'human'],
  235. ['categorias', 'categoria'],
  236. ['houses', 'house'],
  237. ['powerhouses', 'powerhouse'],
  238. ['Buses', 'Bus'],
  239. ['buses', 'bus'],
  240. ['menus', 'menu'],
  241. ['news', 'news'],
  242. ['food_menus', 'food_menu'],
  243. ['Menus', 'Menu'],
  244. ['FoodMenus', 'FoodMenu'],
  245. ['quizzes', 'quiz'],
  246. ['matrix_rows', 'matrix_row'],
  247. ['matrices', 'matrix'],
  248. ['vertices', 'vertex'],
  249. ['indices', 'index'],
  250. ['Aliases', 'Alias'],
  251. ['Aliases', 'Aliases'],
  252. ['Media', 'Media'],
  253. ['NodeMedia', 'NodeMedia'],
  254. ['alumni', 'alumnus'],
  255. ['bacilli', 'bacillus'],
  256. ['cacti', 'cactus'],
  257. ['foci', 'focus'],
  258. ['fungi', 'fungus'],
  259. ['nuclei', 'nucleus'],
  260. ['octopuses', 'octopus'],
  261. ['radii', 'radius'],
  262. ['stimuli', 'stimulus'],
  263. ['syllabi', 'syllabus'],
  264. ['termini', 'terminus'],
  265. ['viruses', 'virus'],
  266. ['people', 'person'],
  267. ['people', 'people'],
  268. ['gloves', 'glove'],
  269. ['crises', 'crisis'],
  270. ['taxes', 'tax'],
  271. ['waves', 'wave'],
  272. ['bureaus', 'bureau'],
  273. ['cafes', 'cafe'],
  274. ['roofs', 'roof'],
  275. ['foes', 'foe'],
  276. ['cookies', 'cookie'],
  277. ['wolves', 'wolf'],
  278. ['thieves', 'thief'],
  279. ['potatoes', 'potato'],
  280. ['heroes', 'hero'],
  281. ['buffaloes', 'buffalo'],
  282. ['teeth', 'tooth'],
  283. ['geese', 'goose'],
  284. ['feet', 'foot'],
  285. ['objectives', 'objective'],
  286. ['briefs', 'brief'],
  287. ['quotas', 'quota'],
  288. ['curves', 'curve'],
  289. ['body_curves', 'body_curve'],
  290. ['metadata', 'metadata'],
  291. ['files_metadata', 'files_metadata'],
  292. ['stadia', 'stadia'],
  293. ['Addresses', 'Address'],
  294. ['sieves', 'sieve'],
  295. ['blue_octopuses', 'blue_octopus'],
  296. ['chefs', 'chef'],
  297. ['', ''],
  298. ['pokemon', 'pokemon']
  299. ];
  300. }
  301. /**
  302. * Test that overlapping irregulars don't collide.
  303. *
  304. * @return void
  305. */
  306. public function testPluralizeMultiWordIrregular()
  307. {
  308. Inflector::rules('irregular', [
  309. 'pregunta_frecuente' => 'preguntas_frecuentes',
  310. 'categoria_pregunta_frecuente' => 'categorias_preguntas_frecuentes',
  311. ]);
  312. $this->assertEquals('preguntas_frecuentes', Inflector::pluralize('pregunta_frecuente'));
  313. $this->assertEquals(
  314. 'categorias_preguntas_frecuentes',
  315. Inflector::pluralize('categoria_pregunta_frecuente')
  316. );
  317. $this->assertEquals(
  318. 'faq_categorias_preguntas_frecuentes',
  319. Inflector::pluralize('faq_categoria_pregunta_frecuente')
  320. );
  321. }
  322. /**
  323. * testInflectingMultiWordIrregulars
  324. *
  325. * @return void
  326. */
  327. public function testInflectingMultiWordIrregulars()
  328. {
  329. // unset the default rules in order to avoid them possibly matching
  330. // the words in case the irregular regex won't match, the tests
  331. // should fail in that case
  332. Inflector::rules('plural', [
  333. 'rules' => [],
  334. ]);
  335. Inflector::rules('singular', [
  336. 'rules' => [],
  337. ]);
  338. $this->assertEquals('wisdom tooth', Inflector::singularize('wisdom teeth'));
  339. $this->assertEquals('wisdom-tooth', Inflector::singularize('wisdom-teeth'));
  340. $this->assertEquals('wisdom_tooth', Inflector::singularize('wisdom_teeth'));
  341. $this->assertEquals('sweet potatoes', Inflector::pluralize('sweet potato'));
  342. $this->assertEquals('sweet-potatoes', Inflector::pluralize('sweet-potato'));
  343. $this->assertEquals('sweet_potatoes', Inflector::pluralize('sweet_potato'));
  344. }
  345. /**
  346. * testSlug method
  347. *
  348. * @group deprecated
  349. * @return void
  350. */
  351. public function testSlug()
  352. {
  353. $this->deprecated(function () {
  354. $result = Inflector::slug('Foo Bar: Not just for breakfast any-more');
  355. $expected = 'Foo-Bar-Not-just-for-breakfast-any-more';
  356. $this->assertEquals($expected, $result);
  357. $result = Inflector::slug('this/is/a/path');
  358. $expected = 'this-is-a-path';
  359. $this->assertEquals($expected, $result);
  360. $result = Inflector::slug('Foo Bar: Not just for breakfast any-more', '_');
  361. $expected = 'Foo_Bar_Not_just_for_breakfast_any_more';
  362. $this->assertEquals($expected, $result);
  363. $result = Inflector::slug('Foo Bar: Not just for breakfast any-more', '+');
  364. $expected = 'Foo+Bar+Not+just+for+breakfast+any+more';
  365. $this->assertEquals($expected, $result);
  366. $result = Inflector::slug('Äpfel Über Öl grün ärgert groß öko', '-');
  367. $expected = 'Aepfel-Ueber-Oel-gruen-aergert-gross-oeko';
  368. $this->assertEquals($expected, $result);
  369. $result = Inflector::slug('The truth - and- more- news', '-');
  370. $expected = 'The-truth-and-more-news';
  371. $this->assertEquals($expected, $result);
  372. $result = Inflector::slug('The truth: and more news', '-');
  373. $expected = 'The-truth-and-more-news';
  374. $this->assertEquals($expected, $result);
  375. $result = Inflector::slug('La langue française est un attribut de souveraineté en France', '-');
  376. $expected = 'La-langue-francaise-est-un-attribut-de-souverainete-en-France';
  377. $this->assertEquals($expected, $result);
  378. $result = Inflector::slug('!@$#exciting stuff! - what !@-# was that?', '-');
  379. $expected = 'exciting-stuff-what-was-that';
  380. $this->assertEquals($expected, $result);
  381. $result = Inflector::slug('20% of profits went to me!', '-');
  382. $expected = '20-of-profits-went-to-me';
  383. $this->assertEquals($expected, $result);
  384. $result = Inflector::slug('#this melts your face1#2#3', '-');
  385. $expected = 'this-melts-your-face1-2-3';
  386. $this->assertEquals($expected, $result);
  387. $result = Inflector::slug('controller/action/りんご/1');
  388. $expected = 'controller-action-りんご-1';
  389. $this->assertEquals($expected, $result);
  390. $result = Inflector::slug('の話が出たので大丈夫かなあと');
  391. $expected = 'の話が出たので大丈夫かなあと';
  392. $this->assertEquals($expected, $result);
  393. $result = Inflector::slug('posts/view/한국어/page:1/sort:asc');
  394. $expected = 'posts-view-한국어-page-1-sort-asc';
  395. $this->assertEquals($expected, $result);
  396. $result = Inflector::slug("non\xc2\xa0breaking\xc2\xa0space");
  397. $this->assertEquals('non-breaking-space', $result);
  398. $result = Inflector::slug('Foo Bar: Not just for breakfast any-more', '');
  399. $expected = 'FooBarNotjustforbreakfastanymore';
  400. $this->assertEquals($expected, $result);
  401. });
  402. }
  403. /**
  404. * Test slug() with a complete list of special chars.
  405. *
  406. * @group deprecated
  407. * @return void
  408. */
  409. public function testSlugCharList()
  410. {
  411. $this->deprecated(function () {
  412. foreach (self::$maps as $language => $list) {
  413. foreach ($list as $from => $to) {
  414. $result = Inflector::slug($from);
  415. $this->assertEquals($to, $result, $from . ' (' . $language . ') should be ' . $to . ' - but is ' . $result);
  416. }
  417. }
  418. });
  419. }
  420. /**
  421. * testSlugWithMap method
  422. *
  423. * @group deprecated
  424. * @return void
  425. */
  426. public function testSlugWithMap()
  427. {
  428. $this->deprecated(function () {
  429. Inflector::rules('transliteration', ['r' => '1']);
  430. $result = Inflector::slug('replace every r');
  431. $expected = '1eplace-eve1y-1';
  432. $this->assertEquals($expected, $result);
  433. $result = Inflector::slug('replace every r', '_');
  434. $expected = '1eplace_eve1y_1';
  435. $this->assertEquals($expected, $result);
  436. });
  437. }
  438. /**
  439. * testSlugWithMapOverridingDefault method
  440. *
  441. * @group deprecated
  442. * @return void
  443. */
  444. public function testSlugWithMapOverridingDefault()
  445. {
  446. $this->deprecated(function () {
  447. Inflector::rules('transliteration', ['å' => 'aa', 'ø' => 'oe']);
  448. $result = Inflector::slug('Testing æ ø å', '-');
  449. $expected = 'Testing-ae-oe-aa';
  450. $this->assertEquals($expected, $result);
  451. });
  452. }
  453. /**
  454. * testUnderscore method
  455. *
  456. * @return void
  457. */
  458. public function testUnderscore()
  459. {
  460. $this->assertSame('test_thing', Inflector::underscore('TestThing'));
  461. $this->assertSame('test_thing', Inflector::underscore('testThing'));
  462. $this->assertSame('test_thing_extra', Inflector::underscore('TestThingExtra'));
  463. $this->assertSame('test_thing_extra', Inflector::underscore('testThingExtra'));
  464. $this->assertSame('test_this_thing', Inflector::underscore('test-this-thing'));
  465. $this->assertSame('test_thing_extrå', Inflector::underscore('testThingExtrå'));
  466. // Identical checks test the cache code path.
  467. $this->assertSame('test_thing', Inflector::underscore('TestThing'));
  468. $this->assertSame('test_thing', Inflector::underscore('testThing'));
  469. $this->assertSame('test_thing_extra', Inflector::underscore('TestThingExtra'));
  470. $this->assertSame('test_thing_extra', Inflector::underscore('testThingExtra'));
  471. $this->assertSame('test_thing_extrå', Inflector::underscore('testThingExtrå'));
  472. // Test stupid values
  473. $this->assertSame('', Inflector::underscore(''));
  474. $this->assertSame('0', Inflector::underscore(0));
  475. $this->assertSame('', Inflector::underscore(false));
  476. }
  477. /**
  478. * testDasherized method
  479. *
  480. * @return void
  481. */
  482. public function testDasherized()
  483. {
  484. $this->assertSame('test-thing', Inflector::dasherize('TestThing'));
  485. $this->assertSame('test-thing', Inflector::dasherize('testThing'));
  486. $this->assertSame('test-thing-extra', Inflector::dasherize('TestThingExtra'));
  487. $this->assertSame('test-thing-extra', Inflector::dasherize('testThingExtra'));
  488. $this->assertSame('test-this-thing', Inflector::dasherize('test_this_thing'));
  489. // Test stupid values
  490. $this->assertSame('', Inflector::dasherize(null));
  491. $this->assertSame('', Inflector::dasherize(''));
  492. $this->assertSame('0', Inflector::dasherize(0));
  493. $this->assertSame('', Inflector::dasherize(false));
  494. }
  495. /**
  496. * Demonstrate the expected output for bad inputs
  497. *
  498. * @return void
  499. */
  500. public function testCamelize()
  501. {
  502. $this->assertSame('TestThing', Inflector::camelize('test_thing'));
  503. $this->assertSame('Test-thing', Inflector::camelize('test-thing'));
  504. $this->assertSame('TestThing', Inflector::camelize('test thing'));
  505. $this->assertSame('Test_thing', Inflector::camelize('test_thing', '-'));
  506. $this->assertSame('TestThing', Inflector::camelize('test-thing', '-'));
  507. $this->assertSame('TestThing', Inflector::camelize('test thing', '-'));
  508. $this->assertSame('Test_thing', Inflector::camelize('test_thing', ' '));
  509. $this->assertSame('Test-thing', Inflector::camelize('test-thing', ' '));
  510. $this->assertSame('TestThing', Inflector::camelize('test thing', ' '));
  511. $this->assertSame('TestPlugin.TestPluginComments', Inflector::camelize('TestPlugin.TestPluginComments'));
  512. }
  513. /**
  514. * testVariableNaming method
  515. *
  516. * @return void
  517. */
  518. public function testVariableNaming()
  519. {
  520. $this->assertEquals('testField', Inflector::variable('test_field'));
  521. $this->assertEquals('testFieLd', Inflector::variable('test_fieLd'));
  522. $this->assertEquals('testField', Inflector::variable('test field'));
  523. $this->assertEquals('testField', Inflector::variable('Test_field'));
  524. }
  525. /**
  526. * testClassNaming method
  527. *
  528. * @return void
  529. */
  530. public function testClassNaming()
  531. {
  532. $this->assertEquals('ArtistsGenre', Inflector::classify('artists_genres'));
  533. $this->assertEquals('FileSystem', Inflector::classify('file_systems'));
  534. $this->assertEquals('News', Inflector::classify('news'));
  535. $this->assertEquals('Bureau', Inflector::classify('bureaus'));
  536. }
  537. /**
  538. * testTableNaming method
  539. *
  540. * @return void
  541. */
  542. public function testTableNaming()
  543. {
  544. $this->assertEquals('artists_genres', Inflector::tableize('ArtistsGenre'));
  545. $this->assertEquals('file_systems', Inflector::tableize('FileSystem'));
  546. $this->assertEquals('news', Inflector::tableize('News'));
  547. $this->assertEquals('bureaus', Inflector::tableize('Bureau'));
  548. }
  549. /**
  550. * testHumanization method
  551. *
  552. * @return void
  553. */
  554. public function testHumanization()
  555. {
  556. $this->assertEquals('Posts', Inflector::humanize('posts'));
  557. $this->assertEquals('Posts Tags', Inflector::humanize('posts_tags'));
  558. $this->assertEquals('File Systems', Inflector::humanize('file_systems'));
  559. $this->assertSame('', Inflector::humanize(null));
  560. $this->assertSame('', Inflector::humanize(false));
  561. $this->assertSame('Hello Wörld', Inflector::humanize('hello_wörld'));
  562. $this->assertSame('福岡 City', Inflector::humanize('福岡_city'));
  563. }
  564. /**
  565. * testCustomPluralRule method
  566. *
  567. * @return void
  568. */
  569. public function testCustomPluralRule()
  570. {
  571. Inflector::rules('plural', ['/^(custom)$/i' => '\1izables']);
  572. Inflector::rules('uninflected', ['uninflectable']);
  573. $this->assertEquals('customizables', Inflector::pluralize('custom'));
  574. $this->assertEquals('uninflectable', Inflector::pluralize('uninflectable'));
  575. Inflector::rules('plural', ['/^(alert)$/i' => '\1ables']);
  576. Inflector::rules('irregular', ['amaze' => 'amazable', 'phone' => 'phonezes']);
  577. Inflector::rules('uninflected', ['noflect', 'abtuse']);
  578. $this->assertEquals('noflect', Inflector::pluralize('noflect'));
  579. $this->assertEquals('abtuse', Inflector::pluralize('abtuse'));
  580. $this->assertEquals('alertables', Inflector::pluralize('alert'));
  581. $this->assertEquals('amazable', Inflector::pluralize('amaze'));
  582. $this->assertEquals('phonezes', Inflector::pluralize('phone'));
  583. }
  584. /**
  585. * testCustomSingularRule method
  586. *
  587. * @return void
  588. */
  589. public function testCustomSingularRule()
  590. {
  591. Inflector::rules('uninflected', ['singulars']);
  592. Inflector::rules('singular', ['/(eple)r$/i' => '\1', '/(jente)r$/i' => '\1']);
  593. $this->assertEquals('eple', Inflector::singularize('epler'));
  594. $this->assertEquals('jente', Inflector::singularize('jenter'));
  595. Inflector::rules('singular', ['/^(bil)er$/i' => '\1', '/^(inflec|contribu)tors$/i' => '\1ta']);
  596. Inflector::rules('irregular', ['spinor' => 'spins']);
  597. $this->assertEquals('spinor', Inflector::singularize('spins'));
  598. $this->assertEquals('inflecta', Inflector::singularize('inflectors'));
  599. $this->assertEquals('contributa', Inflector::singularize('contributors'));
  600. $this->assertEquals('singulars', Inflector::singularize('singulars'));
  601. }
  602. /**
  603. * testCustomTransliterationRule method
  604. *
  605. * @group deprecated
  606. * @return void
  607. */
  608. public function testCustomTransliterationRule()
  609. {
  610. $this->deprecated(function () {
  611. $this->assertEquals('Testing-ae-o-a', Inflector::slug('Testing æ ø å'));
  612. Inflector::rules('transliteration', ['å' => 'aa', 'ø' => 'oe']);
  613. $this->assertEquals('Testing-ae-oe-aa', Inflector::slug('Testing æ ø å'));
  614. Inflector::rules('transliteration', ['æ' => 'ae', 'å' => 'aa'], true);
  615. $this->assertEquals('Testing-ae-ø-aa', Inflector::slug('Testing æ ø å'));
  616. });
  617. }
  618. /**
  619. * test that setting new rules clears the inflector caches.
  620. *
  621. * @return void
  622. */
  623. public function testRulesClearsCaches()
  624. {
  625. $this->assertEquals('Banana', Inflector::singularize('Bananas'));
  626. $this->assertEquals('bananas', Inflector::tableize('Banana'));
  627. $this->assertEquals('Bananas', Inflector::pluralize('Banana'));
  628. Inflector::rules('singular', ['/(.*)nas$/i' => '\1zzz']);
  629. $this->assertEquals('Banazzz', Inflector::singularize('Bananas'), 'Was inflected with old rules.');
  630. Inflector::rules('plural', ['/(.*)na$/i' => '\1zzz']);
  631. Inflector::rules('irregular', ['corpus' => 'corpora']);
  632. $this->assertEquals('Banazzz', Inflector::pluralize('Banana'), 'Was inflected with old rules.');
  633. $this->assertEquals('corpora', Inflector::pluralize('corpus'), 'Was inflected with old irregular form.');
  634. }
  635. /**
  636. * Test resetting inflection rules.
  637. *
  638. * @return void
  639. */
  640. public function testCustomRuleWithReset()
  641. {
  642. $uninflected = ['atlas', 'lapis', 'onibus', 'pires', 'virus', '.*x'];
  643. $pluralIrregular = ['as' => 'ases'];
  644. Inflector::rules('singular', ['/^(.*)(a|e|o|u)is$/i' => '\1\2l'], true);
  645. Inflector::rules('plural', ['/^(.*)(a|e|o|u)l$/i' => '\1\2is'], true);
  646. Inflector::rules('uninflected', $uninflected, true);
  647. Inflector::rules('irregular', $pluralIrregular, true);
  648. $this->assertEquals('Alcoois', Inflector::pluralize('Alcool'));
  649. $this->assertEquals('Atlas', Inflector::pluralize('Atlas'));
  650. $this->assertEquals('Alcool', Inflector::singularize('Alcoois'));
  651. $this->assertEquals('Atlas', Inflector::singularize('Atlas'));
  652. }
  653. }