|
10 | 10 |
|
11 | 11 | final class PatternFormatterTest extends TestCase |
12 | 12 | { |
13 | | - private PatternFormatter $formatter; |
14 | | - |
15 | | - protected function setUp(): void |
| 13 | + public function testFormatWithNonArrayParameter(): void |
16 | 14 | { |
17 | | - $this->formatter = new PatternFormatter(); |
| 15 | + $formatter = new PatternFormatter(); |
| 16 | + $value = ['Hello %s!', 'World']; |
| 17 | + $result = $formatter->format($value); |
| 18 | + |
| 19 | + $this->assertSame(['Hello World!'], $result); |
18 | 20 | } |
19 | 21 |
|
20 | 22 | public function testFormatWithSingleElement(): void |
21 | 23 | { |
| 24 | + $formatter = new PatternFormatter(); |
22 | 25 | $value = ['Hello World']; |
23 | | - $result = $this->formatter->format($value); |
| 26 | + $result = $formatter->format($value); |
24 | 27 |
|
25 | 28 | $this->assertSame(['Hello World'], $result); |
26 | 29 | } |
27 | 30 |
|
28 | 31 | public function testFormatWithSingleElementNumber(): void |
29 | 32 | { |
| 33 | + $formatter = new PatternFormatter(); |
30 | 34 | $value = [123]; |
31 | | - $result = $this->formatter->format($value); |
| 35 | + $result = $formatter->format($value); |
32 | 36 |
|
33 | 37 | $this->assertSame(['123'], $result); |
34 | 38 | } |
35 | 39 |
|
36 | | - public function testFormatWithSingleElementBoolean(): void |
37 | | - { |
38 | | - $value = [true]; |
39 | | - $result = $this->formatter->format($value); |
40 | | - |
41 | | - $this->assertSame(['1'], $result); |
42 | | - } |
43 | | - |
44 | | - public function testFormatWithSingleElementNull(): void |
45 | | - { |
46 | | - $value = [null]; |
47 | | - $result = $this->formatter->format($value); |
48 | | - |
49 | | - $this->assertSame([''], $result); |
50 | | - } |
51 | | - |
52 | | - public function testFormatWithPatternAndParameters(): void |
| 40 | + public function testFormatWithPatternAndArrayParameters(): void |
53 | 41 | { |
| 42 | + $formatter = new PatternFormatter(); |
54 | 43 | $value = ['Hello %s, you are %d years old', ['John', 25]]; |
55 | | - $result = $this->formatter->format($value); |
| 44 | + $result = $formatter->format($value); |
56 | 45 |
|
57 | 46 | $this->assertSame(['Hello John, you are 25 years old'], $result); |
58 | 47 | } |
59 | 48 |
|
60 | | - public function testFormatWithPatternAndSingleParameter(): void |
61 | | - { |
62 | | - $value = ['Welcome %s!', ['Alice']]; |
63 | | - $result = $this->formatter->format($value); |
64 | | - |
65 | | - $this->assertSame(['Welcome Alice!'], $result); |
66 | | - } |
67 | | - |
68 | 49 | public function testFormatWithPatternAndNullParameters(): void |
69 | 50 | { |
| 51 | + $formatter = new PatternFormatter(); |
70 | 52 | $value = ['Simple pattern', null]; |
71 | | - $result = $this->formatter->format($value); |
| 53 | + $result = $formatter->format($value); |
72 | 54 |
|
73 | 55 | $this->assertSame(['Simple pattern'], $result); |
74 | 56 | } |
75 | 57 |
|
76 | | - public function testFormatWithPatternAndEmptyParameters(): void |
77 | | - { |
78 | | - $value = ['No parameters', []]; |
79 | | - $result = $this->formatter->format($value); |
80 | | - |
81 | | - $this->assertSame(['No parameters'], $result); |
82 | | - } |
83 | | - |
84 | 58 | public function testFormatWithPatternAndClosureParameters(): void |
85 | 59 | { |
| 60 | + $formatter = new PatternFormatter(); |
86 | 61 | $closure = fn() => ['Dynamic', 'Parameters']; |
87 | 62 | $value = ['%s %s from closure', $closure]; |
88 | | - $result = $this->formatter->format($value); |
| 63 | + $result = $formatter->format($value); |
89 | 64 |
|
90 | 65 | $this->assertSame(['Dynamic Parameters from closure'], $result); |
91 | 66 | } |
92 | 67 |
|
93 | | - public function testFormatWithPatternAndClosureReturningString(): void |
94 | | - { |
95 | | - $closure = fn() => ['SingleValue']; |
96 | | - $value = ['Value: %s', $closure]; |
97 | | - $result = $this->formatter->format($value); |
98 | | - |
99 | | - $this->assertSame(['Value: SingleValue'], $result); |
100 | | - } |
101 | | - |
102 | | - public function testFormatWithComplexPattern(): void |
103 | | - { |
104 | | - $value = ['User: %s (ID: %d, Active: %s)', ['John Doe', 123, 'Yes']]; |
105 | | - $result = $this->formatter->format($value); |
106 | | - |
107 | | - $this->assertSame(['User: John Doe (ID: 123, Active: Yes)'], $result); |
108 | | - } |
109 | | - |
110 | | - public function testFormatWithNumericPattern(): void |
111 | | - { |
112 | | - $value = ['%.2f%%', [85.678]]; |
113 | | - $result = $this->formatter->format($value); |
114 | | - |
115 | | - $this->assertSame(['85.68%'], $result); |
116 | | - } |
117 | | - |
118 | 68 | public function testFormatWithNonArrayThrowsException(): void |
119 | 69 | { |
120 | | - $this->expectException(BadMethodCallException::class); |
121 | | - $this->expectExceptionMessage('PatternFormatter expects an array value.'); |
| 70 | + $formatter = new PatternFormatter(); |
122 | 71 |
|
123 | | - $this->formatter->format('not an array'); |
124 | | - } |
125 | | - |
126 | | - public function testFormatWithNullThrowsException(): void |
127 | | - { |
128 | 72 | $this->expectException(BadMethodCallException::class); |
129 | 73 | $this->expectExceptionMessage('PatternFormatter expects an array value.'); |
130 | 74 |
|
131 | | - $this->formatter->format(null); |
| 75 | + $formatter->format('not an array'); |
132 | 76 | } |
133 | 77 |
|
134 | 78 | public function testFormatWithEmptyArrayThrowsException(): void |
135 | 79 | { |
| 80 | + $formatter = new PatternFormatter(); |
| 81 | + |
136 | 82 | $this->expectException(BadMethodCallException::class); |
137 | 83 | $this->expectExceptionMessage('PatternFormatter expects an array with one or two elements.'); |
138 | 84 |
|
139 | | - $this->formatter->format([]); |
| 85 | + $formatter->format([]); |
140 | 86 | } |
141 | 87 |
|
142 | 88 | public function testFormatWithThreeElementsThrowsException(): void |
143 | 89 | { |
144 | | - $this->expectException(BadMethodCallException::class); |
145 | | - $this->expectExceptionMessage('PatternFormatter expects an array with one or two elements.'); |
146 | | - |
147 | | - $this->formatter->format(['pattern', ['param1'], 'extra']); |
148 | | - } |
| 90 | + $formatter = new PatternFormatter(); |
149 | 91 |
|
150 | | - public function testFormatWithFourElementsThrowsException(): void |
151 | | - { |
152 | 92 | $this->expectException(BadMethodCallException::class); |
153 | 93 | $this->expectExceptionMessage('PatternFormatter expects an array with one or two elements.'); |
154 | 94 |
|
155 | | - $this->formatter->format(['pattern', ['param1'], 'extra', 'more']); |
| 95 | + $formatter->format(['pattern', ['param1'], 'extra']); |
156 | 96 | } |
157 | 97 |
|
158 | | - public function testFormatWithParametersMethod(): void |
| 98 | + public function testNormalizeParametersWithNonArrayConvertsToArray(): void |
159 | 99 | { |
160 | | - $value = ['Hello %s!', ['World']]; |
161 | | - $result = $this->formatter->formatWithParameters($value); |
| 100 | + $formatter = new PatternFormatter(); |
| 101 | + $result = $formatter->normalizeParameters('single_value'); |
162 | 102 |
|
163 | | - $this->assertSame(['Hello World!'], $result); |
| 103 | + $this->assertSame(['single_value'], $result); |
164 | 104 | } |
165 | 105 |
|
166 | | - public function testFormatWithParametersMethodAndClosure(): void |
| 106 | + public function testNormalizeParametersWithClosure(): void |
167 | 107 | { |
168 | | - $closure = fn() => ['Closure', 'Test']; |
169 | | - $value = ['%s %s', $closure]; |
170 | | - $result = $this->formatter->formatWithParameters($value); |
| 108 | + $formatter = new PatternFormatter(); |
| 109 | + $closure = fn() => ['test', 'values']; |
| 110 | + $result = $formatter->normalizeParameters($closure); |
171 | 111 |
|
172 | | - $this->assertSame(['Closure Test'], $result); |
| 112 | + $this->assertSame(['test', 'values'], $result); |
173 | 113 | } |
174 | 114 |
|
175 | | - public function testFormatWithOption(): void |
| 115 | + public function testNormalizeParametersWithMixedTypes(): void |
176 | 116 | { |
177 | | - $value = ['Test %s', ['value']]; |
178 | | - $result = $this->formatter->format($value, 'some_option'); |
| 117 | + $formatter = new PatternFormatter(); |
| 118 | + $parameters = [true, 42, 3.14, 'string', new \stdClass(), null]; |
| 119 | + $result = $formatter->normalizeParameters($parameters); |
179 | 120 |
|
180 | | - $this->assertSame(['Test value'], $result); |
| 121 | + $this->assertSame([true, 42, 3.14, 'string', null, null], $result); |
181 | 122 | } |
182 | 123 | } |
0 commit comments