public function validate() { $errors = $this->validator->validate($this); if ($this->getDivisor() === "0") { $errors->add(new ConstraintViolation("divisor has to be a non zero value", array(), null, null, null)); } $errorMessage = ""; foreach ($errors as $error) { $errorMessage .= $error->getMessage() . PHP_EOL; } if (!empty($errorMessage)) { throw new BadRequestException($errorMessage); } }
/** * @expectedException Symfony\Component\HttpKernel\Exception\BadRequestHttpException * @expectedMessage expected exception. */ public function testValidationErrorsInStrictMode() { $param = $this->createMockedParam('foo', null, [], false, null, ['constraint']); list($fetcher, $method) = $this->getFetcherToCheckValidation($param); $errors = $this->getMock('Symfony\\Component\\Validator\\ConstraintViolationListInterface'); $errors->expects($this->once())->method('count')->willReturn(1); $this->validator->expects($this->once())->method('validate')->with('value', array('constraint'))->willReturn($errors); $this->violationFormatter->expects($this->once())->method('formatList')->with($param, $errors)->willReturn('expected exception.'); $method->invokeArgs($fetcher, array($param, 'value', true)); }