|
| 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(string $delimiter, string $testString, bool $removeEmpty, array $expectedResult): void |
| 20 | + { |
| 21 | + self::assertSame($expectedResult, ArrayUtility::trimExplode($delimiter, $testString, $removeEmpty)); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @return array[] |
| 26 | + */ |
| 27 | + public static function trimExplodeReturnsCorrectResultDataProvider(): array |
| 28 | + { |
| 29 | + return [ |
| 30 | + 'spaces at element start and end' => [ |
| 31 | + ',', |
| 32 | + ' a , b , c ,d ,, e,f,', |
| 33 | + false, |
| 34 | + ['a', 'b', 'c', 'd', '', 'e', 'f', ''], |
| 35 | + ], |
| 36 | + 'removes newline' => [ |
| 37 | + ',', |
| 38 | + ' a , b , ' . chr(10) . ' ,d ,, e,f,', |
| 39 | + true, |
| 40 | + ['a', 'b', 'd', 'e', 'f'], |
| 41 | + ], |
| 42 | + 'removes empty elements' => [ |
| 43 | + ',', |
| 44 | + 'a , b , c , ,d ,, ,e,f,', |
| 45 | + true, |
| 46 | + ['a', 'b', 'c', 'd', 'e', 'f'], |
| 47 | + ], |
| 48 | + 'keeps zero as string' => [ |
| 49 | + ',', |
| 50 | + 'a , b , c , ,d ,, ,e,f, 0 ,', |
| 51 | + true, |
| 52 | + ['a', 'b', 'c', 'd', 'e', 'f', '0'], |
| 53 | + ], |
| 54 | + 'keeps whitespace inside elements' => [ |
| 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 internal regex delimiter as explode delimiter' => [ |
| 61 | + '/', |
| 62 | + 'a / b / c / /d // /e/f/ g h /', |
| 63 | + true, |
| 64 | + ['a', 'b', 'c', 'd', 'e', 'f', 'g h'], |
| 65 | + ], |
| 66 | + 'can use whitespaces as delimiter' => [ |
| 67 | + ' ', |
| 68 | + '* * * * *', |
| 69 | + true, |
| 70 | + ['*', '*', '*', '*', '*'], |
| 71 | + ], |
| 72 | + 'can use words as delimiter' => [ |
| 73 | + 'All', |
| 74 | + 'HelloAllTogether', |
| 75 | + true, |
| 76 | + ['Hello', 'Together'], |
| 77 | + ], |
| 78 | + 'can use word with appended and prepended spaces as delimiter' => [ |
| 79 | + ' all ', |
| 80 | + 'Hello all together', |
| 81 | + true, |
| 82 | + ['Hello', 'together'], |
| 83 | + ], |
| 84 | + 'can use word with appended and prepended spaces as delimiter and do not remove empty' => [ |
| 85 | + ' all ', |
| 86 | + 'Hello all together all there all all are all none', |
| 87 | + false, |
| 88 | + ['Hello', 'together', 'there', '', 'are', 'none'], |
| 89 | + ], |
| 90 | + 'can use words as delimiter and do not remove empty' => [ |
| 91 | + 'all there', |
| 92 | + 'Helloall theretogether all there all there are all there none', |
| 93 | + false, |
| 94 | + ['Hello', 'together', '', 'are', 'none'], |
| 95 | + ], |
| 96 | + 'can use words as delimiter, remove empty' => [ |
| 97 | + 'all there', |
| 98 | + 'Helloall theretogether all there all there are all there none', |
| 99 | + true, |
| 100 | + ['Hello', 'together', 'are', 'none'], |
| 101 | + ], |
| 102 | + 'can use new line as delimiter' => [ |
| 103 | + chr(10), |
| 104 | + "Hello\nall\ntogether", |
| 105 | + true, |
| 106 | + ['Hello', 'all', 'together'], |
| 107 | + ], |
| 108 | + 'works with whitespace separator' => [ |
| 109 | + "\t", |
| 110 | + " a b \t c \t \t d \t e \t u j \t s", |
| 111 | + false, |
| 112 | + ['a b', 'c', '', 'd', 'e', 'u j', 's'], |
| 113 | + ], |
| 114 | + 'works with whitespace separator and remove empty' => [ |
| 115 | + "\t", |
| 116 | + " a b \t c \t \t d \t e \t u j \t s", |
| 117 | + true, |
| 118 | + ['a b', 'c', 'd', 'e', 'u j', 's'], |
| 119 | + ], |
| 120 | + ]; |
| 121 | + } |
| 122 | +} |
0 commit comments