|
4 | 4 |
|
5 | 5 | namespace Patchlevel\Hydrator\Tests\Unit\Normalizer; |
6 | 6 |
|
| 7 | +use InvalidArgumentException; |
7 | 8 | use Patchlevel\Hydrator\Hydrator; |
8 | 9 | use Patchlevel\Hydrator\Normalizer\ArrayNormalizer; |
9 | 10 | use Patchlevel\Hydrator\Normalizer\HydratorAwareNormalizer; |
|
12 | 13 | use PHPUnit\Framework\Attributes\CoversClass; |
13 | 14 | use PHPUnit\Framework\TestCase; |
14 | 15 |
|
| 16 | +use function is_int; |
| 17 | + |
15 | 18 | #[CoversClass(ArrayNormalizer::class)] |
16 | 19 | final class ArrayNormalizerTest extends TestCase |
17 | 20 | { |
@@ -155,6 +158,83 @@ public function denormalize(mixed $value, array $context = []): mixed |
155 | 158 | $this->assertSame([$context, $context], $innerNormalizer->contexts); |
156 | 159 | } |
157 | 160 |
|
| 161 | + public function testNormalizeDoesNotMutateSourceArrayWithReferencedElement(): void |
| 162 | + { |
| 163 | + $innerNormalizer = new class implements Normalizer { |
| 164 | + /** @param array<string, mixed> $context */ |
| 165 | + public function normalize(mixed $value, array $context): int |
| 166 | + { |
| 167 | + if (!is_int($value)) { |
| 168 | + throw new InvalidArgumentException(); |
| 169 | + } |
| 170 | + |
| 171 | + return $value + 100; |
| 172 | + } |
| 173 | + |
| 174 | + /** @param array<string, mixed> $context */ |
| 175 | + public function denormalize(mixed $value, array $context): int |
| 176 | + { |
| 177 | + if (!is_int($value)) { |
| 178 | + throw new InvalidArgumentException(); |
| 179 | + } |
| 180 | + |
| 181 | + return $value - 100; |
| 182 | + } |
| 183 | + }; |
| 184 | + |
| 185 | + $source = [1, 2, 3]; |
| 186 | + |
| 187 | + // A leftover reference from a previous `foreach ($source as &$row)` without |
| 188 | + // unset() turns the last element into a PHP reference. Iterating the source |
| 189 | + // by reference inside the normalizer must not write back into it. |
| 190 | + // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach |
| 191 | + foreach ($source as &$row) { |
| 192 | + } |
| 193 | + |
| 194 | + $normalizer = new ArrayNormalizer($innerNormalizer); |
| 195 | + $result = $normalizer->normalize($source, []); |
| 196 | + |
| 197 | + self::assertSame([101, 102, 103], $result); |
| 198 | + self::assertSame([1, 2, 3], $source); |
| 199 | + } |
| 200 | + |
| 201 | + public function testDenormalizeDoesNotMutateSourceArrayWithReferencedElement(): void |
| 202 | + { |
| 203 | + $innerNormalizer = new class implements Normalizer { |
| 204 | + /** @param array<string, mixed> $context */ |
| 205 | + public function normalize(mixed $value, array $context): int |
| 206 | + { |
| 207 | + if (!is_int($value)) { |
| 208 | + throw new InvalidArgumentException(); |
| 209 | + } |
| 210 | + |
| 211 | + return $value + 100; |
| 212 | + } |
| 213 | + |
| 214 | + /** @param array<string, mixed> $context */ |
| 215 | + public function denormalize(mixed $value, array $context): int |
| 216 | + { |
| 217 | + if (!is_int($value)) { |
| 218 | + throw new InvalidArgumentException(); |
| 219 | + } |
| 220 | + |
| 221 | + return $value - 100; |
| 222 | + } |
| 223 | + }; |
| 224 | + |
| 225 | + $source = [101, 102, 103]; |
| 226 | + |
| 227 | + // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach |
| 228 | + foreach ($source as &$row) { |
| 229 | + } |
| 230 | + |
| 231 | + $normalizer = new ArrayNormalizer($innerNormalizer); |
| 232 | + $result = $normalizer->denormalize($source, []); |
| 233 | + |
| 234 | + self::assertSame([1, 2, 3], $result); |
| 235 | + self::assertSame([101, 102, 103], $source); |
| 236 | + } |
| 237 | + |
158 | 238 | public function testPassHydrator(): void |
159 | 239 | { |
160 | 240 | $hydrator = $this->createMock(Hydrator::class); |
|
0 commit comments