|
| 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\QuoteModifier; |
| 12 | +use Respect\StringFormatter\Test\Helper\TestingModifier; |
| 13 | + |
| 14 | +#[CoversClass(QuoteModifier::class)] |
| 15 | +final class QuoteModifierTest extends TestCase |
| 16 | +{ |
| 17 | + #[Test] |
| 18 | + #[DataProvider('providerForPipeNotQuoteCases')] |
| 19 | + public function itShouldDelegateToNextModifierWhenPipeIsNotQuote(mixed $value, string|null $pipe): void |
| 20 | + { |
| 21 | + $nextModifier = new TestingModifier(); |
| 22 | + $modifier = new QuoteModifier($nextModifier); |
| 23 | + $expected = $nextModifier->modify($value, $pipe); |
| 24 | + |
| 25 | + $actual = $modifier->modify($value, $pipe); |
| 26 | + |
| 27 | + self::assertSame($expected, $actual); |
| 28 | + } |
| 29 | + |
| 30 | + /** @return array<string, array{0: mixed, 1: string|null}> */ |
| 31 | + public static function providerForPipeNotQuoteCases(): array |
| 32 | + { |
| 33 | + return [ |
| 34 | + 'non-string pipe value' => ['some string', 'notQuote'], |
| 35 | + 'null pipe value' => ['some string', null], |
| 36 | + ]; |
| 37 | + } |
| 38 | + |
| 39 | + #[Test] |
| 40 | + #[DataProvider('providerForNonScalarWithQuotePipe')] |
| 41 | + public function itShouldDelegateToNextModifierWhenValueIsNotScalarAndPipeIsQuote(mixed $value): void |
| 42 | + { |
| 43 | + $nextModifier = new TestingModifier(); |
| 44 | + $modifier = new QuoteModifier($nextModifier); |
| 45 | + // Non-scalar values with 'quote' pipe should delegate with null pipe |
| 46 | + $expected = $nextModifier->modify($value, null); |
| 47 | + |
| 48 | + $actual = $modifier->modify($value, 'quote'); |
| 49 | + |
| 50 | + self::assertSame($expected, $actual); |
| 51 | + } |
| 52 | + |
| 53 | + /** @return array<string, array{0: mixed}> */ |
| 54 | + public static function providerForNonScalarWithQuotePipe(): array |
| 55 | + { |
| 56 | + return [ |
| 57 | + 'array value with quote pipe' => [['not', 'a', 'string']], |
| 58 | + 'null value with quote pipe' => [null], |
| 59 | + 'object value with quote pipe' => [(object) ['key' => 'value']], |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + #[Test] |
| 64 | + #[DataProvider('providerForScalarQuotingCases')] |
| 65 | + public function itShouldQuoteScalarValuesWithQuotePipe( |
| 66 | + mixed $value, |
| 67 | + string $expected, |
| 68 | + ): void { |
| 69 | + $modifier = new QuoteModifier(new TestingModifier()); |
| 70 | + |
| 71 | + $actual = $modifier->modify($value, 'quote'); |
| 72 | + |
| 73 | + self::assertSame($expected, $actual); |
| 74 | + } |
| 75 | + |
| 76 | + /** @return array<string, array{0: mixed, 1: string}> */ |
| 77 | + public static function providerForScalarQuotingCases(): array |
| 78 | + { |
| 79 | + return [ |
| 80 | + 'integer value' => [42, '`42`'], |
| 81 | + 'float value' => [3.14, '`3.14`'], |
| 82 | + 'boolean true value' => [true, '`1`'], |
| 83 | + 'boolean false value' => [false, '``'], |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + #[Test] |
| 88 | + #[DataProvider('providerForStringQuoting')] |
| 89 | + public function itShouldQuoteStringsWithVariousContent(string $input, string $expected, string $quote = '`'): void |
| 90 | + { |
| 91 | + $modifier = new QuoteModifier(new TestingModifier(), $quote); |
| 92 | + |
| 93 | + $actual = $modifier->modify($input, 'quote'); |
| 94 | + |
| 95 | + self::assertSame($expected, $actual); |
| 96 | + } |
| 97 | + |
| 98 | + /** @return array<string, array{0: string, 1: string, 2: string}> */ |
| 99 | + public static function providerForStringQuoting(): array |
| 100 | + { |
| 101 | + return [ |
| 102 | + 'simple string' => ['hello', '`hello`', '`'], |
| 103 | + 'empty string' => ['', '``', '`'], |
| 104 | + 'string with backticks' => ['he `hello` there', '`he \\`hello\\` there`', '`'], |
| 105 | + 'string with backslashes' => ['path\\to\\file', '`path\\to\\file`', '`'], |
| 106 | + 'string with special characters' => ['!@#$%^&*()', '`!@#$%^&*()`', '`'], |
| 107 | + 'string with newlines' => ["line1\nline2", "`line1\nline2`", '`'], |
| 108 | + 'unicode characters' => ['héllo 🌍', '`héllo 🌍`', '`'], |
| 109 | + 'emoji string' => ['😀🎉', '`😀🎉`', '`'], |
| 110 | + 'mixed language string' => ['Hello 世界', '`Hello 世界`', '`'], |
| 111 | + 'html entities' => ['<div>', '`<div>`', '`'], |
| 112 | + 'url string' => ['https://example.com', '`https://example.com`', '`'], |
| 113 | + 'email string' => ['user@example.com', '`user@example.com`', '`'], |
| 114 | + 'single quote string' => ["don't", "`don't`", '`'], |
| 115 | + 'string with single quotes' => ["he's 'great'", "`he's 'great'`", '`'], |
| 116 | + 'string with mixed quotes' => ['say "hello" and \'goodbye\'', "`say \"hello\" and 'goodbye'`", '`'], |
| 117 | + 'test with single quotes' => ["can't stop", '"can\'t stop"', '"'], |
| 118 | + 'test with double quotes' => ['hello "world"', '"hello \"world\""', '"'], |
| 119 | + 'test with both quotes' => ['hello "world" and \'test\'', '"hello \"world\" and \'test\'"', '"'], |
| 120 | + ]; |
| 121 | + } |
| 122 | +} |
0 commit comments