diff --git a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php index 6d459982bd..b36c458e4f 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php @@ -165,7 +165,10 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($entity2, $constraint); - $this->assertViolation('myMessage', array(), 'property.path.name', 'Foo'); + $this->buildViolation('myMessage') + ->atPath('property.path.name') + ->setInvalidValue('Foo') + ->assertRaised(); } public function testValidateCustomErrorPath() @@ -185,7 +188,10 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($entity2, $constraint); - $this->assertViolation('myMessage', array(), 'property.path.bar', 'Foo'); + $this->buildViolation('myMessage') + ->atPath('property.path.bar') + ->setInvalidValue('Foo') + ->assertRaised(); } public function testValidateUniquenessWithNull() @@ -233,7 +239,10 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($entity2, $constraint); - $this->assertViolation('myMessage', array(), 'property.path.name', 'Foo'); + $this->buildViolation('myMessage') + ->atPath('property.path.name') + ->setInvalidValue('Foo') + ->assertRaised(); } public function testValidateUniquenessAfterConsideringMultipleQueryResults() @@ -352,7 +361,10 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($associated2, $constraint); - $this->assertViolation('myMessage', array(), 'property.path.single', 1); + $this->buildViolation('myMessage') + ->atPath('property.path.single') + ->setInvalidValue(1) + ->assertRaised(); } public function testAssociatedEntityWithNull() diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php index 371fe927b8..72b322bca3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php @@ -107,7 +107,7 @@ EOF } if (!$matches) { - $output->writeln('None of the routes match'); + $output->writeln(sprintf('None of the routes match the path "%s"', $input->getArgument('path_info'))); return 1; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TestCaseMethod.tpl b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TestCaseMethod.tpl deleted file mode 100644 index 52790018fe..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TestCaseMethod.tpl +++ /dev/null @@ -1,47 +0,0 @@ -collectRawCodeCoverageInformation({collectCodeCoverageInformation}); - - $test = new {className}('{methodName}', unserialize('{data}'), '{dataName}'); - $test->setDependencyInput(unserialize('{dependencyInput}')); - $test->setInIsolation(TRUE); - - ob_end_clean(); - ob_start(); - $test->run($result); - $output = ob_get_clean(); - - print serialize( - array( - 'testResult' => $test->getResult(), - 'numAssertions' => $test->getNumAssertions(), - 'result' => $result, - 'output' => $output - ) - ); - - ob_start(); -} - -{globals} - -if (isset($GLOBALS['__PHPUNIT_BOOTSTRAP'])) { - require_once $GLOBALS['__PHPUNIT_BOOTSTRAP']; - unset($GLOBALS['__PHPUNIT_BOOTSTRAP']); -} - -{constants} - -__phpunit_run_isolated_test(); -ob_end_clean(); -?> diff --git a/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php b/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php index e0acb56564..33a5507c7e 100644 --- a/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php +++ b/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php @@ -39,20 +39,19 @@ class ExceptionController * @param Request $request The request * @param FlattenException $exception A FlattenException instance * @param DebugLoggerInterface $logger A DebugLoggerInterface instance - * @param string $_format The format to use for rendering (html, xml, ...) * * @return Response * * @throws \InvalidArgumentException When the exception template does not exist */ - public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null, $_format = 'html') + public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) { $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); $code = $exception->getStatusCode(); return new Response($this->twig->render( - $this->findTemplate($request, $_format, $code, $this->debug), + $this->findTemplate($request, $request->getRequestFormat(), $code, $this->debug), array( 'status_code' => $code, 'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar.html.twig index bd76510a89..1170b9c4b5 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar.html.twig @@ -1,6 +1,6 @@ {% if 'normal' != position %} -
+
{% endif %} -
+
{% for name, template in templates %} {{ template.renderblock('toolbar', { 'collector': profile.getcollector(name), diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index eca9d49835..c9b7534e94 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -213,16 +213,6 @@ class Command */ public function run(InputInterface $input, OutputInterface $output) { - if (null !== $this->processTitle) { - if (function_exists('cli_set_process_title')) { - cli_set_process_title($this->processTitle); - } elseif (function_exists('setproctitle')) { - setproctitle($this->processTitle); - } elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) { - $output->writeln('Install the proctitle PECL to be able to change the process title.'); - } - } - // force the creation of the synopsis before the merge with the app definition $this->getSynopsis(); @@ -240,6 +230,16 @@ class Command $this->initialize($input, $output); + if (null !== $this->processTitle) { + if (function_exists('cli_set_process_title')) { + cli_set_process_title($this->processTitle); + } elseif (function_exists('setproctitle')) { + setproctitle($this->processTitle); + } elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) { + $output->writeln('Install the proctitle PECL to be able to change the process title.'); + } + } + if ($input->isInteractive()) { $this->interact($input, $output); } diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php index 924e1263a7..85e3226a00 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php @@ -56,8 +56,7 @@ class FormType extends BaseType ->setDataLocked($isDataOptionSet) ->setDataMapper($options['compound'] ? new PropertyPathMapper($this->propertyAccessor) : null) ->setMethod($options['method']) - ->setAction($options['action']) - ; + ->setAction($options['action']); if ($options['trim']) { $builder->addEventSubscriber(new TrimListener()); @@ -187,26 +186,27 @@ class FormType extends BaseType }; $resolver->setDefaults(array( - 'data_class' => $dataClass, - 'empty_data' => $emptyData, - 'trim' => true, - 'required' => true, - 'read_only' => false, - 'max_length' => null, - 'pattern' => null, - 'property_path' => null, - 'mapped' => true, - 'by_reference' => true, - 'error_bubbling' => $errorBubbling, - 'label_attr' => array(), - 'virtual' => null, - 'inherit_data' => $inheritData, - 'compound' => true, - 'method' => 'POST', + 'data_class' => $dataClass, + 'empty_data' => $emptyData, + 'trim' => true, + 'required' => true, + 'read_only' => false, + 'max_length' => null, + 'pattern' => null, + 'property_path' => null, + 'mapped' => true, + 'by_reference' => true, + 'error_bubbling' => $errorBubbling, + 'label_attr' => array(), + 'virtual' => null, + 'inherit_data' => $inheritData, + 'compound' => true, + 'method' => 'POST', // According to RFC 2396 (http://www.ietf.org/rfc/rfc2396.txt) // section 4.2., empty URIs are considered same-document references - 'action' => '', - 'attr' => $defaultAttr, + 'action' => '', + 'attr' => $defaultAttr, + 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', )); $resolver->setAllowedTypes(array( diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index 2094699481..03805fef52 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -12,8 +12,10 @@ namespace Symfony\Component\Form\Extension\HttpFoundation; use Symfony\Component\Form\Exception\UnexpectedTypeException; +use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\RequestHandlerInterface; +use Symfony\Component\Form\Util\ServerParams; use Symfony\Component\HttpFoundation\Request; /** @@ -24,6 +26,19 @@ use Symfony\Component\HttpFoundation\Request; */ class HttpFoundationRequestHandler implements RequestHandlerInterface { + /** + * @var ServerParams + */ + private $serverParams; + + /** + * {@inheritdoc} + */ + public function __construct(ServerParams $serverParams = null) + { + $this->serverParams = $serverParams ?: new ServerParams(); + } + /** * {@inheritdoc} */ @@ -53,6 +68,25 @@ class HttpFoundationRequestHandler implements RequestHandlerInterface $data = $request->query->get($name); } } else { + // Mark the form with an error if the uploaded size was too large + // This is done here and not in FormValidator because $_POST is + // empty when that error occurs. Hence the form is never submitted. + $contentLength = $this->serverParams->getContentLength(); + $maxContentLength = $this->serverParams->getPostMaxSize(); + + if (!empty($maxContentLength) && $contentLength > $maxContentLength) { + // Submit the form, but don't clear the default values + $form->submit(null, false); + + $form->addError(new FormError( + $form->getConfig()->getOption('post_max_size_message'), + null, + array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize()) + )); + + return; + } + if ('' === $name) { $params = $request->request->all(); $files = $request->files->all(); diff --git a/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php b/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php index c5bc4a2211..1673bcb188 100644 --- a/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php +++ b/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Form\Extension\Validator\Constraints; use Symfony\Component\Form\FormInterface; -use Symfony\Component\Form\Extension\Validator\Util\ServerParams; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Context\ExecutionContextInterface; @@ -23,22 +22,6 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; */ class FormValidator extends ConstraintValidator { - /** - * @var ServerParams - */ - private $serverParams; - - /** - * Creates a validator with the given server parameters. - * - * @param ServerParams $params The server parameters. Default - * parameters are created if null. - */ - public function __construct(ServerParams $params = null) - { - $this->serverParams = $params ?: new ServerParams(); - } - /** * {@inheritdoc} */ @@ -151,29 +134,6 @@ class FormValidator extends ConstraintValidator ); } } - - // Mark the form with an error if the uploaded size was too large - $length = $this->serverParams->getContentLength(); - - if ($form->isRoot() && null !== $length) { - $max = $this->serverParams->getPostMaxSize(); - - if (!empty($max) && $length > $max) { - if ($this->context instanceof ExecutionContextInterface) { - $this->context->buildViolation($config->getOption('post_max_size_message')) - ->setParameter('{{ max }}', $this->serverParams->getNormalizedIniPostMaxSize()) - ->setInvalidValue($length) - ->addViolation(); - } else { - // 2.4 API - $this->context->addViolation( - $config->getOption('post_max_size_message'), - array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize()), - $length - ); - } - } - } } /** diff --git a/src/Symfony/Component/Form/Extension/Validator/Type/FormTypeValidatorExtension.php b/src/Symfony/Component/Form/Extension/Validator/Type/FormTypeValidatorExtension.php index 87abd9320a..21c3adc62e 100644 --- a/src/Symfony/Component/Form/Extension/Validator/Type/FormTypeValidatorExtension.php +++ b/src/Symfony/Component/Form/Extension/Validator/Type/FormTypeValidatorExtension.php @@ -75,7 +75,6 @@ class FormTypeValidatorExtension extends BaseValidatorExtension 'invalid_message_parameters' => array(), 'allow_extra_fields' => false, 'extra_fields_message' => 'This form should not contain extra fields.', - 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', )); $resolver->setNormalizers(array( diff --git a/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php b/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php index 9dd045235c..c058d60cae 100644 --- a/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php +++ b/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php @@ -11,75 +11,9 @@ namespace Symfony\Component\Form\Extension\Validator\Util; -use Symfony\Component\HttpFoundation\RequestStack; - /** * @author Bernhard Schussek */ -class ServerParams +class ServerParams extends \Symfony\Component\Form\Util\ServerParams { - private $requestStack; - - public function __construct(RequestStack $requestStack = null) - { - $this->requestStack = $requestStack; - } - - /** - * Returns maximum post size in bytes. - * - * @return null|int The maximum post size in bytes - */ - public function getPostMaxSize() - { - $iniMax = strtolower($this->getNormalizedIniPostMaxSize()); - - if ('' === $iniMax) { - return; - } - - $max = ltrim($iniMax, '+'); - if (0 === strpos($max, '0x')) { - $max = intval($max, 16); - } elseif (0 === strpos($max, '0')) { - $max = intval($max, 8); - } else { - $max = intval($max); - } - - switch (substr($iniMax, -1)) { - case 't': $max *= 1024; - case 'g': $max *= 1024; - case 'm': $max *= 1024; - case 'k': $max *= 1024; - } - - return $max; - } - - /** - * Returns the normalized "post_max_size" ini setting. - * - * @return string - */ - public function getNormalizedIniPostMaxSize() - { - return strtoupper(trim(ini_get('post_max_size'))); - } - - /** - * Returns the content length of the request. - * - * @return mixed The request content length. - */ - public function getContentLength() - { - if (null !== $this->requestStack && null !== $request = $this->requestStack->getCurrentRequest()) { - return $request->server->get('CONTENT_LENGTH'); - } - - return isset($_SERVER['CONTENT_LENGTH']) - ? (int) $_SERVER['CONTENT_LENGTH'] - : null; - } } diff --git a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php index cb6583686a..f0b0ac5c36 100644 --- a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php +++ b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php @@ -277,10 +277,10 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface } } } + } - if (null !== $defaultValue) { - $guesses[] = new ValueGuess($defaultValue, Guess::LOW_CONFIDENCE); - } + if (null !== $defaultValue) { + $guesses[] = new ValueGuess($defaultValue, Guess::LOW_CONFIDENCE); } return Guess::getBestGuess($guesses); diff --git a/src/Symfony/Component/Form/NativeRequestHandler.php b/src/Symfony/Component/Form/NativeRequestHandler.php index fefe546af8..9df9066886 100644 --- a/src/Symfony/Component/Form/NativeRequestHandler.php +++ b/src/Symfony/Component/Form/NativeRequestHandler.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Form; use Symfony\Component\Form\Exception\UnexpectedTypeException; +use Symfony\Component\Form\Util\ServerParams; /** * A request handler using PHP's super globals $_GET, $_POST and $_SERVER. @@ -20,6 +21,19 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException; */ class NativeRequestHandler implements RequestHandlerInterface { + /** + * @var ServerParams + */ + private $serverParams; + + /** + * {@inheritdoc} + */ + public function __construct(ServerParams $params = null) + { + $this->serverParams = $params ?: new ServerParams(); + } + /** * The allowed keys of the $_FILES array. * @@ -62,6 +76,25 @@ class NativeRequestHandler implements RequestHandlerInterface $data = $_GET[$name]; } } else { + // Mark the form with an error if the uploaded size was too large + // This is done here and not in FormValidator because $_POST is + // empty when that error occurs. Hence the form is never submitted. + $contentLength = $this->serverParams->getContentLength(); + $maxContentLength = $this->serverParams->getPostMaxSize(); + + if (!empty($maxContentLength) && $contentLength > $maxContentLength) { + // Submit the form, but don't clear the default values + $form->submit(null, false); + + $form->addError(new FormError( + $form->getConfig()->getOption('post_max_size_message'), + null, + array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize()) + )); + + return; + } + $fixedFiles = array(); foreach ($_FILES as $name => $file) { $fixedFiles[$name] = self::stripEmptyFiles(self::fixPhpFilesArray($file)); diff --git a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php index fbba16ba17..3546d6a4bf 100644 --- a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php @@ -11,21 +11,38 @@ namespace Symfony\Component\Form\Tests; +use Symfony\Component\Form\FormError; +use Symfony\Component\Form\FormFactory; +use Symfony\Component\Form\Forms; +use Symfony\Component\Form\RequestHandlerInterface; + /** * @author Bernhard Schussek */ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase { /** - * @var \Symfony\Component\Form\RequestHandlerInterface + * @var RequestHandlerInterface */ protected $requestHandler; + /** + * @var FormFactory + */ + protected $factory; + protected $request; + protected $serverParams; + protected function setUp() { + $this->serverParams = $this->getMock( + 'Symfony\Component\Form\Util\ServerParams', + array('getNormalizedIniPostMaxSize', 'getContentLength') + ); $this->requestHandler = $this->getRequestHandler(); + $this->factory = Forms::createFormFactoryBuilder()->getFormFactory(); $this->request = null; } @@ -249,6 +266,50 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase $this->requestHandler->handleRequest($form, $this->request); } + /** + * @dataProvider getPostMaxSizeFixtures + */ + public function testAddFormErrorIfPostMaxSizeExceeded($contentLength, $iniMax, $shouldFail, array $errorParams = array()) + { + $this->serverParams->expects($this->once()) + ->method('getContentLength') + ->will($this->returnValue($contentLength)); + $this->serverParams->expects($this->any()) + ->method('getNormalizedIniPostMaxSize') + ->will($this->returnValue($iniMax)); + + $options = array('post_max_size_message' => 'Max {{ max }}!'); + $form = $this->factory->createNamed('name', 'text', null, $options); + $this->setRequestData('POST', array(), array()); + + $this->requestHandler->handleRequest($form, $this->request); + + if ($shouldFail) { + $errors = array(new FormError($options['post_max_size_message'], null, $errorParams)); + + $this->assertEquals($errors, iterator_to_array($form->getErrors())); + $this->assertTrue($form->isSubmitted()); + } else { + $this->assertCount(0, $form->getErrors()); + $this->assertFalse($form->isSubmitted()); + } + } + + public function getPostMaxSizeFixtures() + { + return array( + array(pow(1024, 3) + 1, '1G', true, array('{{ max }}' => '1G')), + array(pow(1024, 3), '1G', false), + array(pow(1024, 2) + 1, '1M', true, array('{{ max }}' => '1M')), + array(pow(1024, 2), '1M', false), + array(1024 + 1, '1K', true, array('{{ max }}' => '1K')), + array(1024, '1K', false), + array(null, '1K', false), + array(1024, '', false), + array(1024, 0, false), + ); + } + abstract protected function setRequestData($method, $data, $files = array()); abstract protected function getRequestHandler(); diff --git a/src/Symfony/Component/Form/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php index cf5d63d90e..dcd26891c1 100644 --- a/src/Symfony/Component/Form/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php @@ -43,7 +43,7 @@ class HttpFoundationRequestHandlerTest extends AbstractRequestHandlerTest protected function getRequestHandler() { - return new HttpFoundationRequestHandler(); + return new HttpFoundationRequestHandler($this->serverParams); } protected function getMockFile() diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php index f2d52e04c8..99c5e584c1 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php @@ -225,10 +225,12 @@ class FormValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($form, new Form()); - $this->assertViolation('invalid_message_key', array( - '{{ value }}' => 'foo', - '{{ foo }}' => 'bar', - ), 'property.path', 'foo', null, Form::ERR_INVALID); + $this->buildViolation('invalid_message_key') + ->setParameter('{{ value }}', 'foo') + ->setParameter('{{ foo }}', 'bar') + ->setInvalidValue('foo') + ->setCode(Form::ERR_INVALID) + ->assertRaised(); } public function testAddInvalidErrorEvenIfNoValidationGroups() @@ -257,10 +259,12 @@ class FormValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($form, new Form()); - $this->assertViolation('invalid_message_key', array( - '{{ value }}' => 'foo', - '{{ foo }}' => 'bar', - ), 'property.path', 'foo', null, Form::ERR_INVALID); + $this->buildViolation('invalid_message_key') + ->setParameter('{{ value }}', 'foo') + ->setParameter('{{ foo }}', 'bar') + ->setInvalidValue('foo') + ->setCode(Form::ERR_INVALID) + ->assertRaised(); } public function testDontValidateConstraintsIfNotSynchronized() @@ -289,9 +293,11 @@ class FormValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($form, new Form()); - $this->assertViolation('invalid_message_key', array( - '{{ value }}' => 'foo', - ), 'property.path','foo', null, Form::ERR_INVALID); + $this->buildViolation('invalid_message_key') + ->setParameter('{{ value }}', 'foo') + ->setInvalidValue('foo') + ->setCode(Form::ERR_INVALID) + ->assertRaised(); } // https://github.com/symfony/symfony/issues/4359 @@ -570,69 +576,6 @@ class FormValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($form, new Form()); } - /** - * @dataProvider getPostMaxSizeFixtures - */ - public function testPostMaxSizeViolation($contentLength, $iniMax, $nbViolation, array $params = array()) - { - $this->serverParams->expects($this->once()) - ->method('getContentLength') - ->will($this->returnValue($contentLength)); - $this->serverParams->expects($this->any()) - ->method('getNormalizedIniPostMaxSize') - ->will($this->returnValue($iniMax)); - - $options = array('post_max_size_message' => 'Max {{ max }}!'); - $form = $this->getBuilder('name', null, $options)->getForm(); - - $this->validator->validate($form, new Form()); - - $violations = array(); - - for ($i = 0; $i < $nbViolation; ++$i) { - $violations[] = $this->createViolation($options['post_max_size_message'], $params, 'property.path', $contentLength); - } - - $this->assertViolations($violations); - } - - public function getPostMaxSizeFixtures() - { - return array( - array(pow(1024, 3) + 1, '1G', 1, array('{{ max }}' => '1G')), - array(pow(1024, 3), '1G', 0), - array(pow(1024, 2) + 1, '1M', 1, array('{{ max }}' => '1M')), - array(pow(1024, 2), '1M', 0), - array(1024 + 1, '1K', 1, array('{{ max }}' => '1K')), - array(1024, '1K', 0), - array(null, '1K', 0), - array(1024, '', 0), - array(1024, 0, 0), - ); - } - - public function testNoViolationIfNotRoot() - { - $this->serverParams->expects($this->once()) - ->method('getContentLength') - ->will($this->returnValue(1025)); - $this->serverParams->expects($this->never()) - ->method('getNormalizedIniPostMaxSize'); - - $parent = $this->getBuilder() - ->setCompound(true) - ->setDataMapper($this->getDataMapper()) - ->getForm(); - $form = $this->getForm(); - $parent->add($form); - - $this->expectNoValidate(); - - $this->validator->validate($form, new Form()); - - $this->assertNoViolation(); - } - /** * Access has to be public, as this method is called via callback array * in {@link testValidateFormDataCanHandleCallbackValidationGroups()} diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php index 090abf4486..0e3934bc9d 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php @@ -13,32 +13,91 @@ namespace Symfony\Component\Form\Tests\Extension\Validator; use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser; use Symfony\Component\Form\Guess\Guess; +use Symfony\Component\Form\Guess\ValueGuess; +use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\Length; +use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Component\Validator\Constraints\NotNull; +use Symfony\Component\Validator\Constraints\Range; +use Symfony\Component\Validator\Constraints\True; use Symfony\Component\Validator\Constraints\Type; +use Symfony\Component\Validator\Mapping\ClassMetadata; /** -* @author franek -*/ + * @author franek + * @author Bernhard Schussek + */ class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase { - private $typeGuesser; + const TEST_CLASS = 'Symfony\Component\Form\Tests\Extension\Validator\ValidatorTypeGuesserTest_TestClass'; - public function setUp() + const TEST_PROPERTY = 'property'; + + /** + * @var ValidatorTypeGuesser + */ + private $guesser; + + /** + * @var ClassMetadata + */ + private $metadata; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $metadataFactory; + + protected function setUp() { if (!class_exists('Symfony\Component\Validator\Constraint')) { $this->markTestSkipped('The "Validator" component is not available'); } - $metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface'); + $this->metadata = new ClassMetadata(self::TEST_CLASS); + $this->metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface'); + $this->metadataFactory->expects($this->any()) + ->method('getMetadataFor') + ->with(self::TEST_CLASS) + ->will($this->returnValue($this->metadata)); + $this->guesser = new ValidatorTypeGuesser($this->metadataFactory); + } - $this->typeGuesser = new ValidatorTypeGuesser($metadataFactory); + public function guessRequiredProvider() + { + return array( + array(new NotNull(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)), + array(new NotBlank(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)), + array(new True(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)), + array(new Length(10), new ValueGuess(false, Guess::LOW_CONFIDENCE)), + array(new Range(array('min' => 1, 'max' => 20)), new ValueGuess(false, Guess::LOW_CONFIDENCE)), + ); + } + + /** + * @dataProvider guessRequiredProvider + */ + public function testGuessRequired($constraint, $guess) + { + // add distracting constraint + $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, new Email()); + + // add constraint under test + $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, $constraint); + + $this->assertEquals($guess, $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY)); + } + + public function testGuessRequiredReturnsFalseForUnmappedProperties() + { + $this->assertEquals(new ValueGuess(false, Guess::LOW_CONFIDENCE), $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY)); } public function testGuessMaxLengthForConstraintWithMaxValue() { $constraint = new Length(array('max' => '2')); - $result = $this->typeGuesser->guessMaxLengthForConstraint($constraint); + $result = $this->guesser->guessMaxLengthForConstraint($constraint); $this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result); $this->assertEquals(2, $result->getValue()); $this->assertEquals(Guess::HIGH_CONFIDENCE, $result->getConfidence()); @@ -48,24 +107,11 @@ class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase { $constraint = new Length(array('min' => '2')); - $result = $this->typeGuesser->guessMaxLengthForConstraint($constraint); + $result = $this->guesser->guessMaxLengthForConstraint($constraint); $this->assertNull($result); } - /** -* @dataProvider dataProviderTestGuessMaxLengthForConstraintWithType -*/ - public function testGuessMaxLengthForConstraintWithType($type) - { - $constraint = new Type($type); - - $result = $this->typeGuesser->guessMaxLengthForConstraint($constraint); - $this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result); - $this->assertEquals(null, $result->getValue()); - $this->assertEquals(Guess::MEDIUM_CONFIDENCE, $result->getConfidence()); - } - - public static function dataProviderTestGuessMaxLengthForConstraintWithType() + public function maxLengthTypeProvider() { return array ( array('double'), @@ -74,4 +120,22 @@ class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase array('real'), ); } + + /** + * @dataProvider maxLengthTypeProvider + */ + public function testGuessMaxLengthForConstraintWithType($type) + { + $constraint = new Type($type); + + $result = $this->guesser->guessMaxLengthForConstraint($constraint); + $this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result); + $this->assertNull($result->getValue()); + $this->assertEquals(Guess::MEDIUM_CONFIDENCE, $result->getConfidence()); + } +} + +class ValidatorTypeGuesserTest_TestClass +{ + private $property; } diff --git a/src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php index 02b0a4ed74..eac767f8c8 100644 --- a/src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php @@ -203,7 +203,7 @@ class NativeRequestHandlerTest extends AbstractRequestHandlerTest protected function getRequestHandler() { - return new NativeRequestHandler(); + return new NativeRequestHandler($this->serverParams); } protected function getMockFile() diff --git a/src/Symfony/Component/Form/Util/ServerParams.php b/src/Symfony/Component/Form/Util/ServerParams.php new file mode 100644 index 0000000000..82f352895b --- /dev/null +++ b/src/Symfony/Component/Form/Util/ServerParams.php @@ -0,0 +1,85 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Util; + +use Symfony\Component\HttpFoundation\RequestStack; + +/** + * @author Bernhard Schussek + */ +class ServerParams +{ + private $requestStack; + + public function __construct(RequestStack $requestStack = null) + { + $this->requestStack = $requestStack; + } + + /** + * Returns maximum post size in bytes. + * + * @return null|int The maximum post size in bytes + */ + public function getPostMaxSize() + { + $iniMax = strtolower($this->getNormalizedIniPostMaxSize()); + + if ('' === $iniMax) { + return; + } + + $max = ltrim($iniMax, '+'); + if (0 === strpos($max, '0x')) { + $max = intval($max, 16); + } elseif (0 === strpos($max, '0')) { + $max = intval($max, 8); + } else { + $max = intval($max); + } + + switch (substr($iniMax, -1)) { + case 't': $max *= 1024; + case 'g': $max *= 1024; + case 'm': $max *= 1024; + case 'k': $max *= 1024; + } + + return $max; + } + + /** + * Returns the normalized "post_max_size" ini setting. + * + * @return string + */ + public function getNormalizedIniPostMaxSize() + { + return strtoupper(trim(ini_get('post_max_size'))); + } + + /** + * Returns the content length of the request. + * + * @return mixed The request content length. + */ + public function getContentLength() + { + if (null !== $this->requestStack && null !== $request = $this->requestStack->getCurrentRequest()) { + return $request->server->get('CONTENT_LENGTH'); + } + + return isset($_SERVER['CONTENT_LENGTH']) + ? (int) $_SERVER['CONTENT_LENGTH'] + : null; + } +} diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index f183db43f1..fbb86f6edd 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -1077,12 +1077,16 @@ class Response return false; } - $lastModified = $request->headers->get('If-Modified-Since'); - $notModified = false; + $notModified = false; + $lastModified = $this->headers->get('Last-Modified'); + $modifiedSince = $request->headers->get('If-Modified-Since'); + if ($etags = $request->getEtags()) { - $notModified = (in_array($this->getEtag(), $etags) || in_array('*', $etags)) && (!$lastModified || $this->headers->get('Last-Modified') == $lastModified); - } elseif ($lastModified) { - $notModified = $lastModified == $this->headers->get('Last-Modified'); + $notModified = in_array($this->getEtag(), $etags) || in_array('*', $etags); + } + + if ($modifiedSince && $lastModified) { + $notModified = strtotime($modifiedSince) >= strtotime($lastModified) && (!$etags || $notModified); } if ($notModified) { diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php index 18698aef29..5b7de70bd8 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -141,16 +141,24 @@ class ResponseTest extends ResponseTestCase public function testIsNotModifiedLastModified() { + $before = 'Sun, 25 Aug 2013 18:32:31 GMT'; $modified = 'Sun, 25 Aug 2013 18:33:31 GMT'; + $after = 'Sun, 25 Aug 2013 19:33:31 GMT'; $request = new Request(); $request->headers->set('If-Modified-Since', $modified); $response = new Response(); - $response->headers->set('Last-Modified', $modified); + $response->headers->set('Last-Modified', $modified); $this->assertTrue($response->isNotModified($request)); + $response->headers->set('Last-Modified', $before); + $this->assertTrue($response->isNotModified($request)); + + $response->headers->set('Last-Modified', $after); + $this->assertFalse($response->isNotModified($request)); + $response->headers->set('Last-Modified', ''); $this->assertFalse($response->isNotModified($request)); } @@ -175,6 +183,50 @@ class ResponseTest extends ResponseTestCase $this->assertFalse($response->isNotModified($request)); } + public function testIsNotModifiedLastModifiedAndEtag() + { + $before = 'Sun, 25 Aug 2013 18:32:31 GMT'; + $modified = 'Sun, 25 Aug 2013 18:33:31 GMT'; + $after = 'Sun, 25 Aug 2013 19:33:31 GMT'; + $etag = 'randomly_generated_etag'; + + $request = new Request(); + $request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree')); + $request->headers->set('If-Modified-Since', $modified); + + $response = new Response(); + + $response->headers->set('ETag', $etag); + $response->headers->set('Last-Modified', $after); + $this->assertFalse($response->isNotModified($request)); + + $response->headers->set('ETag', 'non-existent-etag'); + $response->headers->set('Last-Modified', $before); + $this->assertFalse($response->isNotModified($request)); + + $response->headers->set('ETag', $etag); + $response->headers->set('Last-Modified', $modified); + $this->assertTrue($response->isNotModified($request)); + } + + public function testIsNotModifiedIfModifiedSinceAndEtagWithoutLastModified() + { + $modified = 'Sun, 25 Aug 2013 18:33:31 GMT'; + $etag = 'randomly_generated_etag'; + + $request = new Request(); + $request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree')); + $request->headers->set('If-Modified-Since', $modified); + + $response = new Response(); + + $response->headers->set('ETag', $etag); + $this->assertTrue($response->isNotModified($request)); + + $response->headers->set('ETag', 'non-existent-etag'); + $this->assertFalse($response->isNotModified($request)); + } + public function testIsValidateable() { $response = new Response('', 200, array('Last-Modified' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822))); diff --git a/src/Symfony/Component/Security/Core/Authentication/Provider/RememberMeAuthenticationProvider.php b/src/Symfony/Component/Security/Core/Authentication/Provider/RememberMeAuthenticationProvider.php index 234bddbc43..82be1d13e9 100644 --- a/src/Symfony/Component/Security/Core/Authentication/Provider/RememberMeAuthenticationProvider.php +++ b/src/Symfony/Component/Security/Core/Authentication/Provider/RememberMeAuthenticationProvider.php @@ -50,7 +50,7 @@ class RememberMeAuthenticationProvider implements AuthenticationProviderInterfac } $user = $token->getUser(); - $this->userChecker->checkPostAuth($user); + $this->userChecker->checkPreAuth($user); $authenticatedToken = new RememberMeToken($user, $this->providerKey, $this->key); $authenticatedToken->setAttributes($token->getAttributes()); diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php index d278ba45fe..a6fff4bfa9 100644 --- a/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Security\Core\Tests\Authentication\Provider; use Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider; -use Symfony\Component\Security\Core\Exception\AccountExpiredException; +use Symfony\Component\Security\Core\Exception\DisabledException; use Symfony\Component\Security\Core\Role\Role; class RememberMeAuthenticationProviderTest extends \PHPUnit_Framework_TestCase @@ -45,15 +45,14 @@ class RememberMeAuthenticationProviderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \Symfony\Component\Security\Core\Exception\AccountExpiredException + * @expectedException \Symfony\Component\Security\Core\Exception\DisabledException */ - public function testAuthenticateWhenPostChecksFails() + public function testAuthenticateWhenPreChecksFails() { $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'); $userChecker->expects($this->once()) - ->method('checkPostAuth') - ->will($this->throwException(new AccountExpiredException())) - ; + ->method('checkPreAuth') + ->will($this->throwException(new DisabledException())); $provider = $this->getProvider($userChecker); @@ -65,8 +64,7 @@ class RememberMeAuthenticationProviderTest extends \PHPUnit_Framework_TestCase $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); $user->expects($this->exactly(2)) ->method('getRoles') - ->will($this->returnValue(array('ROLE_FOO'))) - ; + ->will($this->returnValue(array('ROLE_FOO'))); $provider = $this->getProvider(); @@ -86,16 +84,14 @@ class RememberMeAuthenticationProviderTest extends \PHPUnit_Framework_TestCase $user ->expects($this->any()) ->method('getRoles') - ->will($this->returnValue(array())) - ; + ->will($this->returnValue(array())); } $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', array('getProviderKey'), array($user, 'foo', $key)); $token ->expects($this->once()) ->method('getProviderKey') - ->will($this->returnValue('foo')) - ; + ->will($this->returnValue('foo')); return $token; } diff --git a/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php b/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php index 258ca96d6d..a6f4f77109 100644 --- a/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php +++ b/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php @@ -76,6 +76,12 @@ class SimplePreAuthenticationListener implements ListenerInterface try { $token = $this->simpleAuthenticator->createToken($request, $this->providerKey); + + // allow null to be returned to skip authentication + if (null === $token) { + return; + } + $token = $this->authenticationManager->authenticate($token); $this->securityContext->setToken($token); } catch (AuthenticationException $e) { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php b/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php index 40deb1a0fe..3503fcf4bc 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php @@ -134,11 +134,11 @@ abstract class AbstractComparisonValidatorTestCase extends AbstractConstraintVal $this->validator->validate($dirtyValue, $constraint); - $this->assertViolation('Constraint Message', array( - '{{ value }}' => $dirtyValueAsString, - '{{ compared_value }}' => $comparedValueString, - '{{ compared_value_type }}' => $comparedValueType, - )); + $this->buildViolation('Constraint Message') + ->setParameter('{{ value }}', $dirtyValueAsString) + ->setParameter('{{ compared_value }}', $comparedValueString) + ->setParameter('{{ compared_value_type }}', $comparedValueType) + ->assertRaised(); } /** diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php index cc288daf18..533c6cd952 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php @@ -11,12 +11,14 @@ namespace Symfony\Component\Validator\Tests\Constraints; +use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\NotNull; use Symfony\Component\Validator\ConstraintValidatorInterface; use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\Context\ExecutionContext; use Symfony\Component\Validator\Context\ExecutionContextInterface; use Symfony\Component\Validator\Context\LegacyExecutionContext; +use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\PropertyMetadata; use Symfony\Component\Validator\Tests\Fixtures\StubGlobalExecutionContext; @@ -157,6 +159,19 @@ abstract class AbstractConstraintValidatorTest extends \PHPUnit_Framework_TestCa return $context; } + /** + * @param $message + * @param array $parameters + * @param string $propertyPath + * @param string $invalidValue + * @param null $plural + * @param null $code + * + * @return ConstraintViolation + * + * @deprecated To be removed in Symfony 3.0. Use + * {@link buildViolation()} instead. + */ protected function createViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null) { return new ConstraintViolation( @@ -334,14 +349,34 @@ abstract class AbstractConstraintValidatorTest extends \PHPUnit_Framework_TestCa $this->assertCount(0, $this->context->getViolations()); } + /** + * @param $message + * @param array $parameters + * @param string $propertyPath + * @param string $invalidValue + * @param null $plural + * @param null $code + * + * @deprecated To be removed in Symfony 3.0. Use + * {@link buildViolation()} instead. + */ protected function assertViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null) { - $violations = $this->context->getViolations(); - - $this->assertCount(1, $violations); - $this->assertEquals($this->createViolation($message, $parameters, $propertyPath, $invalidValue, $plural, $code), current(iterator_to_array($violations))); + $this->buildViolation($message) + ->setParameters($parameters) + ->atPath($propertyPath) + ->setInvalidValue($invalidValue) + ->setCode($code) + ->setPlural($plural) + ->assertRaised(); } + /** + * @param array $expected + * + * @deprecated To be removed in Symfony 3.0. Use + * {@link buildViolation()} instead. + */ protected function assertViolations(array $expected) { $violations = $this->context->getViolations(); @@ -355,7 +390,142 @@ abstract class AbstractConstraintValidatorTest extends \PHPUnit_Framework_TestCa } } + /** + * @param $message + * + * @return ConstraintViolationAssertion + */ + protected function buildViolation($message) + { + return new ConstraintViolationAssertion($this->context, $message, $this->constraint); + } + abstract protected function getApiVersion(); abstract protected function createValidator(); } + +/** + * @internal + */ +class ConstraintViolationAssertion +{ + /** + * @var LegacyExecutionContextInterface + */ + private $context; + + /** + * @var ConstraintViolationAssertion[] + */ + private $assertions; + + private $message; + private $parameters = array(); + private $invalidValue = 'InvalidValue'; + private $propertyPath = 'property.path'; + private $translationDomain; + private $plural; + private $code; + private $constraint; + + public function __construct(LegacyExecutionContextInterface $context, $message, Constraint $constraint = null, array $assertions = array()) + { + $this->context = $context; + $this->message = $message; + $this->constraint = $constraint; + $this->assertions = $assertions; + } + + public function atPath($path) + { + $this->propertyPath = $path; + + return $this; + } + + public function setParameter($key, $value) + { + $this->parameters[$key] = $value; + + return $this; + } + + public function setParameters(array $parameters) + { + $this->parameters = $parameters; + + return $this; + } + + public function setTranslationDomain($translationDomain) + { + $this->translationDomain = $translationDomain; + + return $this; + } + + public function setInvalidValue($invalidValue) + { + $this->invalidValue = $invalidValue; + + return $this; + } + + public function setPlural($number) + { + $this->plural = $number; + + return $this; + } + + public function setCode($code) + { + $this->code = $code; + + return $this; + } + + public function buildNextViolation($message) + { + $assertions = $this->assertions; + $assertions[] = $this; + + return new self($this->context, $message, $this->constraint, $assertions); + } + + public function assertRaised() + { + $expected = array(); + foreach ($this->assertions as $assertion) { + $expected[] = $assertion->getViolation(); + } + $expected[] = $this->getViolation(); + + $violations = iterator_to_array($this->context->getViolations()); + + \PHPUnit_Framework_Assert::assertCount(count($expected), $violations); + + reset($violations); + + foreach ($expected as $violation) { + \PHPUnit_Framework_Assert::assertEquals($violation, current($violations)); + next($violations); + } + } + + private function getViolation() + { + return new ConstraintViolation( + null, + $this->message, + $this->parameters, + $this->context->getRoot(), + $this->propertyPath, + $this->invalidValue, + $this->plural, + $this->code, + $this->constraint + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php index 2fa5fce26c..a7f3d7dd58 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php @@ -52,10 +52,9 @@ class BlankValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation( - 'myMessage', - array('{{ value }}' => $valueAsString) - ); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', $valueAsString) + ->assertRaised(); } public function getInvalidValues() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php index a276cfe4aa..f92f23ec62 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php @@ -70,9 +70,9 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($object, $constraint); - $this->assertViolation('My message', array( - '{{ value }}' => 'foobar', - )); + $this->buildViolation('My message') + ->setParameter('{{ value }}', 'foobar') + ->assertRaised(); } public function testSingleMethodExplicitName() @@ -206,9 +206,9 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($object, $constraint); - $this->assertViolation('My message', array( - '{{ value }}' => 'foobar', - )); + $this->buildViolation('My message') + ->setParameter('{{ value }}', 'foobar') + ->assertRaised(); } // BC with Symfony < 2.4 @@ -219,14 +219,11 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($object, $constraint); - $this->assertViolations(array( - $this->createViolation('My message', array( - '{{ value }}' => 'foobar', - )), - $this->createViolation('Static message', array( - '{{ value }}' => 'baz', - )), - )); + $this->buildViolation('My message') + ->setParameter('{{ value }}', 'foobar') + ->buildNextViolation('Static message') + ->setParameter('{{ value }}', 'baz') + ->assertRaised(); } // BC with Symfony < 2.4 @@ -239,14 +236,11 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($object, $constraint); - $this->assertViolations(array( - $this->createViolation('My message', array( - '{{ value }}' => 'foobar', - )), - $this->createViolation('Static message', array( - '{{ value }}' => 'baz', - )), - )); + $this->buildViolation('My message') + ->setParameter('{{ value }}', 'foobar') + ->buildNextViolation('Static message') + ->setParameter('{{ value }}', 'baz') + ->assertRaised(); } // BC with Symfony < 2.4 @@ -259,9 +253,9 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($object, $constraint); - $this->assertViolation('Callback message', array( - '{{ value }}' => 'foobar', - )); + $this->buildViolation('Callback message') + ->setParameter('{{ value }}', 'foobar') + ->assertRaised(); } // BC with Symfony < 2.4 @@ -274,9 +268,9 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($object, $constraint); - $this->assertViolation('Callback message', array( - '{{ value }}' => 'foobar', - )); + $this->buildViolation('Callback message') + ->setParameter('{{ value }}', 'foobar') + ->assertRaised(); } /** diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php index 7511157994..40bba756c2 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php @@ -63,9 +63,9 @@ class CardSchemeValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($number, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => is_string($number) ? '"'.$number.'"' : $number, - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', is_string($number) ? '"'.$number.'"' : $number) + ->assertRaised(); } public function getValidNumbers() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index d07464e3cc..a299dfc871 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -144,9 +144,9 @@ class ChoiceValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate('baz', $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"baz"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"baz"') + ->assertRaised(); } public function testInvalidChoiceMultiple() @@ -159,9 +159,9 @@ class ChoiceValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate(array('foo', 'baz'), $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"baz"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"baz"') + ->assertRaised(); } public function testTooFewChoices() @@ -179,9 +179,11 @@ class ChoiceValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ limit }}' => 2, - ), 'property.path', $value, 2); + $this->buildViolation('myMessage') + ->setParameter('{{ limit }}', 2) + ->setInvalidValue($value) + ->setPlural(2) + ->assertRaised(); } public function testTooManyChoices() @@ -199,9 +201,11 @@ class ChoiceValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ limit }}' => 2, - ), 'property.path', $value, 2); + $this->buildViolation('myMessage') + ->setParameter('{{ limit }}', 2) + ->setInvalidValue($value) + ->setPlural(2) + ->assertRaised(); } public function testNonStrict() @@ -239,9 +243,9 @@ class ChoiceValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate('2', $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"2"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"2"') + ->assertRaised(); } public function testNonStrictWithMultipleChoices() @@ -268,8 +272,8 @@ class ChoiceValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate(array(2, '3'), $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"3"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"3"') + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php index 989f69f7e8..fb80070a46 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php @@ -142,9 +142,11 @@ abstract class CollectionValidatorTest extends AbstractConstraintValidatorTest 'extraFieldsMessage' => 'myMessage', ))); - $this->assertViolation('myMessage', array( - '{{ field }}' => '"baz"', - ), 'property.path[baz]', 6); + $this->buildViolation('myMessage') + ->setParameter('{{ field }}', '"baz"') + ->atPath('property.path[baz]') + ->setInvalidValue(6) + ->assertRaised(); } // bug fix @@ -201,9 +203,11 @@ abstract class CollectionValidatorTest extends AbstractConstraintValidatorTest 'missingFieldsMessage' => 'myMessage', ))); - $this->assertViolation('myMessage', array( - '{{ field }}' => '"foo"', - ), 'property.path[foo]', null); + $this->buildViolation('myMessage') + ->setParameter('{{ field }}', '"foo"') + ->atPath('property.path[foo]') + ->setInvalidValue(null) + ->assertRaised(); } public function testMissingFieldsAllowed() @@ -311,9 +315,11 @@ abstract class CollectionValidatorTest extends AbstractConstraintValidatorTest 'missingFieldsMessage' => 'myMessage', ))); - $this->assertViolation('myMessage', array( - '{{ field }}' => '"foo"', - ), 'property.path[foo]', null); + $this->buildViolation('myMessage') + ->setParameter('{{ field }}', '"foo"') + ->atPath('property.path[foo]') + ->setInvalidValue(null) + ->assertRaised(); } public function testRequiredFieldSingleConstraint() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php index 840981363e..f8ce076011 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php @@ -127,10 +127,12 @@ abstract class CountValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ count }}' => count($value), - '{{ limit }}' => 4, - ), 'property.path', $value, 4); + $this->buildViolation('myMessage') + ->setParameter('{{ count }}', count($value)) + ->setParameter('{{ limit }}', 4) + ->setInvalidValue($value) + ->setPlural(4) + ->assertRaised(); } /** @@ -145,10 +147,12 @@ abstract class CountValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ count }}' => count($value), - '{{ limit }}' => 4, - ), 'property.path', $value, 4); + $this->buildViolation('myMessage') + ->setParameter('{{ count }}', count($value)) + ->setParameter('{{ limit }}', 4) + ->setInvalidValue($value) + ->setPlural(4) + ->assertRaised(); } /** @@ -164,10 +168,12 @@ abstract class CountValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ count }}' => count($value), - '{{ limit }}' => 4, - ), 'property.path', $value, 4); + $this->buildViolation('myMessage') + ->setParameter('{{ count }}', count($value)) + ->setParameter('{{ limit }}', 4) + ->setInvalidValue($value) + ->setPlural(4) + ->assertRaised(); } public function testDefaultOption() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php index ad4f95185d..b13351181a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php @@ -87,9 +87,9 @@ class CountryValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($country, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$country.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$country.'"') + ->assertRaised(); } public function getInvalidCountries() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php index 5dcb557a15..e5bb060d9c 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php @@ -101,9 +101,9 @@ class CurrencyValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($currency, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$currency.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$currency.'"') + ->assertRaised(); } public function getInvalidCurrencies() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php index 255bb739bc..89de5fd9a5 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php @@ -86,9 +86,9 @@ class DateTimeValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($dateTime, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$dateTime.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$dateTime.'"') + ->assertRaised(); } public function getInvalidDateTimes() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php index 1747b674eb..352ded5422 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php @@ -86,9 +86,9 @@ class DateValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($date, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$date.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$date.'"') + ->assertRaised(); } public function getInvalidDates() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php index afeef1065b..fb39f88540 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php @@ -79,9 +79,9 @@ class EmailValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($email, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$email.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$email.'"') + ->assertRaised(); } public function getInvalidEmails() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php index ebc3fbf437..479888e53b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php @@ -49,8 +49,8 @@ class FalseValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate(true, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => 'true', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 'true') + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php index f1f8db692a..25def64c19 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php @@ -28,8 +28,8 @@ class FileValidatorPathTest extends FileValidatorTest $this->validator->validate('foobar', $constraint); - $this->assertViolation('myMessage', array( - '{{ file }}' => '"foobar"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ file }}', '"foobar"') + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php index 6f330f0ee1..8ecdf321a8 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php @@ -176,12 +176,12 @@ abstract class FileValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($this->getFile($this->path), $constraint); - $this->assertViolation('myMessage', array( - '{{ limit }}' => $limitAsString, - '{{ size }}' => $sizeAsString, - '{{ suffix }}' => $suffix, - '{{ file }}' => '"'.$this->path.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ limit }}', $limitAsString) + ->setParameter('{{ size }}', $sizeAsString) + ->setParameter('{{ suffix }}', $suffix) + ->setParameter('{{ file }}', '"'.$this->path.'"') + ->assertRaised(); } public function provideMaxSizeNotExceededTests() @@ -290,18 +290,15 @@ abstract class FileValidatorTest extends AbstractConstraintValidatorTest $file = $this ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') ->setConstructorArgs(array(__DIR__.'/Fixtures/foo')) - ->getMock() - ; + ->getMock(); $file ->expects($this->once()) ->method('getPathname') - ->will($this->returnValue($this->path)) - ; + ->will($this->returnValue($this->path)); $file ->expects($this->once()) ->method('getMimeType') - ->will($this->returnValue('image/jpg')) - ; + ->will($this->returnValue('image/jpg')); $constraint = new File(array( 'mimeTypes' => array('image/png', 'image/jpg'), @@ -317,18 +314,15 @@ abstract class FileValidatorTest extends AbstractConstraintValidatorTest $file = $this ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') ->setConstructorArgs(array(__DIR__.'/Fixtures/foo')) - ->getMock() - ; + ->getMock(); $file ->expects($this->once()) ->method('getPathname') - ->will($this->returnValue($this->path)) - ; + ->will($this->returnValue($this->path)); $file ->expects($this->once()) ->method('getMimeType') - ->will($this->returnValue('image/jpg')) - ; + ->will($this->returnValue('image/jpg')); $constraint = new File(array( 'mimeTypes' => array('image/*'), @@ -344,18 +338,15 @@ abstract class FileValidatorTest extends AbstractConstraintValidatorTest $file = $this ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') ->setConstructorArgs(array(__DIR__.'/Fixtures/foo')) - ->getMock() - ; + ->getMock(); $file ->expects($this->once()) ->method('getPathname') - ->will($this->returnValue($this->path)) - ; + ->will($this->returnValue($this->path)); $file ->expects($this->once()) ->method('getMimeType') - ->will($this->returnValue('application/pdf')) - ; + ->will($this->returnValue('application/pdf')); $constraint = new File(array( 'mimeTypes' => array('image/png', 'image/jpg'), @@ -364,11 +355,11 @@ abstract class FileValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($file, $constraint); - $this->assertViolation('myMessage', array( - '{{ type }}' => '"application/pdf"', - '{{ types }}' => '"image/png", "image/jpg"', - '{{ file }}' => '"'.$this->path.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ type }}', '"application/pdf"') + ->setParameter('{{ types }}', '"image/png", "image/jpg"') + ->setParameter('{{ file }}', '"'.$this->path.'"') + ->assertRaised(); } public function testInvalidWildcardMimeType() @@ -376,18 +367,15 @@ abstract class FileValidatorTest extends AbstractConstraintValidatorTest $file = $this ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') ->setConstructorArgs(array(__DIR__.'/Fixtures/foo')) - ->getMock() - ; + ->getMock(); $file ->expects($this->once()) ->method('getPathname') - ->will($this->returnValue($this->path)) - ; + ->will($this->returnValue($this->path)); $file ->expects($this->once()) ->method('getMimeType') - ->will($this->returnValue('application/pdf')) - ; + ->will($this->returnValue('application/pdf')); $constraint = new File(array( 'mimeTypes' => array('image/*', 'image/jpg'), @@ -396,11 +384,11 @@ abstract class FileValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($file, $constraint); - $this->assertViolation('myMessage', array( - '{{ type }}' => '"application/pdf"', - '{{ types }}' => '"image/*", "image/jpg"', - '{{ file }}' => '"'.$this->path.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ type }}', '"application/pdf"') + ->setParameter('{{ types }}', '"image/*", "image/jpg"') + ->setParameter('{{ file }}', '"'.$this->path.'"') + ->assertRaised(); } public function testDisallowEmpty() @@ -430,7 +418,9 @@ abstract class FileValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($file, $constraint); - $this->assertViolation('myMessage', $params); + $this->buildViolation('myMessage') + ->setParameters($params) + ->assertRaised(); } public function uploadedFileErrorProvider() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php index 07139edb9b..d2ecf8f51b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php @@ -164,9 +164,9 @@ class IbanValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($iban, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$iban.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$iban.'"') + ->assertRaised(); } public function getInvalidIbans() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php index 9ef7508e45..dffbae3967 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php @@ -94,10 +94,10 @@ class ImageValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($this->image, $constraint); - $this->assertViolation('myMessage', array( - '{{ width }}' => '2', - '{{ min_width }}' => '3', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ width }}', '2') + ->setParameter('{{ min_width }}', '3') + ->assertRaised(); } public function testWidthTooBig() @@ -109,10 +109,10 @@ class ImageValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($this->image, $constraint); - $this->assertViolation('myMessage', array( - '{{ width }}' => '2', - '{{ max_width }}' => '1', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ width }}', '2') + ->setParameter('{{ max_width }}', '1') + ->assertRaised(); } public function testHeightTooSmall() @@ -124,10 +124,10 @@ class ImageValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($this->image, $constraint); - $this->assertViolation('myMessage', array( - '{{ height }}' => '2', - '{{ min_height }}' => '3', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ height }}', '2') + ->setParameter('{{ min_height }}', '3') + ->assertRaised(); } public function testHeightTooBig() @@ -139,10 +139,10 @@ class ImageValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($this->image, $constraint); - $this->assertViolation('myMessage', array( - '{{ height }}' => '2', - '{{ max_height }}' => '1', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ height }}', '2') + ->setParameter('{{ max_height }}', '1') + ->assertRaised(); } /** diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php index a00459d8f7..fc40e6104e 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php @@ -151,9 +151,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidIpsV4() @@ -183,9 +183,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidPrivateIpsV4() @@ -209,9 +209,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidReservedIpsV4() @@ -235,9 +235,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidPublicIpsV4() @@ -257,9 +257,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidIpsV6() @@ -293,9 +293,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidPrivateIpsV6() @@ -319,9 +319,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidReservedIpsV6() @@ -344,9 +344,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidPublicIpsV6() @@ -366,9 +366,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidIpsAll() @@ -388,9 +388,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidPrivateIpsAll() @@ -410,9 +410,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidReservedIpsAll() @@ -432,9 +432,9 @@ class IpValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($ip, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$ip.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$ip.'"') + ->assertRaised(); } public function getInvalidPublicIpsAll() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php index 671c12c8c0..6f80e66a45 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php @@ -176,9 +176,9 @@ class IsbnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($isbn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$isbn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$isbn.'"') + ->assertRaised(); } /** @@ -205,9 +205,9 @@ class IsbnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($isbn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$isbn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$isbn.'"') + ->assertRaised(); } /** @@ -233,8 +233,8 @@ class IsbnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($isbn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$isbn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$isbn.'"') + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php index 1cf3ca74bd..4d353cd2b8 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php @@ -146,9 +146,9 @@ class IssnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($issn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$issn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$issn.'"') + ->assertRaised(); } /** @@ -163,9 +163,9 @@ class IssnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($issn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$issn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$issn.'"') + ->assertRaised(); } /** @@ -191,9 +191,9 @@ class IssnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($issn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$issn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$issn.'"') + ->assertRaised(); } /** @@ -207,9 +207,9 @@ class IssnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($issn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$issn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$issn.'"') + ->assertRaised(); } /** @@ -223,8 +223,8 @@ class IssnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($issn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$issn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$issn.'"') + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php index f2d86a7384..6f7c3900e5 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php @@ -87,9 +87,9 @@ class LanguageValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($language, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$language.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$language.'"') + ->assertRaised(); } public function getInvalidLanguages() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php index 13afa0e775..ab0d0ff9c9 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php @@ -156,10 +156,12 @@ class LengthValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$value.'"', - '{{ limit }}' => 4, - ), 'property.path', $value, 4); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$value.'"') + ->setParameter('{{ limit }}', 4) + ->setInvalidValue($value) + ->setPlural(4) + ->assertRaised(); } /** @@ -178,10 +180,12 @@ class LengthValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$value.'"', - '{{ limit }}' => 4, - ), 'property.path', $value, 4); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$value.'"') + ->setParameter('{{ limit }}', 4) + ->setInvalidValue($value) + ->setPlural(4) + ->assertRaised(); } /** @@ -201,10 +205,12 @@ class LengthValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$value.'"', - '{{ limit }}' => 4, - ), 'property.path', $value, 4); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$value.'"') + ->setParameter('{{ limit }}', 4) + ->setInvalidValue($value) + ->setPlural(4) + ->assertRaised(); } public function testConstraintGetDefaultOption() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php index 6f25b52261..e5e2f3009f 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php @@ -89,9 +89,9 @@ class LocaleValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($locale, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$locale.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$locale.'"') + ->assertRaised(); } public function getInvalidLocales() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php index dd6706225f..d395056f00 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php @@ -86,9 +86,9 @@ class LuhnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($number, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$number.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$number.'"') + ->assertRaised(); } public function getInvalidNumbers() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php index 8ae7247ccb..c248246e43 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php @@ -56,9 +56,9 @@ class NotBlankValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate(null, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => 'null', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 'null') + ->assertRaised(); } public function testBlankIsInvalid() @@ -69,9 +69,9 @@ class NotBlankValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate('', $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '""', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '""') + ->assertRaised(); } public function testFalseIsInvalid() @@ -82,9 +82,9 @@ class NotBlankValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate(false, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => 'false', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 'false') + ->assertRaised(); } public function testEmptyArrayIsInvalid() @@ -95,8 +95,8 @@ class NotBlankValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate(array(), $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => 'array', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 'array') + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotNullValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotNullValidatorTest.php index 61b4e477d2..d338f31f79 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotNullValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotNullValidatorTest.php @@ -55,6 +55,6 @@ class NotNullValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate(null, $constraint); - $this->assertViolation('myMessage'); + $this->buildViolation('myMessage')->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php index 34a9a0352c..85df90a8ae 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php @@ -45,9 +45,9 @@ class NullValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => $valueAsString, - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', $valueAsString) + ->assertRaised(); } public function getInvalidValues() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php index 807d05aa3d..9ea79e2a64 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php @@ -114,10 +114,10 @@ class RangeValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => $value, - '{{ limit }}' => 10, - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', $value) + ->setParameter('{{ limit }}', 10) + ->assertRaised(); } /** @@ -132,10 +132,10 @@ class RangeValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => $value, - '{{ limit }}' => 20, - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', $value) + ->setParameter('{{ limit }}', 20) + ->assertRaised(); } /** @@ -152,10 +152,10 @@ class RangeValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMaxMessage', array( - '{{ value }}' => $value, - '{{ limit }}' => 20, - )); + $this->buildViolation('myMaxMessage') + ->setParameter('{{ value }}', $value) + ->setParameter('{{ limit }}', 20) + ->assertRaised(); } /** @@ -172,10 +172,10 @@ class RangeValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMinMessage', array( - '{{ value }}' => $value, - '{{ limit }}' => 10, - )); + $this->buildViolation('myMinMessage') + ->setParameter('{{ value }}', $value) + ->setParameter('{{ limit }}', 10) + ->assertRaised(); } public function getTenthToTwentiethMarch2014() @@ -389,10 +389,10 @@ class RangeValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate(9, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => 9, - '{{ limit }}' => 10, - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 9) + ->setParameter('{{ limit }}', 10) + ->assertRaised(); } public function testMaxMessageIsSet() @@ -405,10 +405,10 @@ class RangeValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate(21, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => 21, - '{{ limit }}' => 20, - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 21) + ->setParameter('{{ limit }}', 20) + ->assertRaised(); } public function testNonNumeric() @@ -419,8 +419,8 @@ class RangeValidatorTest extends AbstractConstraintValidatorTest 'invalidMessage' => 'myMessage', ))); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"abcd"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"abcd"') + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php index 70496ae725..88e565c013 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php @@ -82,9 +82,9 @@ class RegexValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$value.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$value.'"') + ->assertRaised(); } public function getInvalidValues() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php index 1afac22209..23aed04c0f 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php @@ -86,9 +86,9 @@ class TimeValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($time, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$time.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$time.'"') + ->assertRaised(); } public function getInvalidTimes() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php index d9efd3954e..2cdc703183 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php @@ -49,8 +49,8 @@ class TrueValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate(false, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => 'false', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 'false') + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php index a18213fe45..4836928014 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php @@ -56,10 +56,10 @@ class TypeValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate('', $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '""', - '{{ type }}' => 'integer', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '""') + ->setParameter('{{ type }}', 'integer') + ->assertRaised(); } /** @@ -123,10 +123,10 @@ class TypeValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($value, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => $valueAsString, - '{{ type }}' => $type, - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', $valueAsString) + ->setParameter('{{ type }}', $type) + ->assertRaised(); } public function getInvalidValues() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php index 949fd19a1d..3358c7923a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php @@ -124,9 +124,9 @@ class UrlValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($url, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$url.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$url.'"') + ->assertRaised(); } public function getInvalidUrls() diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Loader/AbstractMethodStaticLoader.php b/src/Symfony/Component/Validator/Tests/Mapping/Loader/AbstractStaticMethodLoader.php similarity index 82% rename from src/Symfony/Component/Validator/Tests/Mapping/Loader/AbstractMethodStaticLoader.php rename to src/Symfony/Component/Validator/Tests/Mapping/Loader/AbstractStaticMethodLoader.php index 7c6f362abb..08f219d0a4 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Loader/AbstractMethodStaticLoader.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Loader/AbstractStaticMethodLoader.php @@ -4,7 +4,7 @@ namespace Symfony\Component\Validator\Tests\Mapping\Loader; use Symfony\Component\Validator\Mapping\ClassMetadata; -abstract class AbstractMethodStaticLoader +abstract class AbstractStaticMethodLoader { abstract public static function loadMetadata(ClassMetadata $metadata); } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Loader/StaticMethodLoaderTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Loader/StaticMethodLoaderTest.php index 3f3889bf0e..0c9690f4e3 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Loader/StaticMethodLoaderTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Loader/StaticMethodLoaderTest.php @@ -90,22 +90,19 @@ class StaticMethodLoaderTest extends \PHPUnit_Framework_TestCase public function testLoadClassMetadataIgnoresAbstractMethods() { - error_reporting(E_ALL | E_STRICT); + // Disable error reporting, as AbstractStaticMethodLoader produces a + // strict standards error + error_reporting(0); + + if (0 !== error_reporting()) { + $this->markTestSkipped('Could not disable error reporting'); + } + + include __DIR__.'/AbstractStaticMethodLoader.php'; + + $metadata = new ClassMetadata(__NAMESPACE__.'\AbstractStaticMethodLoader'); $loader = new StaticMethodLoader('loadMetadata'); - $caught = false; - try { - include __DIR__.'/AbstractMethodStaticLoader.php'; - } catch (\Exception $e) { - // catching the PHP notice that is converted to an exception by PHPUnit - $caught = true; - } - - if (!$caught) { - $this->fail('AbstractMethodStaticLoader should produce a strict standard error.'); - } - - $metadata = new ClassMetadata(__NAMESPACE__.'\AbstractMethodStaticLoader'); $loader->loadClassMetadata($metadata); $this->assertCount(0, $metadata->getConstraints());