|
12 | 12 | namespace Twig\Extra\Html\Tests; |
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase; |
| 15 | +use Twig\Error\RuntimeError; |
| 16 | +use Twig\Extra\Html\HtmlAttr\AttributeValueInterface; |
| 17 | +use Twig\Extra\Html\HtmlAttr\MergeableInterface; |
15 | 18 | use Twig\Extra\Html\HtmlExtension; |
16 | 19 |
|
17 | 20 | class HtmlAttrMergeTest extends TestCase |
18 | 21 | { |
19 | 22 | /** |
20 | 23 | * @dataProvider htmlAttrProvider |
21 | 24 | */ |
22 | | - public function testMerge(array $expected, array $inputs) |
| 25 | + public function testMerge(array $expected, array $inputs): void |
23 | 26 | { |
24 | 27 | $result = HtmlExtension::htmlAttrMerge(...$inputs); |
25 | 28 |
|
@@ -126,5 +129,94 @@ public static function htmlAttrProvider(): \Generator |
126 | 129 | ['style' => ['color' => 'red']], |
127 | 130 | ], |
128 | 131 | ]; |
| 132 | + |
| 133 | + // MergeableInterface |
| 134 | + yield 'MergeableInterface mergeInto is called when new value implements interface' => [ |
| 135 | + ['class' => 'merged: old + new'], |
| 136 | + [ |
| 137 | + ['class' => 'old'], |
| 138 | + ['class' => new MergeableStub('new')], |
| 139 | + ], |
| 140 | + ]; |
| 141 | + |
| 142 | + yield 'MergeableInterface appendFrom is called when existing value implements interface' => [ |
| 143 | + ['class' => 'appended: old + new'], |
| 144 | + [ |
| 145 | + ['class' => new MergeableStub('old')], |
| 146 | + ['class' => 'new'], |
| 147 | + ], |
| 148 | + ]; |
| 149 | + |
| 150 | + yield 'MergeableInterface mergeInto is called when both implement interface' => [ |
| 151 | + ['class' => 'merged: value1 + value2'], |
| 152 | + [ |
| 153 | + ['class' => new MergeableStub('value1')], |
| 154 | + ['class' => new MergeableStub('value2')], |
| 155 | + ], |
| 156 | + ]; |
| 157 | + |
| 158 | + yield 'MergeableInterface with array value' => [ |
| 159 | + ['class' => 'appended: base + extra1, extra2'], |
| 160 | + [ |
| 161 | + ['class' => new MergeableStub('base')], |
| 162 | + ['class' => ['extra1', 'extra2']], |
| 163 | + ], |
| 164 | + ]; |
| 165 | + |
| 166 | + // Scalar and object merging |
| 167 | + yield 'string replaces object' => [ |
| 168 | + ['value' => 'new-string'], |
| 169 | + [ |
| 170 | + ['value' => new \stdClass()], |
| 171 | + ['value' => 'new-string'], |
| 172 | + ], |
| 173 | + ]; |
| 174 | + |
| 175 | + yield 'object replaces string' => [ |
| 176 | + ['value' => new \stdClass()], |
| 177 | + [ |
| 178 | + ['value' => 'old-string'], |
| 179 | + ['value' => new \stdClass()], |
| 180 | + ], |
| 181 | + ]; |
| 182 | + } |
| 183 | + |
| 184 | + public function testIncompatibleValuesMergeThrowsException(): void |
| 185 | + { |
| 186 | + $this->expectException(RuntimeError::class); |
| 187 | + $this->expectExceptionMessage('Cannot merge incompatible values for key "test"'); |
| 188 | + |
| 189 | + HtmlExtension::htmlAttrMerge( |
| 190 | + ['test' => ['array']], |
| 191 | + ['test' => 'scalar'] |
| 192 | + ); |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +class MergeableStub implements MergeableInterface |
| 197 | +{ |
| 198 | + public function __construct(private readonly mixed $value) |
| 199 | + { |
| 200 | + } |
| 201 | + |
| 202 | + public function mergeInto(mixed $previous): self |
| 203 | + { |
| 204 | + $previousValue = $previous instanceof self ? $previous->value : $previous; |
| 205 | + return new self("merged: {$previousValue} + {$this->value}"); |
| 206 | + } |
| 207 | + |
| 208 | + public function appendFrom(mixed $newValue): self |
| 209 | + { |
| 210 | + if (is_array($newValue)) { |
| 211 | + $newValue = implode(', ', $newValue); |
| 212 | + } elseif ($newValue instanceof self) { |
| 213 | + $newValue = $newValue->value; |
| 214 | + } |
| 215 | + return new self("appended: {$this->value} + {$newValue}"); |
| 216 | + } |
| 217 | + |
| 218 | + public function __toString(): string |
| 219 | + { |
| 220 | + return (string) $this->value; |
129 | 221 | } |
130 | 222 | } |
0 commit comments