InflectorTest.php 21 KB

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