PageRenderTime 40ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/unit/InflectorTest.php

https://gitlab.com/xibalba/ocelote
PHP | 163 lines | 129 code | 26 blank | 8 comment | 1 complexity | 198e93d2e815330a48ea3fde26960f6b MD5 | raw file
  1. <?php
  2. namespace michiq;
  3. use PHPUnit\Framework\TestCase;
  4. use xibalba\ocelote\Inflector;
  5. class InflectorTest extends TestCase {
  6. public function testCamelize(){
  7. $this->assertEquals("MeMySelfAndI", Inflector::camelize('me my_self-andI'));
  8. $this->assertEquals("QweQweEwq", Inflector::camelize('qwe qwe^ewq'));
  9. }
  10. public function testClassify(){
  11. $this->assertEquals("HerdTable", Inflector::classify('herd_tables'));
  12. }
  13. public function testFilenamize(){
  14. $this->assertEquals('Llamas.php', Inflector::filenamize('llamas'));
  15. $this->assertEquals('Llamas.hh', Inflector::filenamize('llamas', 'hh'));
  16. $this->assertEquals('llamas.hh', Inflector::filenamize('llamas', 'hh', false));
  17. }
  18. public function testHyphenate(){
  19. $this->assertEquals('herd-llama', Inflector::hyphenate('herd llama'));
  20. }
  21. public function testHumanize(){
  22. $this->assertEquals("Me my self and i", Inflector::humanize('me_my_self_and_i'));
  23. $this->assertEquals("Me My Self And I", Inflector::humanize('me_my_self_and_i', true));
  24. }
  25. public function testOrdinalize(){
  26. $this->assertEquals('21st', Inflector::ordinalize('21'));
  27. $this->assertEquals('22nd', Inflector::ordinalize('22'));
  28. $this->assertEquals('23rd', Inflector::ordinalize('23'));
  29. $this->assertEquals('24th', Inflector::ordinalize('24'));
  30. $this->assertEquals('25th', Inflector::ordinalize('25'));
  31. $this->assertEquals('111th', Inflector::ordinalize('111'));
  32. $this->assertEquals('113th', Inflector::ordinalize('113'));
  33. }
  34. public function testPluralize(){
  35. $testData = [
  36. 'move' => 'moves',
  37. 'foot' => 'feet',
  38. 'child' => 'children',
  39. 'human' => 'humans',
  40. 'man' => 'men',
  41. 'staff' => 'staff',
  42. 'tooth' => 'teeth',
  43. 'person' => 'people',
  44. 'mouse' => 'mice',
  45. 'touch' => 'touches',
  46. 'hash' => 'hashes',
  47. 'shelf' => 'shelves',
  48. 'potato' => 'potatoes',
  49. 'bus' => 'buses',
  50. 'test' => 'tests',
  51. 'car' => 'cars',
  52. ];
  53. foreach ($testData as $testIn => $testOut) {
  54. $this->assertEquals($testOut, Inflector::pluralize($testIn));
  55. $this->assertEquals(ucfirst($testOut), ucfirst(Inflector::pluralize($testIn)));
  56. }
  57. }
  58. public function testSingularize(){
  59. $testData = [
  60. 'moves' => 'move',
  61. 'feet' => 'foot',
  62. 'children' => 'child',
  63. 'humans' => 'human',
  64. 'men' => 'man',
  65. 'staff' => 'staff',
  66. 'teeth' => 'tooth',
  67. 'people' => 'person',
  68. 'mice' => 'mouse',
  69. 'touches' => 'touch',
  70. 'hashes' => 'hash',
  71. 'shelves' => 'shelf',
  72. 'potatoes' => 'potato',
  73. 'buses' => 'bus',
  74. 'tests' => 'test',
  75. 'cars' => 'car',
  76. ];
  77. foreach ($testData as $testIn => $testOut) {
  78. $this->assertEquals($testOut, Inflector::singularize($testIn));
  79. $this->assertEquals(ucfirst($testOut), ucfirst(Inflector::singularize($testIn)));
  80. }
  81. }
  82. public function testSlugCommons(){
  83. $data = [
  84. '' => '',
  85. 'hello world 123' => 'hello-world-123',
  86. 'remove.!?[]{}…symbols' => 'removesymbols',
  87. 'minus-sign' => 'minus-sign',
  88. 'mdash—sign' => 'mdash-sign',
  89. 'ndash–sign' => 'ndash-sign',
  90. 'áàâéèêíìîóòôúùûã' => 'aaaeeeiiiooouuua',
  91. 'älä lyö ääliö ööliä läikkyy' => 'ala-lyo-aalio-oolia-laikkyy',
  92. ];
  93. foreach ($data as $source => $expected) {
  94. $this->assertEquals($expected, Inflector::slug($source));
  95. }
  96. }
  97. public function testSlugIntl(){
  98. if (!extension_loaded('intl')) {
  99. $this->markTestSkipped('intl extension is required.');
  100. }
  101. // Some test strings are from https://github.com/bergie/midgardmvc_helper_urlize. Thank you, Henri Bergius!
  102. $data = [
  103. // Korean
  104. '해동검도' => 'haedong-geomdo',
  105. // Hiragana
  106. 'ひらがな' => 'hiragana',
  107. // Georgian
  108. 'საქართველო' => 'sakartvelo',
  109. // Arabic
  110. 'العربي' => 'alrby',
  111. 'عرب' => 'rb',
  112. // Hebrew
  113. 'עִבְרִית' => 'iberiyt',
  114. // Turkish
  115. 'Sanırım hepimiz aynı şeyi düşünüyoruz.' => 'sanrm-hepimiz-ayn-seyi-dusunuyoruz',
  116. // Russian
  117. 'недвижимость' => 'nedvizimost',
  118. 'Контакты' => 'kontakty',
  119. ];
  120. foreach ($data as $source => $expected) {
  121. $this->assertEquals($expected, Inflector::slug($source));
  122. }
  123. }
  124. public function testTableize(){
  125. $this->assertEquals("herd_tables", Inflector::tableize('HerdTable'));
  126. }
  127. public function testTitleize(){
  128. $this->assertEquals("Me my self and i", Inflector::titleize('MeMySelfAndI'));
  129. $this->assertEquals("Me My Self And I", Inflector::titleize('MeMySelfAndI', true));
  130. }
  131. public function testUnderscore(){
  132. $this->assertEquals("me_my_self_and_i", Inflector::underscore('MeMySelfAndI'));
  133. }
  134. public function testViriablize(){
  135. $this->assertEquals("_23_llamas", Inflector::variablize('23_llamas'));
  136. }
  137. }