From fb1f99137dc023d63bc7022c4788009155efa139 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Tue, 1 Feb 2011 10:59:18 +0100 Subject: [PATCH] [Form] Changed semantics of a "bound" form A form now always has to be bound, independent of whether the request is a POST request or not. The bind() method detects itself whether the request was a post request or not and reads its data accordingly. The "old" bind()/isBound() methods were renamed to submit()/isSubmitted(). $form = new Form('author'); $form->bind($request, $author); if ($form->isValid()) { // isValid() implies isSubmitted(), non-submitted forms can // never be valid // do something with author now } Alternatively, you can only bind global variables, if you don't have a request object. $form->bindGlobals($author); Note that the $author object is in both cases optional. You can also pass no object at all and read the data using $form->getData(), but then no validation will occur. You can also prefill the form with an object during instantiation. $form = new Form('author', array('data' => $author)); $form->bind($request); // etc. --- src/Symfony/Component/Form/ChoiceField.php | 6 +- .../Component/Form/CollectionField.php | 10 +- src/Symfony/Component/Form/DateField.php | 2 +- src/Symfony/Component/Form/Field.php | 42 ++-- .../EntityFieldFactoryGuesser.php | 20 +- .../Form/FieldFactory/FieldFactory.php | 22 +- .../FieldFactoryGuesserInterface.php | 18 +- .../FieldFactory/FieldFactoryInterface.php | 8 +- .../ValidatorFieldFactoryGuesser.php | 18 +- src/Symfony/Component/Form/FieldInterface.php | 45 +--- src/Symfony/Component/Form/Form.php | 218 ++++++++++++------ src/Symfony/Component/Form/FormContext.php | 6 +- .../Component/Form/FormContextInterface.php | 2 +- src/Symfony/Component/Form/FormFactory.php | 4 +- src/Symfony/Component/Form/HybridField.php | 20 +- src/Symfony/Component/Form/PasswordField.php | 2 +- src/Symfony/Component/Form/PropertyPath.php | 10 +- src/Symfony/Component/Form/RepeatedField.php | 2 +- .../Form/Resources/config/validation.xml | 15 +- .../ValueTransformerInterface.php | 6 +- .../Tests/Component/Form/ChoiceFieldTest.php | 24 +- .../Component/Form/CollectionFieldTest.php | 20 +- .../Tests/Component/Form/DateFieldTest.php | 42 ++-- .../Component/Form/DateTimeFieldTest.php | 20 +- .../Component/Form/EntityChoiceFieldTest.php | 54 ++--- .../Form/FieldFactory/FieldFactoryTest.php | 54 ++--- .../Tests/Component/Form/FieldTest.php | 60 ++--- .../Tests/Component/Form/FileFieldTest.php | 36 +-- .../Symfony/Tests/Component/Form/FormTest.php | 182 +++++++++------ .../Tests/Component/Form/IntegerFieldTest.php | 4 +- .../Component/Form/PasswordFieldTest.php | 4 +- .../Component/Form/RepeatedFieldTest.php | 8 +- .../Tests/Component/Form/TimeFieldTest.php | 34 +-- .../Tests/Component/Form/UrlFieldTest.php | 16 +- 34 files changed, 545 insertions(+), 489 deletions(-) diff --git a/src/Symfony/Component/Form/ChoiceField.php b/src/Symfony/Component/Form/ChoiceField.php index 659b6d2dd7..1461788a9b 100644 --- a/src/Symfony/Component/Form/ChoiceField.php +++ b/src/Symfony/Component/Form/ChoiceField.php @@ -75,7 +75,7 @@ class ChoiceField extends HybridField } if ($this->isExpanded()) { - $this->setFieldMode(self::GROUP); + $this->setFieldMode(self::FORM); $choices = $this->getChoices(); @@ -218,13 +218,13 @@ class ChoiceField extends HybridField * Takes care of converting the input from a single radio button * to an array. */ - public function bind($value) + public function submit($value) { if (!$this->isMultipleChoice() && $this->isExpanded()) { $value = null === $value ? array() : array($value => true); } - parent::bind($value); + parent::submit($value); } /** diff --git a/src/Symfony/Component/Form/CollectionField.php b/src/Symfony/Component/Form/CollectionField.php index 9422418c6f..661ad49d90 100644 --- a/src/Symfony/Component/Form/CollectionField.php +++ b/src/Symfony/Component/Form/CollectionField.php @@ -33,7 +33,7 @@ class CollectionField extends Form protected $prototype; /** - * Remembers which fields were removed upon binding + * Remembers which fields were removed upon submitting * @var array */ protected $removedFields = array(); @@ -92,7 +92,7 @@ class CollectionField extends Form parent::setData($collection); } - public function bind($taintedData) + public function submit($taintedData) { $this->removedFields = array(); @@ -113,12 +113,12 @@ class CollectionField extends Form } } - parent::bind($taintedData); + parent::submit($taintedData); } - protected function updateObject(&$objectOrArray) + protected function writeObject(&$objectOrArray) { - parent::updateObject($objectOrArray); + parent::writeObject($objectOrArray); foreach ($this->removedFields as $name) { unset($objectOrArray[$name]); diff --git a/src/Symfony/Component/Form/DateField.php b/src/Symfony/Component/Form/DateField.php index bb99469123..3330bad81f 100644 --- a/src/Symfony/Component/Form/DateField.php +++ b/src/Symfony/Component/Form/DateField.php @@ -149,7 +149,7 @@ class DateField extends HybridField 'output_timezone' => $this->getOption('user_timezone'), ))); - $this->setFieldMode(self::GROUP); + $this->setFieldMode(self::FORM); $this->addChoiceFields(); } diff --git a/src/Symfony/Component/Form/Field.php b/src/Symfony/Component/Form/Field.php index 43d8980a11..1caab1a253 100644 --- a/src/Symfony/Component/Form/Field.php +++ b/src/Symfony/Component/Form/Field.php @@ -53,7 +53,7 @@ class Field extends Configurable implements FieldInterface private $errors = array(); private $key = ''; private $parent = null; - private $bound = false; + private $submitted = false; private $required = null; private $data = null; private $normalizedData = null; @@ -112,9 +112,9 @@ class Field extends Configurable implements FieldInterface /** * Returns the data of the field as it is displayed to the user. * - * @return string|array When the field is not bound, the transformed - * default data is returned. When the field is bound, - * the bound data is returned. + * @return string|array When the field is not submitted, the transformed + * default data is returned. When the field is submitted, + * the submitted data is returned. */ public function getDisplayedData() { @@ -296,13 +296,11 @@ class Field extends Configurable implements FieldInterface * Binds POST data to the field, transforms and validates it. * * @param string|array $taintedData The POST data - * @return Boolean Whether the form is valid - * @throws AlreadyBoundException when the field is already bound */ - public function bind($taintedData) + public function submit($data) { - $this->transformedData = (is_array($taintedData) || is_object($taintedData)) ? $taintedData : (string)$taintedData; - $this->bound = true; + $this->transformedData = (is_array($data) || is_object($data)) ? $data : (string)$data; + $this->submitted = true; $this->errors = array(); if (is_string($this->transformedData) && $this->getOption('trim')) { @@ -320,7 +318,7 @@ class Field extends Configurable implements FieldInterface } /** - * Processes the bound reverse-transformed data. + * Processes the submitted reverse-transformed data. * * This method can be overridden if you want to modify the data entered * by the user. Note that the data is already in reverse transformed format. @@ -348,8 +346,8 @@ class Field extends Configurable implements FieldInterface /** * Returns the normalized data of the field. * - * @return mixed When the field is not bound, the default data is returned. - * When the field is bound, the normalized bound data is + * @return mixed When the field is not submitted, the default data is returned. + * When the field is submitted, the normalized submitted data is * returned if the field is valid, null otherwise. */ protected function getNormalizedData() @@ -368,17 +366,17 @@ class Field extends Configurable implements FieldInterface } /** - * Returns whether the field is bound. + * Returns whether the field is submitted. * - * @return Boolean true if the form is bound to input values, false otherwise + * @return Boolean true if the form is submitted to input values, false otherwise */ - public function isBound() + public function isSubmitted() { - return $this->bound; + return $this->submitted; } /** - * Returns whether the bound value could be reverse transformed correctly + * Returns whether the submitted value could be reverse transformed correctly * * @return Boolean */ @@ -394,13 +392,13 @@ class Field extends Configurable implements FieldInterface */ public function isValid() { - return $this->isBound() ? count($this->errors)==0 : false; // TESTME + return $this->isSubmitted() && !$this->hasErrors(); // TESTME } /** * Returns whether or not there are errors. * - * @return Boolean true if form is bound and not valid + * @return Boolean true if form is submitted and not valid */ public function hasErrors() { @@ -414,7 +412,7 @@ class Field extends Configurable implements FieldInterface /** * Returns all errors * - * @return array An array of FieldError instances that occurred during binding + * @return array An array of FieldError instances that occurred during submitting */ public function getErrors() { @@ -520,7 +518,7 @@ class Field extends Configurable implements FieldInterface /** * {@inheritDoc} */ - public function updateFromProperty(&$objectOrArray) + public function readProperty(&$objectOrArray) { // TODO throw exception if not object or array @@ -532,7 +530,7 @@ class Field extends Configurable implements FieldInterface /** * {@inheritDoc} */ - public function updateProperty(&$objectOrArray) + public function writeProperty(&$objectOrArray) { // TODO throw exception if not object or array diff --git a/src/Symfony/Component/Form/FieldFactory/EntityFieldFactoryGuesser.php b/src/Symfony/Component/Form/FieldFactory/EntityFieldFactoryGuesser.php index 6200519b29..3e716d6a9c 100644 --- a/src/Symfony/Component/Form/FieldFactory/EntityFieldFactoryGuesser.php +++ b/src/Symfony/Component/Form/FieldFactory/EntityFieldFactoryGuesser.php @@ -49,12 +49,10 @@ class EntityFieldFactoryGuesser implements FieldFactoryGuesserInterface /** * @inheritDoc */ - public function guessClass($object, $property) + public function guessClass($class, $property) { - $className = get_class($object); - - if ($this->isMappedClass($className)) { - $metadata = $this->em->getClassMetadata($className); + if ($this->isMappedClass($class)) { + $metadata = $this->em->getClassMetadata($class); if ($metadata->hasAssociation($property)) { $multiple = $metadata->isCollectionValuedAssociation($property); @@ -146,10 +144,10 @@ class EntityFieldFactoryGuesser implements FieldFactoryGuesserInterface /** * @inheritDoc */ - public function guessRequired($object, $property) + public function guessRequired($class, $property) { - if ($this->isMappedClass(get_class($object))) { - $metadata = $this->em->getClassMetadata(get_class($object)); + if ($this->isMappedClass($class)) { + $metadata = $this->em->getClassMetadata($class); if ($metadata->hasField($property)) { if (!$metadata->isNullable($property)) { @@ -170,10 +168,10 @@ class EntityFieldFactoryGuesser implements FieldFactoryGuesserInterface /** * @inheritDoc */ - public function guessMaxLength($object, $property) + public function guessMaxLength($class, $property) { - if ($this->isMappedClass(get_class($object))) { - $metadata = $this->em->getClassMetadata(get_class($object)); + if ($this->isMappedClass($class)) { + $metadata = $this->em->getClassMetadata($class); if (!$metadata->hasAssociation($property)) { $mapping = $metadata->getFieldMapping($property); diff --git a/src/Symfony/Component/Form/FieldFactory/FieldFactory.php b/src/Symfony/Component/Form/FieldFactory/FieldFactory.php index 9eef78f58a..e5aa2613d5 100644 --- a/src/Symfony/Component/Form/FieldFactory/FieldFactory.php +++ b/src/Symfony/Component/Form/FieldFactory/FieldFactory.php @@ -47,32 +47,32 @@ class FieldFactory implements FieldFactoryInterface /** * @inheritDoc */ - public function getInstance($object, $property, array $options = array()) + public function getInstance($class, $property, array $options = array()) { // guess field class and options - $classGuess = $this->guess(function ($guesser) use ($object, $property) { - return $guesser->guessClass($object, $property); + $classGuess = $this->guess(function ($guesser) use ($class, $property) { + return $guesser->guessClass($class, $property); }); if (!$classGuess) { - throw new \RuntimeException(sprintf('No field could be guessed for property "%s" of class %s', $property, get_class($object))); + throw new \RuntimeException(sprintf('No field could be guessed for property "%s" of class %s', $property, $class)); } // guess maximum length - $maxLengthGuess = $this->guess(function ($guesser) use ($object, $property) { - return $guesser->guessMaxLength($object, $property); + $maxLengthGuess = $this->guess(function ($guesser) use ($class, $property) { + return $guesser->guessMaxLength($class, $property); }); // guess whether field is required - $requiredGuess = $this->guess(function ($guesser) use ($object, $property) { - return $guesser->guessRequired($object, $property); + $requiredGuess = $this->guess(function ($guesser) use ($class, $property) { + return $guesser->guessRequired($class, $property); }); // construct field - $class = $classGuess->getClass(); + $fieldClass = $classGuess->getClass(); $textField = 'Symfony\Component\Form\TextField'; - if ($maxLengthGuess && ($class == $textField || is_subclass_of($class, $textField))) { + if ($maxLengthGuess && ($fieldClass == $textField || is_subclass_of($fieldClass, $textField))) { $options = array_merge(array('max_length' => $maxLengthGuess->getValue()), $options); } @@ -83,7 +83,7 @@ class FieldFactory implements FieldFactoryInterface // user options may override guessed options $options = array_merge($classGuess->getOptions(), $options); - return new $class($property, $options); + return new $fieldClass($property, $options); } /** diff --git a/src/Symfony/Component/Form/FieldFactory/FieldFactoryGuesserInterface.php b/src/Symfony/Component/Form/FieldFactory/FieldFactoryGuesserInterface.php index 5d9901f5f0..7df86fd531 100644 --- a/src/Symfony/Component/Form/FieldFactory/FieldFactoryGuesserInterface.php +++ b/src/Symfony/Component/Form/FieldFactory/FieldFactoryGuesserInterface.php @@ -12,36 +12,36 @@ namespace Symfony\Component\Form\FieldFactory; /** - * Guesses field classes and options for the properties of an object + * Guesses field classes and options for the properties of a class * * @author Bernhard Schussek */ interface FieldFactoryGuesserInterface { /** - * Returns a field guess for a given property name + * Returns a field guess for a property name of a class * - * @param object $object The object to guess for + * @param string $class The fully qualified class name * @param string $property The name of the property to guess for * @return FieldFactoryClassGuess A guess for the field's class and options */ - function guessClass($object, $property); + function guessClass($class, $property); /** - * Returns a guess whether the given property is required + * Returns a guess whether a property of a class is required * - * @param object $object The object to guess for + * @param string $class The fully qualified class name * @param string $property The name of the property to guess for * @return FieldFactoryGuess A guess for the field's required setting */ - function guessRequired($object, $property); + function guessRequired($class, $property); /** * Returns a guess about the field's maximum length * - * @param object $object The object to guess for + * @param string $class The fully qualified class name * @param string $property The name of the property to guess for * @return FieldFactoryGuess A guess for the field's maximum length */ - function guessMaxLength($object, $property); + function guessMaxLength($class, $property); } diff --git a/src/Symfony/Component/Form/FieldFactory/FieldFactoryInterface.php b/src/Symfony/Component/Form/FieldFactory/FieldFactoryInterface.php index 0fec48935d..204ca8ed7c 100644 --- a/src/Symfony/Component/Form/FieldFactory/FieldFactoryInterface.php +++ b/src/Symfony/Component/Form/FieldFactory/FieldFactoryInterface.php @@ -12,19 +12,19 @@ namespace Symfony\Component\Form\FieldFactory; /** - * Automatically creates form fields for properties of an object + * Automatically creates form fields for properties of a class * * @author Bernhard Schussek */ interface FieldFactoryInterface { /** - * Returns a field for a given property name + * Returns a field for a given property name of a class * - * @param object $object The object to create a field for + * @param string $class The fully qualified class name * @param string $property The name of the property * @param array $options Custom options for creating the field * @return FieldInterface A field instance */ - function getInstance($object, $property, array $options = array()); + function getInstance($class, $property, array $options = array()); } diff --git a/src/Symfony/Component/Form/FieldFactory/ValidatorFieldFactoryGuesser.php b/src/Symfony/Component/Form/FieldFactory/ValidatorFieldFactoryGuesser.php index 3eb3532855..8af07d0d23 100644 --- a/src/Symfony/Component/Form/FieldFactory/ValidatorFieldFactoryGuesser.php +++ b/src/Symfony/Component/Form/FieldFactory/ValidatorFieldFactoryGuesser.php @@ -34,11 +34,11 @@ class ValidatorFieldFactoryGuesser implements FieldFactoryGuesserInterface /** * @inheritDoc */ - public function guessClass($object, $property) + public function guessClass($class, $property) { $guesser = $this; - return $this->guess($object, $property, function (Constraint $constraint) use ($guesser) { + return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) { return $guesser->guessClassForConstraint($constraint); }); } @@ -46,11 +46,11 @@ class ValidatorFieldFactoryGuesser implements FieldFactoryGuesserInterface /** * @inheritDoc */ - public function guessRequired($object, $property) + public function guessRequired($class, $property) { $guesser = $this; - return $this->guess($object, $property, function (Constraint $constraint) use ($guesser) { + return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) { return $guesser->guessRequiredForConstraint($constraint); }); } @@ -58,11 +58,11 @@ class ValidatorFieldFactoryGuesser implements FieldFactoryGuesserInterface /** * @inheritDoc */ - public function guessMaxLength($object, $property) + public function guessMaxLength($class, $property) { $guesser = $this; - return $this->guess($object, $property, function (Constraint $constraint) use ($guesser) { + return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) { return $guesser->guessMaxLengthForConstraint($constraint); }); } @@ -71,16 +71,16 @@ class ValidatorFieldFactoryGuesser implements FieldFactoryGuesserInterface * Iterates over the constraints of a property, executes a constraints on * them and returns the best guess * - * @param object $object The object to read the constraints from + * @param string $class The class to read the constraints from * @param string $property The property for which to find constraints * @param \Closure $guessForConstraint The closure that returns a guess * for a given constraint * @return FieldFactoryGuess The guessed value with the highest confidence */ - protected function guess($object, $property, \Closure $guessForConstraint) + protected function guess($class, $property, \Closure $guessForConstraint) { $guesses = array(); - $classMetadata = $this->metadataFactory->getClassMetadata(get_class($object)); + $classMetadata = $this->metadataFactory->getClassMetadata($class); $memberMetadatas = $classMetadata->getMemberMetadatas($property); foreach ($memberMetadatas as $memberMetadata) { diff --git a/src/Symfony/Component/Form/FieldInterface.php b/src/Symfony/Component/Form/FieldInterface.php index c12d894805..c22af93c42 100644 --- a/src/Symfony/Component/Form/FieldInterface.php +++ b/src/Symfony/Component/Form/FieldInterface.php @@ -114,7 +114,7 @@ interface FieldInterface * * @param array|object $objectOrArray */ - function updateFromProperty(&$objectOrArray); + function readProperty(&$objectOrArray); /** * Writes a the field value into a property of the object @@ -123,16 +123,7 @@ interface FieldInterface * * @param array|object $objectOrArray */ - function updateProperty(&$objectOrArray); - - /** - * Returns the normalized data of the field. - * - * @return mixed When the field is not bound, the default data is returned. - * When the field is bound, the normalized bound data is - * returned if the field is valid, null otherwise. - */ - function getData(); + function writeProperty(&$objectOrArray); /** * Returns the data of the field as it is displayed to the user. @@ -143,24 +134,6 @@ interface FieldInterface */ function getDisplayedData(); - /** - * Sets the default data - * - * @param mixed $default The default data - * @throws UnexpectedTypeException If the default data is invalid - */ - function setData($default); - - /** - * Binds POST data to the field, transforms and validates it. - * - * @param string|array $taintedData The POST data - * @return Boolean Whether the form is valid - * @throws InvalidConfigurationException when the field is not configured - * correctly - */ - function bind($taintedData); - /** * Recursively adds constraint violations to the fields * @@ -187,13 +160,6 @@ interface FieldInterface */ function addError(FieldError $error, PropertyPathIterator $pathIterator = null, $type = null); - /** - * Returns whether the field is bound. - * - * @return Boolean - */ - function isBound(); - /** * Returns whether the field is valid. * @@ -245,4 +211,11 @@ interface FieldInterface * @param Boolean $required */ function setRequired($required); + + /** + * Writes posted data into the field + * + * @param mixed $data The data from the POST request + */ + function submit($data); } diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index edfc0aad0d..e4e294ae80 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -45,11 +45,23 @@ class Form extends Field implements \IteratorAggregate, FormInterface protected $fields = array(); /** - * Contains the names of bound values who don't belong to any fields + * Contains the names of submitted values who don't belong to any fields * @var array */ protected $extraFields = array(); + /** + * Whether a request was bound to the form + * @var Boolean + */ + protected $bound = false; + + /** + * Stores the class that the data of this form must be instances of + * @var string + */ + protected $dataClass; + /** * Constructor. * @@ -60,6 +72,7 @@ class Form extends Field implements \IteratorAggregate, FormInterface */ public function __construct($name = null, array $options = array()) { + $this->addOption('data_class'); $this->addOption('csrf_field_name', '_token'); $this->addOption('csrf_provider'); $this->addOption('field_factory'); @@ -71,10 +84,13 @@ class Form extends Field implements \IteratorAggregate, FormInterface $options['validation_groups'] = (array)$options['validation_groups']; } + if (isset($options['data_class'])) { + $this->dataClass = $options['data_class']; + } + parent::__construct($name, $options); - // Enable CSRF protection, if necessary - // TODO only in root form + // Enable CSRF protection if ($this->getOption('csrf_provider')) { if (!$this->getOption('csrf_provider') instanceof CsrfProviderInterface) { throw new FormException('The object passed to the "csrf_provider" option must implement CsrfProviderInterface'); @@ -83,9 +99,7 @@ class Form extends Field implements \IteratorAggregate, FormInterface $fieldName = $this->getOption('csrf_field_name'); $token = $this->getOption('csrf_provider')->generateCsrfToken(get_class($this)); - $this->add(new HiddenField($fieldName, array( - 'data' => $token, - ))); + $this->add(new HiddenField($fieldName, array('data' => $token))); } } @@ -152,14 +166,20 @@ class Form extends Field implements \IteratorAggregate, FormInterface throw new UnexpectedTypeException($field, 'FieldInterface or string'); } - $factory = $this->getRoot()->getFieldFactory(); + $factory = $this->getFieldFactory(); if (!$factory) { - throw new \LogicException('A field factory must be available to automatically create fields'); + throw new FormException('A field factory must be set to automatically create fields'); + } + + $class = $this->getDataClass(); + + if (!$class) { + throw new FormException('The data class must be set to automatically create fields'); } $options = func_num_args() > 1 ? func_get_arg(1) : array(); - $field = $factory->getInstance($this->getData(), $field, $options); + $field = $factory->getInstance($class, $field, $options); } if ('' === $field->getKey() || null === $field->getKey()) { @@ -175,7 +195,7 @@ class Form extends Field implements \IteratorAggregate, FormInterface // if the property "data" is NULL, getTransformedData() returns an empty // string if (!empty($data)) { - $field->updateFromProperty($data); + $field->readProperty($data); } return $field; @@ -316,7 +336,11 @@ class Form extends Field implements \IteratorAggregate, FormInterface } if (!empty($data)) { - $this->updateFromObject($data); + if ($this->dataClass && !$data instanceof $this->dataClass) { + throw new FormException(sprintf('Form data should be instance of %s', $this->dataClass)); + } + + $this->readObject($data); } } @@ -340,112 +364,92 @@ class Form extends Field implements \IteratorAggregate, FormInterface /** * Binds POST data to the field, transforms and validates it. * - * @param string|array $taintedData The POST data + * @param string|array $data The POST data */ - public function bind($taintedData) + public function submit($data) { - if (null === $taintedData) { - $taintedData = array(); + if (null === $data) { + $data = array(); } - if (!is_array($taintedData)) { - throw new UnexpectedTypeException($taintedData, 'array'); + if (!is_array($data)) { + throw new UnexpectedTypeException($data, 'array'); } + // remember for later + $submittedData = $data; + foreach ($this->fields as $key => $field) { - if (!isset($taintedData[$key])) { - $taintedData[$key] = null; + if (!isset($data[$key])) { + $data[$key] = null; } } - $taintedData = $this->preprocessData($taintedData); + $data = $this->preprocessData($data); - foreach ($taintedData as $key => $value) { + foreach ($data as $key => $value) { if ($this->has($key)) { - $this->fields[$key]->bind($value); + $this->fields[$key]->submit($value); } } $data = $this->getTransformedData(); - $this->updateObject($data); + $this->writeObject($data); - // bind and reverse transform the data - parent::bind($data); + // set and reverse transform the data + parent::submit($data); $this->extraFields = array(); - foreach ($taintedData as $key => $value) { + foreach ($submittedData as $key => $value) { if (!$this->has($key)) { $this->extraFields[] = $key; } } - - if ($this->isRoot() && null !== $this->getOption('validator')) { -// if () { -// throw new FormException('A validator is required for binding. Forgot to pass it to the constructor of the form?'); -// } - - if ($violations = $this->getOption('validator')->validate($this, $this->getOption('validation_groups'))) { - // TODO: test me - foreach ($violations as $violation) { - $propertyPath = new PropertyPath($violation->getPropertyPath()); - $iterator = $propertyPath->getIterator(); - - if ($iterator->current() == 'data') { - $type = self::DATA_ERROR; - $iterator->next(); // point at the first data element - } else { - $type = self::FIELD_ERROR; - } - - $this->addError(new FieldError($violation->getMessageTemplate(), $violation->getMessageParameters()), $iterator, $type); - } - } - } } /** * Updates the child fields from the properties of the given data * - * This method calls updateFromProperty() on all child fields that have a + * This method calls readProperty() on all child fields that have a * property path set. If a child field has no property path set but - * implements FormInterface, updateProperty() is called on its + * implements FormInterface, writeProperty() is called on its * children instead. * * @param array|object $objectOrArray */ - protected function updateFromObject(&$objectOrArray) + protected function readObject(&$objectOrArray) { $iterator = new RecursiveFieldIterator($this); $iterator = new \RecursiveIteratorIterator($iterator); foreach ($iterator as $field) { - $field->updateFromProperty($objectOrArray); + $field->readProperty($objectOrArray); } } /** * Updates all properties of the given data from the child fields * - * This method calls updateProperty() on all child fields that have a property + * This method calls writeProperty() on all child fields that have a property * path set. If a child field has no property path set but implements - * FormInterface, updateProperty() is called on its children instead. + * FormInterface, writeProperty() is called on its children instead. * * @param array|object $objectOrArray */ - protected function updateObject(&$objectOrArray) + protected function writeObject(&$objectOrArray) { $iterator = new RecursiveFieldIterator($this); $iterator = new \RecursiveIteratorIterator($iterator); foreach ($iterator as $field) { - $field->updateProperty($objectOrArray); + $field->writeProperty($objectOrArray); } } /** - * Processes the bound data before it is passed to the individual fields + * Processes the submitted data before it is passed to the individual fields * * The data is in the user format. * @@ -466,11 +470,11 @@ class Form extends Field implements \IteratorAggregate, FormInterface } /** - * Returns whether this form was bound with extra fields + * Returns whether this form was submitted with extra fields * * @return Boolean */ - public function isBoundWithExtraFields() + public function isSubmittedWithExtraFields() { // TODO: integrate the field names in the error message return count($this->extraFields) > 0; @@ -554,9 +558,9 @@ class Form extends Field implements \IteratorAggregate, FormInterface } /** - * Returns true if the bound field exists (implements the \ArrayAccess interface). + * Returns true if the field exists (implements the \ArrayAccess interface). * - * @param string $key The key of the bound field + * @param string $key The key of the field * * @return Boolean true if the widget exists, false otherwise */ @@ -674,17 +678,57 @@ class Form extends Field implements \IteratorAggregate, FormInterface } /** - * Binds the form with submitted data from a Request object + * Binds a request to the form * - * @param Request $request The request object - * @see bind() + * If the request was a POST request, the data is submitted to the form, + * transformed and written into the form data (an object or an array). + * You can set the form data by passing it in the second parameter + * of this method or by passing it in the "data" option of the form's + * constructor. + * + * @param Request $request The request to bind to the form + * @param array|object $data The data from which to read default values + * and where to write submitted values */ - public function bindRequest(Request $request) + public function bind(Request $request, $data = null) { - $values = $request->request->get($this->getName()); - $files = $request->files->get($this->getName()); + $this->bound = true; - $this->bind(self::deepArrayUnion($values, $files)); + // Store object from which to read the default values and where to + // write the submitted values + if (null !== $data) { + $this->setData($data); + } + + // Store the submitted data in case of a post request + if ('POST' == $request->getMethod()) { + $values = $request->request->get($this->getName(), array()); + $files = $request->files->get($this->getName(), array()); + + $this->submit(self::deepArrayUnion($values, $files)); + + if (null === $this->getOption('validator')) { + throw new FormException('A validator is required for binding. Forgot to pass it to the constructor of the form?'); + } + + // Validate the submitted data + if ($violations = $this->getOption('validator')->validate($this, $this->getOption('validation_groups'))) { + // TODO: test me + foreach ($violations as $violation) { + $propertyPath = new PropertyPath($violation->getPropertyPath()); + $iterator = $propertyPath->getIterator(); + + if ($iterator->current() == 'data') { + $type = self::DATA_ERROR; + $iterator->next(); // point at the first data element + } else { + $type = self::FIELD_ERROR; + } + + $this->addError(new FieldError($violation->getMessageTemplate(), $violation->getMessageParameters()), $iterator, $type); + } + } + } } /** @@ -693,15 +737,17 @@ class Form extends Field implements \IteratorAggregate, FormInterface * * @see bind() */ - public function bindGlobals() + public function bindGlobals($data = null) { - $values = $_POST[$this->getName()]; + $this->bind(Request::createFromGlobals(), $data); + } - // fix files array and convert to UploadedFile instances - $fileBag = new FileBag($_FILES); - $files = $fileBag->get($this->getName()); - - $this->bind(self::deepArrayUnion($values, $files)); + /** + * @var Boolean whether a request and an object were bound to the form + */ + public function isBound() + { + return $this->bound; } /** @@ -755,6 +801,26 @@ class Form extends Field implements \IteratorAggregate, FormInterface } } + /** + * Sets the class that object bound to this form must be instances of + * + * @param string A fully qualified class name + */ + protected function setDataClass($class) + { + $this->dataClass = $class; + } + + /** + * Returns the class that object must have that are bound to this form + * + * @return string A fully qualified class name + */ + public function getDataClass() + { + return $this->dataClass; + } + /** * Merges two arrays without reindexing numeric keys. * diff --git a/src/Symfony/Component/Form/FormContext.php b/src/Symfony/Component/Form/FormContext.php index a8276cd891..3d2ead8ba2 100644 --- a/src/Symfony/Component/Form/FormContext.php +++ b/src/Symfony/Component/Form/FormContext.php @@ -128,18 +128,18 @@ class FormContext implements FormContextInterface /** * @inheritDoc */ - public function getForm($name, $data = null) + public function getForm($name, $data = null, array $options = array()) { return new Form( $name, - array( + array_merge(array( 'data' => $data, 'validator' => $this->validator, 'csrf_field_name' => $this->csrfFieldName, 'csrf_provider' => $this->csrfProtection ? $this->csrfProvider : null, 'validation_groups' => $this->validationGroups, 'field_factory' => $this->fieldFactory, - ) + ), $options) ); } diff --git a/src/Symfony/Component/Form/FormContextInterface.php b/src/Symfony/Component/Form/FormContextInterface.php index 510b7608e8..4b488fed2b 100644 --- a/src/Symfony/Component/Form/FormContextInterface.php +++ b/src/Symfony/Component/Form/FormContextInterface.php @@ -87,5 +87,5 @@ interface FormContextInterface * @param array|object $data The data displayed and modified by the form * @return Form The new form */ - function getForm($name, $data = null); + function getForm($name, $data = null, array $options = array()); } \ No newline at end of file diff --git a/src/Symfony/Component/Form/FormFactory.php b/src/Symfony/Component/Form/FormFactory.php index d93cbcf14a..e320838c0d 100644 --- a/src/Symfony/Component/Form/FormFactory.php +++ b/src/Symfony/Component/Form/FormFactory.php @@ -191,8 +191,8 @@ class FormFactory implements FormContextInterface * @param array|object $data The data displayed and modified by the form * @return Form The new form */ - public function getForm($name, $data = null) + public function getForm($name, $data = null, array $options = array()) { - return $this->defaultContext->getForm($name, $data); + return $this->defaultContext->getForm($name, $data, $options); } } \ No newline at end of file diff --git a/src/Symfony/Component/Form/HybridField.php b/src/Symfony/Component/Form/HybridField.php index b16db73d65..d794fb1901 100644 --- a/src/Symfony/Component/Form/HybridField.php +++ b/src/Symfony/Component/Form/HybridField.php @@ -17,7 +17,7 @@ use Symfony\Component\Form\Exception\FormException; * A field that can dynamically act like a field or like a field group * * You can use the method setFieldMode() to switch between the modes - * HybridField::FIELD and HybridField::GROUP. This is useful when you want + * HybridField::FIELD and HybridField::FORM. This is useful when you want * to create a field that, depending on its configuration, can either be * a single field or a combination of different fields (e.g. a date field * that might be a textbox or several select boxes). @@ -27,7 +27,7 @@ use Symfony\Component\Form\Exception\FormException; class HybridField extends Form { const FIELD = 0; - const GROUP = 1; + const FORM = 1; protected $mode = self::FIELD; @@ -38,7 +38,7 @@ class HybridField extends Form * this field. * * @param integer $mode One of the constants HybridField::FIELD and - * HybridField::GROUP. + * HybridField::FORM. */ public function setFieldMode($mode) { @@ -62,7 +62,7 @@ class HybridField extends Form */ public function isGroup() { - return self::GROUP === $this->mode; + return self::FORM === $this->mode; } /** @@ -93,7 +93,7 @@ class HybridField extends Form */ public function getDisplayedData() { - if ($this->mode === self::GROUP) { + if ($this->mode === self::FORM) { return parent::getDisplayedData(); } else { return Field::getDisplayedData(); @@ -105,7 +105,7 @@ class HybridField extends Form */ public function setData($data) { - if ($this->mode === self::GROUP) { + if ($this->mode === self::FORM) { parent::setData($data); } else { Field::setData($data); @@ -115,12 +115,12 @@ class HybridField extends Form /** * {@inheritDoc} */ - public function bind($data) + public function submit($data) { - if ($this->mode === self::GROUP) { - parent::bind($data); + if ($this->mode === self::FORM) { + parent::submit($data); } else { - Field::bind($data); + Field::submit($data); } } } \ No newline at end of file diff --git a/src/Symfony/Component/Form/PasswordField.php b/src/Symfony/Component/Form/PasswordField.php index b7fa4e14da..1efac6fa91 100644 --- a/src/Symfony/Component/Form/PasswordField.php +++ b/src/Symfony/Component/Form/PasswordField.php @@ -38,7 +38,7 @@ class PasswordField extends TextField */ public function getDisplayedData() { - return $this->getOption('always_empty') || !$this->isBound() + return $this->getOption('always_empty') || !$this->isSubmitted() ? '' : parent::getDisplayedData(); } diff --git a/src/Symfony/Component/Form/PropertyPath.php b/src/Symfony/Component/Form/PropertyPath.php index bd5abb9b09..40a256aef2 100644 --- a/src/Symfony/Component/Form/PropertyPath.php +++ b/src/Symfony/Component/Form/PropertyPath.php @@ -216,7 +216,7 @@ class PropertyPath implements \IteratorAggregate */ public function setValue(&$objectOrArray, $value) { - $this->updatePropertyPath($objectOrArray, 0, $value); + $this->writePropertyPath($objectOrArray, 0, $value); } /** @@ -259,7 +259,7 @@ class PropertyPath implements \IteratorAggregate * @param mixed $value The value to set at the end of the * property path */ - protected function updatePropertyPath(&$objectOrArray, $currentIndex, $value) + protected function writePropertyPath(&$objectOrArray, $currentIndex, $value) { $property = $this->elements[$currentIndex]; @@ -276,9 +276,9 @@ class PropertyPath implements \IteratorAggregate $nestedObject =& $objectOrArray[$property]; } - $this->updatePropertyPath($nestedObject, $currentIndex + 1, $value); + $this->writePropertyPath($nestedObject, $currentIndex + 1, $value); } else { - $this->updateProperty($objectOrArray, $currentIndex, $value); + $this->writeProperty($objectOrArray, $currentIndex, $value); } } @@ -342,7 +342,7 @@ class PropertyPath implements \IteratorAggregate * path * @param mixed $value The value to set */ - protected function updateProperty(&$objectOrArray, $currentIndex, $value) + protected function writeProperty(&$objectOrArray, $currentIndex, $value) { $property = $this->elements[$currentIndex]; diff --git a/src/Symfony/Component/Form/RepeatedField.php b/src/Symfony/Component/Form/RepeatedField.php index fc843bb89a..3d2581b608 100644 --- a/src/Symfony/Component/Form/RepeatedField.php +++ b/src/Symfony/Component/Form/RepeatedField.php @@ -92,7 +92,7 @@ class RepeatedField extends Form */ public function getData() { - if ($this->isBound() && $this->isFirstEqualToSecond()) { + if ($this->isSubmitted() && $this->isFirstEqualToSecond()) { return $this->get($this->getOption('first_key'))->getData(); } diff --git a/src/Symfony/Component/Form/Resources/config/validation.xml b/src/Symfony/Component/Form/Resources/config/validation.xml index c4ee988a85..881f44024e 100644 --- a/src/Symfony/Component/Form/Resources/config/validation.xml +++ b/src/Symfony/Component/Form/Resources/config/validation.xml @@ -12,21 +12,18 @@ - + - - - - - - - - + + + + + diff --git a/src/Symfony/Component/Form/ValueTransformer/ValueTransformerInterface.php b/src/Symfony/Component/Form/ValueTransformer/ValueTransformerInterface.php index d0d4aa0d18..663011d5b8 100644 --- a/src/Symfony/Component/Form/ValueTransformer/ValueTransformerInterface.php +++ b/src/Symfony/Component/Form/ValueTransformer/ValueTransformerInterface.php @@ -24,8 +24,8 @@ interface ValueTransformerInterface * This method is called on two occasions inside a form field: * * 1. When the form field is initialized with the data attached from the datasource (object or array). - * 2. When data from a request is bound using {@link Field::bind()} to transform the new input data - * back into the renderable format. For example if you have a date field and bind '2009-10-10' onto + * 2. When data from a request is bound using {@link Field::submit()} to transform the new input data + * back into the renderable format. For example if you have a date field and submit '2009-10-10' onto * it you might accept this value because its easily parsed, but the transformer still writes back * "2009/10/10" onto the form field (for further displaying or other purposes). * @@ -50,7 +50,7 @@ interface ValueTransformerInterface * Transforms a value from the transformed representation to its original * representation. * - * This method is called when {@link Field::bind()} is called to transform the requests tainted data + * This method is called when {@link Field::submit()} is called to transform the requests tainted data * into an acceptable format for your data processing/model layer. * * This method must be able to deal with empty values. Usually this will diff --git a/tests/Symfony/Tests/Component/Form/ChoiceFieldTest.php b/tests/Symfony/Tests/Component/Form/ChoiceFieldTest.php index 8e84731341..5b1e516d8e 100644 --- a/tests/Symfony/Tests/Component/Form/ChoiceFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/ChoiceFieldTest.php @@ -140,7 +140,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getChoicesVariants */ - public function testBindSingleNonExpanded($choices) + public function testSubmitSingleNonExpanded($choices) { $field = new ChoiceField('name', array( 'multiple' => false, @@ -148,7 +148,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase 'choices' => $choices, )); - $field->bind('b'); + $field->submit('b'); $this->assertEquals('b', $field->getData()); $this->assertEquals('b', $field->getDisplayedData()); @@ -157,7 +157,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getChoicesVariants */ - public function testBindMultipleNonExpanded($choices) + public function testSubmitMultipleNonExpanded($choices) { $field = new ChoiceField('name', array( 'multiple' => true, @@ -165,7 +165,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase 'choices' => $choices, )); - $field->bind(array('a', 'b')); + $field->submit(array('a', 'b')); $this->assertEquals(array('a', 'b'), $field->getData()); $this->assertEquals(array('a', 'b'), $field->getDisplayedData()); @@ -174,7 +174,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getChoicesVariants */ - public function testBindSingleExpanded($choices) + public function testSubmitSingleExpanded($choices) { $field = new ChoiceField('name', array( 'multiple' => false, @@ -182,7 +182,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase 'choices' => $choices, )); - $field->bind('b'); + $field->submit('b'); $this->assertSame('b', $field->getData()); $this->assertSame(false, $field['a']->getData()); @@ -201,7 +201,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getNumericChoicesVariants */ - public function testBindSingleExpandedNumericChoices($choices) + public function testSubmitSingleExpandedNumericChoices($choices) { $field = new ChoiceField('name', array( 'multiple' => false, @@ -209,7 +209,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase 'choices' => $choices, )); - $field->bind('1'); + $field->submit('1'); $this->assertSame(1, $field->getData()); $this->assertSame(false, $field[0]->getData()); @@ -228,7 +228,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getChoicesVariants */ - public function testBindMultipleExpanded($choices) + public function testSubmitMultipleExpanded($choices) { $field = new ChoiceField('name', array( 'multiple' => true, @@ -236,7 +236,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase 'choices' => $choices, )); - $field->bind(array('a' => 'a', 'b' => 'b')); + $field->submit(array('a' => 'a', 'b' => 'b')); $this->assertSame(array('a', 'b'), $field->getData()); $this->assertSame(true, $field['a']->getData()); @@ -255,7 +255,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getNumericChoicesVariants */ - public function testBindMultipleExpandedNumericChoices($choices) + public function testSubmitMultipleExpandedNumericChoices($choices) { $field = new ChoiceField('name', array( 'multiple' => true, @@ -263,7 +263,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase 'choices' => $choices, )); - $field->bind(array(1 => 1, 2 => 2)); + $field->submit(array(1 => 1, 2 => 2)); $this->assertSame(array(1, 2), $field->getData()); $this->assertSame(false, $field[0]->getData()); diff --git a/tests/Symfony/Tests/Component/Form/CollectionFieldTest.php b/tests/Symfony/Tests/Component/Form/CollectionFieldTest.php index 9f39b91629..e066f56ce8 100644 --- a/tests/Symfony/Tests/Component/Form/CollectionFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/CollectionFieldTest.php @@ -81,11 +81,11 @@ class CollectionFieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals(2, count($field)); } - public function testNotResizedIfBoundWithMissingData() + public function testNotResizedIfSubmittedWithMissingData() { $field = new CollectionField(new TestField('emails')); $field->setData(array('foo@foo.com', 'bar@bar.com')); - $field->bind(array('foo@bar.com')); + $field->submit(array('foo@bar.com')); $this->assertTrue($field->has('0')); $this->assertTrue($field->has('1')); @@ -93,37 +93,37 @@ class CollectionFieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals(null, $field[1]->getData()); } - public function testResizedIfBoundWithMissingDataAndModifiable() + public function testResizedIfSubmittedWithMissingDataAndModifiable() { $field = new CollectionField(new TestField('emails'), array( 'modifiable' => true, )); $field->setData(array('foo@foo.com', 'bar@bar.com')); - $field->bind(array('foo@bar.com')); + $field->submit(array('foo@bar.com')); $this->assertTrue($field->has('0')); $this->assertFalse($field->has('1')); $this->assertEquals('foo@bar.com', $field[0]->getData()); } - public function testNotResizedIfBoundWithExtraData() + public function testNotResizedIfSubmittedWithExtraData() { $field = new CollectionField(new TestField('emails')); $field->setData(array('foo@bar.com')); - $field->bind(array('foo@foo.com', 'bar@bar.com')); + $field->submit(array('foo@foo.com', 'bar@bar.com')); $this->assertTrue($field->has('0')); $this->assertFalse($field->has('1')); $this->assertEquals('foo@foo.com', $field[0]->getData()); } - public function testResizedUpIfBoundWithExtraDataAndModifiable() + public function testResizedUpIfSubmittedWithExtraDataAndModifiable() { $field = new CollectionField(new TestField('emails'), array( 'modifiable' => true, )); $field->setData(array('foo@bar.com')); - $field->bind(array('foo@foo.com', 'bar@bar.com')); + $field->submit(array('foo@foo.com', 'bar@bar.com')); $this->assertTrue($field->has('0')); $this->assertTrue($field->has('1')); @@ -132,13 +132,13 @@ class CollectionFieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('foo@foo.com', 'bar@bar.com'), $field->getData()); } - public function testResizedDownIfBoundWithLessDataAndModifiable() + public function testResizedDownIfSubmittedWithLessDataAndModifiable() { $field = new CollectionField(new TestField('emails'), array( 'modifiable' => true, )); $field->setData(array('foo@bar.com', 'bar@bar.com')); - $field->bind(array('foo@foo.com')); + $field->submit(array('foo@foo.com')); $this->assertTrue($field->has('0')); $this->assertFalse($field->has('1')); diff --git a/tests/Symfony/Tests/Component/Form/DateFieldTest.php b/tests/Symfony/Tests/Component/Form/DateFieldTest.php index 21e9f1fd67..b9367c988a 100644 --- a/tests/Symfony/Tests/Component/Form/DateFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/DateFieldTest.php @@ -23,31 +23,31 @@ class DateFieldTest extends DateTimeTestCase \Locale::setDefault('de_AT'); } - public function testBind_fromInput_dateTime() + public function testSubmit_fromInput_dateTime() { $field = new DateField('name', array('widget' => 'input', 'type' => DateField::DATETIME)); - $field->bind('2.6.2010'); + $field->submit('2.6.2010'); $this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $field->getData()); $this->assertEquals('02.06.2010', $field->getDisplayedData()); } - public function testBind_fromInput_string() + public function testSubmit_fromInput_string() { $field = new DateField('name', array('widget' => 'input', 'type' => DateField::STRING)); - $field->bind('2.6.2010'); + $field->submit('2.6.2010'); $this->assertEquals('2010-06-02', $field->getData()); $this->assertEquals('02.06.2010', $field->getDisplayedData()); } - public function testBind_fromInput_timestamp() + public function testSubmit_fromInput_timestamp() { $field = new DateField('name', array('widget' => 'input', 'type' => DateField::TIMESTAMP)); - $field->bind('2.6.2010'); + $field->submit('2.6.2010'); $dateTime = new \DateTime('2010-06-02 UTC'); @@ -55,7 +55,7 @@ class DateFieldTest extends DateTimeTestCase $this->assertEquals('02.06.2010', $field->getDisplayedData()); } - public function testBind_fromInput_raw() + public function testSubmit_fromInput_raw() { $field = new DateField('name', array( 'data_timezone' => 'UTC', @@ -64,7 +64,7 @@ class DateFieldTest extends DateTimeTestCase 'type' => DateField::RAW, )); - $field->bind('2.6.2010'); + $field->submit('2.6.2010'); $output = array( 'day' => '2', @@ -76,7 +76,7 @@ class DateFieldTest extends DateTimeTestCase $this->assertEquals('02.06.2010', $field->getDisplayedData()); } - public function testBind_fromChoice() + public function testSubmit_fromChoice() { $field = new DateField('name', array('widget' => DateField::CHOICE)); @@ -86,7 +86,7 @@ class DateFieldTest extends DateTimeTestCase 'year' => '2010', ); - $field->bind($input); + $field->submit($input); $dateTime = new \DateTime('2010-06-02 UTC'); @@ -94,7 +94,7 @@ class DateFieldTest extends DateTimeTestCase $this->assertEquals($input, $field->getDisplayedData()); } - public function testBind_fromChoice_empty() + public function testSubmit_fromChoice_empty() { $field = new DateField('name', array('widget' => DateField::CHOICE, 'required' => false)); @@ -104,7 +104,7 @@ class DateFieldTest extends DateTimeTestCase 'year' => '', ); - $field->bind($input); + $field->submit($input); $this->assertSame(null, $field->getData()); $this->assertEquals($input, $field->getDisplayedData()); @@ -132,7 +132,7 @@ class DateFieldTest extends DateTimeTestCase 'years' => array(2010, 2011), )); - $field->bind('2.6.2010'); + $field->submit('2.6.2010'); $this->assertTrue($field->isYearWithinRange()); } @@ -144,7 +144,7 @@ class DateFieldTest extends DateTimeTestCase 'years' => array(2010, 2011), )); - $field->bind(''); + $field->submit(''); $this->assertTrue($field->isYearWithinRange()); } @@ -156,7 +156,7 @@ class DateFieldTest extends DateTimeTestCase 'years' => array(2010, 2012), )); - $field->bind('2.6.2011'); + $field->submit('2.6.2011'); $this->assertFalse($field->isYearWithinRange()); } @@ -168,7 +168,7 @@ class DateFieldTest extends DateTimeTestCase 'months' => array(6, 7), )); - $field->bind('2.6.2010'); + $field->submit('2.6.2010'); $this->assertTrue($field->isMonthWithinRange()); } @@ -180,7 +180,7 @@ class DateFieldTest extends DateTimeTestCase 'months' => array(6, 7), )); - $field->bind(''); + $field->submit(''); $this->assertTrue($field->isMonthWithinRange()); } @@ -192,7 +192,7 @@ class DateFieldTest extends DateTimeTestCase 'months' => array(6, 8), )); - $field->bind('2.7.2010'); + $field->submit('2.7.2010'); $this->assertFalse($field->isMonthWithinRange()); } @@ -204,7 +204,7 @@ class DateFieldTest extends DateTimeTestCase 'days' => array(6, 7), )); - $field->bind('6.6.2010'); + $field->submit('6.6.2010'); $this->assertTrue($field->isDayWithinRange()); } @@ -216,7 +216,7 @@ class DateFieldTest extends DateTimeTestCase 'days' => array(6, 7), )); - $field->bind(''); + $field->submit(''); $this->assertTrue($field->isDayWithinRange()); } @@ -228,7 +228,7 @@ class DateFieldTest extends DateTimeTestCase 'days' => array(6, 8), )); - $field->bind('7.6.2010'); + $field->submit('7.6.2010'); $this->assertFalse($field->isDayWithinRange()); } diff --git a/tests/Symfony/Tests/Component/Form/DateTimeFieldTest.php b/tests/Symfony/Tests/Component/Form/DateTimeFieldTest.php index 0142560731..6ea8fbff1d 100644 --- a/tests/Symfony/Tests/Component/Form/DateTimeFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/DateTimeFieldTest.php @@ -19,7 +19,7 @@ use Symfony\Component\Form\TimeField; class DateTimeFieldTest extends DateTimeTestCase { - public function testBind_dateTime() + public function testSubmit_dateTime() { $field = new DateTimeField('name', array( 'data_timezone' => 'UTC', @@ -29,7 +29,7 @@ class DateTimeFieldTest extends DateTimeTestCase 'type' => DateTimeField::DATETIME, )); - $field->bind(array( + $field->submit(array( 'date' => array( 'day' => '2', 'month' => '6', @@ -46,7 +46,7 @@ class DateTimeFieldTest extends DateTimeTestCase $this->assertDateTimeEquals($dateTime, $field->getData()); } - public function testBind_string() + public function testSubmit_string() { $field = new DateTimeField('name', array( 'data_timezone' => 'UTC', @@ -56,7 +56,7 @@ class DateTimeFieldTest extends DateTimeTestCase 'time_widget' => TimeField::CHOICE, )); - $field->bind(array( + $field->submit(array( 'date' => array( 'day' => '2', 'month' => '6', @@ -71,7 +71,7 @@ class DateTimeFieldTest extends DateTimeTestCase $this->assertEquals('2010-06-02 03:04:00', $field->getData()); } - public function testBind_timestamp() + public function testSubmit_timestamp() { $field = new DateTimeField('name', array( 'data_timezone' => 'UTC', @@ -81,7 +81,7 @@ class DateTimeFieldTest extends DateTimeTestCase 'time_widget' => TimeField::CHOICE, )); - $field->bind(array( + $field->submit(array( 'date' => array( 'day' => '2', 'month' => '6', @@ -98,7 +98,7 @@ class DateTimeFieldTest extends DateTimeTestCase $this->assertEquals($dateTime->format('U'), $field->getData()); } - public function testBind_withSeconds() + public function testSubmit_withSeconds() { $field = new DateTimeField('name', array( 'data_timezone' => 'UTC', @@ -124,12 +124,12 @@ class DateTimeFieldTest extends DateTimeTestCase ), ); - $field->bind($input); + $field->submit($input); $this->assertDateTimeEquals(new \DateTime('2010-06-02 03:04:05 UTC'), $field->getData()); } - public function testBind_differentTimezones() + public function testSubmit_differentTimezones() { $field = new DateTimeField('name', array( 'data_timezone' => 'America/New_York', @@ -143,7 +143,7 @@ class DateTimeFieldTest extends DateTimeTestCase $dateTime = new \DateTime('2010-06-02 03:04:05 Pacific/Tahiti'); - $field->bind(array( + $field->submit(array( 'date' => array( 'day' => (int)$dateTime->format('d'), 'month' => (int)$dateTime->format('m'), diff --git a/tests/Symfony/Tests/Component/Form/EntityChoiceFieldTest.php b/tests/Symfony/Tests/Component/Form/EntityChoiceFieldTest.php index bb07fbdb6e..21faab8f5d 100644 --- a/tests/Symfony/Tests/Component/Form/EntityChoiceFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/EntityChoiceFieldTest.php @@ -127,7 +127,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase }, )); - $field->bind('2'); + $field->submit('2'); } /** @@ -175,33 +175,33 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase $this->assertEquals(array(), $field->getDisplayedData()); } - public function testBindSingle_null() + public function testSubmitSingle_null() { $field = new EntityChoiceField('name', array( 'multiple' => false, 'em' => $this->em, 'class' => self::SINGLE_IDENT_CLASS, )); - $field->bind(null); + $field->submit(null); $this->assertEquals(null, $field->getData()); $this->assertEquals('', $field->getDisplayedData()); } - public function testBindMultiple_null() + public function testSubmitMultiple_null() { $field = new EntityChoiceField('name', array( 'multiple' => true, 'em' => $this->em, 'class' => self::SINGLE_IDENT_CLASS, )); - $field->bind(null); + $field->submit(null); $this->assertEquals(new ArrayCollection(), $field->getData()); $this->assertEquals(array(), $field->getDisplayedData()); } - public function testBindSingleNonExpanded_singleIdentifier() + public function testSubmitSingleNonExpanded_singleIdentifier() { $entity1 = new SingleIdentEntity(1, 'Foo'); $entity2 = new SingleIdentEntity(2, 'Bar'); @@ -216,14 +216,14 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase 'property' => 'name', )); - $field->bind('2'); + $field->submit('2'); $this->assertTrue($field->isTransformationSuccessful()); $this->assertEquals($entity2, $field->getData()); $this->assertEquals(2, $field->getDisplayedData()); } - public function testBindSingleNonExpanded_compositeIdentifier() + public function testSubmitSingleNonExpanded_compositeIdentifier() { $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); @@ -239,14 +239,14 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase )); // the collection key is used here - $field->bind('1'); + $field->submit('1'); $this->assertTrue($field->isTransformationSuccessful()); $this->assertEquals($entity2, $field->getData()); $this->assertEquals(1, $field->getDisplayedData()); } - public function testBindMultipleNonExpanded_singleIdentifier() + public function testSubmitMultipleNonExpanded_singleIdentifier() { $entity1 = new SingleIdentEntity(1, 'Foo'); $entity2 = new SingleIdentEntity(2, 'Bar'); @@ -262,7 +262,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase 'property' => 'name', )); - $field->bind(array('1', '3')); + $field->submit(array('1', '3')); $expected = new ArrayCollection(array($entity1, $entity3)); @@ -271,7 +271,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase $this->assertEquals(array(1, 3), $field->getDisplayedData()); } - public function testBindMultipleNonExpanded_singleIdentifier_existingData() + public function testSubmitMultipleNonExpanded_singleIdentifier_existingData() { $entity1 = new SingleIdentEntity(1, 'Foo'); $entity2 = new SingleIdentEntity(2, 'Bar'); @@ -290,7 +290,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase $existing = new ArrayCollection(array($entity2)); $field->setData($existing); - $field->bind(array('1', '3')); + $field->submit(array('1', '3')); // entry with index 0 was removed $expected = new ArrayCollection(array(1 => $entity1, 2 => $entity3)); @@ -302,7 +302,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase $this->assertEquals(array(1, 3), $field->getDisplayedData()); } - public function testBindMultipleNonExpanded_compositeIdentifier() + public function testSubmitMultipleNonExpanded_compositeIdentifier() { $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); @@ -319,7 +319,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase )); // because of the composite key collection keys are used - $field->bind(array('0', '2')); + $field->submit(array('0', '2')); $expected = new ArrayCollection(array($entity1, $entity3)); @@ -328,7 +328,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase $this->assertEquals(array(0, 2), $field->getDisplayedData()); } - public function testBindMultipleNonExpanded_compositeIdentifier_existingData() + public function testSubmitMultipleNonExpanded_compositeIdentifier_existingData() { $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); @@ -347,7 +347,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase $existing = new ArrayCollection(array($entity2)); $field->setData($existing); - $field->bind(array('0', '2')); + $field->submit(array('0', '2')); // entry with index 0 was removed $expected = new ArrayCollection(array(1 => $entity1, 2 => $entity3)); @@ -359,7 +359,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase $this->assertEquals(array(0, 2), $field->getDisplayedData()); } - public function testBindSingleExpanded() + public function testSubmitSingleExpanded() { $entity1 = new SingleIdentEntity(1, 'Foo'); $entity2 = new SingleIdentEntity(2, 'Bar'); @@ -374,7 +374,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase 'property' => 'name', )); - $field->bind('2'); + $field->submit('2'); $this->assertTrue($field->isTransformationSuccessful()); $this->assertEquals($entity2, $field->getData()); @@ -385,7 +385,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase $this->assertSame(array('1' => '', '2' => '1'), $field->getDisplayedData()); } - public function testBindMultipleExpanded() + public function testSubmitMultipleExpanded() { $entity1 = new SingleIdentEntity(1, 'Foo'); $entity2 = new SingleIdentEntity(2, 'Bar'); @@ -401,7 +401,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase 'property' => 'name', )); - $field->bind(array('1' => '1', '3' => '3')); + $field->submit(array('1' => '1', '3' => '3')); $expected = new ArrayCollection(array($entity1, $entity3)); @@ -432,7 +432,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase 'property' => 'name', )); - $field->bind('2'); + $field->submit('2'); $this->assertEquals(array(1 => 'Foo', 2 => 'Bar'), $field->getOtherChoices()); $this->assertTrue($field->isTransformationSuccessful()); @@ -455,7 +455,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase 'property' => 'name', )); - $field->bind('3'); + $field->submit('3'); $this->assertFalse($field->isTransformationSuccessful()); $this->assertNull($field->getData()); @@ -476,7 +476,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase 'property' => 'name', )); - $field->bind('2'); + $field->submit('2'); $this->assertFalse($field->isTransformationSuccessful()); $this->assertNull($field->getData()); @@ -500,7 +500,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase 'property' => 'name', )); - $field->bind('3'); + $field->submit('3'); $this->assertFalse($field->isTransformationSuccessful()); $this->assertNull($field->getData()); @@ -524,7 +524,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase 'property' => 'name', )); - $field->bind('3'); + $field->submit('3'); $this->assertFalse($field->isTransformationSuccessful()); $this->assertNull($field->getData()); @@ -548,7 +548,7 @@ class EntityChoiceFieldTest extends DoctrineOrmTestCase 'property' => 'name', )); - $field->bind('2'); + $field->submit('2'); $this->assertFalse($field->isTransformationSuccessful()); $this->assertNull($field->getData()); diff --git a/tests/Symfony/Tests/Component/Form/FieldFactory/FieldFactoryTest.php b/tests/Symfony/Tests/Component/Form/FieldFactory/FieldFactoryTest.php index d178a474c3..19c46488ec 100644 --- a/tests/Symfony/Tests/Component/Form/FieldFactory/FieldFactoryTest.php +++ b/tests/Symfony/Tests/Component/Form/FieldFactory/FieldFactoryTest.php @@ -27,13 +27,10 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase public function testGetInstanceCreatesClassWithHighestConfidence() { - $object = new \stdClass(); - $object->firstName = 'Bernhard'; - $guesser1 = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryGuesserInterface'); $guesser1->expects($this->once()) ->method('guessClass') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(new FieldFactoryClassGuess( 'Symfony\Component\Form\TextField', array('max_length' => 10), @@ -43,7 +40,7 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase $guesser2 = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryGuesserInterface'); $guesser2->expects($this->once()) ->method('guessClass') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(new FieldFactoryClassGuess( 'Symfony\Component\Form\PasswordField', array('max_length' => 7), @@ -51,7 +48,7 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase ))); $factory = new FieldFactory(array($guesser1, $guesser2)); - $field = $factory->getInstance($object, 'firstName'); + $field = $factory->getInstance('Application\Author', 'firstName'); $this->assertEquals('Symfony\Component\Form\PasswordField', get_class($field)); $this->assertEquals(7, $field->getMaxLength()); @@ -59,31 +56,25 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase public function testGetInstanceThrowsExceptionIfNoClassIsFound() { - $object = new \stdClass(); - $object->firstName = 'Bernhard'; - $guesser = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryGuesserInterface'); $guesser->expects($this->once()) ->method('guessClass') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(null)); $factory = new FieldFactory(array($guesser)); $this->setExpectedException('\RuntimeException'); - $field = $factory->getInstance($object, 'firstName'); + $field = $factory->getInstance('Application\Author', 'firstName'); } public function testOptionsCanBeOverridden() { - $object = new \stdClass(); - $object->firstName = 'Bernhard'; - $guesser = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryGuesserInterface'); $guesser->expects($this->once()) ->method('guessClass') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(new FieldFactoryClassGuess( 'Symfony\Component\Form\TextField', array('max_length' => 10), @@ -91,7 +82,7 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase ))); $factory = new FieldFactory(array($guesser)); - $field = $factory->getInstance($object, 'firstName', array('max_length' => 11)); + $field = $factory->getInstance('Application\Author', 'firstName', array('max_length' => 11)); $this->assertEquals('Symfony\Component\Form\TextField', get_class($field)); $this->assertEquals(11, $field->getMaxLength()); @@ -99,13 +90,10 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase public function testGetInstanceUsesMaxLengthIfFoundAndTextField() { - $object = new \stdClass(); - $object->firstName = 'Bernhard'; - $guesser1 = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryGuesserInterface'); $guesser1->expects($this->once()) ->method('guessClass') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(new FieldFactoryClassGuess( 'Symfony\Component\Form\TextField', array('max_length' => 10), @@ -113,7 +101,7 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase ))); $guesser1->expects($this->once()) ->method('guessMaxLength') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(new FieldFactoryGuess( 15, FieldFactoryGuess::MEDIUM_CONFIDENCE @@ -122,14 +110,14 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase $guesser2 = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryGuesserInterface'); $guesser2->expects($this->once()) ->method('guessMaxLength') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(new FieldFactoryGuess( 20, FieldFactoryGuess::HIGH_CONFIDENCE ))); $factory = new FieldFactory(array($guesser1, $guesser2)); - $field = $factory->getInstance($object, 'firstName'); + $field = $factory->getInstance('Application\Author', 'firstName'); $this->assertEquals('Symfony\Component\Form\TextField', get_class($field)); $this->assertEquals(20, $field->getMaxLength()); @@ -137,13 +125,10 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase public function testGetInstanceUsesMaxLengthIfFoundAndSubclassOfTextField() { - $object = new \stdClass(); - $object->firstName = 'Bernhard'; - $guesser = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryGuesserInterface'); $guesser->expects($this->once()) ->method('guessClass') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(new FieldFactoryClassGuess( 'Symfony\Component\Form\PasswordField', array('max_length' => 10), @@ -151,14 +136,14 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase ))); $guesser->expects($this->once()) ->method('guessMaxLength') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(new FieldFactoryGuess( 15, FieldFactoryGuess::MEDIUM_CONFIDENCE ))); $factory = new FieldFactory(array($guesser)); - $field = $factory->getInstance($object, 'firstName'); + $field = $factory->getInstance('Application\Author', 'firstName'); $this->assertEquals('Symfony\Component\Form\PasswordField', get_class($field)); $this->assertEquals(15, $field->getMaxLength()); @@ -166,13 +151,10 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase public function testGetInstanceUsesRequiredSettingWithHighestConfidence() { - $object = new \stdClass(); - $object->firstName = 'Bernhard'; - $guesser1 = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryGuesserInterface'); $guesser1->expects($this->once()) ->method('guessClass') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(new FieldFactoryClassGuess( 'Symfony\Component\Form\TextField', array(), @@ -180,7 +162,7 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase ))); $guesser1->expects($this->once()) ->method('guessRequired') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(new FieldFactoryGuess( true, FieldFactoryGuess::MEDIUM_CONFIDENCE @@ -189,14 +171,14 @@ class FieldFactoryTest extends \PHPUnit_Framework_TestCase $guesser2 = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryGuesserInterface'); $guesser2->expects($this->once()) ->method('guessRequired') - ->with($this->equalTo($object), $this->equalTo('firstName')) + ->with($this->equalTo('Application\Author'), $this->equalTo('firstName')) ->will($this->returnValue(new FieldFactoryGuess( false, FieldFactoryGuess::HIGH_CONFIDENCE ))); $factory = new FieldFactory(array($guesser1, $guesser2)); - $field = $factory->getInstance($object, 'firstName'); + $field = $factory->getInstance('Application\Author', 'firstName'); $this->assertFalse($field->isRequired()); } diff --git a/tests/Symfony/Tests/Component/Form/FieldTest.php b/tests/Symfony/Tests/Component/Form/FieldTest.php index 4022cafe5d..91fe28fea0 100644 --- a/tests/Symfony/Tests/Component/Form/FieldTest.php +++ b/tests/Symfony/Tests/Component/Form/FieldTest.php @@ -95,28 +95,28 @@ class FieldTest extends \PHPUnit_Framework_TestCase public function testFieldWithNoErrorsIsValid() { - $this->field->bind('data'); + $this->field->submit('data'); $this->assertTrue($this->field->isValid()); } public function testFieldWithErrorsIsInvalid() { - $this->field->bind('data'); + $this->field->submit('data'); $this->field->addError(new FieldError('Some error')); $this->assertFalse($this->field->isValid()); } - public function testBindResetsErrors() + public function testSubmitResetsErrors() { $this->field->addError(new FieldError('Some error')); - $this->field->bind('data'); + $this->field->submit('data'); $this->assertTrue($this->field->isValid()); } - public function testUnboundFieldIsInvalid() + public function testUnsubmittedFieldIsInvalid() { $this->assertFalse($this->field->isValid()); } @@ -197,11 +197,11 @@ class FieldTest extends \PHPUnit_Framework_TestCase new RequiredOptionsField('name'); } - public function testIsBound() + public function testIsSubmitted() { - $this->assertFalse($this->field->isBound()); - $this->field->bind('symfony'); - $this->assertTrue($this->field->isBound()); + $this->assertFalse($this->field->isSubmitted()); + $this->field->submit('symfony'); + $this->assertTrue($this->field->isSubmitted()); } public function testDefaultValuesAreTransformedCorrectly() @@ -220,7 +220,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase $this->assertSame('', $this->field->getDisplayedData()); } - public function testBoundValuesAreTransformedCorrectly() + public function testSubmittedValuesAreTransformedCorrectly() { $valueTransformer = $this->createMockTransformer(); $normTransformer = $this->createMockTransformer(); @@ -261,14 +261,14 @@ class FieldTest extends \PHPUnit_Framework_TestCase ->with($this->equalTo('processed[reverse[0]]')) ->will($this->returnValue('transform[processed[reverse[0]]]')); - $field->bind(0); + $field->submit(0); $this->assertEquals('denorm[processed[reverse[0]]]', $field->getData()); $this->assertEquals('processed[reverse[0]]', $field->getNormalizedData()); $this->assertEquals('transform[processed[reverse[0]]]', $field->getDisplayedData()); } - public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue() + public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue() { $transformer = $this->createMockTransformer(); @@ -298,13 +298,13 @@ class FieldTest extends \PHPUnit_Framework_TestCase ->with($this->equalTo('processed')) ->will($this->returnValue('transform[processed]')); - $field->bind(''); + $field->submit(''); $this->assertSame('processed', $field->getData()); $this->assertEquals('transform[processed]', $field->getDisplayedData()); } - public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull() + public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull() { $transformer = $this->createMockTransformer(); @@ -325,15 +325,15 @@ class FieldTest extends \PHPUnit_Framework_TestCase ->with($this->identicalTo(null)) ->will($this->returnValue('')); - $field->bind(''); + $field->submit(''); $this->assertSame(null, $field->getData()); $this->assertEquals('', $field->getDisplayedData()); } - public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer() + public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer() { - $this->field->bind(''); + $this->field->submit(''); $this->assertSame(null, $this->field->getData()); $this->assertEquals('', $this->field->getDisplayedData()); @@ -369,7 +369,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals('transform[norm[0]]', $field->getDisplayedData()); } - public function testBoundValuesAreTrimmedBeforeTransforming() + public function testSubmittedValuesAreTrimmedBeforeTransforming() { // The value is passed to the value transformer $transformer = $this->createMockTransformer(); @@ -388,13 +388,13 @@ class FieldTest extends \PHPUnit_Framework_TestCase 'value_transformer' => $transformer, )); - $field->bind(' a '); + $field->submit(' a '); $this->assertEquals('a', $field->getDisplayedData()); $this->assertEquals('reverse[a]', $field->getData()); } - public function testBoundValuesAreNotTrimmedBeforeTransformingIfDisabled() + public function testSubmittedValuesAreNotTrimmedBeforeTransformingIfDisabled() { // The value is passed to the value transformer $transformer = $this->createMockTransformer(); @@ -414,34 +414,34 @@ class FieldTest extends \PHPUnit_Framework_TestCase 'value_transformer' => $transformer, )); - $field->bind(' a '); + $field->submit(' a '); $this->assertEquals(' a ', $field->getDisplayedData()); $this->assertEquals('reverse[ a ]', $field->getData()); } /* - * This is important so that bind() can work even if setData() was not called + * This is important so that submit() can work even if setData() was not called * before */ - public function testUpdatePropertyTreatsEmptyValuesAsArrays() + public function testWritePropertyTreatsEmptyValuesAsArrays() { $array = null; $field = new TestField('firstName'); - $field->bind('Bernhard'); - $field->updateProperty($array); + $field->submit('Bernhard'); + $field->writeProperty($array); $this->assertEquals(array('firstName' => 'Bernhard'), $array); } - public function testUpdatePropertyDoesNotUpdatePropertyIfPropertyPathIsEmpty() + public function testWritePropertyDoesNotWritePropertyIfPropertyPathIsEmpty() { $object = new Author(); $field = new TestField('firstName', array('property_path' => null)); - $field->bind('Bernhard'); - $field->updateProperty($object); + $field->submit('Bernhard'); + $field->writeProperty($object); $this->assertEquals(null, $object->firstName); } @@ -452,7 +452,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase 'trim' => false, )); - $field->bind('a'); + $field->submit('a'); $this->assertEquals('a', $field->getDisplayedData()); $this->assertTrue($field->isTransformationSuccessful()); @@ -471,7 +471,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase 'value_transformer' => $transformer, )); - $field->bind('a'); + $field->submit('a'); $this->assertEquals('a', $field->getDisplayedData()); $this->assertFalse($field->isTransformationSuccessful()); diff --git a/tests/Symfony/Tests/Component/Form/FileFieldTest.php b/tests/Symfony/Tests/Component/Form/FileFieldTest.php index 31ca55ac6f..61b0900294 100644 --- a/tests/Symfony/Tests/Component/Form/FileFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/FileFieldTest.php @@ -52,7 +52,7 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase file_put_contents($path, 'foobar'); } - public function testBindUploadsNewFiles() + public function testSubmitUploadsNewFiles() { $tmpDir = realpath(self::$tmpDir); $tmpName = md5(session_id() . '$secret$' . '12345'); @@ -73,7 +73,7 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase ->method('getOriginalName') ->will($this->returnValue('original_name.jpg')); - $this->field->bind(array( + $this->field->submit(array( 'file' => $file, 'token' => '12345', 'original_name' => '', @@ -91,12 +91,12 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase $this->assertTrue($this->field->isUploadComplete()); } - public function testBindKeepsUploadedFilesOnErrors() + public function testSubmitKeepsUploadedFilesOnErrors() { $tmpPath = self::$tmpDir . '/' . md5(session_id() . '$secret$' . '12345'); $this->createTmpFile($tmpPath); - $this->field->bind(array( + $this->field->submit(array( 'file' => '', 'token' => '12345', 'original_name' => 'original_name.jpg', @@ -111,7 +111,7 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals(realpath($tmpPath), realpath($this->field->getData())); } - public function testBindKeepsOldFileIfNotOverwritten() + public function testSubmitKeepsOldFileIfNotOverwritten() { $oldPath = tempnam(sys_get_temp_dir(), 'FileFieldTest'); $this->createTmpFile($oldPath); @@ -120,7 +120,7 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oldPath, $this->field->getData()); - $this->field->bind(array( + $this->field->submit(array( 'file' => '', 'token' => '12345', 'original_name' => '', @@ -135,14 +135,14 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oldPath, $this->field->getData()); } - public function testBindHandlesUploadErrIniSize() + public function testSubmitHandlesUploadErrIniSize() { $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false); $file->expects($this->any()) ->method('getError') ->will($this->returnValue(UPLOAD_ERR_INI_SIZE)); - $this->field->bind(array( + $this->field->submit(array( 'file' => $file, 'token' => '12345', 'original_name' => '' @@ -151,14 +151,14 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase $this->assertTrue($this->field->isIniSizeExceeded()); } - public function testBindHandlesUploadErrFormSize() + public function testSubmitHandlesUploadErrFormSize() { $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false); $file->expects($this->any()) ->method('getError') ->will($this->returnValue(UPLOAD_ERR_FORM_SIZE)); - $this->field->bind(array( + $this->field->submit(array( 'file' => $file, 'token' => '12345', 'original_name' => '' @@ -167,14 +167,14 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase $this->assertTrue($this->field->isFormSizeExceeded()); } - public function testBindHandlesUploadErrPartial() + public function testSubmitHandlesUploadErrPartial() { $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false); $file->expects($this->any()) ->method('getError') ->will($this->returnValue(UPLOAD_ERR_PARTIAL)); - $this->field->bind(array( + $this->field->submit(array( 'file' => $file, 'token' => '12345', 'original_name' => '' @@ -183,7 +183,7 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase $this->assertFalse($this->field->isUploadComplete()); } - public function testBindThrowsExceptionOnUploadErrNoTmpDir() + public function testSubmitThrowsExceptionOnUploadErrNoTmpDir() { $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false); $file->expects($this->any()) @@ -192,14 +192,14 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase $this->setExpectedException('Symfony\Component\Form\Exception\FormException'); - $this->field->bind(array( + $this->field->submit(array( 'file' => $file, 'token' => '12345', 'original_name' => '' )); } - public function testBindThrowsExceptionOnUploadErrCantWrite() + public function testSubmitThrowsExceptionOnUploadErrCantWrite() { $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false); $file->expects($this->any()) @@ -208,14 +208,14 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase $this->setExpectedException('Symfony\Component\Form\Exception\FormException'); - $this->field->bind(array( + $this->field->submit(array( 'file' => $file, 'token' => '12345', 'original_name' => '' )); } - public function testBindThrowsExceptionOnUploadErrExtension() + public function testSubmitThrowsExceptionOnUploadErrExtension() { $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false); $file->expects($this->any()) @@ -224,7 +224,7 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase $this->setExpectedException('Symfony\Component\Form\Exception\FormException'); - $this->field->bind(array( + $this->field->submit(array( 'file' => $file, 'token' => '12345', 'original_name' => '' diff --git a/tests/Symfony/Tests/Component/Form/FormTest.php b/tests/Symfony/Tests/Component/Form/FormTest.php index 2aa9e366ff..aff86d7eb0 100644 --- a/tests/Symfony/Tests/Component/Form/FormTest.php +++ b/tests/Symfony/Tests/Component/Form/FormTest.php @@ -124,7 +124,7 @@ class FormTest extends \PHPUnit_Framework_TestCase public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled() { - $this->form->bind(array()); + $this->form->submit(array()); $this->assertTrue($this->form->isCsrfTokenValid()); } @@ -144,7 +144,7 @@ class FormTest extends \PHPUnit_Framework_TestCase $field = $form->getCsrfFieldName(); - $form->bind(array($field => 'ABCDEF')); + $form->submit(array($field => 'ABCDEF')); $this->assertTrue($form->isCsrfTokenValid()); } @@ -164,7 +164,7 @@ class FormTest extends \PHPUnit_Framework_TestCase $field = $form->getCsrfFieldName(); - $form->bind(array($field => 'ABCDEF')); + $form->submit(array($field => 'ABCDEF')); $this->assertFalse($form->isCsrfTokenValid()); } @@ -205,21 +205,23 @@ class FormTest extends \PHPUnit_Framework_TestCase ->method('validate') ->with($this->equalTo($form), $this->equalTo(array('group'))); - $form->bind(array()); // irrelevant + // data is irrelevant + $form->bind($this->createPostRequest()); } -// public function testBindThrowsExceptionIfNoValidatorIsSet() -// { -// $field = $this->createMockField('firstName'); -// $form = new Form('author'); -// $form->add($field); -// -// $this->setExpectedException('Symfony\Component\Form\Exception\FormException'); -// -// $form->bind(array()); // irrelevant -// } + public function testBindThrowsExceptionIfNoValidatorIsSet() + { + $field = $this->createMockField('firstName'); + $form = new Form('author'); + $form->add($field); - public function testBindRequest() + $this->setExpectedException('Symfony\Component\Form\Exception\FormException'); + + // data is irrelevant + $form->bind($this->createPostRequest()); + } + + public function testBindReadsRequestData() { $values = array( 'author' => array( @@ -237,8 +239,6 @@ class FormTest extends \PHPUnit_Framework_TestCase ), ); - $request = new Request(array(), $values, array(), array(), $files); - $form = new Form('author', array('validator' => $this->validator)); $form->add(new TestField('name')); $imageForm = new Form('image'); @@ -246,7 +246,7 @@ class FormTest extends \PHPUnit_Framework_TestCase $imageForm->add(new TestField('filename')); $form->add($imageForm); - $form->bindRequest($request); + $form->bind($this->createPostRequest($values, $files)); $file = new UploadedFile('abcdef.png', 'upload.png', 'image/png', 123, UPLOAD_ERR_OK); @@ -255,6 +255,16 @@ class FormTest extends \PHPUnit_Framework_TestCase $this->assertEquals($file, $form['image']['file']->getData()); } + public function testBindAcceptsObject() + { + $object = new \stdClass(); + $form = new Form('author', array('validator' => $this->validator)); + + $form->bind(new Request(), $object); + + $this->assertSame($object, $form->getData()); + } + public function testBindGlobals() { $_POST = array( @@ -272,6 +282,8 @@ class FormTest extends \PHPUnit_Framework_TestCase 'type' => array('image' => array('file' => 'image/png')), ), ); + // don't erase other variables + $_SERVER['REQUEST_METHOD'] = 'POST'; $form = new Form('author', array('validator' => $this->validator)); @@ -290,7 +302,7 @@ class FormTest extends \PHPUnit_Framework_TestCase $this->assertEquals($file, $form['image']['file']->getData()); } - public function testUpdateFromPropertyIsIgnoredIfPropertyPathIsNull() + public function testReadPropertyIsIgnoredIfPropertyPathIsNull() { $author = new Author(); $author->child = new Author(); @@ -299,13 +311,13 @@ class FormTest extends \PHPUnit_Framework_TestCase $form = new Form('child'); $form->setData($standaloneChild); $form->setPropertyPath(null); - $form->updateFromProperty($author); + $form->readProperty($author); // should not be $author->child!! $this->assertSame($standaloneChild, $form->getData()); } - public function testUpdatePropertyIsIgnoredIfPropertyPathIsNull() + public function testWritePropertyIsIgnoredIfPropertyPathIsNull() { $author = new Author(); $author->child = $child = new Author(); @@ -314,7 +326,7 @@ class FormTest extends \PHPUnit_Framework_TestCase $form = new Form('child'); $form->setData($standaloneChild); $form->setPropertyPath(null); - $form->updateProperty($author); + $form->writeProperty($author); // $author->child was not modified $this->assertSame($child, $author->child); @@ -370,12 +382,12 @@ class FormTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, iterator_to_array($form)); } - public function testIsBound() + public function testIsSubmitted() { $form = new Form('author', array('validator' => $this->validator)); - $this->assertFalse($form->isBound()); - $form->bind(array('firstName' => 'Bernhard')); - $this->assertTrue($form->isBound()); + $this->assertFalse($form->isSubmitted()); + $form->submit(array('firstName' => 'Bernhard')); + $this->assertTrue($form->isSubmitted()); } public function testValidIfAllFieldsAreValid() @@ -384,7 +396,7 @@ class FormTest extends \PHPUnit_Framework_TestCase $form->add($this->createValidMockField('firstName')); $form->add($this->createValidMockField('lastName')); - $form->bind(array('firstName' => 'Bernhard', 'lastName' => 'Potencier')); + $form->submit(array('firstName' => 'Bernhard', 'lastName' => 'Potencier')); $this->assertTrue($form->isValid()); } @@ -395,20 +407,20 @@ class FormTest extends \PHPUnit_Framework_TestCase $form->add($this->createInvalidMockField('firstName')); $form->add($this->createValidMockField('lastName')); - $form->bind(array('firstName' => 'Bernhard', 'lastName' => 'Potencier')); + $form->submit(array('firstName' => 'Bernhard', 'lastName' => 'Potencier')); $this->assertFalse($form->isValid()); } - public function testInvalidIfBoundWithExtraFields() + public function testInvalidIfSubmittedWithExtraFields() { $form = new Form('author', array('validator' => $this->validator)); $form->add($this->createValidMockField('firstName')); $form->add($this->createValidMockField('lastName')); - $form->bind(array('foo' => 'bar', 'firstName' => 'Bernhard', 'lastName' => 'Potencier')); + $form->submit(array('foo' => 'bar', 'firstName' => 'Bernhard', 'lastName' => 'Potencier')); - $this->assertTrue($form->isBoundWithExtraFields()); + $this->assertTrue($form->isSubmittedWithExtraFields()); } public function testHasNoErrorsIfOnlyFieldHasErrors() @@ -416,12 +428,12 @@ class FormTest extends \PHPUnit_Framework_TestCase $form = new Form('author', array('validator' => $this->validator)); $form->add($this->createInvalidMockField('firstName')); - $form->bind(array('firstName' => 'Bernhard')); + $form->submit(array('firstName' => 'Bernhard')); $this->assertFalse($form->hasErrors()); } - public function testBindForwardsPreprocessedData() + public function testSubmitForwardsPreprocessedData() { $field = $this->createMockField('firstName'); @@ -440,23 +452,23 @@ class FormTest extends \PHPUnit_Framework_TestCase // The preprocessed data is then forwarded to the fields $field->expects($this->once()) - ->method('bind') + ->method('submit') ->with($this->equalTo('preprocessed[Bernhard]')); - $form->bind(array('firstName' => 'Bernhard')); + $form->submit(array('firstName' => 'Bernhard')); } - public function testBindForwardsNullIfValueIsMissing() + public function testSubmitForwardsNullIfValueIsMissing() { $field = $this->createMockField('firstName'); $field->expects($this->once()) - ->method('bind') + ->method('submit') ->with($this->equalTo(null)); $form = new Form('author', array('validator' => $this->validator)); $form->add($field); - $form->bind(array()); + $form->submit(array()); } public function testAddErrorMapsFieldValidationErrorsOntoFields() @@ -654,7 +666,7 @@ class FormTest extends \PHPUnit_Framework_TestCase { $form = new Form('author', array('validator' => $this->validator)); $form->add($this->createMockField('firstName')); - $form->bind(array('firstName' => 'Bernhard')); + $form->bind(new Request()); $this->setExpectedException('Symfony\Component\Form\Exception\AlreadyBoundException'); $form->add($this->createMockField('lastName')); @@ -666,9 +678,8 @@ class FormTest extends \PHPUnit_Framework_TestCase $field = $this->createMockField('firstName'); $field->expects($this->once()) - ->method('setParent'); - // PHPUnit fails to compare infinitely recursive objects - //->with($this->equalTo($form)); + ->method('setParent') + ->with($this->equalTo($form)); $form->add($field); } @@ -709,7 +720,7 @@ class FormTest extends \PHPUnit_Framework_TestCase ->method('getPropertyPath') ->will($this->returnValue(new PropertyPath('firstName'))); $field->expects($this->once()) - ->method('updateFromProperty') + ->method('readProperty') ->with($this->equalTo($transformedAuthor)); $form->add($field); @@ -732,7 +743,7 @@ class FormTest extends \PHPUnit_Framework_TestCase $field = $this->createMockField('firstName'); $field->expects($this->never()) - ->method('updateFromProperty'); + ->method('readProperty'); $form->add($field); } @@ -748,7 +759,7 @@ class FormTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException Symfony\Component\Form\Exception\FormException + * @expectedException Symfony\Component\Form\Exception\FieldDefinitionException */ public function testAddThrowsExceptionIfAnonymousField() { @@ -759,17 +770,22 @@ class FormTest extends \PHPUnit_Framework_TestCase $form->add($field); } + /** + * @expectedException Symfony\Component\Form\Exception\FormException + */ public function testAddThrowsExceptionIfStringButNoFieldFactory() { - $rootForm = $this->createMockForm(); - $rootForm->expects($this->once()) - ->method('getFieldFactory') - ->will($this->returnValue(null)); + $form = new Form('author', array('data_class' => 'Application\Entity')); - $form = new Form('author'); - $form->setParent($rootForm); + $form->add('firstName'); + } - $this->setExpectedException('\LogicException'); + /** + * @expectedException Symfony\Component\Form\Exception\FormException + */ + public function testAddThrowsExceptionIfStringButNoClass() + { + $form = new Form('author', array('field_factory' => new \stdClass())); $form->add('firstName'); } @@ -782,17 +798,15 @@ class FormTest extends \PHPUnit_Framework_TestCase $factory = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryInterface'); $factory->expects($this->once()) ->method('getInstance') - ->with($this->equalTo($author), $this->equalTo('firstName'), $this->equalTo(array('foo' => 'bar'))) + ->with($this->equalTo('stdClass'), $this->equalTo('firstName'), $this->equalTo(array('foo' => 'bar'))) ->will($this->returnValue($field)); - $rootForm = $this->createMockForm(); - $rootForm->expects($this->once()) - ->method('getFieldFactory') - ->will($this->returnValue($factory)); + $form = new Form('author', array( + 'data' => $author, + 'data_class' => 'stdClass', + 'field_factory' => $factory, + )); - $form = new Form('author'); - $form->setParent($rootForm); - $form->setData($author); $form->add('firstName', array('foo' => 'bar')); $this->assertSame($field, $form['firstName']); @@ -817,14 +831,14 @@ class FormTest extends \PHPUnit_Framework_TestCase $field = $this->createMockField('firstName'); $field->expects($this->once()) - ->method('updateFromProperty') + ->method('readProperty') ->with($this->equalTo($transformedAuthor)); $form->add($field); $field = $this->createMockField('lastName'); $field->expects($this->once()) - ->method('updateFromProperty') + ->method('readProperty') ->with($this->equalTo($transformedAuthor)); $form->add($field); @@ -866,14 +880,14 @@ class FormTest extends \PHPUnit_Framework_TestCase // top-level group because the nested group is virtual $field = $this->createMockField('firstName'); $field->expects($this->once()) - ->method('updateFromProperty') + ->method('readProperty') ->with($this->equalTo($author)); $nestedForm->add($field); $field = $this->createMockField('lastName'); $field->expects($this->once()) - ->method('updateFromProperty') + ->method('readProperty') ->with($this->equalTo($author)); $nestedForm->add($field); @@ -891,7 +905,28 @@ class FormTest extends \PHPUnit_Framework_TestCase $form->setData('foobar'); } - public function testBindUpdatesTransformedDataFromAllFields() + /** + * @expectedException Symfony\Component\Form\Exception\FormException + */ + public function testSetDataMatchesAgainstDataClass_fails() + { + $form = new Form('author', array( + 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author', + )); + + $form->setData(new \stdClass()); + } + + public function testSetDataMatchesAgainstDataClass_succeeds() + { + $form = new Form('author', array( + 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author', + )); + + $form->setData(new Author()); + } + + public function testSubmitUpdatesTransformedDataFromAllFields() { $originalAuthor = new Author(); $transformedAuthor = new Author(); @@ -914,19 +949,19 @@ class FormTest extends \PHPUnit_Framework_TestCase $field = $this->createMockField('firstName'); $field->expects($this->once()) - ->method('updateProperty') + ->method('writeProperty') ->with($this->equalTo($transformedAuthor)); $form->add($field); $field = $this->createMockField('lastName'); $field->expects($this->once()) - ->method('updateProperty') + ->method('writeProperty') ->with($this->equalTo($transformedAuthor)); $form->add($field); - $form->bind(array()); // irrelevant + $form->submit(array()); // irrelevant } public function testGetDataReturnsObject() @@ -978,7 +1013,7 @@ class FormTest extends \PHPUnit_Framework_TestCase $this->assertNotSame($clone['firstName'], $form['firstName']); } - public function testBindWithoutPriorSetData() + public function testSubmitWithoutPriorSetData() { return; // TODO $field = $this->createMockField('firstName'); @@ -989,7 +1024,7 @@ class FormTest extends \PHPUnit_Framework_TestCase $form = new Form('author'); $form->add($field); - $form->bind(array('firstName' => 'Bernhard')); + $form->submit(array('firstName' => 'Bernhard')); $this->assertEquals(array('firstName' => 'Bernhard'), $form->getData()); } @@ -1128,4 +1163,11 @@ class FormTest extends \PHPUnit_Framework_TestCase { return $this->getMock('Symfony\Component\Form\CsrfProvider\CsrfProviderInterface'); } + + protected function createPostRequest(array $values = array(), array $files = array()) + { + $server = array('REQUEST_METHOD' => 'POST'); + + return new Request(array(), $values, array(), array(), $files, $server); + } } diff --git a/tests/Symfony/Tests/Component/Form/IntegerFieldTest.php b/tests/Symfony/Tests/Component/Form/IntegerFieldTest.php index cef31e5440..52b107c103 100644 --- a/tests/Symfony/Tests/Component/Form/IntegerFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/IntegerFieldTest.php @@ -17,11 +17,11 @@ use Symfony\Component\Form\IntegerField; class IntegerFieldTest extends LocalizedTestCase { - public function testBindCastsToInteger() + public function testSubmitCastsToInteger() { $field = new IntegerField('name'); - $field->bind('1.678'); + $field->submit('1.678'); $this->assertSame(1, $field->getData()); $this->assertSame('1', $field->getDisplayedData()); diff --git a/tests/Symfony/Tests/Component/Form/PasswordFieldTest.php b/tests/Symfony/Tests/Component/Form/PasswordFieldTest.php index 5e083dd623..d96f80a061 100644 --- a/tests/Symfony/Tests/Component/Form/PasswordFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/PasswordFieldTest.php @@ -22,7 +22,7 @@ class PasswordFieldTest extends \PHPUnit_Framework_TestCase $this->assertSame('', $field->getDisplayedData()); - $field->bind('after'); + $field->submit('after'); $this->assertSame('', $field->getDisplayedData()); } @@ -34,7 +34,7 @@ class PasswordFieldTest extends \PHPUnit_Framework_TestCase $this->assertSame('', $field->getDisplayedData()); - $field->bind('after'); + $field->submit('after'); $this->assertSame('after', $field->getDisplayedData()); } diff --git a/tests/Symfony/Tests/Component/Form/RepeatedFieldTest.php b/tests/Symfony/Tests/Component/Form/RepeatedFieldTest.php index 1a8a8add89..49b573a482 100644 --- a/tests/Symfony/Tests/Component/Form/RepeatedFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/RepeatedFieldTest.php @@ -33,11 +33,11 @@ class RepeatedFieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals('foobar', $this->field['second']->getData()); } - public function testBindUnequal() + public function testSubmitUnequal() { $input = array('first' => 'foo', 'second' => 'bar'); - $this->field->bind($input); + $this->field->submit($input); $this->assertEquals('foo', $this->field['first']->getDisplayedData()); $this->assertEquals('bar', $this->field['second']->getDisplayedData()); @@ -46,11 +46,11 @@ class RepeatedFieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals(null, $this->field->getData()); } - public function testBindEqual() + public function testSubmitEqual() { $input = array('first' => 'foo', 'second' => 'foo'); - $this->field->bind($input); + $this->field->submit($input); $this->assertEquals('foo', $this->field['first']->getDisplayedData()); $this->assertEquals('foo', $this->field['second']->getDisplayedData()); diff --git a/tests/Symfony/Tests/Component/Form/TimeFieldTest.php b/tests/Symfony/Tests/Component/Form/TimeFieldTest.php index 54f4e1879f..43751c33c3 100644 --- a/tests/Symfony/Tests/Component/Form/TimeFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/TimeFieldTest.php @@ -17,7 +17,7 @@ use Symfony\Component\Form\TimeField; class TimeFieldTest extends DateTimeTestCase { - public function testBind_dateTime() + public function testSubmit_dateTime() { $field = new TimeField('name', array( 'data_timezone' => 'UTC', @@ -30,7 +30,7 @@ class TimeFieldTest extends DateTimeTestCase 'minute' => '4', ); - $field->bind($input); + $field->submit($input); $dateTime = new \DateTime('1970-01-01 03:04:00 UTC'); @@ -38,7 +38,7 @@ class TimeFieldTest extends DateTimeTestCase $this->assertEquals($input, $field->getDisplayedData()); } - public function testBind_string() + public function testSubmit_string() { $field = new TimeField('name', array( 'data_timezone' => 'UTC', @@ -51,13 +51,13 @@ class TimeFieldTest extends DateTimeTestCase 'minute' => '4', ); - $field->bind($input); + $field->submit($input); $this->assertEquals('03:04:00', $field->getData()); $this->assertEquals($input, $field->getDisplayedData()); } - public function testBind_timestamp() + public function testSubmit_timestamp() { $field = new TimeField('name', array( 'data_timezone' => 'UTC', @@ -70,7 +70,7 @@ class TimeFieldTest extends DateTimeTestCase 'minute' => '4', ); - $field->bind($input); + $field->submit($input); $dateTime = new \DateTime('1970-01-01 03:04:00 UTC'); @@ -78,7 +78,7 @@ class TimeFieldTest extends DateTimeTestCase $this->assertEquals($input, $field->getDisplayedData()); } - public function testBind_raw() + public function testSubmit_raw() { $field = new TimeField('name', array( 'data_timezone' => 'UTC', @@ -96,7 +96,7 @@ class TimeFieldTest extends DateTimeTestCase 'minute' => '4', ); - $field->bind($input); + $field->submit($input); $this->assertEquals($data, $field->getData()); $this->assertEquals($input, $field->getDisplayedData()); @@ -148,7 +148,7 @@ class TimeFieldTest extends DateTimeTestCase 'hours' => array(6, 7), )); - $field->bind(array('hour' => '06', 'minute' => '12')); + $field->submit(array('hour' => '06', 'minute' => '12')); $this->assertTrue($field->isHourWithinRange()); } @@ -159,7 +159,7 @@ class TimeFieldTest extends DateTimeTestCase 'hours' => array(6, 7), )); - $field->bind(array('hour' => '', 'minute' => '')); + $field->submit(array('hour' => '', 'minute' => '')); $this->assertTrue($field->isHourWithinRange()); } @@ -170,7 +170,7 @@ class TimeFieldTest extends DateTimeTestCase 'hours' => array(6, 7), )); - $field->bind(array('hour' => '08', 'minute' => '12')); + $field->submit(array('hour' => '08', 'minute' => '12')); $this->assertFalse($field->isHourWithinRange()); } @@ -181,7 +181,7 @@ class TimeFieldTest extends DateTimeTestCase 'minutes' => array(6, 7), )); - $field->bind(array('hour' => '06', 'minute' => '06')); + $field->submit(array('hour' => '06', 'minute' => '06')); $this->assertTrue($field->isMinuteWithinRange()); } @@ -192,7 +192,7 @@ class TimeFieldTest extends DateTimeTestCase 'minutes' => array(6, 7), )); - $field->bind(array('hour' => '', 'minute' => '')); + $field->submit(array('hour' => '', 'minute' => '')); $this->assertTrue($field->isMinuteWithinRange()); } @@ -203,7 +203,7 @@ class TimeFieldTest extends DateTimeTestCase 'minutes' => array(6, 7), )); - $field->bind(array('hour' => '06', 'minute' => '08')); + $field->submit(array('hour' => '06', 'minute' => '08')); $this->assertFalse($field->isMinuteWithinRange()); } @@ -215,7 +215,7 @@ class TimeFieldTest extends DateTimeTestCase 'with_seconds' => true, )); - $field->bind(array('hour' => '04', 'minute' => '05', 'second' => '06')); + $field->submit(array('hour' => '04', 'minute' => '05', 'second' => '06')); $this->assertTrue($field->isSecondWithinRange()); } @@ -227,7 +227,7 @@ class TimeFieldTest extends DateTimeTestCase 'with_seconds' => true, )); - $field->bind(array('hour' => '', 'minute' => '')); + $field->submit(array('hour' => '', 'minute' => '')); $this->assertTrue($field->isSecondWithinRange()); } @@ -239,7 +239,7 @@ class TimeFieldTest extends DateTimeTestCase 'with_seconds' => true, )); - $field->bind(array('hour' => '04', 'minute' => '05', 'second' => '08')); + $field->submit(array('hour' => '04', 'minute' => '05', 'second' => '08')); $this->assertFalse($field->isSecondWithinRange()); } diff --git a/tests/Symfony/Tests/Component/Form/UrlFieldTest.php b/tests/Symfony/Tests/Component/Form/UrlFieldTest.php index 272975c353..e189a3653f 100644 --- a/tests/Symfony/Tests/Component/Form/UrlFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/UrlFieldTest.php @@ -17,47 +17,47 @@ use Symfony\Component\Form\UrlField; class UrlFieldTest extends LocalizedTestCase { - public function testBindAddsDefaultProtocolIfNoneIsIncluded() + public function testSubmitAddsDefaultProtocolIfNoneIsIncluded() { $field = new UrlField('name'); - $field->bind('www.domain.com'); + $field->submit('www.domain.com'); $this->assertSame('http://www.domain.com', $field->getData()); $this->assertSame('http://www.domain.com', $field->getDisplayedData()); } - public function testBindAddsNoDefaultProtocolIfAlreadyIncluded() + public function testSubmitAddsNoDefaultProtocolIfAlreadyIncluded() { $field = new UrlField('name', array( 'default_protocol' => 'http', )); - $field->bind('ftp://www.domain.com'); + $field->submit('ftp://www.domain.com'); $this->assertSame('ftp://www.domain.com', $field->getData()); $this->assertSame('ftp://www.domain.com', $field->getDisplayedData()); } - public function testBindAddsNoDefaultProtocolIfEmpty() + public function testSubmitAddsNoDefaultProtocolIfEmpty() { $field = new UrlField('name', array( 'default_protocol' => 'http', )); - $field->bind(''); + $field->submit(''); $this->assertSame(null, $field->getData()); $this->assertSame('', $field->getDisplayedData()); } - public function testBindAddsNoDefaultProtocolIfSetToNull() + public function testSubmitAddsNoDefaultProtocolIfSetToNull() { $field = new UrlField('name', array( 'default_protocol' => null, )); - $field->bind('www.domain.com'); + $field->submit('www.domain.com'); $this->assertSame('www.domain.com', $field->getData()); $this->assertSame('www.domain.com', $field->getDisplayedData());