diff --git a/src/ValidatorChain.php b/src/ValidatorChain.php index 9d0e70f80..0c8f1bded 100644 --- a/src/ValidatorChain.php +++ b/src/ValidatorChain.php @@ -262,7 +262,7 @@ public function isValid($value, $context = null) public function merge(ValidatorChain $validatorChain) { foreach ($validatorChain->validators->toArray(PriorityQueue::EXTR_BOTH) as $item) { - $this->attach($item['data'], $item['priority']); + $this->attach($item['data']['instance'], $item['data']['breakChainOnFailure'], $item['priority']); } return $this; @@ -299,6 +299,14 @@ public function __invoke($value) return $this->isValid($value); } + /** + * Deep clone handling + */ + public function __clone() + { + $this->validators = clone $this->validators; + } + /** * Prepare validator chain for serialization * diff --git a/test/ValidatorChainTest.php b/test/ValidatorChainTest.php index 93c1ca802..b7180e944 100644 --- a/test/ValidatorChainTest.php +++ b/test/ValidatorChainTest.php @@ -186,6 +186,42 @@ public function testPrependValidatorsAreExecutedAccordingToPriority() $this->assertArrayHasKey('error', $messages); } + /** + * @group 6386 + * @group 6496 + */ + public function testMergeValidatorChains() + { + $mergedValidatorChain = new ValidatorChain(); + + $mergedValidatorChain->attach($this->getValidatorTrue()); + $this->validator->attach($this->getValidatorTrue()); + + $this->validator->merge($mergedValidatorChain); + + $this->assertCount(2, $this->validator->getValidators()); + } + + /** + * @group 6386 + * @group 6496 + */ + public function testValidatorChainIsCloneable() + { + $this->validator->attach(new NotEmpty()); + + $this->assertCount(1, $this->validator->getValidators()); + + $clonedValidatorChain = clone $this->validator; + + $this->assertCount(1, $clonedValidatorChain->getValidators()); + + $clonedValidatorChain->attach(new NotEmpty()); + + $this->assertCount(1, $this->validator->getValidators()); + $this->assertCount(2, $clonedValidatorChain->getValidators()); + } + public function testCountGivesCountOfAttachedValidators() { $this->populateValidatorChain();