InflectorTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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/5/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. use PHPUnit\Framework\Attributes\DataProvider;
  20. /**
  21. * Short description for class.
  22. */
  23. class InflectorTest extends TestCase
  24. {
  25. /**
  26. * A list of chars to test transliteration.
  27. *
  28. * @var array<string, array>
  29. */
  30. public static $maps = [
  31. 'de' => [ /* German */
  32. 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss',
  33. 'ẞ' => 'SS',
  34. ],
  35. 'latin' => [
  36. 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Å' => 'A', 'Ă' => 'A', 'Æ' => 'AE', 'Ç' =>
  37. 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
  38. 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ő' => 'O', 'Ø' => 'O',
  39. 'Ș' => 'S', 'Ț' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ű' => 'U',
  40. 'Ý' => 'Y', 'Þ' => 'TH', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a',
  41. 'å' => 'a', 'ă' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
  42. 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' =>
  43. 'o', 'ô' => 'o', 'õ' => 'o', 'ő' => 'o', 'ø' => 'o', 'ș' => 's', 'ț' => 't', 'ù' => 'u', 'ú' => 'u',
  44. 'û' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y',
  45. ],
  46. 'tr' => [ /* Turkish */
  47. 'ş' => 's', 'Ş' => 'S', 'ı' => 'i', 'İ' => 'I', 'ç' => 'c', 'Ç' => 'C', 'ğ' => 'g', 'Ğ' => 'G',
  48. ],
  49. 'uk' => [ /* Ukrainian */
  50. 'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g',
  51. ],
  52. 'cs' => [ /* Czech */
  53. 'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
  54. 'ž' => 'z', 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T',
  55. 'Ů' => 'U', 'Ž' => 'Z',
  56. ],
  57. 'pl' => [ /* Polish */
  58. 'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ś' => 's', 'ź' => 'z',
  59. 'ż' => 'z', 'Ą' => 'A', 'Ć' => 'C', 'Ł' => 'L', 'Ń' => 'N', 'Ó' => 'O', 'Ś' => 'S',
  60. 'Ź' => 'Z', 'Ż' => 'Z',
  61. ],
  62. 'ro' => [ /* Romanian */
  63. 'ă' => 'a', 'â' => 'a', 'î' => 'i', 'ș' => 's', 'ț' => 't', 'Ţ' => 'T', 'ţ' => 't',
  64. ],
  65. 'lv' => [ /* Latvian */
  66. 'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n',
  67. 'š' => 's', 'ū' => 'u', 'ž' => 'z', 'Ā' => 'A', 'Č' => 'C', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'I',
  68. 'Ķ' => 'K', 'Ļ' => 'L', 'Ņ' => 'N', 'Š' => 'S', 'Ū' => 'U', 'Ž' => 'Z',
  69. ],
  70. 'lt' => [ /* Lithuanian */
  71. 'ą' => 'a', 'č' => 'c', 'ę' => 'e', 'ė' => 'e', 'į' => 'i', 'š' => 's', 'ų' => 'u', 'ū' => 'u', 'ž' => 'z',
  72. 'Ą' => 'A', 'Č' => 'C', 'Ę' => 'E', 'Ė' => 'E', 'Į' => 'I', 'Š' => 'S', 'Ų' => 'U', 'Ū' => 'U', 'Ž' => 'Z',
  73. ],
  74. ];
  75. /**
  76. * tearDown
  77. */
  78. public function tearDown(): void
  79. {
  80. parent::tearDown();
  81. Inflector::reset();
  82. }
  83. /**
  84. * testInflectingSingulars method
  85. */
  86. #[DataProvider('singularizeProvider')]
  87. public function testInflectingSingulars(string $singular, string $plural): void
  88. {
  89. $this->assertSame($singular, Inflector::singularize($plural));
  90. $singular = Inflector::camelize($singular);
  91. $plural = Inflector::camelize($plural);
  92. $this->assertSame($singular, Inflector::singularize($plural));
  93. }
  94. /**
  95. * Data provider for testing singularize()
  96. *
  97. * @return array
  98. */
  99. public static function singularizeProvider(): array
  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. ['lens', 'lenses'],
  190. ['species', 'species'],
  191. ['animal_species', 'animal_species'],
  192. ];
  193. }
  194. /**
  195. * Test that overlapping irregulars don't collide.
  196. */
  197. public function testSingularizeMultiWordIrregular(): void
  198. {
  199. Inflector::rules('irregular', [
  200. 'pregunta_frecuente' => 'preguntas_frecuentes',
  201. 'categoria_pregunta_frecuente' => 'categorias_preguntas_frecuentes',
  202. ]);
  203. $this->assertSame('pregunta_frecuente', Inflector::singularize('preguntas_frecuentes'));
  204. $this->assertSame(
  205. 'categoria_pregunta_frecuente',
  206. Inflector::singularize('categorias_preguntas_frecuentes')
  207. );
  208. $this->assertSame(
  209. 'faq_categoria_pregunta_frecuente',
  210. Inflector::singularize('faq_categorias_preguntas_frecuentes')
  211. );
  212. }
  213. /**
  214. * testInflectingPlurals method
  215. */
  216. #[DataProvider('pluralizeProvider')]
  217. public function testInflectingPlurals(string $plural, string $singular): void
  218. {
  219. $this->assertSame($plural, Inflector::pluralize($singular));
  220. $plural = Inflector::camelize($plural);
  221. $singular = Inflector::camelize($singular);
  222. $this->assertSame($plural, Inflector::pluralize($singular));
  223. }
  224. /**
  225. * Data provider for testing pluralize()
  226. *
  227. * @return array
  228. */
  229. public static function pluralizeProvider(): array
  230. {
  231. return [
  232. ['axmen', 'axman'],
  233. ['men', 'man'],
  234. ['women', 'woman'],
  235. ['humans', 'human'],
  236. ['axmen', 'axman'],
  237. ['men', 'man'],
  238. ['women', 'woman'],
  239. ['humans', 'human'],
  240. ['categorias', 'categoria'],
  241. ['houses', 'house'],
  242. ['powerhouses', 'powerhouse'],
  243. ['Buses', 'Bus'],
  244. ['buses', 'bus'],
  245. ['menus', 'menu'],
  246. ['news', 'news'],
  247. ['food_menus', 'food_menu'],
  248. ['Menus', 'Menu'],
  249. ['FoodMenus', 'FoodMenu'],
  250. ['quizzes', 'quiz'],
  251. ['matrix_rows', 'matrix_row'],
  252. ['matrices', 'matrix'],
  253. ['vertices', 'vertex'],
  254. ['indexes', 'index'],
  255. ['Aliases', 'Alias'],
  256. ['Aliases', 'Aliases'],
  257. ['Media', 'Media'],
  258. ['NodeMedia', 'NodeMedia'],
  259. ['alumni', 'alumnus'],
  260. ['bacilli', 'bacillus'],
  261. ['cacti', 'cactus'],
  262. ['foci', 'focus'],
  263. ['fungi', 'fungus'],
  264. ['nuclei', 'nucleus'],
  265. ['octopuses', 'octopus'],
  266. ['radii', 'radius'],
  267. ['stimuli', 'stimulus'],
  268. ['syllabi', 'syllabus'],
  269. ['termini', 'terminus'],
  270. ['viruses', 'virus'],
  271. ['people', 'person'],
  272. ['people', 'people'],
  273. ['gloves', 'glove'],
  274. ['crises', 'crisis'],
  275. ['taxes', 'tax'],
  276. ['waves', 'wave'],
  277. ['bureaus', 'bureau'],
  278. ['cafes', 'cafe'],
  279. ['roofs', 'roof'],
  280. ['foes', 'foe'],
  281. ['cookies', 'cookie'],
  282. ['wolves', 'wolf'],
  283. ['thieves', 'thief'],
  284. ['potatoes', 'potato'],
  285. ['heroes', 'hero'],
  286. ['buffaloes', 'buffalo'],
  287. ['teeth', 'tooth'],
  288. ['geese', 'goose'],
  289. ['feet', 'foot'],
  290. ['objectives', 'objective'],
  291. ['briefs', 'brief'],
  292. ['quotas', 'quota'],
  293. ['curves', 'curve'],
  294. ['body_curves', 'body_curve'],
  295. ['metadata', 'metadata'],
  296. ['files_metadata', 'files_metadata'],
  297. ['stadia', 'stadia'],
  298. ['Addresses', 'Address'],
  299. ['sieves', 'sieve'],
  300. ['blue_octopuses', 'blue_octopus'],
  301. ['chefs', 'chef'],
  302. ['', ''],
  303. ['pokemon', 'pokemon'],
  304. ];
  305. }
  306. /**
  307. * Test that overlapping irregulars don't collide.
  308. */
  309. public function testPluralizeMultiWordIrregular(): void
  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. public function testInflectingMultiWordIrregulars(): void
  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->assertSame('wisdom tooth', Inflector::singularize('wisdom teeth'));
  340. $this->assertSame('wisdom-tooth', Inflector::singularize('wisdom-teeth'));
  341. $this->assertSame('wisdom_tooth', Inflector::singularize('wisdom_teeth'));
  342. $this->assertSame('sweet potatoes', Inflector::pluralize('sweet potato'));
  343. $this->assertSame('sweet-potatoes', Inflector::pluralize('sweet-potato'));
  344. $this->assertSame('sweet_potatoes', Inflector::pluralize('sweet_potato'));
  345. }
  346. /**
  347. * testUnderscore method
  348. */
  349. public function testUnderscore(): void
  350. {
  351. $this->assertSame('test_thing', Inflector::underscore('TestThing'));
  352. $this->assertSame('test_thing', Inflector::underscore('testThing'));
  353. $this->assertSame('test_thing_extra', Inflector::underscore('TestThingExtra'));
  354. $this->assertSame('test_thing_extra', Inflector::underscore('testThingExtra'));
  355. $this->assertSame('test_this_thing', Inflector::underscore('test-this-thing'));
  356. $this->assertSame('test_thing_extrå', Inflector::underscore('testThingExtrå'));
  357. // Identical checks test the cache code path.
  358. $this->assertSame('test_thing', Inflector::underscore('TestThing'));
  359. $this->assertSame('test_thing', Inflector::underscore('testThing'));
  360. $this->assertSame('test_thing_extra', Inflector::underscore('TestThingExtra'));
  361. $this->assertSame('test_thing_extra', Inflector::underscore('testThingExtra'));
  362. $this->assertSame('test_thing_extrå', Inflector::underscore('testThingExtrå'));
  363. // Test other values
  364. $this->assertSame('0', Inflector::underscore('0'));
  365. }
  366. /**
  367. * testDasherized method
  368. */
  369. public function testDasherized(): void
  370. {
  371. $this->assertSame('test-thing', Inflector::dasherize('TestThing'));
  372. $this->assertSame('test-thing', Inflector::dasherize('testThing'));
  373. $this->assertSame('test-thing-extra', Inflector::dasherize('TestThingExtra'));
  374. $this->assertSame('test-thing-extra', Inflector::dasherize('testThingExtra'));
  375. $this->assertSame('test-this-thing', Inflector::dasherize('test_this_thing'));
  376. // Test stupid values
  377. $this->assertSame('', Inflector::dasherize(''));
  378. $this->assertSame('0', Inflector::dasherize('0'));
  379. }
  380. /**
  381. * Demonstrate the expected output for bad inputs
  382. */
  383. public function testCamelize(): void
  384. {
  385. $this->assertSame('TestThing', Inflector::camelize('test_thing'));
  386. $this->assertSame('Test-thing', Inflector::camelize('test-thing'));
  387. $this->assertSame('TestThing', Inflector::camelize('test thing'));
  388. $this->assertSame('Test_thing', Inflector::camelize('test_thing', '-'));
  389. $this->assertSame('TestThing', Inflector::camelize('test-thing', '-'));
  390. $this->assertSame('TestThing', Inflector::camelize('test thing', '-'));
  391. $this->assertSame('Test_thing', Inflector::camelize('test_thing', ' '));
  392. $this->assertSame('Test-thing', Inflector::camelize('test-thing', ' '));
  393. $this->assertSame('TestThing', Inflector::camelize('test thing', ' '));
  394. $this->assertSame('TestPlugin.TestPluginComments', Inflector::camelize('TestPlugin.TestPluginComments'));
  395. }
  396. /**
  397. * testVariableNaming method
  398. */
  399. public function testVariableNaming(): void
  400. {
  401. $this->assertSame('testField', Inflector::variable('test_field'));
  402. $this->assertSame('testFieLd', Inflector::variable('test_fieLd'));
  403. $this->assertSame('testField', Inflector::variable('test field'));
  404. $this->assertSame('testField', Inflector::variable('Test_field'));
  405. }
  406. /**
  407. * testClassNaming method
  408. */
  409. public function testClassNaming(): void
  410. {
  411. $this->assertSame('ArtistsGenre', Inflector::classify('artists_genres'));
  412. $this->assertSame('FileSystem', Inflector::classify('file_systems'));
  413. $this->assertSame('News', Inflector::classify('news'));
  414. $this->assertSame('Bureau', Inflector::classify('bureaus'));
  415. }
  416. /**
  417. * testTableNaming method
  418. */
  419. public function testTableNaming(): void
  420. {
  421. $this->assertSame('artists_genres', Inflector::tableize('ArtistsGenre'));
  422. $this->assertSame('file_systems', Inflector::tableize('FileSystem'));
  423. $this->assertSame('news', Inflector::tableize('News'));
  424. $this->assertSame('bureaus', Inflector::tableize('Bureau'));
  425. }
  426. /**
  427. * testHumanization method
  428. */
  429. public function testHumanization(): void
  430. {
  431. $this->assertSame('Posts', Inflector::humanize('posts'));
  432. $this->assertSame('Posts Tags', Inflector::humanize('posts_tags'));
  433. $this->assertSame('File Systems', Inflector::humanize('file_systems'));
  434. $this->assertSame('Hello Wörld', Inflector::humanize('hello_wörld'));
  435. $this->assertSame('福岡 City', Inflector::humanize('福岡_city'));
  436. }
  437. /**
  438. * testCustomPluralRule method
  439. */
  440. public function testCustomPluralRule(): void
  441. {
  442. Inflector::rules('plural', ['/^(custom)$/i' => '\1izables']);
  443. Inflector::rules('uninflected', ['uninflectable']);
  444. $this->assertSame('customizables', Inflector::pluralize('custom'));
  445. $this->assertSame('uninflectable', Inflector::pluralize('uninflectable'));
  446. Inflector::rules('plural', ['/^(alert)$/i' => '\1ables']);
  447. Inflector::rules('irregular', ['amaze' => 'amazable', 'phone' => 'phonezes']);
  448. Inflector::rules('uninflected', ['noflect', 'abtuse']);
  449. $this->assertSame('noflect', Inflector::pluralize('noflect'));
  450. $this->assertSame('abtuse', Inflector::pluralize('abtuse'));
  451. $this->assertSame('alertables', Inflector::pluralize('alert'));
  452. $this->assertSame('amazable', Inflector::pluralize('amaze'));
  453. $this->assertSame('phonezes', Inflector::pluralize('phone'));
  454. $this->assertSame('criteria', Inflector::pluralize('criterion'));
  455. $this->assertSame('test_criteria', Inflector::pluralize('test_criterion'));
  456. $this->assertSame('Criteria', Inflector::pluralize('Criterion'));
  457. $this->assertSame('TestCriteria', Inflector::pluralize('TestCriterion'));
  458. $this->assertSame('Test Criteria', Inflector::pluralize('Test Criterion'));
  459. }
  460. /**
  461. * testCustomSingularRule method
  462. */
  463. public function testCustomSingularRule(): void
  464. {
  465. Inflector::rules('uninflected', ['singulars']);
  466. Inflector::rules('singular', ['/(eple)r$/i' => '\1', '/(jente)r$/i' => '\1']);
  467. $this->assertSame('eple', Inflector::singularize('epler'));
  468. $this->assertSame('jente', Inflector::singularize('jenter'));
  469. Inflector::rules('singular', ['/^(bil)er$/i' => '\1', '/^(inflec|contribu)tors$/i' => '\1ta']);
  470. Inflector::rules('irregular', ['spinor' => 'spins']);
  471. $this->assertSame('spinor', Inflector::singularize('spins'));
  472. $this->assertSame('inflecta', Inflector::singularize('inflectors'));
  473. $this->assertSame('contributa', Inflector::singularize('contributors'));
  474. $this->assertSame('singulars', Inflector::singularize('singulars'));
  475. $this->assertSame('criterion', Inflector::singularize('criteria'));
  476. $this->assertSame('test_criterion', Inflector::singularize('test_criteria'));
  477. $this->assertSame('Criterion', Inflector::singularize('Criteria'));
  478. $this->assertSame('TestCriterion', Inflector::singularize('TestCriteria'));
  479. $this->assertSame('Test Criterion', Inflector::singularize('Test Criteria'));
  480. }
  481. /**
  482. * test that setting new rules clears the inflector caches.
  483. */
  484. public function testRulesClearsCaches(): void
  485. {
  486. $this->assertSame('Banana', Inflector::singularize('Bananas'));
  487. $this->assertSame('bananas', Inflector::tableize('Banana'));
  488. $this->assertSame('Bananas', Inflector::pluralize('Banana'));
  489. Inflector::rules('singular', ['/(.*)nas$/i' => '\1zzz']);
  490. $this->assertSame('Banazzz', Inflector::singularize('Bananas'), 'Was inflected with old rules.');
  491. Inflector::rules('plural', ['/(.*)na$/i' => '\1zzz']);
  492. Inflector::rules('irregular', ['corpus' => 'corpora']);
  493. $this->assertSame('Banazzz', Inflector::pluralize('Banana'), 'Was inflected with old rules.');
  494. $this->assertSame('corpora', Inflector::pluralize('corpus'), 'Was inflected with old irregular form.');
  495. }
  496. /**
  497. * Test resetting inflection rules.
  498. */
  499. public function testCustomRuleWithReset(): void
  500. {
  501. $uninflected = ['atlas', 'lapis', 'onibus', 'pires', 'virus', '.*x'];
  502. $pluralIrregular = ['as' => 'ases'];
  503. Inflector::rules('singular', ['/^(.*)(a|e|o|u)is$/i' => '\1\2l'], true);
  504. Inflector::rules('plural', ['/^(.*)(a|e|o|u)l$/i' => '\1\2is'], true);
  505. Inflector::rules('uninflected', $uninflected, true);
  506. Inflector::rules('irregular', $pluralIrregular, true);
  507. $this->assertSame('Alcoois', Inflector::pluralize('Alcool'));
  508. $this->assertSame('Atlas', Inflector::pluralize('Atlas'));
  509. $this->assertSame('Alcool', Inflector::singularize('Alcoois'));
  510. $this->assertSame('Atlas', Inflector::singularize('Atlas'));
  511. }
  512. }