|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * SPDX-FileCopyrightText: (c) Respect Project Contributors |
| 5 | + * SPDX-License-Identifier: ISC |
| 6 | + * SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace Respect\StringFormatter\Test\Unit; |
| 12 | + |
| 13 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\Attributes\Test; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Respect\StringFormatter\PlaceholderFormatter; |
| 18 | + |
| 19 | +#[CoversClass(PlaceholderFormatter::class)] |
| 20 | +final class PlaceholderFormatterEscapingTest extends TestCase |
| 21 | +{ |
| 22 | + /** @param array<string, mixed> $parameters */ |
| 23 | + #[Test] |
| 24 | + #[DataProvider('providerForEscapedColons')] |
| 25 | + public function itShouldHandleEscapedColonsInFormatterArguments( |
| 26 | + array $parameters, |
| 27 | + string $template, |
| 28 | + string $expected, |
| 29 | + ): void { |
| 30 | + $formatter = new PlaceholderFormatter($parameters); |
| 31 | + $actual = $formatter->format($template); |
| 32 | + |
| 33 | + self::assertSame($expected, $actual); |
| 34 | + } |
| 35 | + |
| 36 | + /** @return array<string, array{0: array<string, mixed>, 1: string, 2: string}> */ |
| 37 | + public static function providerForEscapedColons(): array |
| 38 | + { |
| 39 | + return [ |
| 40 | + 'pattern with escaped colon' => [ |
| 41 | + ['time' => '1234'], |
| 42 | + '{{time|pattern:##\:##}}', |
| 43 | + '12:34', |
| 44 | + ], |
| 45 | + 'pattern with multiple escaped colons' => [ |
| 46 | + ['time' => '123456'], |
| 47 | + '{{time|pattern:##\:##\:##}}', |
| 48 | + '12:34:56', |
| 49 | + ], |
| 50 | + ]; |
| 51 | + } |
| 52 | + |
| 53 | + /** @param array<string, mixed> $parameters */ |
| 54 | + #[Test] |
| 55 | + #[DataProvider('providerForEscapedPipes')] |
| 56 | + public function itShouldHandleEscapedPipesInFormatterArguments( |
| 57 | + array $parameters, |
| 58 | + string $template, |
| 59 | + string $expected, |
| 60 | + ): void { |
| 61 | + $formatter = new PlaceholderFormatter($parameters); |
| 62 | + $actual = $formatter->format($template); |
| 63 | + |
| 64 | + self::assertSame($expected, $actual); |
| 65 | + } |
| 66 | + |
| 67 | + /** @return array<string, array{0: array<string, mixed>, 1: string, 2: string}> */ |
| 68 | + public static function providerForEscapedPipes(): array |
| 69 | + { |
| 70 | + return [ |
| 71 | + 'pattern with escaped pipe' => [ |
| 72 | + ['value' => '123456'], |
| 73 | + '{{value|pattern:###\|###}}', |
| 74 | + '123|456', |
| 75 | + ], |
| 76 | + 'pattern with multiple escaped pipes' => [ |
| 77 | + ['value' => '12345678'], |
| 78 | + '{{value|pattern:##\|##\|##\|##}}', |
| 79 | + '12|34|56|78', |
| 80 | + ], |
| 81 | + ]; |
| 82 | + } |
| 83 | +} |
0 commit comments