|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Support\Tests; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\TestWith; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Tempest\Support\StringHelper; |
| 10 | + |
| 11 | +/** |
| 12 | + * @internal |
| 13 | + * @small |
| 14 | + */ |
| 15 | +final class StringHelperTest extends TestCase |
| 16 | +{ |
| 17 | + public function test_title(): void |
| 18 | + { |
| 19 | + $this->assertSame('Jefferson Costella', StringHelper::title('jefferson costella')); |
| 20 | + $this->assertSame('Jefferson Costella', StringHelper::title('jefFErson coSTella')); |
| 21 | + |
| 22 | + $this->assertSame('', StringHelper::title('')); |
| 23 | + $this->assertSame('123 Tempest', StringHelper::title('123 tempest')); |
| 24 | + $this->assertSame('❤Tempest', StringHelper::title('❤tempest')); |
| 25 | + $this->assertSame('Tempest ❤', StringHelper::title('tempest ❤')); |
| 26 | + $this->assertSame('Tempest123', StringHelper::title('tempest123')); |
| 27 | + $this->assertSame('Tempest123', StringHelper::title('Tempest123')); |
| 28 | + |
| 29 | + $longString = 'lorem ipsum '.str_repeat('dolor sit amet ', 1000); |
| 30 | + $expectedResult = 'Lorem Ipsum Dolor Sit Amet '.str_repeat('Dolor Sit Amet ', 999); |
| 31 | + $this->assertSame($expectedResult, StringHelper::title($longString)); |
| 32 | + } |
| 33 | + |
| 34 | + public function test_deduplicate(): void |
| 35 | + { |
| 36 | + $this->assertSame('/some/odd/path/', StringHelper::deduplicate('/some//odd//path/', '/')); |
| 37 | + $this->assertSame(' tempest php framework ', StringHelper::deduplicate(' tempest php framework ')); |
| 38 | + $this->assertSame('what', StringHelper::deduplicate('whaaat', 'a')); |
| 39 | + $this->assertSame('ムだム', StringHelper::deduplicate('ムだだム', 'だ')); |
| 40 | + } |
| 41 | + |
| 42 | + public function test_pascal(): void |
| 43 | + { |
| 44 | + $this->assertSame('', StringHelper::pascal('')); |
| 45 | + $this->assertSame('FooBar', StringHelper::pascal('foo bar')); |
| 46 | + $this->assertSame('FooBar', StringHelper::pascal('foo - bar')); |
| 47 | + $this->assertSame('FooBar', StringHelper::pascal('foo__bar')); |
| 48 | + $this->assertSame('FooBar', StringHelper::pascal('_foo__bar')); |
| 49 | + $this->assertSame('FooBar', StringHelper::pascal('-foo__bar')); |
| 50 | + $this->assertSame('FooBar', StringHelper::pascal('fooBar')); |
| 51 | + $this->assertSame('FooBar', StringHelper::pascal('foo_bar')); |
| 52 | + $this->assertSame('FooBar1', StringHelper::pascal('foo_bar1')); |
| 53 | + $this->assertSame('1fooBar', StringHelper::pascal('1foo_bar')); |
| 54 | + $this->assertSame('1fooBar11', StringHelper::pascal('1foo_bar11')); |
| 55 | + $this->assertSame('1foo1bar1', StringHelper::pascal('1foo_1bar1')); |
| 56 | + $this->assertSame('FooBarBaz', StringHelper::pascal('foo-barBaz')); |
| 57 | + $this->assertSame('FooBarBaz', StringHelper::pascal('foo-bar_baz')); |
| 58 | + // TODO: support when `mb_ucfirst` has landed in PHP 8.4 |
| 59 | + // $this->assertSame('ÖffentlicheÜberraschungen', StringHelper::pascal('öffentliche-überraschungen')); |
| 60 | + } |
| 61 | + |
| 62 | + public function test_kebab(): void |
| 63 | + { |
| 64 | + $this->assertSame('', StringHelper::kebab('')); |
| 65 | + $this->assertSame('foo-bar', StringHelper::kebab('foo bar')); |
| 66 | + $this->assertSame('foo-bar', StringHelper::kebab('foo - bar')); |
| 67 | + $this->assertSame('foo-bar', StringHelper::kebab('foo__bar')); |
| 68 | + $this->assertSame('foo-bar', StringHelper::kebab('_foo__bar')); |
| 69 | + $this->assertSame('foo-bar', StringHelper::kebab('-foo__bar')); |
| 70 | + $this->assertSame('foo-bar', StringHelper::kebab('fooBar')); |
| 71 | + $this->assertSame('foo-bar', StringHelper::kebab('foo_bar')); |
| 72 | + $this->assertSame('foo-bar1', StringHelper::kebab('foo_bar1')); |
| 73 | + $this->assertSame('1foo-bar', StringHelper::kebab('1foo_bar')); |
| 74 | + $this->assertSame('1foo-bar11', StringHelper::kebab('1foo_bar11')); |
| 75 | + $this->assertSame('1foo-1bar1', StringHelper::kebab('1foo_1bar1')); |
| 76 | + $this->assertSame('foo-bar-baz', StringHelper::kebab('foo-barBaz')); |
| 77 | + $this->assertSame('foo-bar-baz', StringHelper::kebab('foo-bar_baz')); |
| 78 | + } |
| 79 | + |
| 80 | + public function test_snake(): void |
| 81 | + { |
| 82 | + $this->assertSame('', StringHelper::snake('')); |
| 83 | + $this->assertSame('foo_bar', StringHelper::snake('foo bar')); |
| 84 | + $this->assertSame('foo_bar', StringHelper::snake('foo - bar')); |
| 85 | + $this->assertSame('foo_bar', StringHelper::snake('foo__bar')); |
| 86 | + $this->assertSame('foo_bar', StringHelper::snake('_foo__bar')); |
| 87 | + $this->assertSame('foo_bar', StringHelper::snake('-foo__bar')); |
| 88 | + $this->assertSame('foo_bar', StringHelper::snake('fooBar')); |
| 89 | + $this->assertSame('foo_bar', StringHelper::snake('foo_bar')); |
| 90 | + $this->assertSame('foo_bar1', StringHelper::snake('foo_bar1')); |
| 91 | + $this->assertSame('1foo_bar', StringHelper::snake('1foo_bar')); |
| 92 | + $this->assertSame('1foo_bar11', StringHelper::snake('1foo_bar11')); |
| 93 | + $this->assertSame('1foo_1bar1', StringHelper::snake('1foo_1bar1')); |
| 94 | + $this->assertSame('foo_bar_baz', StringHelper::snake('foo-barBaz')); |
| 95 | + $this->assertSame('foo_bar_baz', StringHelper::snake('foo-bar_baz')); |
| 96 | + } |
| 97 | + |
| 98 | + #[TestWith([0])] |
| 99 | + #[TestWith([16])] |
| 100 | + #[TestWith([100])] |
| 101 | + public function test_random(int $length): void |
| 102 | + { |
| 103 | + $this->assertEquals($length, strlen(StringHelper::random($length))); |
| 104 | + } |
| 105 | + |
| 106 | + public function test_finish(): void |
| 107 | + { |
| 108 | + $this->assertSame('foo/', StringHelper::finish('foo', '/')); |
| 109 | + $this->assertSame('foo/', StringHelper::finish('foo/', '/')); |
| 110 | + $this->assertSame('abbc', StringHelper::finish('abbcbc', 'bc')); |
| 111 | + $this->assertSame('abcbbc', StringHelper::finish('abcbbcbc', 'bc')); |
| 112 | + } |
| 113 | + |
| 114 | + public function test_str_after(): void |
| 115 | + { |
| 116 | + $this->assertSame('nah', StringHelper::after('hannah', 'han')); |
| 117 | + $this->assertSame('nah', StringHelper::after('hannah', 'n')); |
| 118 | + $this->assertSame('nah', StringHelper::after('ééé hannah', 'han')); |
| 119 | + $this->assertSame('hannah', StringHelper::after('hannah', 'xxxx')); |
| 120 | + $this->assertSame('hannah', StringHelper::after('hannah', '')); |
| 121 | + $this->assertSame('nah', StringHelper::after('han0nah', '0')); |
| 122 | + $this->assertSame('nah', StringHelper::after('han0nah', 0)); |
| 123 | + $this->assertSame('nah', StringHelper::after('han2nah', 2)); |
| 124 | + } |
| 125 | + |
| 126 | + public function test_str_after_last(): void |
| 127 | + { |
| 128 | + $this->assertSame('tte', StringHelper::afterLast('yvette', 'yve')); |
| 129 | + $this->assertSame('e', StringHelper::afterLast('yvette', 't')); |
| 130 | + $this->assertSame('e', StringHelper::afterLast('ééé yvette', 't')); |
| 131 | + $this->assertSame('', StringHelper::afterLast('yvette', 'tte')); |
| 132 | + $this->assertSame('yvette', StringHelper::afterLast('yvette', 'xxxx')); |
| 133 | + $this->assertSame('yvette', StringHelper::afterLast('yvette', '')); |
| 134 | + $this->assertSame('te', StringHelper::afterLast('yv0et0te', '0')); |
| 135 | + $this->assertSame('te', StringHelper::afterLast('yv0et0te', 0)); |
| 136 | + $this->assertSame('te', StringHelper::afterLast('yv2et2te', 2)); |
| 137 | + $this->assertSame('foo', StringHelper::afterLast('----foo', '---')); |
| 138 | + } |
| 139 | + |
| 140 | + public function test_str_between(): void |
| 141 | + { |
| 142 | + $this->assertSame('abc', StringHelper::between('abc', '', 'c')); |
| 143 | + $this->assertSame('abc', StringHelper::between('abc', 'a', '')); |
| 144 | + $this->assertSame('abc', StringHelper::between('abc', '', '')); |
| 145 | + $this->assertSame('b', StringHelper::between('abc', 'a', 'c')); |
| 146 | + $this->assertSame('b', StringHelper::between('dddabc', 'a', 'c')); |
| 147 | + $this->assertSame('b', StringHelper::between('abcddd', 'a', 'c')); |
| 148 | + $this->assertSame('b', StringHelper::between('dddabcddd', 'a', 'c')); |
| 149 | + $this->assertSame('nn', StringHelper::between('hannah', 'ha', 'ah')); |
| 150 | + $this->assertSame('a]ab[b', StringHelper::between('[a]ab[b]', '[', ']')); |
| 151 | + $this->assertSame('foo', StringHelper::between('foofoobar', 'foo', 'bar')); |
| 152 | + $this->assertSame('bar', StringHelper::between('foobarbar', 'foo', 'bar')); |
| 153 | + $this->assertSame('234', StringHelper::between('12345', 1, 5)); |
| 154 | + $this->assertSame('45', StringHelper::between('123456789', '123', '6789')); |
| 155 | + $this->assertSame('nothing', StringHelper::between('nothing', 'foo', 'bar')); |
| 156 | + } |
| 157 | + |
| 158 | + public function test_str_before(): void |
| 159 | + { |
| 160 | + $this->assertSame('han', StringHelper::before('hannah', 'nah')); |
| 161 | + $this->assertSame('ha', StringHelper::before('hannah', 'n')); |
| 162 | + $this->assertSame('ééé ', StringHelper::before('ééé hannah', 'han')); |
| 163 | + $this->assertSame('hannah', StringHelper::before('hannah', 'xxxx')); |
| 164 | + $this->assertSame('hannah', StringHelper::before('hannah', '')); |
| 165 | + $this->assertSame('han', StringHelper::before('han0nah', '0')); |
| 166 | + $this->assertSame('han', StringHelper::before('han0nah', 0)); |
| 167 | + $this->assertSame('han', StringHelper::before('han2nah', 2)); |
| 168 | + $this->assertSame('', StringHelper::before('', '')); |
| 169 | + $this->assertSame('', StringHelper::before('', 'a')); |
| 170 | + $this->assertSame('', StringHelper::before('a', 'a')); |
| 171 | + $this-> assertSame( 'foo', StringHelper:: before( '[email protected]', '@')); |
| 172 | + $this->assertSame('foo', StringHelper::before('foo@@bar.com', '@')); |
| 173 | + $this-> assertSame( '', StringHelper:: before( '@[email protected]', '@')); |
| 174 | + } |
| 175 | + |
| 176 | + public function test_str_before_last(): void |
| 177 | + { |
| 178 | + $this->assertSame('yve', StringHelper::beforeLast('yvette', 'tte')); |
| 179 | + $this->assertSame('yvet', StringHelper::beforeLast('yvette', 't')); |
| 180 | + $this->assertSame('ééé ', StringHelper::beforeLast('ééé yvette', 'yve')); |
| 181 | + $this->assertSame('', StringHelper::beforeLast('yvette', 'yve')); |
| 182 | + $this->assertSame('yvette', StringHelper::beforeLast('yvette', 'xxxx')); |
| 183 | + $this->assertSame('yvette', StringHelper::beforeLast('yvette', '')); |
| 184 | + $this->assertSame('yv0et', StringHelper::beforeLast('yv0et0te', '0')); |
| 185 | + $this->assertSame('yv0et', StringHelper::beforeLast('yv0et0te', 0)); |
| 186 | + $this->assertSame('yv2et', StringHelper::beforeLast('yv2et2te', 2)); |
| 187 | + $this->assertSame('', StringHelper::beforeLast('', 'test')); |
| 188 | + $this->assertSame('', StringHelper::beforeLast('yvette', 'yvette')); |
| 189 | + $this->assertSame('tempest', StringHelper::beforeLast('tempest framework', ' ')); |
| 190 | + $this->assertSame('yvette', StringHelper::beforeLast("yvette\tyv0et0te", "\t")); |
| 191 | + } |
| 192 | +} |
0 commit comments