|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace a9f\Fractor\Tests\Helper; |
| 6 | + |
| 7 | +use a9f\Fractor\Helper\ArrayUtility; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\Attributes\Test; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class ArrayUtilityTest extends TestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @param string[] $expectedResult |
| 16 | + */ |
| 17 | + #[DataProvider('trimExplodeReturnsCorrectResultDataProvider')] |
| 18 | + #[Test] |
| 19 | + public function trimExplodeReturnsCorrectResult( |
| 20 | + string $delimiter, |
| 21 | + string $testString, |
| 22 | + bool $removeEmpty, |
| 23 | + array $expectedResult |
| 24 | + ): void { |
| 25 | + self::assertSame($expectedResult, ArrayUtility::trimExplode($delimiter, $testString, $removeEmpty)); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @return array<string, array{0: string, 1: string, 2:bool, 3:array<string>}> |
| 30 | + */ |
| 31 | + public static function trimExplodeReturnsCorrectResultDataProvider(): array |
| 32 | + { |
| 33 | + return [ |
| 34 | + 'spaces at element start and end' => [ |
| 35 | + ',', |
| 36 | + ' a , b , c ,d ,, e,f,', |
| 37 | + false, |
| 38 | + ['a', 'b', 'c', 'd', '', 'e', 'f', ''], |
| 39 | + ], |
| 40 | + 'removes newline' => [',', ' a , b , ' . chr(10) . ' ,d ,, e,f,', true, ['a', 'b', 'd', 'e', 'f']], |
| 41 | + 'removes empty elements' => [',', 'a , b , c , ,d ,, ,e,f,', true, ['a', 'b', 'c', 'd', 'e', 'f']], |
| 42 | + 'keeps zero as string' => [ |
| 43 | + ',', |
| 44 | + 'a , b , c , ,d ,, ,e,f, 0 ,', |
| 45 | + true, |
| 46 | + ['a', 'b', 'c', 'd', 'e', 'f', '0'], |
| 47 | + ], |
| 48 | + 'keeps whitespace inside elements' => [ |
| 49 | + ',', |
| 50 | + 'a , b , c , ,d ,, ,e,f, g h ,', |
| 51 | + true, |
| 52 | + ['a', 'b', 'c', 'd', 'e', 'f', 'g h'], |
| 53 | + ], |
| 54 | + 'can use internal regex delimiter as explode delimiter' => [ |
| 55 | + '/', |
| 56 | + 'a / b / c / /d // /e/f/ g h /', |
| 57 | + true, |
| 58 | + ['a', 'b', 'c', 'd', 'e', 'f', 'g h'], |
| 59 | + ], |
| 60 | + 'can use whitespaces as delimiter' => [' ', '* * * * *', true, ['*', '*', '*', '*', '*']], |
| 61 | + 'can use words as delimiter' => ['All', 'HelloAllTogether', true, ['Hello', 'Together']], |
| 62 | + 'can use word with appended and prepended spaces as delimiter' => [ |
| 63 | + ' all ', |
| 64 | + 'Hello all together', |
| 65 | + true, |
| 66 | + ['Hello', 'together'], |
| 67 | + ], |
| 68 | + 'can use word with appended and prepended spaces as delimiter and do not remove empty' => [ |
| 69 | + ' all ', |
| 70 | + 'Hello all together all there all all are all none', |
| 71 | + false, |
| 72 | + ['Hello', 'together', 'there', '', 'are', 'none'], |
| 73 | + ], |
| 74 | + 'can use words as delimiter and do not remove empty' => [ |
| 75 | + 'all there', |
| 76 | + 'Helloall theretogether all there all there are all there none', |
| 77 | + false, |
| 78 | + ['Hello', 'together', '', 'are', 'none'], |
| 79 | + ], |
| 80 | + 'can use words as delimiter, remove empty' => [ |
| 81 | + 'all there', |
| 82 | + 'Helloall theretogether all there all there are all there none', |
| 83 | + true, |
| 84 | + ['Hello', 'together', 'are', 'none'], |
| 85 | + ], |
| 86 | + 'can use new line as delimiter' => [ |
| 87 | + chr(10), |
| 88 | + "Hello\nall\ntogether", |
| 89 | + true, |
| 90 | + ['Hello', 'all', 'together'], |
| 91 | + ], |
| 92 | + 'works with whitespace separator' => [ |
| 93 | + "\t", |
| 94 | + " a b \t c \t \t d \t e \t u j \t s", |
| 95 | + false, |
| 96 | + ['a b', 'c', '', 'd', 'e', 'u j', 's'], |
| 97 | + ], |
| 98 | + 'works with whitespace separator and remove empty' => [ |
| 99 | + "\t", |
| 100 | + " a b \t c \t \t d \t e \t u j \t s", |
| 101 | + true, |
| 102 | + ['a b', 'c', 'd', 'e', 'u j', 's'], |
| 103 | + ], |
| 104 | + ]; |
| 105 | + } |
| 106 | +} |
0 commit comments