|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Respect\StringFormatter\Test\Unit\Modifier; |
| 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\Modifier\QuoteModifier; |
| 12 | +use Respect\StringFormatter\Test\Helper\TestingModifier; |
| 13 | +use Respect\StringFormatter\Test\Helper\TestingQuoter; |
| 14 | + |
| 15 | +use function str_repeat; |
| 16 | +use function uniqid; |
| 17 | + |
| 18 | +#[CoversClass(QuoteModifier::class)] |
| 19 | +final class QuoteModifierTest extends TestCase |
| 20 | +{ |
| 21 | + #[Test] |
| 22 | + #[DataProvider('providerForDelegationToNextModifier')] |
| 23 | + public function itShouldDelegateToNextModifierWhenConditionsAreNotMet( |
| 24 | + mixed $value, |
| 25 | + string|null $pipe, |
| 26 | + ): void { |
| 27 | + $nextModifier = new TestingModifier(); |
| 28 | + $modifier = new QuoteModifier($nextModifier, new TestingQuoter()); |
| 29 | + $expected = $nextModifier->modify($value, $pipe); |
| 30 | + |
| 31 | + $actual = $modifier->modify($value, $pipe); |
| 32 | + |
| 33 | + self::assertSame($expected, $actual); |
| 34 | + } |
| 35 | + |
| 36 | + /** @return array<string, array{0: mixed, 1: string|null}> */ |
| 37 | + public static function providerForDelegationToNextModifier(): array |
| 38 | + { |
| 39 | + return [ |
| 40 | + 'non-string pipe value' => ['some string', 'notQuote'], |
| 41 | + 'null pipe value' => ['some string', null], |
| 42 | + 'array value with quote pipe' => [['not', 'a', 'string'], 'quote'], |
| 43 | + 'integer value with quote pipe' => [42, 'quote'], |
| 44 | + 'float value with quote pipe' => [3.14, 'quote'], |
| 45 | + 'boolean true value with quote pipe' => [true, 'quote'], |
| 46 | + 'boolean false value with quote pipe' => [false, 'quote'], |
| 47 | + 'null value with quote pipe' => [null, 'quote'], |
| 48 | + 'object value with quote pipe' => [(object) ['key' => 'value'], 'quote'], |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + #[Test] |
| 53 | + #[DataProvider('providerForStringQuoting')] |
| 54 | + public function itShouldQuoteStringsWithVariousContent(string $input): void |
| 55 | + { |
| 56 | + $expected = uniqid(); |
| 57 | + $quoter = new TestingQuoter($expected); |
| 58 | + $modifier = new QuoteModifier(new TestingModifier(), $quoter); |
| 59 | + |
| 60 | + $actual = $modifier->modify($input, 'quote'); |
| 61 | + |
| 62 | + self::assertSame($expected, $actual); |
| 63 | + } |
| 64 | + |
| 65 | + /** @return array<string, array{0: string}> */ |
| 66 | + public static function providerForStringQuoting(): array |
| 67 | + { |
| 68 | + return [ |
| 69 | + 'simple string' => ['hello'], |
| 70 | + 'empty string' => [''], |
| 71 | + 'string with spaces' => ['hello world'], |
| 72 | + 'string with quotes' => ['he said "hello"'], |
| 73 | + 'string with backslashes' => ['path\\to\\file'], |
| 74 | + 'string with special characters' => ['!@#$%^&*()'], |
| 75 | + 'string with newlines' => ["line1\nline2"], |
| 76 | + 'string with tabs' => ["col1\tcol2"], |
| 77 | + 'unicode characters' => ['héllo 🌍'], |
| 78 | + 'emoji string' => ['😀🎉'], |
| 79 | + 'mixed language string' => ['Hello 世界'], |
| 80 | + 'string with escaped quotes' => ['he said \"hello\"'], |
| 81 | + 'html entities' => ['<div>'], |
| 82 | + 'url string' => ['https://example.com/path?param=value'], |
| 83 | + 'email string' => ['user@example.com'], |
| 84 | + 'very long string' => [str_repeat('a', 1000)], |
| 85 | + ]; |
| 86 | + } |
| 87 | +} |
0 commit comments