InflectorTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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/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. ['cache', 'caches'],
  187. ];
  188. }
  189. /**
  190. * Test that overlapping irregulars don't collide.
  191. *
  192. * @return void
  193. */
  194. public function testSingularizeMultiWordIrregular()
  195. {
  196. Inflector::rules('irregular', [
  197. 'pregunta_frecuente' => 'preguntas_frecuentes',
  198. 'categoria_pregunta_frecuente' => 'categorias_preguntas_frecuentes',
  199. ]);
  200. $this->assertEquals('pregunta_frecuente', Inflector::singularize('preguntas_frecuentes'));
  201. $this->assertEquals(
  202. 'categoria_pregunta_frecuente',
  203. Inflector::singularize('categorias_preguntas_frecuentes')
  204. );
  205. $this->assertEquals(
  206. 'faq_categoria_pregunta_frecuente',
  207. Inflector::singularize('faq_categorias_preguntas_frecuentes')
  208. );
  209. }
  210. /**
  211. * testInflectingPlurals method
  212. *
  213. * @dataProvider pluralizeProvider
  214. * @return void
  215. */
  216. public function testInflectingPlurals($plural, $singular)
  217. {
  218. $this->assertEquals($plural, Inflector::pluralize($singular));
  219. }
  220. /**
  221. * Data provider for testing pluralize()
  222. *
  223. * @return array
  224. */
  225. public function pluralizeProvider()
  226. {
  227. return [
  228. ['axmen', 'axman'],
  229. ['men', 'man'],
  230. ['women', 'woman'],
  231. ['humans', 'human'],
  232. ['axmen', 'axman'],
  233. ['men', 'man'],
  234. ['women', 'woman'],
  235. ['humans', 'human'],
  236. ['categorias', 'categoria'],
  237. ['houses', 'house'],
  238. ['powerhouses', 'powerhouse'],
  239. ['Buses', 'Bus'],
  240. ['buses', 'bus'],
  241. ['menus', 'menu'],
  242. ['news', 'news'],
  243. ['food_menus', 'food_menu'],
  244. ['Menus', 'Menu'],
  245. ['FoodMenus', 'FoodMenu'],
  246. ['quizzes', 'quiz'],
  247. ['matrix_rows', 'matrix_row'],
  248. ['matrices', 'matrix'],
  249. ['vertices', 'vertex'],
  250. ['indices', 'index'],
  251. ['Aliases', 'Alias'],
  252. ['Aliases', 'Aliases'],
  253. ['Media', 'Media'],
  254. ['NodeMedia', 'NodeMedia'],
  255. ['alumni', 'alumnus'],
  256. ['bacilli', 'bacillus'],
  257. ['cacti', 'cactus'],
  258. ['foci', 'focus'],
  259. ['fungi', 'fungus'],
  260. ['nuclei', 'nucleus'],
  261. ['octopuses', 'octopus'],
  262. ['radii', 'radius'],
  263. ['stimuli', 'stimulus'],
  264. ['syllabi', 'syllabus'],
  265. ['termini', 'terminus'],
  266. ['viruses', 'virus'],
  267. ['people', 'person'],
  268. ['people', 'people'],
  269. ['gloves', 'glove'],
  270. ['crises', 'crisis'],
  271. ['taxes', 'tax'],
  272. ['waves', 'wave'],
  273. ['bureaus', 'bureau'],
  274. ['cafes', 'cafe'],
  275. ['roofs', 'roof'],
  276. ['foes', 'foe'],
  277. ['cookies', 'cookie'],
  278. ['wolves', 'wolf'],
  279. ['thieves', 'thief'],
  280. ['potatoes', 'potato'],
  281. ['heroes', 'hero'],
  282. ['buffaloes', 'buffalo'],
  283. ['teeth', 'tooth'],
  284. ['geese', 'goose'],
  285. ['feet', 'foot'],
  286. ['objectives', 'objective'],
  287. ['briefs', 'brief'],
  288. ['quotas', 'quota'],
  289. ['curves', 'curve'],
  290. ['body_curves', 'body_curve'],
  291. ['metadata', 'metadata'],
  292. ['files_metadata', 'files_metadata'],
  293. ['stadia', 'stadia'],
  294. ['Addresses', 'Address'],
  295. ['sieves', 'sieve'],
  296. ['blue_octopuses', 'blue_octopus'],
  297. ['chefs', 'chef'],
  298. ['', ''],
  299. ['pokemon', 'pokemon'],
  300. ];
  301. }
  302. /**
  303. * Test that overlapping irregulars don't collide.
  304. *
  305. * @return void
  306. */
  307. public function testPluralizeMultiWordIrregular()
  308. {
  309. Inflector::rules('irregular', [
  310. 'pregunta_frecuente' => 'preguntas_frecuentes',
  311. 'categoria_pregunta_frecuente' => 'categorias_preguntas_frecuentes',
  312. ]);
  313. $this->assertEquals('preguntas_frecuentes', Inflector::pluralize('pregunta_frecuente'));
  314. $this->assertEquals(
  315. 'categorias_preguntas_frecuentes',
  316. Inflector::pluralize('categoria_pregunta_frecuente')
  317. );
  318. $this->assertEquals(
  319. 'faq_categorias_preguntas_frecuentes',
  320. Inflector::pluralize('faq_categoria_pregunta_frecuente')
  321. );
  322. }
  323. /**
  324. * testInflectingMultiWordIrregulars
  325. *
  326. * @return void
  327. */
  328. public function testInflectingMultiWordIrregulars()
  329. {
  330. // unset the default rules in order to avoid them possibly matching
  331. // the words in case the irregular regex won't match, the tests
  332. // should fail in that case
  333. Inflector::rules('plural', [
  334. 'rules' => [],
  335. ]);
  336. Inflector::rules('singular', [
  337. 'rules' => [],
  338. ]);
  339. $this->assertEquals('wisdom tooth', Inflector::singularize('wisdom teeth'));
  340. $this->assertEquals('wisdom-tooth', Inflector::singularize('wisdom-teeth'));
  341. $this->assertEquals('wisdom_tooth', Inflector::singularize('wisdom_teeth'));
  342. $this->assertEquals('sweet potatoes', Inflector::pluralize('sweet potato'));
  343. $this->assertEquals('sweet-potatoes', Inflector::pluralize('sweet-potato'));
  344. $this->assertEquals('sweet_potatoes', Inflector::pluralize('sweet_potato'));
  345. }
  346. /**
  347. * testSlug method
  348. *
  349. * @group deprecated
  350. * @return void
  351. */
  352. public function testSlug()
  353. {
  354. $this->deprecated(function () {
  355. $result = Inflector::slug('Foo Bar: Not just for breakfast any-more');
  356. $expected = 'Foo-Bar-Not-just-for-breakfast-any-more';
  357. $this->assertEquals($expected, $result);
  358. $result = Inflector::slug('this/is/a/path');
  359. $expected = 'this-is-a-path';
  360. $this->assertEquals($expected, $result);
  361. $result = Inflector::slug('Foo Bar: Not just for breakfast any-more', '_');
  362. $expected = 'Foo_Bar_Not_just_for_breakfast_any_more';
  363. $this->assertEquals($expected, $result);
  364. $result = Inflector::slug('Foo Bar: Not just for breakfast any-more', '+');
  365. $expected = 'Foo+Bar+Not+just+for+breakfast+any+more';
  366. $this->assertEquals($expected, $result);
  367. $result = Inflector::slug('Äpfel Über Öl grün ärgert groß öko', '-');
  368. $expected = 'Aepfel-Ueber-Oel-gruen-aergert-gross-oeko';
  369. $this->assertEquals($expected, $result);
  370. $result = Inflector::slug('The truth - and- more- news', '-');
  371. $expected = 'The-truth-and-more-news';
  372. $this->assertEquals($expected, $result);
  373. $result = Inflector::slug('The truth: and more news', '-');
  374. $expected = 'The-truth-and-more-news';
  375. $this->assertEquals($expected, $result);
  376. $result = Inflector::slug('La langue française est un attribut de souveraineté en France', '-');
  377. $expected = 'La-langue-francaise-est-un-attribut-de-souverainete-en-France';
  378. $this->assertEquals($expected, $result);
  379. $result = Inflector::slug('!@$#exciting stuff! - what !@-# was that?', '-');
  380. $expected = 'exciting-stuff-what-was-that';
  381. $this->assertEquals($expected, $result);
  382. $result = Inflector::slug('20% of profits went to me!', '-');
  383. $expected = '20-of-profits-went-to-me';
  384. $this->assertEquals($expected, $result);
  385. $result = Inflector::slug('#this melts your face1#2#3', '-');
  386. $expected = 'this-melts-your-face1-2-3';
  387. $this->assertEquals($expected, $result);
  388. $result = Inflector::slug('controller/action/りんご/1');
  389. $expected = 'controller-action-りんご-1';
  390. $this->assertEquals($expected, $result);
  391. $result = Inflector::slug('の話が出たので大丈夫かなあと');
  392. $expected = 'の話が出たので大丈夫かなあと';
  393. $this->assertEquals($expected, $result);
  394. $result = Inflector::slug('posts/view/한국어/page:1/sort:asc');
  395. $expected = 'posts-view-한국어-page-1-sort-asc';
  396. $this->assertEquals($expected, $result);
  397. $result = Inflector::slug("non\xc2\xa0breaking\xc2\xa0space");
  398. $this->assertEquals('non-breaking-space', $result);
  399. $result = Inflector::slug('Foo Bar: Not just for breakfast any-more', '');
  400. $expected = 'FooBarNotjustforbreakfastanymore';
  401. $this->assertEquals($expected, $result);
  402. });
  403. }
  404. /**
  405. * Test slug() with a complete list of special chars.
  406. *
  407. * @group deprecated
  408. * @return void
  409. */
  410. public function testSlugCharList()
  411. {
  412. $this->deprecated(function () {
  413. foreach (self::$maps as $language => $list) {
  414. foreach ($list as $from => $to) {
  415. $result = Inflector::slug($from);
  416. $this->assertEquals($to, $result, $from . ' (' . $language . ') should be ' . $to . ' - but is ' . $result);
  417. }
  418. }
  419. });
  420. }
  421. /**
  422. * testSlugWithMap method
  423. *
  424. * @group deprecated
  425. * @return void
  426. */
  427. public function testSlugWithMap()
  428. {
  429. $this->deprecated(function () {
  430. Inflector::rules('transliteration', ['r' => '1']);
  431. $result = Inflector::slug('replace every r');
  432. $expected = '1eplace-eve1y-1';
  433. $this->assertEquals($expected, $result);
  434. $result = Inflector::slug('replace every r', '_');
  435. $expected = '1eplace_eve1y_1';
  436. $this->assertEquals($expected, $result);
  437. });
  438. }
  439. /**
  440. * testSlugWithMapOverridingDefault method
  441. *
  442. * @group deprecated
  443. * @return void
  444. */
  445. public function testSlugWithMapOverridingDefault()
  446. {
  447. $this->deprecated(function () {
  448. Inflector::rules('transliteration', ['å' => 'aa', 'ø' => 'oe']);
  449. $result = Inflector::slug('Testing æ ø å', '-');
  450. $expected = 'Testing-ae-oe-aa';
  451. $this->assertEquals($expected, $result);
  452. });
  453. }
  454. /**
  455. * testUnderscore method
  456. *
  457. * @return void
  458. */
  459. public function testUnderscore()
  460. {
  461. $this->assertSame('test_thing', Inflector::underscore('TestThing'));
  462. $this->assertSame('test_thing', Inflector::underscore('testThing'));
  463. $this->assertSame('test_thing_extra', Inflector::underscore('TestThingExtra'));
  464. $this->assertSame('test_thing_extra', Inflector::underscore('testThingExtra'));
  465. $this->assertSame('test_this_thing', Inflector::underscore('test-this-thing'));
  466. $this->assertSame('test_thing_extrå', Inflector::underscore('testThingExtrå'));
  467. // Identical checks test the cache code path.
  468. $this->assertSame('test_thing', Inflector::underscore('TestThing'));
  469. $this->assertSame('test_thing', Inflector::underscore('testThing'));
  470. $this->assertSame('test_thing_extra', Inflector::underscore('TestThingExtra'));
  471. $this->assertSame('test_thing_extra', Inflector::underscore('testThingExtra'));
  472. $this->assertSame('test_thing_extrå', Inflector::underscore('testThingExtrå'));
  473. // Test stupid values
  474. $this->assertSame('', Inflector::underscore(''));
  475. $this->assertSame('0', Inflector::underscore(0));
  476. $this->assertSame('', Inflector::underscore(false));
  477. }
  478. /**
  479. * testDasherized method
  480. *
  481. * @return void
  482. */
  483. public function testDasherized()
  484. {
  485. $this->assertSame('test-thing', Inflector::dasherize('TestThing'));
  486. $this->assertSame('test-thing', Inflector::dasherize('testThing'));
  487. $this->assertSame('test-thing-extra', Inflector::dasherize('TestThingExtra'));
  488. $this->assertSame('test-thing-extra', Inflector::dasherize('testThingExtra'));
  489. $this->assertSame('test-this-thing', Inflector::dasherize('test_this_thing'));
  490. // Test stupid values
  491. $this->assertSame('', Inflector::dasherize(null));
  492. $this->assertSame('', Inflector::dasherize(''));
  493. $this->assertSame('0', Inflector::dasherize(0));
  494. $this->assertSame('', Inflector::dasherize(false));
  495. }
  496. /**
  497. * Demonstrate the expected output for bad inputs
  498. *
  499. * @return void
  500. */
  501. public function testCamelize()
  502. {
  503. $this->assertSame('TestThing', Inflector::camelize('test_thing'));
  504. $this->assertSame('Test-thing', Inflector::camelize('test-thing'));
  505. $this->assertSame('TestThing', Inflector::camelize('test thing'));
  506. $this->assertSame('Test_thing', Inflector::camelize('test_thing', '-'));
  507. $this->assertSame('TestThing', Inflector::camelize('test-thing', '-'));
  508. $this->assertSame('TestThing', Inflector::camelize('test thing', '-'));
  509. $this->assertSame('Test_thing', Inflector::camelize('test_thing', ' '));
  510. $this->assertSame('Test-thing', Inflector::camelize('test-thing', ' '));
  511. $this->assertSame('TestThing', Inflector::camelize('test thing', ' '));
  512. $this->assertSame('TestPlugin.TestPluginComments', Inflector::camelize('TestPlugin.TestPluginComments'));
  513. }
  514. /**
  515. * testVariableNaming method
  516. *
  517. * @return void
  518. */
  519. public function testVariableNaming()
  520. {
  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. $this->assertEquals('testField', Inflector::variable('Test_field'));
  525. }
  526. /**
  527. * testClassNaming method
  528. *
  529. * @return void
  530. */
  531. public function testClassNaming()
  532. {
  533. $this->assertEquals('ArtistsGenre', Inflector::classify('artists_genres'));
  534. $this->assertEquals('FileSystem', Inflector::classify('file_systems'));
  535. $this->assertEquals('News', Inflector::classify('news'));
  536. $this->assertEquals('Bureau', Inflector::classify('bureaus'));
  537. }
  538. /**
  539. * testTableNaming method
  540. *
  541. * @return void
  542. */
  543. public function testTableNaming()
  544. {
  545. $this->assertEquals('artists_genres', Inflector::tableize('ArtistsGenre'));
  546. $this->assertEquals('file_systems', Inflector::tableize('FileSystem'));
  547. $this->assertEquals('news', Inflector::tableize('News'));
  548. $this->assertEquals('bureaus', Inflector::tableize('Bureau'));
  549. }
  550. /**
  551. * testHumanization method
  552. *
  553. * @return void
  554. */
  555. public function testHumanization()
  556. {
  557. $this->assertEquals('Posts', Inflector::humanize('posts'));
  558. $this->assertEquals('Posts Tags', Inflector::humanize('posts_tags'));
  559. $this->assertEquals('File Systems', Inflector::humanize('file_systems'));
  560. $this->assertSame('', Inflector::humanize(null));
  561. $this->assertSame('', Inflector::humanize(false));
  562. $this->assertSame('Hello Wörld', Inflector::humanize('hello_wörld'));
  563. $this->assertSame('福岡 City', Inflector::humanize('福岡_city'));
  564. }
  565. /**
  566. * testCustomPluralRule method
  567. *
  568. * @return void
  569. */
  570. public function testCustomPluralRule()
  571. {
  572. Inflector::rules('plural', ['/^(custom)$/i' => '\1izables']);
  573. Inflector::rules('uninflected', ['uninflectable']);
  574. $this->assertEquals('customizables', Inflector::pluralize('custom'));
  575. $this->assertEquals('uninflectable', Inflector::pluralize('uninflectable'));
  576. Inflector::rules('plural', ['/^(alert)$/i' => '\1ables']);
  577. Inflector::rules('irregular', ['amaze' => 'amazable', 'phone' => 'phonezes']);
  578. Inflector::rules('uninflected', ['noflect', 'abtuse']);
  579. $this->assertEquals('noflect', Inflector::pluralize('noflect'));
  580. $this->assertEquals('abtuse', Inflector::pluralize('abtuse'));
  581. $this->assertEquals('alertables', Inflector::pluralize('alert'));
  582. $this->assertEquals('amazable', Inflector::pluralize('amaze'));
  583. $this->assertEquals('phonezes', Inflector::pluralize('phone'));
  584. $this->assertSame('criteria', Inflector::pluralize('criterion'));
  585. $this->assertSame('test_criteria', Inflector::pluralize('test_criterion'));
  586. $this->assertSame('Criteria', Inflector::pluralize('Criterion'));
  587. $this->assertSame('TestCriteria', Inflector::pluralize('TestCriterion'));
  588. $this->assertSame('Test Criteria', Inflector::pluralize('Test Criterion'));
  589. }
  590. /**
  591. * testCustomSingularRule method
  592. *
  593. * @return void
  594. */
  595. public function testCustomSingularRule()
  596. {
  597. Inflector::rules('uninflected', ['singulars']);
  598. Inflector::rules('singular', ['/(eple)r$/i' => '\1', '/(jente)r$/i' => '\1']);
  599. $this->assertEquals('eple', Inflector::singularize('epler'));
  600. $this->assertEquals('jente', Inflector::singularize('jenter'));
  601. Inflector::rules('singular', ['/^(bil)er$/i' => '\1', '/^(inflec|contribu)tors$/i' => '\1ta']);
  602. Inflector::rules('irregular', ['spinor' => 'spins']);
  603. $this->assertEquals('spinor', Inflector::singularize('spins'));
  604. $this->assertEquals('inflecta', Inflector::singularize('inflectors'));
  605. $this->assertEquals('contributa', Inflector::singularize('contributors'));
  606. $this->assertEquals('singulars', Inflector::singularize('singulars'));
  607. $this->assertSame('criterion', Inflector::singularize('criteria'));
  608. $this->assertSame('test_criterion', Inflector::singularize('test_criteria'));
  609. $this->assertSame('Criterion', Inflector::singularize('Criteria'));
  610. $this->assertSame('TestCriterion', Inflector::singularize('TestCriteria'));
  611. $this->assertSame('Test Criterion', Inflector::singularize('Test Criteria'));
  612. }
  613. /**
  614. * testCustomTransliterationRule method
  615. *
  616. * @group deprecated
  617. * @return void
  618. */
  619. public function testCustomTransliterationRule()
  620. {
  621. $this->deprecated(function () {
  622. $this->assertEquals('Testing-ae-o-a', Inflector::slug('Testing æ ø å'));
  623. Inflector::rules('transliteration', ['å' => 'aa', 'ø' => 'oe']);
  624. $this->assertEquals('Testing-ae-oe-aa', Inflector::slug('Testing æ ø å'));
  625. Inflector::rules('transliteration', ['æ' => 'ae', 'å' => 'aa'], true);
  626. $this->assertEquals('Testing-ae-ø-aa', Inflector::slug('Testing æ ø å'));
  627. });
  628. }
  629. /**
  630. * test that setting new rules clears the inflector caches.
  631. *
  632. * @return void
  633. */
  634. public function testRulesClearsCaches()
  635. {
  636. $this->assertEquals('Banana', Inflector::singularize('Bananas'));
  637. $this->assertEquals('bananas', Inflector::tableize('Banana'));
  638. $this->assertEquals('Bananas', Inflector::pluralize('Banana'));
  639. Inflector::rules('singular', ['/(.*)nas$/i' => '\1zzz']);
  640. $this->assertEquals('Banazzz', Inflector::singularize('Bananas'), 'Was inflected with old rules.');
  641. Inflector::rules('plural', ['/(.*)na$/i' => '\1zzz']);
  642. Inflector::rules('irregular', ['corpus' => 'corpora']);
  643. $this->assertEquals('Banazzz', Inflector::pluralize('Banana'), 'Was inflected with old rules.');
  644. $this->assertEquals('corpora', Inflector::pluralize('corpus'), 'Was inflected with old irregular form.');
  645. }
  646. /**
  647. * Test resetting inflection rules.
  648. *
  649. * @return void
  650. */
  651. public function testCustomRuleWithReset()
  652. {
  653. $uninflected = ['atlas', 'lapis', 'onibus', 'pires', 'virus', '.*x'];
  654. $pluralIrregular = ['as' => 'ases'];
  655. Inflector::rules('singular', ['/^(.*)(a|e|o|u)is$/i' => '\1\2l'], true);
  656. Inflector::rules('plural', ['/^(.*)(a|e|o|u)l$/i' => '\1\2is'], true);
  657. Inflector::rules('uninflected', $uninflected, true);
  658. Inflector::rules('irregular', $pluralIrregular, true);
  659. $this->assertEquals('Alcoois', Inflector::pluralize('Alcool'));
  660. $this->assertEquals('Atlas', Inflector::pluralize('Atlas'));
  661. $this->assertEquals('Alcool', Inflector::singularize('Alcoois'));
  662. $this->assertEquals('Atlas', Inflector::singularize('Atlas'));
  663. }
  664. }