From c65b4c7d2d507fdc2b220a4d46ef7dad15d8ded7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pol=C3=ADvka?= Date: Tue, 29 Jul 2014 11:52:49 +0200 Subject: [PATCH 01/14] [WebProfilerBundle] turbolinks compatibility --- .../Resources/views/Profiler/toolbar.html.twig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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), From 42ec76e9d022788817fafeae0dbf94fe2281656a Mon Sep 17 00:00:00 2001 From: Sergey Kolodyazhnyy Date: Wed, 18 Jun 2014 22:51:10 +0200 Subject: [PATCH 02/14] Response::isNotModified returns true when If-Modified-Since is later than Last-Modified --- .../Component/HttpFoundation/Response.php | 14 ++- .../HttpFoundation/Tests/ResponseTest.php | 96 +++++++++++++++++++ 2 files changed, 105 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index 44d87d3007..cffe28f07e 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -1033,12 +1033,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 b76179aae0..35ca767795 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -118,6 +118,102 @@ class ResponseTest extends ResponseTestCase $this->assertFalse($modified); } + public function testIsNotModifiedNotSafe() + { + $request = Request::create('/homepage', 'POST'); + + $response = new Response(); + $this->assertFalse($response->isNotModified($request)); + } + + 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); + $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)); + } + + public function testIsNotModifiedEtag() + { + $etagOne = 'randomly_generated_etag'; + $etagTwo = 'randomly_generated_etag_2'; + + $request = new Request(); + $request->headers->set('if_none_match', sprintf('%s, %s, %s', $etagOne, $etagTwo, 'etagThree')); + + $response = new Response(); + + $response->headers->set('ETag', $etagOne); + $this->assertTrue($response->isNotModified($request)); + + $response->headers->set('ETag', $etagTwo); + $this->assertTrue($response->isNotModified($request)); + + $response->headers->set('ETag', ''); + $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))); From 47802105d3ab82c08303900c0ded5e977c4889d1 Mon Sep 17 00:00:00 2001 From: rpg600 Date: Mon, 8 Sep 2014 10:53:21 +0200 Subject: [PATCH 03/14] [Form] Add a form error if post_max_size has been reached. --- .../HttpFoundationRequestHandler.php | 19 +++++++++++++++++ .../Component/Form/NativeRequestHandler.php | 18 ++++++++++++++++ .../Form/Tests/AbstractRequestHandlerTest.php | 21 +++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index 2094699481..065d01aec8 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Form\Extension\HttpFoundation; use Symfony\Component\Form\Exception\UnexpectedTypeException; +use Symfony\Component\Form\Extension\Validator\Util\ServerParams; +use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\RequestHandlerInterface; 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 $params = null) + { + $this->serverParams = $params ?: new ServerParams(); + } + /** * {@inheritdoc} */ @@ -61,6 +76,10 @@ class HttpFoundationRequestHandler implements RequestHandlerInterface $params = $request->request->get($name, $default); $files = $request->files->get($name, $default); } else { + if ($this->serverParams->getContentLength() > $this->serverParams->getPostMaxSize()) { + $form->addError(new FormError('Max post size exceeded.')); + } + // Don't submit the form if it is not present in the request return; } diff --git a/src/Symfony/Component/Form/NativeRequestHandler.php b/src/Symfony/Component/Form/NativeRequestHandler.php index fefe546af8..02c90c8798 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\Extension\Validator\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. * @@ -75,6 +89,10 @@ class NativeRequestHandler implements RequestHandlerInterface $params = array_key_exists($name, $_POST) ? $_POST[$name] : $default; $files = array_key_exists($name, $fixedFiles) ? $fixedFiles[$name] : $default; } else { + if ($this->serverParams->getContentLength() > $this->serverParams->getPostMaxSize()) { + $form->addError(new FormError('Max post size exceeded.')); + } + // Don't submit the form if it is not present in the request return; } diff --git a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php index fbba16ba17..8eb4816d57 100644 --- a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Form\Tests; +use Symfony\Component\Form\Forms; + /** * @author Bernhard Schussek */ @@ -21,11 +23,17 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase */ protected $requestHandler; + /** + * @var \Symfony\Component\Form\FormFactory + */ + protected $factory; + protected $request; protected function setUp() { $this->requestHandler = $this->getRequestHandler(); + $this->factory = Forms::createFormFactoryBuilder()->getFormFactory(); $this->request = null; } @@ -249,6 +257,19 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase $this->requestHandler->handleRequest($form, $this->request); } + public function testAddFormErrorIfPostMaxSizeExceeded() + { + $form = $this->factory->createNamed('name', 'text'); + $this->setRequestData('POST', array(), array()); + $_SERVER['CONTENT_LENGTH'] = 1000000000; + + $this->requestHandler->handleRequest($form, $this->request); + + $this->assertEquals("ERROR: Max post size exceeded.\n", $form->getErrorsAsString()); + + unset($_SERVER['CONTENT_LENGTH']); + } + abstract protected function setRequestData($method, $data, $files = array()); abstract protected function getRequestHandler(); From 759ae1a7a1bec646791770f52caecd7450c21a8e Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 15 Sep 2014 16:48:43 +0200 Subject: [PATCH 04/14] [Form] Moved POST_MAX_SIZE validation from FormValidator to request handler --- .../FrameworkBundle/Resources/config/form.xml | 3 + .../Form/Extension/Core/Type/FormType.php | 38 +++++----- .../HttpFoundationExtension.php | 13 +++- .../HttpFoundationRequestHandler.php | 29 ++++++-- .../Type/FormTypeHttpFoundationExtension.php | 5 +- .../Validator/Constraints/FormValidator.php | 32 --------- .../Type/FormTypeValidatorExtension.php | 1 - .../Extension/Validator/Util/ServerParams.php | 55 +------------- .../Component/Form/NativeRequestHandler.php | 25 +++++-- .../Form/Tests/AbstractRequestHandlerTest.php | 54 ++++++++++++-- .../HttpFoundationRequestHandlerTest.php | 2 +- .../Constraints/FormValidatorTest.php | 63 ---------------- .../Form/Tests/NativeRequestHandlerTest.php | 2 +- .../Component/Form/Util/ServerParams.php | 72 +++++++++++++++++++ 14 files changed, 201 insertions(+), 193 deletions(-) create mode 100644 src/Symfony/Component/Form/Util/ServerParams.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml index bf63332ced..7980131433 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml @@ -151,8 +151,11 @@ + + + diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php index 6da2034731..b417afdbcb 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()); @@ -170,25 +169,26 @@ 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' => '', + 'action' => '', + '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/HttpFoundationExtension.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationExtension.php index 08bd89c9e4..33e9c1c4d4 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationExtension.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationExtension.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Form\Extension\HttpFoundation; use Symfony\Component\Form\AbstractExtension; +use Symfony\Component\Form\Util\ServerParams; /** * Integrates the HttpFoundation component with the Form library. @@ -20,10 +21,20 @@ use Symfony\Component\Form\AbstractExtension; */ class HttpFoundationExtension extends AbstractExtension { + /** + * @var ServerParams + */ + private $serverParams; + + public function __construct(ServerParams $serverParams = null) + { + $this->serverParams = $serverParams; + } + protected function loadTypeExtensions() { return array( - new Type\FormTypeHttpFoundationExtension(), + new Type\FormTypeHttpFoundationExtension($this->serverParams), ); } } diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index 065d01aec8..03805fef52 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -12,10 +12,10 @@ namespace Symfony\Component\Form\Extension\HttpFoundation; use Symfony\Component\Form\Exception\UnexpectedTypeException; -use Symfony\Component\Form\Extension\Validator\Util\ServerParams; 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; /** @@ -34,9 +34,9 @@ class HttpFoundationRequestHandler implements RequestHandlerInterface /** * {@inheritdoc} */ - public function __construct(ServerParams $params = null) + public function __construct(ServerParams $serverParams = null) { - $this->serverParams = $params ?: new ServerParams(); + $this->serverParams = $serverParams ?: new ServerParams(); } /** @@ -68,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(); @@ -76,10 +95,6 @@ class HttpFoundationRequestHandler implements RequestHandlerInterface $params = $request->request->get($name, $default); $files = $request->files->get($name, $default); } else { - if ($this->serverParams->getContentLength() > $this->serverParams->getPostMaxSize()) { - $form->addError(new FormError('Max post size exceeded.')); - } - // Don't submit the form if it is not present in the request return; } diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php b/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php index 9b09b05c39..4596f06b98 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php @@ -15,6 +15,7 @@ use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\Extension\HttpFoundation\EventListener\BindRequestListener; use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\Util\ServerParams; /** * @author Bernhard Schussek @@ -31,10 +32,10 @@ class FormTypeHttpFoundationExtension extends AbstractTypeExtension */ private $requestHandler; - public function __construct() + public function __construct(ServerParams $serverParams = null) { $this->listener = new BindRequestListener(); - $this->requestHandler = new HttpFoundationRequestHandler(); + $this->requestHandler = new HttpFoundationRequestHandler($serverParams); } /** diff --git a/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php b/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php index 154e865923..2851016f40 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; @@ -21,22 +20,6 @@ use Symfony\Component\Validator\ConstraintValidator; */ 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} */ @@ -113,21 +96,6 @@ class FormValidator extends ConstraintValidator $form->getExtraData() ); } - - // 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) { - $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 344bddadc1..ae39af66fc 100644 --- a/src/Symfony/Component/Form/Extension/Validator/Type/FormTypeValidatorExtension.php +++ b/src/Symfony/Component/Form/Extension/Validator/Type/FormTypeValidatorExtension.php @@ -66,7 +66,6 @@ class FormTypeValidatorExtension extends BaseValidatorExtension 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), '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 58fdc25e22..c058d60cae 100644 --- a/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php +++ b/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php @@ -14,59 +14,6 @@ namespace Symfony\Component\Form\Extension\Validator\Util; /** * @author Bernhard Schussek */ -class ServerParams +class ServerParams extends \Symfony\Component\Form\Util\ServerParams { - /** - * 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() - { - return isset($_SERVER['CONTENT_LENGTH']) - ? (int) $_SERVER['CONTENT_LENGTH'] - : null; - } } diff --git a/src/Symfony/Component/Form/NativeRequestHandler.php b/src/Symfony/Component/Form/NativeRequestHandler.php index 02c90c8798..9df9066886 100644 --- a/src/Symfony/Component/Form/NativeRequestHandler.php +++ b/src/Symfony/Component/Form/NativeRequestHandler.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Form; use Symfony\Component\Form\Exception\UnexpectedTypeException; -use Symfony\Component\Form\Extension\Validator\Util\ServerParams; +use Symfony\Component\Form\Util\ServerParams; /** * A request handler using PHP's super globals $_GET, $_POST and $_SERVER. @@ -76,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)); @@ -89,10 +108,6 @@ class NativeRequestHandler implements RequestHandlerInterface $params = array_key_exists($name, $_POST) ? $_POST[$name] : $default; $files = array_key_exists($name, $fixedFiles) ? $fixedFiles[$name] : $default; } else { - if ($this->serverParams->getContentLength() > $this->serverParams->getPostMaxSize()) { - $form->addError(new FormError('Max post size exceeded.')); - } - // Don't submit the form if it is not present in the request return; } diff --git a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php index 8eb4816d57..b017db90d0 100644 --- a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php @@ -11,7 +11,10 @@ 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 @@ -19,19 +22,25 @@ use Symfony\Component\Form\Forms; abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase { /** - * @var \Symfony\Component\Form\RequestHandlerInterface + * @var RequestHandlerInterface */ protected $requestHandler; /** - * @var \Symfony\Component\Form\FormFactory + * @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; @@ -257,17 +266,48 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase $this->requestHandler->handleRequest($form, $this->request); } - public function testAddFormErrorIfPostMaxSizeExceeded() + /** + * @dataProvider getPostMaxSizeFixtures + */ + public function testAddFormErrorIfPostMaxSizeExceeded($contentLength, $iniMax, $shouldFail, array $errorParams = array()) { - $form = $this->factory->createNamed('name', 'text'); + $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()); - $_SERVER['CONTENT_LENGTH'] = 1000000000; $this->requestHandler->handleRequest($form, $this->request); - $this->assertEquals("ERROR: Max post size exceeded.\n", $form->getErrorsAsString()); + if ($shouldFail) { + $errors = array(new FormError($options['post_max_size_message'], null, $errorParams)); - unset($_SERVER['CONTENT_LENGTH']); + $this->assertEquals($errors, $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()); 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 19d5cec9f4..8032d6273d 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php @@ -542,69 +542,6 @@ class FormValidatorTest extends AbstractConstraintValidatorTest ), 'property.path', array('foo' => 'bar')); } - /** - * @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/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..3b1f835182 --- /dev/null +++ b/src/Symfony/Component/Form/Util/ServerParams.php @@ -0,0 +1,72 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Util; + +/** + * @author Bernhard Schussek + */ +class ServerParams +{ + /** + * 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() + { + return isset($_SERVER['CONTENT_LENGTH']) + ? (int) $_SERVER['CONTENT_LENGTH'] + : null; + } +} From e85cb7fe2a3fb5e3b0026159939d433501dc97bc Mon Sep 17 00:00:00 2001 From: adenkejawen Date: Fri, 18 Jul 2014 10:15:01 +0700 Subject: [PATCH 05/14] added the possibility to return null from SimplePreAuthenticationListener --- .../Http/Firewall/SimplePreAuthenticationListener.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php b/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php index 258ca96d6d..47996b2d95 100644 --- a/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php +++ b/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php @@ -21,6 +21,7 @@ use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; /** * SimplePreAuthenticationListener implements simple proxying to an authenticator. @@ -75,9 +76,13 @@ class SimplePreAuthenticationListener implements ListenerInterface } try { + $this->securityContext->setToken(null); $token = $this->simpleAuthenticator->createToken($request, $this->providerKey); - $token = $this->authenticationManager->authenticate($token); - $this->securityContext->setToken($token); + + if ($token instanceof TokenInterface) { + $token = $this->authenticationManager->authenticate($token); + $this->securityContext->setToken($token); + } } catch (AuthenticationException $e) { $this->securityContext->setToken(null); From faa8e98063612aa9dfae575569b710d0488082a5 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 23 Sep 2014 16:12:58 +0200 Subject: [PATCH 06/14] fixed bug --- .../Http/Firewall/SimplePreAuthenticationListener.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php b/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php index 47996b2d95..a6f4f77109 100644 --- a/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php +++ b/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php @@ -21,7 +21,6 @@ use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; -use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; /** * SimplePreAuthenticationListener implements simple proxying to an authenticator. @@ -76,13 +75,15 @@ class SimplePreAuthenticationListener implements ListenerInterface } try { - $this->securityContext->setToken(null); $token = $this->simpleAuthenticator->createToken($request, $this->providerKey); - if ($token instanceof TokenInterface) { - $token = $this->authenticationManager->authenticate($token); - $this->securityContext->setToken($token); + // allow null to be returned to skip authentication + if (null === $token) { + return; } + + $token = $this->authenticationManager->authenticate($token); + $this->securityContext->setToken($token); } catch (AuthenticationException $e) { $this->securityContext->setToken(null); From 24c5ba4df343beb7cad299f49f78ce122a95f49a Mon Sep 17 00:00:00 2001 From: Vyacheslav Salakhutdinov Date: Thu, 11 Sep 2014 15:55:28 +0000 Subject: [PATCH 07/14] Use request format from request in twig ExceptionController --- .../Bundle/TwigBundle/Controller/ExceptionController.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php b/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php index 9557fb4aba..54ccbb03e0 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] : '', From fd77b09fba02e9eacf302275f1b0b556b5f29105 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 27 Feb 2013 12:10:38 +0100 Subject: [PATCH 08/14] [Form] Fixed ValidatorTypeGuesser to guess properties without constraints not to be required --- src/Symfony/Component/Form/CHANGELOG.md | 3 +- .../Validator/ValidatorTypeGuesser.php | 6 +- .../Validator/ValidatorTypeGuesserTest.php | 108 ++++++++++++++---- 3 files changed, 90 insertions(+), 27 deletions(-) diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index a696c7be9a..7d1f86281e 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -1,9 +1,8 @@ CHANGELOG ========= - 2.3.0 ------- +----- * deprecated FormPerformanceTestCase and FormIntegrationTestCase in the Symfony\Component\Form\Tests namespace and moved them to the Symfony\Component\Form\Test namespace * deprecated TypeTestCase in the Symfony\Component\Form\Tests\Extension\Core\Type namespace and moved it to the Symfony\Component\Form\Test namespace diff --git a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php index 589fc12a36..8884629dc9 100644 --- a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php +++ b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php @@ -276,10 +276,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/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; } From 1b1303a8d848df84651a50762dc534915eaedc09 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 24 Sep 2014 15:56:15 +0200 Subject: [PATCH 09/14] [Validator] Fixed StaticMethodLoaderTest to actually test something --- ...der.php => AbstractStaticMethodLoader.php} | 2 +- .../Mapping/Loader/StaticMethodLoaderTest.php | 25 ++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) rename src/Symfony/Component/Validator/Tests/Mapping/Loader/{AbstractMethodStaticLoader.php => AbstractStaticMethodLoader.php} (82%) 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()); From a38d1cd8bf19c7ffe4dd411ffc0e0ad5bbefbfaa Mon Sep 17 00:00:00 2001 From: Mathieu Morlon Date: Tue, 23 Sep 2014 11:21:00 +0200 Subject: [PATCH 10/14] bug #10242 Missing checkPreAuth from RememberMeAuthenticationProvider --- .../RememberMeAuthenticationProvider.php | 2 +- .../RememberMeAuthenticationProviderTest.php | 20 ++++++++----------- 2 files changed, 9 insertions(+), 13 deletions(-) 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/Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php b/src/Symfony/Component/Security/Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php index 88eefbbd2a..54fb4ea9ed 100644 --- a/src/Symfony/Component/Security/Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php +++ b/src/Symfony/Component/Security/Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Security\Tests\Core\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; } From 8d13af77dbc8923ca4137fdbef9df6d71f2ae8ce Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Thu, 25 Sep 2014 01:57:27 +0300 Subject: [PATCH 11/14] [FrameworkBundle] output failed matched path for clarification --- .../Bundle/FrameworkBundle/Command/RouterMatchCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php index 79bb035474..7d1f16696a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php @@ -89,7 +89,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; } From 83f556f0f9712d8d19c83dde2c91ea8487a5bda3 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Thu, 25 Sep 2014 02:08:46 +0300 Subject: [PATCH 12/14] remove obsolete test file --- .../Tests/Command/TestCaseMethod.tpl | 47 ------------------- 1 file changed, 47 deletions(-) delete mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Command/TestCaseMethod.tpl 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(); -?> From 8e5537b8a514a039c80f11d5b9fb75b718b7aeb9 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 24 Sep 2014 13:02:35 +0200 Subject: [PATCH 13/14] [Validator] Simplified testing of violations --- .../Constraints/UniqueEntityValidatorTest.php | 20 +- .../Constraints/FormValidatorTest.php | 35 ++-- .../AbstractComparisonValidatorTestCase.php | 10 +- .../AbstractConstraintValidatorTest.php | 176 +++++++++++++++++- .../Tests/Constraints/BlankValidatorTest.php | 7 +- .../Constraints/CallbackValidatorTest.php | 50 +++-- .../Constraints/CardSchemeValidatorTest.php | 6 +- .../Tests/Constraints/ChoiceValidatorTest.php | 40 ++-- .../Constraints/CollectionValidatorTest.php | 24 ++- .../Tests/Constraints/CountValidatorTest.php | 30 +-- .../Constraints/CountryValidatorTest.php | 6 +- .../Constraints/CurrencyValidatorTest.php | 6 +- .../Constraints/DateTimeValidatorTest.php | 6 +- .../Tests/Constraints/DateValidatorTest.php | 6 +- .../Tests/Constraints/EmailValidatorTest.php | 6 +- .../Tests/Constraints/FalseValidatorTest.php | 6 +- .../Constraints/FileValidatorPathTest.php | 6 +- .../Tests/Constraints/FileValidatorTest.php | 72 +++---- .../Tests/Constraints/IbanValidatorTest.php | 6 +- .../Tests/Constraints/ImageValidatorTest.php | 32 ++-- .../Tests/Constraints/IpValidatorTest.php | 72 +++---- .../Tests/Constraints/IsbnValidatorTest.php | 18 +- .../Tests/Constraints/IssnValidatorTest.php | 30 +-- .../Constraints/LanguageValidatorTest.php | 6 +- .../Tests/Constraints/LengthValidatorTest.php | 30 +-- .../Tests/Constraints/LocaleValidatorTest.php | 6 +- .../Tests/Constraints/LuhnValidatorTest.php | 6 +- .../Constraints/NotBlankValidatorTest.php | 24 +-- .../Constraints/NotNullValidatorTest.php | 2 +- .../Tests/Constraints/NullValidatorTest.php | 6 +- .../Tests/Constraints/RangeValidatorTest.php | 54 +++--- .../Tests/Constraints/RegexValidatorTest.php | 6 +- .../Tests/Constraints/TimeValidatorTest.php | 6 +- .../Tests/Constraints/TrueValidatorTest.php | 6 +- .../Tests/Constraints/TypeValidatorTest.php | 16 +- .../Tests/Constraints/UrlValidatorTest.php | 7 +- 36 files changed, 516 insertions(+), 329 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php index 44dfc853df..3cf2cbcc6b 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php @@ -159,7 +159,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() @@ -179,7 +182,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() @@ -227,7 +233,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 testValidateUniquenessUsingCustomRepositoryMethod() @@ -321,7 +330,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/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php index 8032d6273d..41c7439ba8 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php @@ -219,10 +219,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() @@ -251,10 +253,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() @@ -283,9 +287,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 @@ -537,9 +543,10 @@ class FormValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($form, new Form()); - $this->assertViolation('Extra!', array( - '{{ extra_fields }}' => 'foo', - ), 'property.path', array('foo' => 'bar')); + $this->buildViolation('Extra!') + ->setParameter('{{ extra_fields }}', 'foo') + ->setInvalidValue(array('foo' => 'bar')) + ->assertRaised(); } /** diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php b/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php index 8ced06b1bd..b7117cc4ea 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php @@ -84,11 +84,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 864b58a35b..2ba823eebc 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php @@ -13,8 +13,7 @@ namespace Symfony\Component\Validator\Tests\Constraints; 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\ExecutionContextInterface; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\PropertyMetadata; use Symfony\Component\Validator\Tests\Fixtures\StubGlobalExecutionContext; @@ -80,6 +79,19 @@ abstract class AbstractConstraintValidatorTest extends \PHPUnit_Framework_TestCa ->getMock(); } + /** + * @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( @@ -169,14 +181,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), $violations[0]); + $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(); @@ -190,5 +222,137 @@ abstract class AbstractConstraintValidatorTest extends \PHPUnit_Framework_TestCa } } + /** + * @param $message + * + * @return ConstraintViolationAssertion + */ + protected function buildViolation($message) + { + return new ConstraintViolationAssertion($this->context, $message); + } + abstract protected function createValidator(); } + +/** + * @internal + */ +class ConstraintViolationAssertion +{ + /** + * @var ExecutionContextInterface + */ + private $context; + + /** + * @var ConstraintViolationAssertion[] + */ + private $assertions; + + private $message; + private $parameters = array(); + private $invalidValue = 'InvalidValue'; + private $propertyPath = 'property.path'; + private $translationDomain; + private $plural; + private $code; + + public function __construct(ExecutionContextInterface $context, $message, array $assertions = array()) + { + $this->context = $context; + $this->message = $message; + $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, $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 + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php index c73a4a9cf6..ef10c16b4e 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php @@ -46,10 +46,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 377999bc76..384efdd3ec 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php @@ -64,9 +64,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() @@ -76,9 +76,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 testMultipleMethods() @@ -88,14 +88,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(); } public function testMultipleMethodsExplicitName() @@ -107,14 +104,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(); } public function testSingleStaticMethod() @@ -126,9 +120,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(); } public function testSingleStaticMethodExplicitName() @@ -140,9 +134,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 0b3b04e067..9a786cb6ac 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php @@ -57,9 +57,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 335f620218..6ac78f2a53 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -138,9 +138,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() @@ -153,9 +153,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() @@ -173,9 +173,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() @@ -193,9 +195,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() @@ -233,9 +237,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() @@ -262,8 +266,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 a620ee576c..2fabf754c6 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php @@ -136,9 +136,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 @@ -195,9 +197,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() @@ -305,9 +309,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 1b8bb09232..f69995e790 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php @@ -121,10 +121,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(); } /** @@ -139,10 +141,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(); } /** @@ -158,10 +162,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 a8090aee20..2a435a287a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php @@ -81,9 +81,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 c9238c2711..9d26a6f5d1 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php @@ -95,9 +95,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 76961236d3..19f089efb3 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php @@ -80,9 +80,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 2adcd46a43..e2e044541d 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php @@ -80,9 +80,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 18e23b8b4d..e291124f29 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php @@ -73,9 +73,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 7de6b9c5ae..e74cf4acb8 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php @@ -43,8 +43,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 c93a514304..19bbe3c479 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php @@ -162,12 +162,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() @@ -231,18 +231,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'), @@ -258,18 +255,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/*'), @@ -285,18 +279,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'), @@ -305,11 +296,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() @@ -317,18 +308,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'), @@ -337,11 +325,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(); } /** @@ -358,7 +346,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 6a84a66324..13af716429 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php @@ -158,9 +158,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 35a934ee27..bb7954ed72 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php @@ -81,10 +81,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() @@ -96,10 +96,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() @@ -111,10 +111,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() @@ -126,10 +126,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 5316330623..5cc733020d 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php @@ -145,9 +145,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() @@ -177,9 +177,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() @@ -203,9 +203,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() @@ -229,9 +229,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() @@ -251,9 +251,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() @@ -287,9 +287,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() @@ -313,9 +313,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() @@ -338,9 +338,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() @@ -360,9 +360,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() @@ -382,9 +382,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() @@ -404,9 +404,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() @@ -426,9 +426,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 8de704edee..b5c4c8b1f7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php @@ -170,9 +170,9 @@ class IsbnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($isbn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$isbn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$isbn.'"') + ->assertRaised(); } /** @@ -199,9 +199,9 @@ class IsbnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($isbn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$isbn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$isbn.'"') + ->assertRaised(); } /** @@ -232,8 +232,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 992536ebf2..98b6e1fc36 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php @@ -140,9 +140,9 @@ class IssnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($issn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$issn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$issn.'"') + ->assertRaised(); } /** @@ -157,9 +157,9 @@ class IssnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($issn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$issn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$issn.'"') + ->assertRaised(); } /** @@ -185,9 +185,9 @@ class IssnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($issn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$issn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$issn.'"') + ->assertRaised(); } /** @@ -201,9 +201,9 @@ class IssnValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate($issn, $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '"'.$issn.'"', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$issn.'"') + ->assertRaised(); } /** @@ -217,8 +217,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 5ef9d3a6ef..a9197970be 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php @@ -81,9 +81,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 4457d264dd..1d492a53e1 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php @@ -150,10 +150,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(); } /** @@ -172,10 +174,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(); } /** @@ -195,10 +199,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 31d988df59..f9bfcab951 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php @@ -83,9 +83,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 830c0436ea..974523d202 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php @@ -80,9 +80,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 0f9337b8b7..abbe8c2a28 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php @@ -50,9 +50,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() @@ -63,9 +63,9 @@ class NotBlankValidatorTest extends AbstractConstraintValidatorTest $this->validator->validate('', $constraint); - $this->assertViolation('myMessage', array( - '{{ value }}' => '""', - )); + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '""') + ->assertRaised(); } public function testFalseIsInvalid() @@ -76,9 +76,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() @@ -89,8 +89,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 f9de951b61..bd87878314 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotNullValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotNullValidatorTest.php @@ -49,6 +49,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 a66bf83414..713eabf026 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php @@ -39,9 +39,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 3af24e896c..fd46c634a1 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php @@ -107,10 +107,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(); } /** @@ -125,10 +125,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(); } /** @@ -145,10 +145,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(); } /** @@ -165,10 +165,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 getInvalidValues() @@ -192,10 +192,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() @@ -208,10 +208,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() @@ -222,8 +222,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 e28a8e03cc..8afb037752 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php @@ -76,9 +76,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 3536a72a25..4eb947899a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php @@ -80,9 +80,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 68f207eba2..e0c3ce7875 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php @@ -43,8 +43,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 7a7fcea74e..2b735cbab2 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php @@ -50,10 +50,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(); } /** @@ -117,10 +117,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 353c9acc67..5406b354ce 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php @@ -13,7 +13,6 @@ namespace Symfony\Component\Validator\Tests\Constraints; use Symfony\Component\Validator\Constraints\Url; use Symfony\Component\Validator\Constraints\UrlValidator; -use Symfony\Component\Validator\Validation; class UrlValidatorTest extends AbstractConstraintValidatorTest { @@ -119,9 +118,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() From 6cbc862e19a11a89e469b515dec59b717bfe3b27 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 25 Sep 2014 11:23:31 +0200 Subject: [PATCH 14/14] [Form] Removed constructor argument from FormTypeHttpFoundationExtension for forward compatibility with 2.5 --- .../FrameworkBundle/Resources/config/form.xml | 3 --- .../HttpFoundation/HttpFoundationExtension.php | 13 +------------ .../Type/FormTypeHttpFoundationExtension.php | 5 ++--- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml index 7980131433..bf63332ced 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml @@ -151,11 +151,8 @@ - - - diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationExtension.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationExtension.php index 33e9c1c4d4..08bd89c9e4 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationExtension.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationExtension.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Form\Extension\HttpFoundation; use Symfony\Component\Form\AbstractExtension; -use Symfony\Component\Form\Util\ServerParams; /** * Integrates the HttpFoundation component with the Form library. @@ -21,20 +20,10 @@ use Symfony\Component\Form\Util\ServerParams; */ class HttpFoundationExtension extends AbstractExtension { - /** - * @var ServerParams - */ - private $serverParams; - - public function __construct(ServerParams $serverParams = null) - { - $this->serverParams = $serverParams; - } - protected function loadTypeExtensions() { return array( - new Type\FormTypeHttpFoundationExtension($this->serverParams), + new Type\FormTypeHttpFoundationExtension(), ); } } diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php b/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php index 4596f06b98..9b09b05c39 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php @@ -15,7 +15,6 @@ use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\Extension\HttpFoundation\EventListener\BindRequestListener; use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Form\Util\ServerParams; /** * @author Bernhard Schussek @@ -32,10 +31,10 @@ class FormTypeHttpFoundationExtension extends AbstractTypeExtension */ private $requestHandler; - public function __construct(ServerParams $serverParams = null) + public function __construct() { $this->listener = new BindRequestListener(); - $this->requestHandler = new HttpFoundationRequestHandler($serverParams); + $this->requestHandler = new HttpFoundationRequestHandler(); } /**