|
193 | 193 | expect($errors)->toBe(['Value must be a string']); |
194 | 194 | }); |
195 | 195 |
|
| 196 | +it('should clone validators without sharing pipeline state', function () { |
| 197 | + $original = Validator::isString() |
| 198 | + ->transform(fn ($v) => explode(',', trim($v))) // String → Array |
| 199 | + ->pipe('array_reverse') // Array operations use current type |
| 200 | + ->transform(fn ($v) => implode('-', $v)); // Array → String |
| 201 | + |
| 202 | + $clone = $original->clone(); |
| 203 | + |
| 204 | + $result = $clone->validate('a,b,c'); |
| 205 | + expect($result)->toBe('c-b-a'); |
| 206 | + |
| 207 | + $currentType = new ReflectionProperty($original, 'currentType'); |
| 208 | + $currentType->setAccessible(true); |
| 209 | + |
| 210 | + expect($currentType->getValue($original))->toBeNull(); // Original instance untouched |
| 211 | +}); |
| 212 | + |
| 213 | +it('should deep clone nested schemas for associative validators', function () { |
| 214 | + $original = Validator::isAssociative([ |
| 215 | + 'nickname' => Validator::isString(), |
| 216 | + ]); |
| 217 | + |
| 218 | + $clone = $original->clone(); |
| 219 | + |
| 220 | + $schemaProperty = new ReflectionProperty($clone, 'schema'); |
| 221 | + $schemaProperty->setAccessible(true); |
| 222 | + $cloneSchema = $schemaProperty->getValue($clone); |
| 223 | + |
| 224 | + $cloneSchema['nickname']->default('buddy'); |
| 225 | + |
| 226 | + $originalSchemaProperty = new ReflectionProperty($original, 'schema'); |
| 227 | + $originalSchemaProperty->setAccessible(true); |
| 228 | + $originalSchema = $originalSchemaProperty->getValue($original); |
| 229 | + |
| 230 | + expect($cloneSchema['nickname'])->not->toBe($originalSchema['nickname']); // No shared instances |
| 231 | + |
| 232 | + expect($original->validate([]))->toBe([]); |
| 233 | + expect($clone->validate([]))->toBe(['nickname' => 'buddy']); |
| 234 | +}); |
| 235 | + |
196 | 236 | it('should fail fast in single pipeline - first validation error stops execution', function () { |
197 | 237 | $validator = Validator::isString()->minLength(10)->maxLength(3)->email(); |
198 | 238 |
|
|
0 commit comments