InflectorTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. * @return void
  349. */
  350. public function testSlug()
  351. {
  352. $result = Inflector::slug('Foo Bar: Not just for breakfast any-more');
  353. $expected = 'Foo-Bar-Not-just-for-breakfast-any-more';
  354. $this->assertEquals($expected, $result);
  355. $result = Inflector::slug('this/is/a/path');
  356. $expected = 'this-is-a-path';
  357. $this->assertEquals($expected, $result);
  358. $result = Inflector::slug('Foo Bar: Not just for breakfast any-more', '_');
  359. $expected = 'Foo_Bar_Not_just_for_breakfast_any_more';
  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('Äpfel Über Öl grün ärgert groß öko', '-');
  365. $expected = 'Aepfel-Ueber-Oel-gruen-aergert-gross-oeko';
  366. $this->assertEquals($expected, $result);
  367. $result = Inflector::slug('The truth - and- more- news', '-');
  368. $expected = 'The-truth-and-more-news';
  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('La langue française est un attribut de souveraineté en France', '-');
  374. $expected = 'La-langue-francaise-est-un-attribut-de-souverainete-en-France';
  375. $this->assertEquals($expected, $result);
  376. $result = Inflector::slug('!@$#exciting stuff! - what !@-# was that?', '-');
  377. $expected = 'exciting-stuff-what-was-that';
  378. $this->assertEquals($expected, $result);
  379. $result = Inflector::slug('20% of profits went to me!', '-');
  380. $expected = '20-of-profits-went-to-me';
  381. $this->assertEquals($expected, $result);
  382. $result = Inflector::slug('#this melts your face1#2#3', '-');
  383. $expected = 'this-melts-your-face1-2-3';
  384. $this->assertEquals($expected, $result);
  385. $result = Inflector::slug('controller/action/りんご/1');
  386. $expected = 'controller-action-りんご-1';
  387. $this->assertEquals($expected, $result);
  388. $result = Inflector::slug('の話が出たので大丈夫かなあと');
  389. $expected = 'の話が出たので大丈夫かなあと';
  390. $this->assertEquals($expected, $result);
  391. $result = Inflector::slug('posts/view/한국어/page:1/sort:asc');
  392. $expected = 'posts-view-한국어-page-1-sort-asc';
  393. $this->assertEquals($expected, $result);
  394. $result = Inflector::slug("non\xc2\xa0breaking\xc2\xa0space");
  395. $this->assertEquals('non-breaking-space', $result);
  396. $result = Inflector::slug('Foo Bar: Not just for breakfast any-more', '');
  397. $expected = 'FooBarNotjustforbreakfastanymore';
  398. $this->assertEquals($expected, $result);
  399. }
  400. /**
  401. * Test slug() with a complete list of special chars.
  402. *
  403. * @return void
  404. */
  405. public function testSlugCharList()
  406. {
  407. foreach (self::$maps as $language => $list) {
  408. foreach ($list as $from => $to) {
  409. $result = Inflector::slug($from);
  410. $this->assertEquals($to, $result, $from . ' (' . $language . ') should be ' . $to . ' - but is ' . $result);
  411. }
  412. }
  413. }
  414. /**
  415. * testSlugWithMap method
  416. *
  417. * @return void
  418. */
  419. public function testSlugWithMap()
  420. {
  421. Inflector::rules('transliteration', ['r' => '1']);
  422. $result = Inflector::slug('replace every r');
  423. $expected = '1eplace-eve1y-1';
  424. $this->assertEquals($expected, $result);
  425. $result = Inflector::slug('replace every r', '_');
  426. $expected = '1eplace_eve1y_1';
  427. $this->assertEquals($expected, $result);
  428. }
  429. /**
  430. * testSlugWithMapOverridingDefault method
  431. *
  432. * @return void
  433. */
  434. public function testSlugWithMapOverridingDefault()
  435. {
  436. Inflector::rules('transliteration', ['å' => 'aa', 'ø' => 'oe']);
  437. $result = Inflector::slug('Testing æ ø å', '-');
  438. $expected = 'Testing-ae-oe-aa';
  439. $this->assertEquals($expected, $result);
  440. }
  441. /**
  442. * testUnderscore method
  443. *
  444. * @return void
  445. */
  446. public function testUnderscore()
  447. {
  448. $this->assertSame('test_thing', Inflector::underscore('TestThing'));
  449. $this->assertSame('test_thing', Inflector::underscore('testThing'));
  450. $this->assertSame('test_thing_extra', Inflector::underscore('TestThingExtra'));
  451. $this->assertSame('test_thing_extra', Inflector::underscore('testThingExtra'));
  452. $this->assertSame('test_this_thing', Inflector::underscore('test-this-thing'));
  453. $this->assertSame('test_thing_extrå', Inflector::underscore('testThingExtrå'));
  454. // Identical checks test the cache code path.
  455. $this->assertSame('test_thing', Inflector::underscore('TestThing'));
  456. $this->assertSame('test_thing', Inflector::underscore('testThing'));
  457. $this->assertSame('test_thing_extra', Inflector::underscore('TestThingExtra'));
  458. $this->assertSame('test_thing_extra', Inflector::underscore('testThingExtra'));
  459. $this->assertSame('test_thing_extrå', Inflector::underscore('testThingExtrå'));
  460. // Test stupid values
  461. $this->assertSame('', Inflector::underscore(''));
  462. $this->assertSame('0', Inflector::underscore(0));
  463. $this->assertSame('', Inflector::underscore(false));
  464. }
  465. /**
  466. * testDasherized method
  467. *
  468. * @return void
  469. */
  470. public function testDasherized()
  471. {
  472. $this->assertSame('test-thing', Inflector::dasherize('TestThing'));
  473. $this->assertSame('test-thing', Inflector::dasherize('testThing'));
  474. $this->assertSame('test-thing-extra', Inflector::dasherize('TestThingExtra'));
  475. $this->assertSame('test-thing-extra', Inflector::dasherize('testThingExtra'));
  476. $this->assertSame('test-this-thing', Inflector::dasherize('test_this_thing'));
  477. // Test stupid values
  478. $this->assertSame('', Inflector::dasherize(null));
  479. $this->assertSame('', Inflector::dasherize(''));
  480. $this->assertSame('0', Inflector::dasherize(0));
  481. $this->assertSame('', Inflector::dasherize(false));
  482. }
  483. /**
  484. * Demonstrate the expected output for bad inputs
  485. *
  486. * @return void
  487. */
  488. public function testCamelize()
  489. {
  490. $this->assertSame('TestThing', Inflector::camelize('test_thing'));
  491. $this->assertSame('Test-thing', Inflector::camelize('test-thing'));
  492. $this->assertSame('TestThing', Inflector::camelize('test thing'));
  493. $this->assertSame('Test_thing', Inflector::camelize('test_thing', '-'));
  494. $this->assertSame('TestThing', Inflector::camelize('test-thing', '-'));
  495. $this->assertSame('TestThing', Inflector::camelize('test thing', '-'));
  496. $this->assertSame('Test_thing', Inflector::camelize('test_thing', ' '));
  497. $this->assertSame('Test-thing', Inflector::camelize('test-thing', ' '));
  498. $this->assertSame('TestThing', Inflector::camelize('test thing', ' '));
  499. $this->assertSame('TestPlugin.TestPluginComments', Inflector::camelize('TestPlugin.TestPluginComments'));
  500. }
  501. /**
  502. * testVariableNaming method
  503. *
  504. * @return void
  505. */
  506. public function testVariableNaming()
  507. {
  508. $this->assertEquals('testField', Inflector::variable('test_field'));
  509. $this->assertEquals('testFieLd', Inflector::variable('test_fieLd'));
  510. $this->assertEquals('testField', Inflector::variable('test field'));
  511. $this->assertEquals('testField', Inflector::variable('Test_field'));
  512. }
  513. /**
  514. * testClassNaming method
  515. *
  516. * @return void
  517. */
  518. public function testClassNaming()
  519. {
  520. $this->assertEquals('ArtistsGenre', Inflector::classify('artists_genres'));
  521. $this->assertEquals('FileSystem', Inflector::classify('file_systems'));
  522. $this->assertEquals('News', Inflector::classify('news'));
  523. $this->assertEquals('Bureau', Inflector::classify('bureaus'));
  524. }
  525. /**
  526. * testTableNaming method
  527. *
  528. * @return void
  529. */
  530. public function testTableNaming()
  531. {
  532. $this->assertEquals('artists_genres', Inflector::tableize('ArtistsGenre'));
  533. $this->assertEquals('file_systems', Inflector::tableize('FileSystem'));
  534. $this->assertEquals('news', Inflector::tableize('News'));
  535. $this->assertEquals('bureaus', Inflector::tableize('Bureau'));
  536. }
  537. /**
  538. * testHumanization method
  539. *
  540. * @return void
  541. */
  542. public function testHumanization()
  543. {
  544. $this->assertEquals('Posts', Inflector::humanize('posts'));
  545. $this->assertEquals('Posts Tags', Inflector::humanize('posts_tags'));
  546. $this->assertEquals('File Systems', Inflector::humanize('file_systems'));
  547. $this->assertSame('', Inflector::humanize(null));
  548. $this->assertSame('', Inflector::humanize(false));
  549. $this->assertSame('Hello Wörld', Inflector::humanize('hello_wörld'));
  550. $this->assertSame('福岡 City', Inflector::humanize('福岡_city'));
  551. }
  552. /**
  553. * testCustomPluralRule method
  554. *
  555. * @return void
  556. */
  557. public function testCustomPluralRule()
  558. {
  559. Inflector::rules('plural', ['/^(custom)$/i' => '\1izables']);
  560. Inflector::rules('uninflected', ['uninflectable']);
  561. $this->assertEquals('customizables', Inflector::pluralize('custom'));
  562. $this->assertEquals('uninflectable', Inflector::pluralize('uninflectable'));
  563. Inflector::rules('plural', ['/^(alert)$/i' => '\1ables']);
  564. Inflector::rules('irregular', ['amaze' => 'amazable', 'phone' => 'phonezes']);
  565. Inflector::rules('uninflected', ['noflect', 'abtuse']);
  566. $this->assertEquals('noflect', Inflector::pluralize('noflect'));
  567. $this->assertEquals('abtuse', Inflector::pluralize('abtuse'));
  568. $this->assertEquals('alertables', Inflector::pluralize('alert'));
  569. $this->assertEquals('amazable', Inflector::pluralize('amaze'));
  570. $this->assertEquals('phonezes', Inflector::pluralize('phone'));
  571. }
  572. /**
  573. * testCustomSingularRule method
  574. *
  575. * @return void
  576. */
  577. public function testCustomSingularRule()
  578. {
  579. Inflector::rules('uninflected', ['singulars']);
  580. Inflector::rules('singular', ['/(eple)r$/i' => '\1', '/(jente)r$/i' => '\1']);
  581. $this->assertEquals('eple', Inflector::singularize('epler'));
  582. $this->assertEquals('jente', Inflector::singularize('jenter'));
  583. Inflector::rules('singular', ['/^(bil)er$/i' => '\1', '/^(inflec|contribu)tors$/i' => '\1ta']);
  584. Inflector::rules('irregular', ['spinor' => 'spins']);
  585. $this->assertEquals('spinor', Inflector::singularize('spins'));
  586. $this->assertEquals('inflecta', Inflector::singularize('inflectors'));
  587. $this->assertEquals('contributa', Inflector::singularize('contributors'));
  588. $this->assertEquals('singulars', Inflector::singularize('singulars'));
  589. }
  590. /**
  591. * testCustomTransliterationRule method
  592. *
  593. * @return void
  594. */
  595. public function testCustomTransliterationRule()
  596. {
  597. $this->assertEquals('Testing-ae-o-a', Inflector::slug('Testing æ ø å'));
  598. Inflector::rules('transliteration', ['å' => 'aa', 'ø' => 'oe']);
  599. $this->assertEquals('Testing-ae-oe-aa', Inflector::slug('Testing æ ø å'));
  600. Inflector::rules('transliteration', ['æ' => 'ae', 'å' => 'aa'], true);
  601. $this->assertEquals('Testing-ae-ø-aa', Inflector::slug('Testing æ ø å'));
  602. }
  603. /**
  604. * test that setting new rules clears the inflector caches.
  605. *
  606. * @return void
  607. */
  608. public function testRulesClearsCaches()
  609. {
  610. $this->assertEquals('Banana', Inflector::singularize('Bananas'));
  611. $this->assertEquals('bananas', Inflector::tableize('Banana'));
  612. $this->assertEquals('Bananas', Inflector::pluralize('Banana'));
  613. Inflector::rules('singular', ['/(.*)nas$/i' => '\1zzz']);
  614. $this->assertEquals('Banazzz', Inflector::singularize('Bananas'), 'Was inflected with old rules.');
  615. Inflector::rules('plural', ['/(.*)na$/i' => '\1zzz']);
  616. Inflector::rules('irregular', ['corpus' => 'corpora']);
  617. $this->assertEquals('Banazzz', Inflector::pluralize('Banana'), 'Was inflected with old rules.');
  618. $this->assertEquals('corpora', Inflector::pluralize('corpus'), 'Was inflected with old irregular form.');
  619. }
  620. /**
  621. * Test resetting inflection rules.
  622. *
  623. * @return void
  624. */
  625. public function testCustomRuleWithReset()
  626. {
  627. $uninflected = ['atlas', 'lapis', 'onibus', 'pires', 'virus', '.*x'];
  628. $pluralIrregular = ['as' => 'ases'];
  629. Inflector::rules('singular', ['/^(.*)(a|e|o|u)is$/i' => '\1\2l'], true);
  630. Inflector::rules('plural', ['/^(.*)(a|e|o|u)l$/i' => '\1\2is'], true);
  631. Inflector::rules('uninflected', $uninflected, true);
  632. Inflector::rules('irregular', $pluralIrregular, true);
  633. $this->assertEquals('Alcoois', Inflector::pluralize('Alcool'));
  634. $this->assertEquals('Atlas', Inflector::pluralize('Atlas'));
  635. $this->assertEquals('Alcool', Inflector::singularize('Alcoois'));
  636. $this->assertEquals('Atlas', Inflector::singularize('Atlas'));
  637. }
  638. }