SupportPluralizerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Illuminate\Support\Str;
  4. use PHPUnit\Framework\TestCase;
  5. class SupportPluralizerTest extends TestCase
  6. {
  7. public function testBasicSingular()
  8. {
  9. $this->assertSame('child', Str::singular('children'));
  10. }
  11. public function testBasicPlural()
  12. {
  13. $this->assertSame('children', Str::plural('child'));
  14. $this->assertSame('cod', Str::plural('cod'));
  15. $this->assertSame('The words', Str::plural('The word'));
  16. $this->assertSame('Bouquetés', Str::plural('Bouqueté'));
  17. }
  18. public function testCaseSensitiveSingularUsage()
  19. {
  20. $this->assertSame('Child', Str::singular('Children'));
  21. $this->assertSame('CHILD', Str::singular('CHILDREN'));
  22. $this->assertSame('Test', Str::singular('Tests'));
  23. }
  24. public function testCaseSensitiveSingularPlural()
  25. {
  26. $this->assertSame('Children', Str::plural('Child'));
  27. $this->assertSame('CHILDREN', Str::plural('CHILD'));
  28. $this->assertSame('Tests', Str::plural('Test'));
  29. $this->assertSame('children', Str::plural('cHiLd'));
  30. }
  31. public function testIfEndOfWordPlural()
  32. {
  33. $this->assertSame('VortexFields', Str::plural('VortexField'));
  34. $this->assertSame('MatrixFields', Str::plural('MatrixField'));
  35. $this->assertSame('IndexFields', Str::plural('IndexField'));
  36. $this->assertSame('VertexFields', Str::plural('VertexField'));
  37. // This is expected behavior, use "Str::pluralStudly" instead.
  38. $this->assertSame('RealHumen', Str::plural('RealHuman'));
  39. }
  40. public function testPluralWithNegativeCount()
  41. {
  42. $this->assertSame('test', Str::plural('test', 1));
  43. $this->assertSame('tests', Str::plural('test', 2));
  44. $this->assertSame('test', Str::plural('test', -1));
  45. $this->assertSame('tests', Str::plural('test', -2));
  46. }
  47. public function testPluralStudly()
  48. {
  49. $this->assertPluralStudly('RealHumans', 'RealHuman');
  50. $this->assertPluralStudly('Models', 'Model');
  51. $this->assertPluralStudly('VortexFields', 'VortexField');
  52. $this->assertPluralStudly('MultipleWordsInOneStrings', 'MultipleWordsInOneString');
  53. }
  54. public function testPluralStudlyWithCount()
  55. {
  56. $this->assertPluralStudly('RealHuman', 'RealHuman', 1);
  57. $this->assertPluralStudly('RealHumans', 'RealHuman', 2);
  58. $this->assertPluralStudly('RealHuman', 'RealHuman', -1);
  59. $this->assertPluralStudly('RealHumans', 'RealHuman', -2);
  60. }
  61. public function testPluralNotAppliedForStringEndingWithNonAlphanumericCharacter()
  62. {
  63. $this->assertSame('Alien.', Str::plural('Alien.'));
  64. $this->assertSame('Alien!', Str::plural('Alien!'));
  65. $this->assertSame('Alien ', Str::plural('Alien '));
  66. $this->assertSame('50%', Str::plural('50%'));
  67. }
  68. public function testPluralAppliedForStringEndingWithNumericCharacter()
  69. {
  70. $this->assertSame('User1s', Str::plural('User1'));
  71. $this->assertSame('User2s', Str::plural('User2'));
  72. $this->assertSame('User3s', Str::plural('User3'));
  73. }
  74. public function testPluralSupportsArrays()
  75. {
  76. $this->assertSame('users', Str::plural('user', []));
  77. $this->assertSame('user', Str::plural('user', ['one']));
  78. $this->assertSame('users', Str::plural('user', ['one', 'two']));
  79. }
  80. public function testPluralSupportsCollections()
  81. {
  82. $this->assertSame('users', Str::plural('user', collect()));
  83. $this->assertSame('user', Str::plural('user', collect(['one'])));
  84. $this->assertSame('users', Str::plural('user', collect(['one', 'two'])));
  85. }
  86. public function testPluralStudlySupportsArrays()
  87. {
  88. $this->assertPluralStudly('SomeUsers', 'SomeUser', []);
  89. $this->assertPluralStudly('SomeUser', 'SomeUser', ['one']);
  90. $this->assertPluralStudly('SomeUsers', 'SomeUser', ['one', 'two']);
  91. }
  92. public function testPluralStudlySupportsCollections()
  93. {
  94. $this->assertPluralStudly('SomeUsers', 'SomeUser', collect());
  95. $this->assertPluralStudly('SomeUser', 'SomeUser', collect(['one']));
  96. $this->assertPluralStudly('SomeUsers', 'SomeUser', collect(['one', 'two']));
  97. }
  98. private function assertPluralStudly($expected, $value, $count = 2)
  99. {
  100. $this->assertSame($expected, Str::pluralStudly($value, $count));
  101. }
  102. }