|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Respect\StringFormatter\Test\Unit\Modifiers; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\Attributes\Test; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Respect\StringFormatter\Modifiers\ListModifier; |
| 12 | +use Respect\StringFormatter\Test\Helper\TestingModifier; |
| 13 | + |
| 14 | +#[CoversClass(ListModifier::class)] |
| 15 | +final class ListModifierTest extends TestCase |
| 16 | +{ |
| 17 | + #[Test] |
| 18 | + #[DataProvider('providerNonSupportedValuesAndPipes')] |
| 19 | + public function itShouldDelegateToNextModifier(string|null $pipe, mixed $value): void |
| 20 | + { |
| 21 | + $nextModifier = new TestingModifier(); |
| 22 | + |
| 23 | + $modifier = new ListModifier($nextModifier); |
| 24 | + |
| 25 | + $result = $modifier->modify($value, $pipe); |
| 26 | + |
| 27 | + self::assertSame($nextModifier->modify($value, $pipe), $result); |
| 28 | + } |
| 29 | + |
| 30 | + /** @return array<string, array{0: string|null, 1: mixed}> */ |
| 31 | + public static function providerNonSupportedValuesAndPipes(): array |
| 32 | + { |
| 33 | + return [ |
| 34 | + 'pipe is null' => [null, ['a', 'b', 'c']], |
| 35 | + 'pipe is not list' => ['notList', ['a', 'b', 'c']], |
| 36 | + 'value is not array' => ['list:and', 'not an array'], |
| 37 | + 'value is empty array' => ['list:or', []], |
| 38 | + 'modifier is not well formatted' => ['list(and")', []], |
| 39 | + ]; |
| 40 | + } |
| 41 | + |
| 42 | + /** @param array<int|string, string> $value */ |
| 43 | + #[Test] |
| 44 | + #[DataProvider('providerSupportedValuesAndPipes')] |
| 45 | + public function itShouldModifyValue(string $pipe, array $value, string $expected): void |
| 46 | + { |
| 47 | + $modifier = new ListModifier(new TestingModifier()); |
| 48 | + |
| 49 | + $result = $modifier->modify($value, $pipe); |
| 50 | + |
| 51 | + self::assertSame($expected, $result); |
| 52 | + } |
| 53 | + |
| 54 | + /** @return array<string, array{0: string, 1: array<int|string, string>, 2: string}> */ |
| 55 | + public static function providerSupportedValuesAndPipes(): array |
| 56 | + { |
| 57 | + return [ |
| 58 | + 'with a single value' => [ |
| 59 | + 'list', |
| 60 | + ['apple'], |
| 61 | + 'apple', |
| 62 | + ], |
| 63 | + ':and with a single value' => [ |
| 64 | + 'list:and', |
| 65 | + ['apple'], |
| 66 | + 'apple', |
| 67 | + ], |
| 68 | + ':or with a single value' => [ |
| 69 | + 'list:or', |
| 70 | + ['apple'], |
| 71 | + 'apple', |
| 72 | + ], |
| 73 | + 'with two values' => [ |
| 74 | + 'list', |
| 75 | + ['apple', 'banana'], |
| 76 | + 'apple and banana', |
| 77 | + ], |
| 78 | + ':and with two values' => [ |
| 79 | + 'list:and', |
| 80 | + ['apple', 'banana'], |
| 81 | + 'apple and banana', |
| 82 | + ], |
| 83 | + ':or with two values' => [ |
| 84 | + 'list:or', |
| 85 | + ['apple', 'banana'], |
| 86 | + 'apple or banana', |
| 87 | + ], |
| 88 | + 'with multiple values' => [ |
| 89 | + 'list', |
| 90 | + ['apple', 'banana', 'cherry', 'date', 'elderberry'], |
| 91 | + 'apple, banana, cherry, date, and elderberry', |
| 92 | + ], |
| 93 | + ':and with multiple values' => [ |
| 94 | + 'list:and', |
| 95 | + ['apple', 'banana', 'cherry', 'date', 'elderberry'], |
| 96 | + 'apple, banana, cherry, date, and elderberry', |
| 97 | + ], |
| 98 | + ':or with multiple values' => [ |
| 99 | + 'list:or', |
| 100 | + ['apple', 'banana', 'cherry', 'date', 'elderberry'], |
| 101 | + 'apple, banana, cherry, date, or elderberry', |
| 102 | + ], |
| 103 | + 'with associative array' => [ |
| 104 | + 'list', |
| 105 | + ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry', 'd' => 'date', 'e' => 'elderberry'], |
| 106 | + 'apple, banana, cherry, date, and elderberry', |
| 107 | + ], |
| 108 | + ]; |
| 109 | + } |
| 110 | +} |
0 commit comments