|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://hyperf.wiki |
| 9 | + * @contact group@hyperf.io |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | + |
| 13 | +namespace HyperfTest\Stringable; |
| 14 | + |
| 15 | +use Hyperf\Stringable\Str; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +use function Hyperf\Collection\collect; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + * @coversNothing |
| 23 | + */ |
| 24 | +class PluralizerTest extends TestCase |
| 25 | +{ |
| 26 | + public function testBasicSingular(): void |
| 27 | + { |
| 28 | + $this->assertSame('child', Str::singular('children')); |
| 29 | + } |
| 30 | + |
| 31 | + public function testBasicPlural(): void |
| 32 | + { |
| 33 | + $this->assertSame('children', Str::plural('child')); |
| 34 | + $this->assertSame('cod', Str::plural('cod')); |
| 35 | + $this->assertSame('The words', Str::plural('The word')); |
| 36 | + $this->assertSame('Bouquetés', Str::plural('Bouqueté')); |
| 37 | + } |
| 38 | + |
| 39 | + public function testCaseSensitiveSingularUsage(): void |
| 40 | + { |
| 41 | + $this->assertSame('Child', Str::singular('Children')); |
| 42 | + $this->assertSame('CHILD', Str::singular('CHILDREN')); |
| 43 | + $this->assertSame('Test', Str::singular('Tests')); |
| 44 | + } |
| 45 | + |
| 46 | + public function testCaseSensitiveSingularPlural(): void |
| 47 | + { |
| 48 | + $this->assertSame('Children', Str::plural('Child')); |
| 49 | + $this->assertSame('CHILDREN', Str::plural('CHILD')); |
| 50 | + $this->assertSame('Tests', Str::plural('Test')); |
| 51 | + $this->assertSame('children', Str::plural('cHiLd')); |
| 52 | + } |
| 53 | + |
| 54 | + public function testIfEndOfWordPlural(): void |
| 55 | + { |
| 56 | + $this->assertSame('VortexFields', Str::plural('VortexField')); |
| 57 | + $this->assertSame('MatrixFields', Str::plural('MatrixField')); |
| 58 | + $this->assertSame('IndexFields', Str::plural('IndexField')); |
| 59 | + $this->assertSame('VertexFields', Str::plural('VertexField')); |
| 60 | + |
| 61 | + // This is expected behavior, use "Str::pluralStudly" instead. |
| 62 | + $this->assertSame('RealHumen', Str::plural('RealHuman')); |
| 63 | + } |
| 64 | + |
| 65 | + public function testPluralWithNegativeCount(): void |
| 66 | + { |
| 67 | + $this->assertSame('test', Str::plural('test', 1)); |
| 68 | + $this->assertSame('tests', Str::plural('test', 2)); |
| 69 | + $this->assertSame('test', Str::plural('test', -1)); |
| 70 | + $this->assertSame('tests', Str::plural('test', -2)); |
| 71 | + } |
| 72 | + |
| 73 | + public function testPluralStudly(): void |
| 74 | + { |
| 75 | + $this->assertPluralStudly('RealHumans', 'RealHuman'); |
| 76 | + $this->assertPluralStudly('Models', 'Model'); |
| 77 | + $this->assertPluralStudly('VortexFields', 'VortexField'); |
| 78 | + $this->assertPluralStudly('MultipleWordsInOneStrings', 'MultipleWordsInOneString'); |
| 79 | + } |
| 80 | + |
| 81 | + public function testPluralStudlyWithCount(): void |
| 82 | + { |
| 83 | + $this->assertPluralStudly('RealHuman', 'RealHuman', 1); |
| 84 | + $this->assertPluralStudly('RealHumans', 'RealHuman', 2); |
| 85 | + $this->assertPluralStudly('RealHuman', 'RealHuman', -1); |
| 86 | + $this->assertPluralStudly('RealHumans', 'RealHuman', -2); |
| 87 | + } |
| 88 | + |
| 89 | + public function testPluralNotAppliedForStringEndingWithNonAlphanumericCharacter(): void |
| 90 | + { |
| 91 | + $this->assertSame('Alien.', Str::plural('Alien.')); |
| 92 | + $this->assertSame('Alien!', Str::plural('Alien!')); |
| 93 | + $this->assertSame('Alien ', Str::plural('Alien ')); |
| 94 | + $this->assertSame('50%', Str::plural('50%')); |
| 95 | + } |
| 96 | + |
| 97 | + public function testPluralAppliedForStringEndingWithNumericCharacter(): void |
| 98 | + { |
| 99 | + $this->assertSame('User1s', Str::plural('User1')); |
| 100 | + $this->assertSame('User2s', Str::plural('User2')); |
| 101 | + $this->assertSame('User3s', Str::plural('User3')); |
| 102 | + } |
| 103 | + |
| 104 | + public function testPluralSupportsArrays(): void |
| 105 | + { |
| 106 | + $this->assertSame('users', Str::plural('user', [])); |
| 107 | + $this->assertSame('user', Str::plural('user', ['one'])); |
| 108 | + $this->assertSame('users', Str::plural('user', ['one', 'two'])); |
| 109 | + } |
| 110 | + |
| 111 | + public function testPluralSupportsCollections() |
| 112 | + { |
| 113 | + $this->assertSame('users', Str::plural('user', collect())); |
| 114 | + $this->assertSame('user', Str::plural('user', collect(['one']))); |
| 115 | + $this->assertSame('users', Str::plural('user', collect(['one', 'two']))); |
| 116 | + } |
| 117 | + |
| 118 | + public function testPluralStudlySupportsArrays() |
| 119 | + { |
| 120 | + $this->assertPluralStudly('SomeUsers', 'SomeUser', []); |
| 121 | + $this->assertPluralStudly('SomeUser', 'SomeUser', ['one']); |
| 122 | + $this->assertPluralStudly('SomeUsers', 'SomeUser', ['one', 'two']); |
| 123 | + } |
| 124 | + |
| 125 | + public function testPluralStudlySupportsCollections(): void |
| 126 | + { |
| 127 | + $this->assertPluralStudly('SomeUsers', 'SomeUser', collect()); |
| 128 | + $this->assertPluralStudly('SomeUser', 'SomeUser', collect(['one'])); |
| 129 | + $this->assertPluralStudly('SomeUsers', 'SomeUser', collect(['one', 'two'])); |
| 130 | + } |
| 131 | + |
| 132 | + public function testPluralizerAboutData() |
| 133 | + { |
| 134 | + $this->assertSame('xxx_datum', Str::singular('xxx_data')); |
| 135 | + $this->assertSame('data', Str::plural('datum')); |
| 136 | + $this->assertSame('data', Str::singular('data')); |
| 137 | + } |
| 138 | + |
| 139 | + private function assertPluralStudly($expected, $value, $count = 2): void |
| 140 | + { |
| 141 | + $this->assertSame($expected, Str::pluralStudly($value, $count)); |
| 142 | + } |
| 143 | +} |
0 commit comments