/** * Execute the validation stack and fail on first * * @param $value * @param null $key * @return bool|ValidationError|null */ protected function validate($value, $key = null) { $executor = clone $this; if (is_null($value) && $executor->optional) { if (is_null($executor->defaultValue)) { return null; } else { $value = $this->defaultValue; } } try { reset($executor->validationStack); do { /** @var callable $validator */ $validator = current($executor->validationStack); $retVal = $validator($value, $key); $value = $retVal === null ? $value : $retVal; } while (next($executor->validationStack)); if ($executor->toBool) { return true; } return $value; } catch (ValidationException $validationException) { if ($executor->toBool) { return false; } return ValidationError::fromException($validationException); } }
public function testFromException() { $valException = $this->getMock(ValidationException::class, [], ['key', 'message']); $valException->expects($this->once())->method('getKey')->willReturn('key'); // Interesting that it doesn't keep track of the times // Called for an internal method. // TODO: figure this out $valException->expects($this->any())->method('getMessage')->willReturn('message'); $valError = ValidationError::fromException($valException); $this->assertInstanceOf(ValidationError::class, $valError); $this->assertEquals('key', $valError->getKey()); $this->assertEquals('message', $valError->getMessage()); }