From 53f292adcc54ee97479c26aca638da713f3013a0 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 5 May 2013 23:28:39 +0200 Subject: [PATCH 1/5] [Form] Removed usage of the ChoiceList::getIndicesFor*() methods where they don't offer any performance benefit --- .../Core/DataTransformer/ChoiceToBooleanArrayTransformer.php | 4 ++-- .../Core/DataTransformer/ChoicesToBooleanArrayTransformer.php | 4 ++-- src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToBooleanArrayTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToBooleanArrayTransformer.php index b8aa95effc7c7..12e1efd081a87 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToBooleanArrayTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToBooleanArrayTransformer.php @@ -56,10 +56,10 @@ public function transform($choice) throw new TransformationFailedException('Can not get the choice list', $e->getCode(), $e); } - $index = current($this->choiceList->getIndicesForChoices(array($choice))); + $valueMap = array_flip($this->choiceList->getValuesForChoices(array($choice))); foreach ($values as $i => $value) { - $values[$i] = $i === $index; + $values[$i] = isset($valueMap[$value]); } return $values; diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToBooleanArrayTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToBooleanArrayTransformer.php index 0c2ea3a394cba..d3b8cc345bbde 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToBooleanArrayTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToBooleanArrayTransformer.php @@ -58,10 +58,10 @@ public function transform($array) throw new TransformationFailedException('Can not get the choice list', $e->getCode(), $e); } - $indexMap = array_flip($this->choiceList->getIndicesForChoices($array)); + $valueMap = array_flip($this->choiceList->getValuesForChoices($array)); foreach ($values as $i => $value) { - $values[$i] = isset($indexMap[$i]); + $values[$i] = isset($valueMap[$value]); } return $values; diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index 28327b6291a13..2f65199692fe8 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -104,7 +104,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) // Check if the choices already contain the empty value // Only add the empty value option if this is not the case - if (0 === count($options['choice_list']->getIndicesForValues(array('')))) { + if (0 === count($options['choice_list']->getChoicesForValues(array('')))) { $view->vars['empty_value'] = $options['empty_value']; } From 30aa1de8dc32be82b91ddd426f667eb1a1028fcf Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 5 May 2013 23:50:06 +0200 Subject: [PATCH 2/5] [Form] Fixed ChoiceList::get*By*() methods to preserve order and array keys --- .../Extension/Core/ChoiceList/ChoiceList.php | 32 ++++++------- .../Core/ChoiceList/ChoiceListInterface.php | 24 ++++++++++ .../Core/ChoiceList/ChoiceListTest.php | 48 ++++++++++++++++++- .../Core/ChoiceList/SimpleChoiceListTest.php | 48 +++++++++++++++++++ 4 files changed, 135 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php index 4fbca4b4516d4..18220a8c42e2c 100644 --- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php +++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php @@ -155,11 +155,11 @@ public function getChoicesForValues(array $values) $values = $this->fixValues($values); $choices = array(); - foreach ($values as $j => $givenValue) { - foreach ($this->values as $i => $value) { + foreach ($values as $i => $givenValue) { + foreach ($this->values as $j => $value) { if ($value === $givenValue) { - $choices[] = $this->choices[$i]; - unset($values[$j]); + $choices[$i] = $this->choices[$j]; + unset($values[$i]); if (0 === count($values)) { break 2; @@ -179,11 +179,11 @@ public function getValuesForChoices(array $choices) $choices = $this->fixChoices($choices); $values = array(); - foreach ($this->choices as $i => $choice) { - foreach ($choices as $j => $givenChoice) { + foreach ($choices as $i => $givenChoice) { + foreach ($this->choices as $j => $choice) { if ($choice === $givenChoice) { - $values[] = $this->values[$i]; - unset($choices[$j]); + $values[$i] = $this->values[$j]; + unset($choices[$i]); if (0 === count($choices)) { break 2; @@ -203,11 +203,11 @@ public function getIndicesForChoices(array $choices) $choices = $this->fixChoices($choices); $indices = array(); - foreach ($this->choices as $i => $choice) { - foreach ($choices as $j => $givenChoice) { + foreach ($choices as $i => $givenChoice) { + foreach ($this->choices as $j => $choice) { if ($choice === $givenChoice) { - $indices[] = $i; - unset($choices[$j]); + $indices[$i] = $j; + unset($choices[$i]); if (0 === count($choices)) { break 2; @@ -227,11 +227,11 @@ public function getIndicesForValues(array $values) $values = $this->fixValues($values); $indices = array(); - foreach ($this->values as $i => $value) { - foreach ($values as $j => $givenValue) { + foreach ($values as $i => $givenValue) { + foreach ($this->values as $j => $value) { if ($value === $givenValue) { - $indices[] = $i; - unset($values[$j]); + $indices[$i] = $j; + unset($values[$i]); if (0 === count($values)) { break 2; diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php index c81c560f4f264..996dbe2591176 100644 --- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php +++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php @@ -95,6 +95,11 @@ public function getRemainingViews(); /** * Returns the choices corresponding to the given values. * + * The choices can have any data type. + * + * The choices must be returned with the same keys and in the same order + * as the corresponding values in the given array. + * * @param array $values An array of choice values. Not existing values in * this array are ignored. * @@ -105,6 +110,11 @@ public function getChoicesForValues(array $values); /** * Returns the values corresponding to the given choices. * + * The values must be strings. + * + * The values must be returned with the same keys and in the same order + * as the corresponding choices in the given array. + * * @param array $choices An array of choices. Not existing choices in this * array are ignored. * @@ -116,6 +126,12 @@ public function getValuesForChoices(array $choices); /** * Returns the indices corresponding to the given choices. * + * The indices must be positive integers or strings accepted by + * {@link FormConfigBuilder::validateName()}. + * + * The indices must be returned with the same keys and in the same order + * as the corresponding choices in the given array. + * * @param array $choices An array of choices. Not existing choices in this * array are ignored. * @@ -126,6 +142,14 @@ public function getIndicesForChoices(array $choices); /** * Returns the indices corresponding to the given values. * + * The indices must be positive integers or strings accepted by + * {@link FormConfigBuilder::validateName()}. + * + * The index "placeholder" is internally reserved. + * + * The indices must be returned with the same keys and in the same order + * as the corresponding values in the given array. + * * @param array $values An array of choice values. Not existing values in * this array are ignored. * diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php index 63eae9bf7f5ef..531bb561b4c40 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php @@ -125,6 +125,18 @@ public function testGetIndicesForChoices() $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices)); } + public function testGetIndicesForChoicesPreservesKeys() + { + $choices = array(5 => $this->obj2, 8 => $this->obj3); + $this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForChoices($choices)); + } + + public function testGetIndicesForChoicesPreservesOrder() + { + $choices = array($this->obj3, $this->obj2); + $this->assertSame(array(2, 1), $this->list->getIndicesForChoices($choices)); + } + public function testGetIndicesForChoicesIgnoresNonExistingChoices() { $choices = array($this->obj2, $this->obj3, 'foobar'); @@ -138,6 +150,20 @@ public function testGetIndicesForValues() $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values)); } + public function testGetIndicesForValuesPreservesKeys() + { + // values and indices are always the same + $values = array(5 => '1', 8 => '2'); + $this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForValues($values)); + } + + public function testGetIndicesForValuesPreservesOrder() + { + // values and indices are always the same + $values = array('2', '1'); + $this->assertSame(array(2, 1), $this->list->getIndicesForValues($values)); + } + public function testGetIndicesForValuesIgnoresNonExistingValues() { $values = array('1', '2', '5'); @@ -150,7 +176,13 @@ public function testGetChoicesForValues() $this->assertSame(array($this->obj2, $this->obj3), $this->list->getChoicesForValues($values)); } - public function testGetChoicesForValuesCorrectOrderingOfResult() + public function testGetChoicesForValuesPreservesKeys() + { + $values = array(5 => '1', 8 => '2'); + $this->assertSame(array(5 => $this->obj2, 8 => $this->obj3), $this->list->getChoicesForValues($values)); + } + + public function testGetChoicesForValuesPreservesOrder() { $values = array('2', '1'); $this->assertSame(array($this->obj3, $this->obj2), $this->list->getChoicesForValues($values)); @@ -168,6 +200,20 @@ public function testGetValuesForChoices() $this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices)); } + + public function testGetValuesForChoicesPreservesKeys() + { + $choices = array(5 => $this->obj2, 8 => $this->obj3); + $this->assertSame(array(5 => '1', 8 => '2'), $this->list->getValuesForChoices($choices)); + } + + + public function testGetValuesForChoicesPreservesOrder() + { + $choices = array($this->obj3, $this->obj2); + $this->assertSame(array('2', '1'), $this->list->getValuesForChoices($choices)); + } + public function testGetValuesForChoicesIgnoresNonExistingChoices() { $choices = array($this->obj2, $this->obj3, 'foobar'); diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php index 69d27a18fd813..85b0ed7485954 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php @@ -79,6 +79,18 @@ public function testGetIndicesForChoices() $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices)); } + public function testGetIndicesForChoicesPreservesKeys() + { + $choices = array(5 => 'b', 8 => 'c'); + $this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForChoices($choices)); + } + + public function testGetIndicesForChoicesPreservesOrder() + { + $choices = array('c', 'b'); + $this->assertSame(array(2, 1), $this->list->getIndicesForChoices($choices)); + } + public function testGetIndicesForChoicesIgnoresNonExistingChoices() { $choices = array('b', 'c', 'foobar'); @@ -98,6 +110,18 @@ public function testGetIndicesForValues() $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values)); } + public function testGetIndicesForValuesPreservesKeys() + { + $values = array(5 => 'b', 8 => 'c'); + $this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForValues($values)); + } + + public function testGetIndicesForValuesPreservesOrder() + { + $values = array('c', 'b'); + $this->assertSame(array(2, 1), $this->list->getIndicesForValues($values)); + } + public function testGetIndicesForValuesIgnoresNonExistingValues() { $values = array('b', 'c', '100'); @@ -117,6 +141,18 @@ public function testGetChoicesForValues() $this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values)); } + public function testGetChoicesForValuesPreservesKeys() + { + $values = array(5 => 'b', 8 => 'c'); + $this->assertSame(array(5 => 'b', 8 => 'c'), $this->list->getChoicesForValues($values)); + } + + public function testGetChoicesForValuesPreservesOrder() + { + $values = array('c', 'b'); + $this->assertSame(array('c', 'b'), $this->list->getChoicesForValues($values)); + } + public function testGetChoicesForValuesIgnoresNonExistingValues() { $values = array('b', 'c', '100'); @@ -136,6 +172,18 @@ public function testGetValuesForChoices() $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices)); } + public function testGetValuesForChoicesPreservesKeys() + { + $choices = array(5 => 'b', 8 => 'c'); + $this->assertSame(array(5 => 'b', 8 => 'c'), $this->list->getValuesForChoices($choices)); + } + + public function testGetValuesForChoicesPreservesOrder() + { + $choices = array('c', 'b'); + $this->assertSame(array('c', 'b'), $this->list->getValuesForChoices($choices)); + } + public function testGetValuesForChoicesIgnoresNonExistingValues() { $choices = array('b', 'c', 'foobar'); From ed837522af4fd2050aec4fafa9dd3366b30b465d Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 6 May 2013 00:28:29 +0200 Subject: [PATCH 3/5] [Form] Fixed expanded choice field to be marked invalid when unknown choices are submitted --- .../BooleanToStringTransformer.php | 6 +- .../FixCheckboxInputListener.php | 40 +- .../EventListener/FixRadioInputListener.php | 18 +- .../Form/Extension/Core/Type/CheckboxType.php | 15 +- src/Symfony/Component/Form/Form.php | 109 ++--- .../BooleanToStringTransformerTest.php | 24 +- .../Extension/Core/Type/CheckboxTypeTest.php | 31 +- .../Extension/Core/Type/ChoiceTypeTest.php | 380 +++++++++++++++++- 8 files changed, 539 insertions(+), 84 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php index 95e7332d3e03f..f8e3b5bfcbc1c 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php @@ -49,15 +49,11 @@ public function __construct($trueValue) */ public function transform($value) { - if (null === $value) { - return null; - } - if (!is_bool($value)) { throw new TransformationFailedException('Expected a Boolean.'); } - return true === $value ? $this->trueValue : null; + return $value ? $this->trueValue : null; } /** diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php index 083e23e9465f5..d5bd573ff88d3 100644 --- a/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php +++ b/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Form\Extension\Core\EventListener; +use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -38,10 +39,43 @@ public function __construct(ChoiceListInterface $choiceList) public function preBind(FormEvent $event) { - $values = (array) $event->getData(); - $indices = $this->choiceList->getIndicesForValues($values); + $data = $event->getData(); - $event->setData(count($indices) > 0 ? array_combine($indices, $values) : array()); + if (is_array($data)) { + // Flip the submitted values for faster lookup + // It's better to flip this array than $existingValues because + // $submittedValues is generally smaller. + $submittedValues = array_flip($data); + + // Since expanded choice fields are completely loaded anyway, we + // can just as well get the values again without losing performance. + $existingValues = $this->choiceList->getValues(); + + // Clear the data array and fill it with correct indices + $data = array(); + + foreach ($existingValues as $index => $value) { + if (isset($submittedValues[$value])) { + // Value was submitted + $data[$index] = $value; + unset($submittedValues[$value]); + } + } + + if (count($submittedValues) > 0) { + throw new TransformationFailedException(sprintf( + 'The following choices were not found: "%s"', + implode('", "', array_keys($submittedValues)) + )); + } + } elseif ('' === $data || null === $data) { + // Empty values are always accepted. + $data = array(); + } + + // Else leave the data unchanged to provoke an error during submission + + $event->setData($data); } public static function getSubscribedEvents() diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php index c60bd26b53718..40de4b2d6abce 100644 --- a/src/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php +++ b/src/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php @@ -38,10 +38,22 @@ public function __construct(ChoiceListInterface $choiceList) public function preBind(FormEvent $event) { - $value = $event->getData(); - $index = current($this->choiceList->getIndicesForValues(array($value))); + $data = $event->getData(); - $event->setData(false !== $index ? array($index => $value) : array()); + // Since expanded choice fields are completely loaded anyway, we + // can just as well get the values again without losing performance. + $existingValues = $this->choiceList->getValues(); + + if (false !== ($index = array_search($data, $existingValues, true))) { + $data = array($index => $data); + } elseif ('' === $data || null === $data) { + // Empty values are always accepted. + $data = array(); + } + + // Else leave the data unchanged to provoke an error during submission + + $event->setData($data); } public static function getSubscribedEvents() diff --git a/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php b/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php index 8d489d6db0cdd..10e84fd57b159 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php @@ -25,9 +25,14 @@ class CheckboxType extends AbstractType */ public function buildForm(FormBuilderInterface $builder, array $options) { - $builder - ->addViewTransformer(new BooleanToStringTransformer($options['value'])) - ; + // Unlike in other types, where the data is NULL by default, it + // needs to be a Boolean here. setData(null) is not acceptable + // for checkboxes and radio buttons (unless a custom model + // transformer handles this case). + // We cannot solve this case via overriding the "data" option, because + // doing so also calls setDataLocked(true). + $builder->setData(isset($options['data']) ? $options['data'] : false); + $builder->addViewTransformer(new BooleanToStringTransformer($options['value'])); } /** @@ -46,8 +51,8 @@ public function buildView(FormView $view, FormInterface $form, array $options) */ public function setDefaultOptions(OptionsResolverInterface $resolver) { - $emptyData = function (FormInterface $form, $clientData) { - return $clientData; + $emptyData = function (FormInterface $form, $viewData) { + return $viewData; }; $resolver->setDefaults(array( diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 92014de3ed364..6f96732d6e7a4 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -531,68 +531,73 @@ public function bind($submittedData) $dispatcher = $this->config->getEventDispatcher(); - // Hook to change content of the data bound by the browser - if ($dispatcher->hasListeners(FormEvents::PRE_BIND) || $dispatcher->hasListeners(FormEvents::BIND_CLIENT_DATA)) { - $event = new FormEvent($this, $submittedData); - $dispatcher->dispatch(FormEvents::PRE_BIND, $event); - // BC until 2.3 - if ($dispatcher->hasListeners(FormEvents::BIND_CLIENT_DATA)) { - trigger_error('The FormEvents::BIND_CLIENT_DATA event is deprecated since 2.1 and will be removed in 2.3. Use the FormEvents::PRE_BIND event instead.', E_USER_DEPRECATED); - } - $dispatcher->dispatch(FormEvents::BIND_CLIENT_DATA, $event); - $submittedData = $event->getData(); - } + $modelData = null; + $normData = null; + $viewData = null; - // Check whether the form is compound. - // This check is preferable over checking the number of children, - // since forms without children may also be compound. - // (think of empty collection forms) - if ($this->config->getCompound()) { - if (!is_array($submittedData)) { - $submittedData = array(); + try { + // Hook to change content of the data bound by the browser + if ($dispatcher->hasListeners(FormEvents::PRE_BIND) || $dispatcher->hasListeners(FormEvents::BIND_CLIENT_DATA)) { + $event = new FormEvent($this, $submittedData); + $dispatcher->dispatch(FormEvents::PRE_BIND, $event); + // BC until 2.3 + if ($dispatcher->hasListeners(FormEvents::BIND_CLIENT_DATA)) { + trigger_error('The FormEvents::BIND_CLIENT_DATA event is deprecated since 2.1 and will be removed in 2.3. Use the FormEvents::PRE_BIND event instead.', E_USER_DEPRECATED); + } + $dispatcher->dispatch(FormEvents::BIND_CLIENT_DATA, $event); + $submittedData = $event->getData(); } - for (reset($this->children); false !== current($this->children); next($this->children)) { - $child = current($this->children); - $name = key($this->children); + // Check whether the form is compound. + // This check is preferable over checking the number of children, + // since forms without children may also be compound. + // (think of empty collection forms) + if ($this->config->getCompound()) { + if (null === $submittedData) { + $submittedData = array(); + } - $child->bind(isset($submittedData[$name]) ? $submittedData[$name] : null); - unset($submittedData[$name]); - } + if (!is_array($submittedData)) { + throw new TransformationFailedException('Compound forms expect an array or NULL on submission.'); + } - $this->extraData = $submittedData; + for (reset($this->children); false !== current($this->children); next($this->children)) { + $child = current($this->children); + $name = key($this->children); - // If the form is compound, the default data in view format - // is reused. The data of the children is merged into this - // default data using the data mapper. - $viewData = $this->viewData; - } else { - // If the form is not compound, the submitted data is also the data in view format. - $viewData = $submittedData; - } + $child->bind(isset($submittedData[$name]) ? $submittedData[$name] : null); + unset($submittedData[$name]); + } - if (FormUtil::isEmpty($viewData)) { - $emptyData = $this->config->getEmptyData(); + $this->extraData = $submittedData; - if ($emptyData instanceof \Closure) { - /* @var \Closure $emptyData */ - $emptyData = $emptyData($this, $viewData); + // If the form is compound, the default data in view format + // is reused. The data of the children is merged into this + // default data using the data mapper. + $viewData = $this->viewData; + } else { + // If the form is not compound, the submitted data is also the data in view format. + $viewData = $submittedData; } - $viewData = $emptyData; - } + if (FormUtil::isEmpty($viewData)) { + $emptyData = $this->config->getEmptyData(); - // Merge form data from children into existing view data - // It is not necessary to invoke this method if the form has no children, - // even if it is compound. - if (count($this->children) > 0) { - $this->config->getDataMapper()->mapFormsToData($this->children, $viewData); - } + if ($emptyData instanceof \Closure) { + /* @var \Closure $emptyData */ + $emptyData = $emptyData($this, $viewData); + } - $modelData = null; - $normData = null; + $viewData = $emptyData; + } + + // Merge form data from children into existing view data + // It is not necessary to invoke this method if the form has no children, + // even if it is compound. + if (count($this->children) > 0) { + $this->config->getDataMapper()->mapFormsToData($this->children, $viewData); + } - try { // Normalize data to unified representation $normData = $this->viewToNorm($viewData); @@ -614,6 +619,12 @@ public function bind($submittedData) $viewData = $this->normToView($normData); } catch (TransformationFailedException $e) { $this->synchronized = false; + + // If $viewData was not yet set, set it to $submittedData so that + // the erroneous data is accessible on the form. + if (null === $viewData) { + $viewData = $submittedData; + } } $this->bound = true; diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/BooleanToStringTransformerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/BooleanToStringTransformerTest.php index 41f8f956d7cb1..4f6238aee0d43 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/BooleanToStringTransformerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/BooleanToStringTransformerTest.php @@ -17,6 +17,9 @@ class BooleanToStringTransformerTest extends \PHPUnit_Framework_TestCase { const TRUE_VALUE = '1'; + /** + * @var BooleanToStringTransformer + */ protected $transformer; protected function setUp() @@ -33,20 +36,29 @@ public function testTransform() { $this->assertEquals(self::TRUE_VALUE, $this->transformer->transform(true)); $this->assertNull($this->transformer->transform(false)); - $this->assertNull($this->transformer->transform(null)); } - public function testTransformExpectsBoolean() + /** + * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException + */ + public function testTransformFailsIfNull() { - $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException'); + $this->transformer->transform(null); + } + /** + * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException + */ + public function testTransformFailsIfString() + { $this->transformer->transform('1'); } - public function testReverseTransformExpectsString() + /** + * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException + */ + public function testReverseTransformFailsIfInteger() { - $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException'); - $this->transformer->reverseTransform(1); } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php index 16a71d8f20002..868635d2d231d 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php @@ -15,6 +15,15 @@ class CheckboxTypeTest extends TypeTestCase { + public function testDataIsFalseByDefault() + { + $form = $this->factory->create('checkbox'); + + $this->assertFalse($form->getData()); + $this->assertFalse($form->getNormData()); + $this->assertNull($form->getViewData()); + } + public function testPassValueToView() { $form = $this->factory->create('checkbox', null, array('value' => 'foobar')); @@ -106,35 +115,37 @@ public function testBindWithEmptyValueUnchecked() } /** - * @dataProvider provideTransformedData + * @dataProvider provideCustomModelTransformerData */ - public function testTransformedData($data, $expected) + public function testCustomModelTransformer($data, $checked) { // present a binary status field as a checkbox $transformer = new CallbackTransformer( function ($value) { - return 'expedited' == $value; + return 'checked' == $value; }, function ($value) { - return $value ? 'expedited' : 'standard'; + return $value ? 'checked' : 'unchecked'; } ); - $form = $this->builder - ->create('expedited_shipping', 'checkbox') + $form = $this->factory->createBuilder('checkbox') ->addModelTransformer($transformer) ->getForm(); + $form->setData($data); $view = $form->createView(); - $this->assertEquals($expected, $view->vars['checked']); + $this->assertSame($data, $form->getData()); + $this->assertSame($checked, $form->getNormData()); + $this->assertEquals($checked, $view->vars['checked']); } - public function provideTransformedData() + public function provideCustomModelTransformerData() { return array( - array('expedited', true), - array('standard', false), + array('checked', true), + array('unchecked', false), ); } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php index 844715c6502d4..6869601f90996 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -177,6 +177,21 @@ public function testBindSingleNonExpanded() $this->assertEquals('b', $form->getViewData()); } + public function testBindSingleNonExpandedInvalidChoice() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => false, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->bind('foobar'); + + $this->assertNull($form->getData()); + $this->assertEquals('foobar', $form->getViewData()); + $this->assertFalse($form->isSynchronized()); + } + public function testBindSingleNonExpandedObjectChoices() { $form = $this->factory->create('choice', null, array( @@ -214,6 +229,36 @@ public function testBindMultipleNonExpanded() $this->assertEquals(array('a', 'b'), $form->getViewData()); } + public function testBindMultipleNonExpandedInvalidScalarChoice() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => true, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->bind('foobar'); + + $this->assertNull($form->getData()); + $this->assertEquals('foobar', $form->getViewData()); + $this->assertFalse($form->isSynchronized()); + } + + public function testBindMultipleNonExpandedInvalidArrayChoice() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => true, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->bind(array('a', 'foobar')); + + $this->assertNull($form->getData()); + $this->assertEquals(array('a', 'foobar'), $form->getViewData()); + $this->assertFalse($form->isSynchronized()); + } + public function testBindMultipleNonExpandedObjectChoices() { $form = $this->factory->create('choice', null, array( @@ -236,7 +281,69 @@ public function testBindMultipleNonExpandedObjectChoices() $this->assertEquals(array('2', '3'), $form->getViewData()); } - public function testBindSingleExpanded() + public function testBindSingleExpandedRequired() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => $this->choices, + )); + + $form->bind('b'); + + $this->assertSame('b', $form->getData()); + $this->assertSame(array( + 0 => false, + 1 => true, + 2 => false, + 3 => false, + 4 => false, + ), $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertTrue($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertSame('b', $form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testBindSingleExpandedRequiredInvalidChoice() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => $this->choices, + )); + + $form->bind('foobar'); + + $this->assertSame(null, $form->getData()); + $this->assertSame('foobar', $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertFalse($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testBindSingleExpandedNonRequired() { $form = $this->factory->create('choice', null, array( 'multiple' => false, @@ -247,6 +354,16 @@ public function testBindSingleExpanded() $form->bind('b'); $this->assertSame('b', $form->getData()); + $this->assertSame(array( + 0 => false, + 1 => true, + 2 => false, + 3 => false, + 4 => false, + ), $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + $this->assertFalse($form[0]->getData()); $this->assertTrue($form[1]->getData()); $this->assertFalse($form[2]->getData()); @@ -259,7 +376,35 @@ public function testBindSingleExpanded() $this->assertNull($form[4]->getViewData()); } - public function testBindSingleExpandedNothingChecked() + public function testBindSingleExpandedNonRequiredInvalidChoice() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => $this->choices, + )); + + $form->bind('foobar'); + + $this->assertSame(null, $form->getData()); + $this->assertSame('foobar', $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertFalse($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testBindSingleExpandedRequiredNull() { $form = $this->factory->create('choice', null, array( 'multiple' => false, @@ -270,6 +415,16 @@ public function testBindSingleExpandedNothingChecked() $form->bind(null); $this->assertNull($form->getData()); + $this->assertSame(array( + 0 => false, + 1 => false, + 2 => false, + 3 => false, + 4 => false, + ), $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + $this->assertFalse($form[0]->getData()); $this->assertFalse($form[1]->getData()); $this->assertFalse($form[2]->getData()); @@ -282,18 +437,173 @@ public function testBindSingleExpandedNothingChecked() $this->assertNull($form[4]->getViewData()); } - public function testBindSingleExpandedWithFalseDoesNotHaveExtraChildren() + public function testBindSingleExpandedRequiredEmpty() { $form = $this->factory->create('choice', null, array( 'multiple' => false, 'expanded' => true, + 'required' => true, + 'choices' => $this->choices, + )); + + $form->bind(''); + + $this->assertNull($form->getData()); + $this->assertSame(array( + 0 => false, + 1 => false, + 2 => false, + 3 => false, + 4 => false, + ), $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testBindSingleExpandedRequiredFalse() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, 'choices' => $this->choices, )); $form->bind(false); + $this->assertNull($form->getData()); + $this->assertSame(array( + 0 => false, + 1 => false, + 2 => false, + 3 => false, + 4 => false, + ), $form->getViewData()); $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testBindSingleExpandedNonRequiredNull() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => false, + 'expanded' => true, + 'choices' => $this->choices, + )); + + $form->bind(null); + $this->assertNull($form->getData()); + $this->assertSame(array( + 0 => false, + 1 => false, + 2 => false, + 3 => false, + 4 => false, + ), $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testBindSingleExpandedNonRequiredEmpty() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => $this->choices, + )); + + $form->bind(''); + + $this->assertNull($form->getData()); + $this->assertSame(array( + 0 => false, + 1 => false, + 2 => false, + 3 => false, + 4 => false, + ), $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testBindSingleExpandedNonRequiredFalse() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => $this->choices, + )); + + $form->bind(false); + + $this->assertNull($form->getData()); + $this->assertSame(array( + 0 => false, + 1 => false, + 2 => false, + 3 => false, + 4 => false, + ), $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); } public function testBindSingleExpandedWithEmptyChild() @@ -381,6 +691,16 @@ public function testBindMultipleExpanded() $form->bind(array('a', 'c')); $this->assertSame(array('a', 'c'), $form->getData()); + $this->assertSame(array( + 0 => true, + 1 => false, + 2 => true, + 3 => false, + 4 => false, + ), $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + $this->assertTrue($form[0]->getData()); $this->assertFalse($form[1]->getData()); $this->assertTrue($form[2]->getData()); @@ -393,6 +713,60 @@ public function testBindMultipleExpanded() $this->assertNull($form[4]->getViewData()); } + public function testBindMultipleExpandedInvalidScalarChoice() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => true, + 'expanded' => true, + 'choices' => $this->choices, + )); + + $form->bind('foobar'); + + $this->assertNull($form->getData()); + $this->assertSame('foobar', $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertFalse($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testBindMultipleExpandedInvalidArrayChoice() + { + $form = $this->factory->create('choice', null, array( + 'multiple' => true, + 'expanded' => true, + 'choices' => $this->choices, + )); + + $form->bind(array('a', 'foobar')); + + $this->assertNull($form->getData()); + $this->assertSame(array('a', 'foobar'), $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertFalse($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + public function testBindMultipleExpandedEmpty() { $form = $this->factory->create('choice', null, array( From 9d3628cf13c5b3f0e935c3c0467d26976a5d9ff8 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Tue, 10 Sep 2013 12:41:39 +0200 Subject: [PATCH 4/5] [Form] Improved test coverage of ChoiceList classes --- .../ChoiceList/AbstractChoiceListTest.php | 297 ++++++++++++++++++ .../Core/ChoiceList/ChoiceListTest.php | 172 +++------- .../Core/ChoiceList/ObjectChoiceListTest.php | 64 ++-- .../Core/ChoiceList/SimpleChoiceListTest.php | 191 ++--------- .../SimpleNumericChoiceListTest.php | 78 +++++ 5 files changed, 486 insertions(+), 316 deletions(-) create mode 100644 src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/AbstractChoiceListTest.php create mode 100644 src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleNumericChoiceListTest.php diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/AbstractChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/AbstractChoiceListTest.php new file mode 100644 index 0000000000000..0e2b6321ccb83 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/AbstractChoiceListTest.php @@ -0,0 +1,297 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList; + +/** + * @author Bernhard Schussek + */ +abstract class AbstractChoiceListTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected $list; + + /** + * @var array + */ + protected $choices; + + /** + * @var array + */ + protected $values; + + /** + * @var array + */ + protected $indices; + + /** + * @var array + */ + protected $labels; + + /** + * @var mixed + */ + protected $choice1; + + /** + * @var mixed + */ + protected $choice2; + + /** + * @var mixed + */ + protected $choice3; + + /** + * @var mixed + */ + protected $choice4; + + /** + * @var string + */ + protected $value1; + + /** + * @var string + */ + protected $value2; + + /** + * @var string + */ + protected $value3; + + /** + * @var string + */ + protected $value4; + + /** + * @var int|string + */ + protected $index1; + + /** + * @var int|string + */ + protected $index2; + + /** + * @var int|string + */ + protected $index3; + + /** + * @var int|string + */ + protected $index4; + + /** + * @var string + */ + protected $label1; + + /** + * @var string + */ + protected $label2; + + /** + * @var string + */ + protected $label3; + + /** + * @var string + */ + protected $label4; + + protected function setUp() + { + parent::setUp(); + + $this->list = $this->createChoiceList(); + + $this->choices = $this->getChoices(); + $this->indices = $this->getIndices(); + $this->values = $this->getValues(); + $this->labels = $this->getLabels(); + + // allow access to the individual entries without relying on their indices + reset($this->choices); + reset($this->indices); + reset($this->values); + reset($this->labels); + + for ($i = 1; $i <= 4; ++$i) { + $this->{'choice'.$i} = current($this->choices); + $this->{'index'.$i} = current($this->indices); + $this->{'value'.$i} = current($this->values); + $this->{'label'.$i} = current($this->labels); + + next($this->choices); + next($this->indices); + next($this->values); + next($this->labels); + } + } + + public function testGetChoices() + { + $this->assertSame($this->choices, $this->list->getChoices()); + } + + public function testGetValues() + { + $this->assertSame($this->values, $this->list->getValues()); + } + + public function testGetIndicesForChoices() + { + $choices = array($this->choice1, $this->choice2); + $this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForChoices($choices)); + } + + public function testGetIndicesForChoicesPreservesKeys() + { + $choices = array(5 => $this->choice1, 8 => $this->choice2); + $this->assertSame(array(5 => $this->index1, 8 => $this->index2), $this->list->getIndicesForChoices($choices)); + } + + public function testGetIndicesForChoicesPreservesOrder() + { + $choices = array($this->choice2, $this->choice1); + $this->assertSame(array($this->index2, $this->index1), $this->list->getIndicesForChoices($choices)); + } + + public function testGetIndicesForChoicesIgnoresNonExistingChoices() + { + $choices = array($this->choice1, $this->choice2, 'foobar'); + $this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForChoices($choices)); + } + + public function testGetIndicesForChoicesEmpty() + { + $this->assertSame(array(), $this->list->getIndicesForChoices(array())); + } + + public function testGetIndicesForValues() + { + // values and indices are always the same + $values = array($this->value1, $this->value2); + $this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForValues($values)); + } + + public function testGetIndicesForValuesPreservesKeys() + { + // values and indices are always the same + $values = array(5 => $this->value1, 8 => $this->value2); + $this->assertSame(array(5 => $this->index1, 8 => $this->index2), $this->list->getIndicesForValues($values)); + } + + public function testGetIndicesForValuesPreservesOrder() + { + $values = array($this->value2, $this->value1); + $this->assertSame(array($this->index2, $this->index1), $this->list->getIndicesForValues($values)); + } + + public function testGetIndicesForValuesIgnoresNonExistingValues() + { + $values = array($this->value1, $this->value2, 'foobar'); + $this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForValues($values)); + } + + public function testGetIndicesForValuesEmpty() + { + $this->assertSame(array(), $this->list->getIndicesForValues(array())); + } + + public function testGetChoicesForValues() + { + $values = array($this->value1, $this->value2); + $this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values)); + } + + public function testGetChoicesForValuesPreservesKeys() + { + $values = array(5 => $this->value1, 8 => $this->value2); + $this->assertSame(array(5 => $this->choice1, 8 => $this->choice2), $this->list->getChoicesForValues($values)); + } + + public function testGetChoicesForValuesPreservesOrder() + { + $values = array($this->value2, $this->value1); + $this->assertSame(array($this->choice2, $this->choice1), $this->list->getChoicesForValues($values)); + } + + public function testGetChoicesForValuesIgnoresNonExistingValues() + { + $values = array($this->value1, $this->value2, 'foobar'); + $this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values)); + } + + // https://github.com/symfony/symfony/issues/3446 + public function testGetChoicesForValuesEmpty() + { + $this->assertSame(array(), $this->list->getChoicesForValues(array())); + } + + public function testGetValuesForChoices() + { + $choices = array($this->choice1, $this->choice2); + $this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices)); + } + + + public function testGetValuesForChoicesPreservesKeys() + { + $choices = array(5 => $this->choice1, 8 => $this->choice2); + $this->assertSame(array(5 => $this->value1, 8 => $this->value2), $this->list->getValuesForChoices($choices)); + } + + + public function testGetValuesForChoicesPreservesOrder() + { + $choices = array($this->choice2, $this->choice1); + $this->assertSame(array($this->value2, $this->value1), $this->list->getValuesForChoices($choices)); + } + + public function testGetValuesForChoicesIgnoresNonExistingChoices() + { + $choices = array($this->choice1, $this->choice2, 'foobar'); + $this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices)); + } + + public function testGetValuesForChoicesEmpty() + { + $this->assertSame(array(), $this->list->getValuesForChoices(array())); + } + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + abstract protected function createChoiceList(); + + abstract protected function getChoices(); + + abstract protected function getLabels(); + + abstract protected function getValues(); + + abstract protected function getIndices(); +} diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php index 531bb561b4c40..59f002d8ccb00 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php @@ -14,7 +14,7 @@ use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList; use Symfony\Component\Form\Extension\Core\View\ChoiceView; -class ChoiceListTest extends \PHPUnit_Framework_TestCase +class ChoiceListTest extends AbstractChoiceListTest { private $obj1; @@ -24,39 +24,14 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase private $obj4; - private $list; - protected function setUp() { - parent::setUp(); - $this->obj1 = new \stdClass(); $this->obj2 = new \stdClass(); $this->obj3 = new \stdClass(); $this->obj4 = new \stdClass(); - $this->list = new ChoiceList( - array( - 'Group 1' => array($this->obj1, $this->obj2), - 'Group 2' => array($this->obj3, $this->obj4), - ), - array( - 'Group 1' => array('A', 'B'), - 'Group 2' => array('C', 'D'), - ), - array($this->obj2, $this->obj3) - ); - } - - protected function tearDown() - { - parent::tearDown(); - - $this->obj1 = null; - $this->obj2 = null; - $this->obj3 = null; - $this->obj4 = null; - $this->list = null; + parent::setUp(); } public function testInitArray() @@ -119,111 +94,10 @@ public function testInitNestedArray() ), $this->list->getRemainingViews()); } - public function testGetIndicesForChoices() - { - $choices = array($this->obj2, $this->obj3); - $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices)); - } - - public function testGetIndicesForChoicesPreservesKeys() - { - $choices = array(5 => $this->obj2, 8 => $this->obj3); - $this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForChoices($choices)); - } - - public function testGetIndicesForChoicesPreservesOrder() - { - $choices = array($this->obj3, $this->obj2); - $this->assertSame(array(2, 1), $this->list->getIndicesForChoices($choices)); - } - - public function testGetIndicesForChoicesIgnoresNonExistingChoices() - { - $choices = array($this->obj2, $this->obj3, 'foobar'); - $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices)); - } - - public function testGetIndicesForValues() - { - // values and indices are always the same - $values = array('1', '2'); - $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values)); - } - - public function testGetIndicesForValuesPreservesKeys() - { - // values and indices are always the same - $values = array(5 => '1', 8 => '2'); - $this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForValues($values)); - } - - public function testGetIndicesForValuesPreservesOrder() - { - // values and indices are always the same - $values = array('2', '1'); - $this->assertSame(array(2, 1), $this->list->getIndicesForValues($values)); - } - - public function testGetIndicesForValuesIgnoresNonExistingValues() - { - $values = array('1', '2', '5'); - $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values)); - } - - public function testGetChoicesForValues() - { - $values = array('1', '2'); - $this->assertSame(array($this->obj2, $this->obj3), $this->list->getChoicesForValues($values)); - } - - public function testGetChoicesForValuesPreservesKeys() - { - $values = array(5 => '1', 8 => '2'); - $this->assertSame(array(5 => $this->obj2, 8 => $this->obj3), $this->list->getChoicesForValues($values)); - } - - public function testGetChoicesForValuesPreservesOrder() - { - $values = array('2', '1'); - $this->assertSame(array($this->obj3, $this->obj2), $this->list->getChoicesForValues($values)); - } - - public function testGetChoicesForValuesIgnoresNonExistingValues() - { - $values = array('1', '2', '5'); - $this->assertSame(array($this->obj2, $this->obj3), $this->list->getChoicesForValues($values)); - } - - public function testGetValuesForChoices() - { - $choices = array($this->obj2, $this->obj3); - $this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices)); - } - - - public function testGetValuesForChoicesPreservesKeys() - { - $choices = array(5 => $this->obj2, 8 => $this->obj3); - $this->assertSame(array(5 => '1', 8 => '2'), $this->list->getValuesForChoices($choices)); - } - - - public function testGetValuesForChoicesPreservesOrder() - { - $choices = array($this->obj3, $this->obj2); - $this->assertSame(array('2', '1'), $this->list->getValuesForChoices($choices)); - } - - public function testGetValuesForChoicesIgnoresNonExistingChoices() - { - $choices = array($this->obj2, $this->obj3, 'foobar'); - $this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices)); - } - /** * @expectedException \InvalidArgumentException */ - public function testNonMatchingLabels() + public function testInitWithInsufficientLabels() { $this->list = new ChoiceList( array($this->obj1, $this->obj2), @@ -231,7 +105,7 @@ public function testNonMatchingLabels() ); } - public function testLabelsContainingNull() + public function testInitWithLabelsContainingNull() { $this->list = new ChoiceList( array($this->obj1, $this->obj2), @@ -243,4 +117,42 @@ public function testLabelsContainingNull() $this->list->getRemainingViews() ); } + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + return new ChoiceList( + array( + 'Group 1' => array($this->obj1, $this->obj2), + 'Group 2' => array($this->obj3, $this->obj4), + ), + array( + 'Group 1' => array('A', 'B'), + 'Group 2' => array('C', 'D'), + ), + array($this->obj2, $this->obj3) + ); + } + + protected function getChoices() + { + return array(0 => $this->obj1, 1 => $this->obj2, 2 => $this->obj3, 3 => $this->obj4); + } + + protected function getLabels() + { + return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D'); + } + + protected function getValues() + { + return array(0 => '0', 1 => '1', 2 => '2', 3 => '3'); + } + + protected function getIndices() + { + return array(0, 1, 2, 3); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php index b7487377e6aad..752f39b6dcbb6 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php @@ -29,7 +29,7 @@ public function __toString() } } -class ObjectChoiceListTest extends \PHPUnit_Framework_TestCase +class ObjectChoiceListTest extends AbstractChoiceListTest { private $obj1; @@ -39,39 +39,14 @@ class ObjectChoiceListTest extends \PHPUnit_Framework_TestCase private $obj4; - /** - * @var ObjectChoiceList - */ - private $list; - protected function setUp() { - parent::setUp(); - $this->obj1 = (object) array('name' => 'A'); $this->obj2 = (object) array('name' => 'B'); $this->obj3 = (object) array('name' => 'C'); $this->obj4 = (object) array('name' => 'D'); - $this->list = new ObjectChoiceList( - array( - 'Group 1' => array($this->obj1, $this->obj2), - 'Group 2' => array($this->obj3, $this->obj4), - ), - 'name', - array($this->obj2, $this->obj3) - ); - } - - protected function tearDown() - { - parent::tearDown(); - - $this->obj1 = null; - $this->obj2 = null; - $this->obj3 = null; - $this->obj4 = null; - $this->list = null; + parent::setUp(); } public function testInitArray() @@ -209,4 +184,39 @@ public function testInitArrayThrowsExceptionIfToStringNotFound() array($this->obj1, $this->obj2, $this->obj3, $this->obj4) ); } + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + return new ObjectChoiceList( + array( + 'Group 1' => array($this->obj1, $this->obj2), + 'Group 2' => array($this->obj3, $this->obj4), + ), + 'name', + array($this->obj2, $this->obj3) + ); + } + + protected function getChoices() + { + return array(0 => $this->obj1, 1 => $this->obj2, 2 => $this->obj3, 3 => $this->obj4); + } + + protected function getLabels() + { + return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D'); + } + + protected function getValues() + { + return array(0 => '0', 1 => '1', 2 => '2', 3 => '3'); + } + + protected function getIndices() + { + return array(0, 1, 2, 3); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php index 85b0ed7485954..838a8e0864e41 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php @@ -11,43 +11,11 @@ namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList; -use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList; use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList; use Symfony\Component\Form\Extension\Core\View\ChoiceView; -class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase +class SimpleChoiceListTest extends AbstractChoiceListTest { - private $list; - - private $numericList; - - protected function setUp() - { - parent::setUp(); - - $choices = array( - 'Group 1' => array('a' => 'A', 'b' => 'B'), - 'Group 2' => array('c' => 'C', 'd' => 'D'), - ); - $numericChoices = array( - 'Group 1' => array(0 => 'A', 1 => 'B'), - 'Group 2' => array(2 => 'C', 3 => 'D'), - ); - - $this->list = new SimpleChoiceList($choices, array('b', 'c')); - - // Use COPY_CHOICE strategy to test for the various associated problems - $this->numericList = new SimpleChoiceList($numericChoices, array(1, 2)); - } - - protected function tearDown() - { - parent::tearDown(); - - $this->list = null; - $this->numericList = null; - } - public function testInitArray() { $choices = array('a' => 'A', 'b' => 'B', 'c' => 'C'); @@ -73,131 +41,6 @@ public function testInitNestedArray() ), $this->list->getRemainingViews()); } - public function testGetIndicesForChoices() - { - $choices = array('b', 'c'); - $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices)); - } - - public function testGetIndicesForChoicesPreservesKeys() - { - $choices = array(5 => 'b', 8 => 'c'); - $this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForChoices($choices)); - } - - public function testGetIndicesForChoicesPreservesOrder() - { - $choices = array('c', 'b'); - $this->assertSame(array(2, 1), $this->list->getIndicesForChoices($choices)); - } - - public function testGetIndicesForChoicesIgnoresNonExistingChoices() - { - $choices = array('b', 'c', 'foobar'); - $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices)); - } - - public function testGetIndicesForChoicesDealsWithNumericChoices() - { - // Pass choices as strings although they are integers - $choices = array('0', '1'); - $this->assertSame(array(0, 1), $this->numericList->getIndicesForChoices($choices)); - } - - public function testGetIndicesForValues() - { - $values = array('b', 'c'); - $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values)); - } - - public function testGetIndicesForValuesPreservesKeys() - { - $values = array(5 => 'b', 8 => 'c'); - $this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForValues($values)); - } - - public function testGetIndicesForValuesPreservesOrder() - { - $values = array('c', 'b'); - $this->assertSame(array(2, 1), $this->list->getIndicesForValues($values)); - } - - public function testGetIndicesForValuesIgnoresNonExistingValues() - { - $values = array('b', 'c', '100'); - $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values)); - } - - public function testGetIndicesForValuesDealsWithNumericValues() - { - // Pass values as strings although they are integers - $values = array('0', '1'); - $this->assertSame(array(0, 1), $this->numericList->getIndicesForValues($values)); - } - - public function testGetChoicesForValues() - { - $values = array('b', 'c'); - $this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values)); - } - - public function testGetChoicesForValuesPreservesKeys() - { - $values = array(5 => 'b', 8 => 'c'); - $this->assertSame(array(5 => 'b', 8 => 'c'), $this->list->getChoicesForValues($values)); - } - - public function testGetChoicesForValuesPreservesOrder() - { - $values = array('c', 'b'); - $this->assertSame(array('c', 'b'), $this->list->getChoicesForValues($values)); - } - - public function testGetChoicesForValuesIgnoresNonExistingValues() - { - $values = array('b', 'c', '100'); - $this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values)); - } - - public function testGetChoicesForValuesDealsWithNumericValues() - { - // Pass values as strings although they are integers - $values = array('0', '1'); - $this->assertSame(array(0, 1), $this->numericList->getChoicesForValues($values)); - } - - public function testGetValuesForChoices() - { - $choices = array('b', 'c'); - $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices)); - } - - public function testGetValuesForChoicesPreservesKeys() - { - $choices = array(5 => 'b', 8 => 'c'); - $this->assertSame(array(5 => 'b', 8 => 'c'), $this->list->getValuesForChoices($choices)); - } - - public function testGetValuesForChoicesPreservesOrder() - { - $choices = array('c', 'b'); - $this->assertSame(array('c', 'b'), $this->list->getValuesForChoices($choices)); - } - - public function testGetValuesForChoicesIgnoresNonExistingValues() - { - $choices = array('b', 'c', 'foobar'); - $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices)); - } - - public function testGetValuesForChoicesDealsWithNumericValues() - { - // Pass values as strings although they are integers - $values = array('0', '1'); - - $this->assertSame(array('0', '1'), $this->numericList->getValuesForChoices($values)); - } - /** * @dataProvider dirtyValuesProvider */ @@ -212,7 +55,6 @@ public function testGetValuesForChoicesDealsWithDirtyValues($choice, $value) 'foo10' => 'Foo 10', ); - // use COPY_CHOICE strategy to test the problems $this->list = new SimpleChoiceList($choices, array()); $this->assertSame(array($value), $this->list->getValuesForChoices(array($choice))); @@ -233,4 +75,35 @@ public function dirtyValuesProvider() array('foo10', 'foo10'), ); } + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + return new SimpleChoiceList(array( + 'Group 1' => array('a' => 'A', 'b' => 'B'), + 'Group 2' => array('c' => 'C', 'd' => 'D'), + ), array('b', 'c')); + } + + protected function getChoices() + { + return array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'); + } + + protected function getLabels() + { + return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D'); + } + + protected function getValues() + { + return array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'); + } + + protected function getIndices() + { + return array(0, 1, 2, 3); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleNumericChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleNumericChoiceListTest.php new file mode 100644 index 0000000000000..2b57288fb7f36 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleNumericChoiceListTest.php @@ -0,0 +1,78 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList; + +use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList; +use Symfony\Component\Form\Extension\Core\View\ChoiceView; + +class SimpleNumericChoiceListTest extends AbstractChoiceListTest +{ + public function testGetIndicesForChoicesDealsWithNumericChoices() + { + // Pass choices as strings although they are integers + $choices = array('0', '1'); + $this->assertSame(array(0, 1), $this->list->getIndicesForChoices($choices)); + } + + public function testGetIndicesForValuesDealsWithNumericValues() + { + // Pass values as strings although they are integers + $values = array('0', '1'); + $this->assertSame(array(0, 1), $this->list->getIndicesForValues($values)); + } + + public function testGetChoicesForValuesDealsWithNumericValues() + { + // Pass values as strings although they are integers + $values = array('0', '1'); + $this->assertSame(array(0, 1), $this->list->getChoicesForValues($values)); + } + + public function testGetValuesForChoicesDealsWithNumericValues() + { + // Pass values as strings although they are integers + $values = array('0', '1'); + + $this->assertSame(array('0', '1'), $this->list->getValuesForChoices($values)); + } + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + return new SimpleChoiceList(array( + 'Group 1' => array(0 => 'A', 1 => 'B'), + 'Group 2' => array(2 => 'C', 3 => 'D'), + ), array(1, 2)); + } + + protected function getChoices() + { + return array(0 => 0, 1 => 1, 2 => 2, 3 => 3); + } + + protected function getLabels() + { + return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D'); + } + + protected function getValues() + { + return array(0 => '0', 1 => '1', 2 => '2', 3 => '3'); + } + + protected function getIndices() + { + return array(0, 1, 2, 3); + } +} From 4d2dc5574d9474a4d97d33f5b1e669846c7493d4 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Tue, 10 Sep 2013 12:42:12 +0200 Subject: [PATCH 5/5] [DoctrineBridge] Improved test coverage of EntityChoiceList --- .../Form/ChoiceList/EntityChoiceList.php | 29 +- .../Doctrine/Test/DoctrineTestHelper.php | 59 +++ .../Doctrine/Tests/DoctrineOrmTestCase.php | 33 +- .../Tests/Fixtures/AssociationEntity.php | 8 +- .../Tests/Fixtures/CompositeIntIdEntity.php | 41 ++ ...Entity.php => CompositeStringIdEntity.php} | 7 +- ...leIdentEntity.php => DoubleNameEntity.php} | 2 +- ...temGroupEntity.php => GroupableEntity.php} | 2 +- ...eIdentEntity.php => SingleIntIdEntity.php} | 2 +- ...ty.php => SingleIntIdNoToStringEntity.php} | 2 +- ...entEntity.php => SingleStringIdEntity.php} | 7 +- .../{CompositeIdentEntity.php => User.php} | 2 +- ...bstractEntityChoiceListCompositeIdTest.php | 58 +++ ...bstractEntityChoiceListSingleIntIdTest.php | 58 +++ ...ractEntityChoiceListSingleStringIdTest.php | 58 +++ .../AbstractEntityChoiceListTest.php | 100 +++++ .../Form/ChoiceList/EntityChoiceListTest.php | 358 ------------------ .../GenericEntityChoiceListTest.php | 286 ++++++++++++++ .../LoadedEntityChoiceListCompositeIdTest.php | 31 ++ .../LoadedEntityChoiceListSingleIntIdTest.php | 31 ++ ...adedEntityChoiceListSingleStringIdTest.php | 31 ++ ...nloadedEntityChoiceListCompositeIdTest.php | 23 ++ ...iceListCompositeIdWithQueryBuilderTest.php | 32 ++ ...nloadedEntityChoiceListSingleIntIdTest.php | 23 ++ ...iceListSingleIntIdWithQueryBuilderTest.php | 32 ++ ...adedEntityChoiceListSingleStringIdTest.php | 23 ++ ...ListSingleStringIdWithQueryBuilderTest.php | 32 ++ .../Form/Type/EntityTypePerformanceTest.php | 2 +- .../Tests/Form/Type/EntityTypeTest.php | 140 +++---- .../Security/User/EntityUserProviderTest.php | 38 +- .../Constraints/UniqueValidatorTest.php | 62 +-- 31 files changed, 1092 insertions(+), 520 deletions(-) create mode 100644 src/Symfony/Bridge/Doctrine/Test/DoctrineTestHelper.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIntIdEntity.php rename src/Symfony/Bridge/Doctrine/Tests/Fixtures/{CompositeStringIdentEntity.php => CompositeStringIdEntity.php} (87%) rename src/Symfony/Bridge/Doctrine/Tests/Fixtures/{DoubleIdentEntity.php => DoubleNameEntity.php} (96%) rename src/Symfony/Bridge/Doctrine/Tests/Fixtures/{ItemGroupEntity.php => GroupableEntity.php} (97%) rename src/Symfony/Bridge/Doctrine/Tests/Fixtures/{SingleIdentEntity.php => SingleIntIdEntity.php} (96%) rename src/Symfony/Bridge/Doctrine/Tests/Fixtures/{NoToStringSingleIdentEntity.php => SingleIntIdNoToStringEntity.php} (94%) rename src/Symfony/Bridge/Doctrine/Tests/Fixtures/{SingleStringIdentEntity.php => SingleStringIdEntity.php} (86%) rename src/Symfony/Bridge/Doctrine/Tests/Fixtures/{CompositeIdentEntity.php => User.php} (95%) create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListCompositeIdTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleIntIdTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleStringIdTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListTest.php delete mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/GenericEntityChoiceListTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListCompositeIdTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleIntIdTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleStringIdTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdWithQueryBuilderTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdWithQueryBuilderTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdTest.php create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdWithQueryBuilderTest.php diff --git a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php index fabd25f7c2001..c3510515c5f5e 100644 --- a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php +++ b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php @@ -208,11 +208,26 @@ public function getChoicesForValues(array $values) // Optimize performance in case we have an entity loader and // a single-field identifier if ($this->idAsValue && $this->entityLoader) { - if (empty($values)) { - return array(); + $unorderedEntities = $this->entityLoader->getEntitiesByIds($this->idField, $values); + $entitiesByValue = array(); + $entities = array(); + + // Maintain order and indices from the given $values + // An alternative approach to the following loop is to add the + // "INDEX BY" clause to the Doctrine query in the loader, + // but I'm not sure whether that's doable in a generic fashion. + foreach ($unorderedEntities as $entity) { + $value = $this->fixValue(current($this->getIdentifierValues($entity))); + $entitiesByValue[$value] = $entity; } - return $this->entityLoader->getEntitiesByIds($this->idField, $values); + foreach ($values as $i => $value) { + if (isset($entitiesByValue[$value])) { + $entities[$i] = $entitiesByValue[$value]; + } + } + + return $entities; } $this->load(); @@ -240,10 +255,10 @@ public function getValuesForChoices(array $entities) if ($this->idAsValue) { $values = array(); - foreach ($entities as $entity) { + foreach ($entities as $i => $entity) { if ($entity instanceof $this->class) { // Make sure to convert to the right format - $values[] = $this->fixValue(current($this->getIdentifierValues($entity))); + $values[$i] = $this->fixValue(current($this->getIdentifierValues($entity))); } } @@ -275,10 +290,10 @@ public function getIndicesForChoices(array $entities) if ($this->idAsIndex) { $indices = array(); - foreach ($entities as $entity) { + foreach ($entities as $i => $entity) { if ($entity instanceof $this->class) { // Make sure to convert to the right format - $indices[] = $this->fixIndex(current($this->getIdentifierValues($entity))); + $indices[$i] = $this->fixIndex(current($this->getIdentifierValues($entity))); } } diff --git a/src/Symfony/Bridge/Doctrine/Test/DoctrineTestHelper.php b/src/Symfony/Bridge/Doctrine/Test/DoctrineTestHelper.php new file mode 100644 index 0000000000000..c763653ad9f33 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Test/DoctrineTestHelper.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Test; + +use Doctrine\Common\Annotations\AnnotationReader; +use Doctrine\ORM\Mapping\Driver\AnnotationDriver; +use Doctrine\ORM\EntityManager; + +/** + * Provides utility functions needed in tests. + * + * @author Bernhard Schussek + */ +class DoctrineTestHelper +{ + /** + * Returns an entity manager for testing. + * + * @return EntityManager + */ + public static function createTestEntityManager() + { + if (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers())) { + \PHPUnit_Framework_TestCase::markTestSkipped('This test requires SQLite support in your environment'); + } + + $config = new \Doctrine\ORM\Configuration(); + $config->setEntityNamespaces(array('SymfonyTestsDoctrine' => 'Symfony\Bridge\Doctrine\Tests\Fixtures')); + $config->setAutoGenerateProxyClasses(true); + $config->setProxyDir(\sys_get_temp_dir()); + $config->setProxyNamespace('SymfonyTests\Doctrine'); + $config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader())); + $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); + $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); + + $params = array( + 'driver' => 'pdo_sqlite', + 'memory' => true, + ); + + return EntityManager::create($params, $config); + } + + /** + * This class cannot be instantiated. + */ + private function __construct() + { + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/DoctrineOrmTestCase.php b/src/Symfony/Bridge/Doctrine/Tests/DoctrineOrmTestCase.php index 005c807b09ce7..970c9973140ff 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/DoctrineOrmTestCase.php +++ b/src/Symfony/Bridge/Doctrine/Tests/DoctrineOrmTestCase.php @@ -11,10 +11,14 @@ namespace Symfony\Bridge\Doctrine\Tests; -use Doctrine\Common\Annotations\AnnotationReader; -use Doctrine\ORM\Mapping\Driver\AnnotationDriver; -use Doctrine\ORM\EntityManager; +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; +/** + * Class DoctrineOrmTestCase + * + * @deprecated Deprecated as of Symfony 2.4, to be removed in Symfony 3.0. + * Use {@link DoctrineTestHelper} instead. + */ abstract class DoctrineOrmTestCase extends \PHPUnit_Framework_TestCase { protected function setUp() @@ -33,27 +37,10 @@ protected function setUp() } /** - * @return EntityManager + * @return \Doctrine\ORM\EntityManager */ - public static function createTestEntityManager($paths = array()) + public static function createTestEntityManager() { - if (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers())) { - self::markTestSkipped('This test requires SQLite support in your environment'); - } - $config = new \Doctrine\ORM\Configuration(); - $config->setEntityNamespaces(array('SymfonyTestsDoctrine' => 'Symfony\Bridge\Doctrine\Tests\Fixtures')); - $config->setAutoGenerateProxyClasses(true); - $config->setProxyDir(\sys_get_temp_dir()); - $config->setProxyNamespace('SymfonyTests\Doctrine'); - $config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader())); - $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); - $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); - - $params = array( - 'driver' => 'pdo_sqlite', - 'memory' => true, - ); - - return EntityManager::create($params, $config); + return DoctrineTestHelper::createTestEntityManager(); } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/AssociationEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/AssociationEntity.php index 0fe3d145a0019..9a33435ff2eb3 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/AssociationEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/AssociationEntity.php @@ -26,18 +26,18 @@ class AssociationEntity private $id; /** - * @ORM\ManyToOne(targetEntity="SingleIdentEntity") - * @var \Symfony\Bridge\Doctrine\Tests\Form\Fixtures\SingleIdentEntity + * @ORM\ManyToOne(targetEntity="SingleIntIdEntity") + * @var \Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity */ public $single; /** - * @ORM\ManyToOne(targetEntity="CompositeIdentEntity") + * @ORM\ManyToOne(targetEntity="CompositeIntIdEntity") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="composite_id1", referencedColumnName="id1"), * @ORM\JoinColumn(name="composite_id2", referencedColumnName="id2") * }) - * @var \Symfony\Bridge\Doctrine\Tests\Form\Fixtures\CompositeIdentEntity + * @var \Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity */ public $composite; } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIntIdEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIntIdEntity.php new file mode 100644 index 0000000000000..740a4f55f49cd --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIntIdEntity.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Fixtures; + +use Doctrine\ORM\Mapping\Id; +use Doctrine\ORM\Mapping\Column; +use Doctrine\ORM\Mapping\Entity; + +/** @Entity */ +class CompositeIntIdEntity +{ + /** @Id @Column(type="integer") */ + protected $id1; + + /** @Id @Column(type="integer") */ + protected $id2; + + /** @Column(type="string") */ + public $name; + + public function __construct($id1, $id2, $name) + { + $this->id1 = $id1; + $this->id2 = $id2; + $this->name = $name; + } + + public function __toString() + { + return $this->name; + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdEntity.php similarity index 87% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdEntity.php index 43c71f6e80720..10e083a8f4298 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class CompositeStringIdentEntity +class CompositeStringIdEntity { /** @Id @Column(type="string") */ protected $id1; @@ -33,4 +33,9 @@ public function __construct($id1, $id2, $name) $this->id2 = $id2; $this->name = $name; } + + public function __toString() + { + return $this->name; + } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleNameEntity.php similarity index 96% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleNameEntity.php index 2ac1ad3a8532d..cfb8e8b6664ff 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleNameEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class DoubleIdentEntity +class DoubleNameEntity { /** @Id @Column(type="integer") */ protected $id; diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/ItemGroupEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/GroupableEntity.php similarity index 97% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/ItemGroupEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/GroupableEntity.php index 04d2ddfde9dc5..2e36204bdfdad 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/ItemGroupEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/GroupableEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class ItemGroupEntity +class GroupableEntity { /** @Id @Column(type="integer") */ protected $id; diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdEntity.php similarity index 96% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdEntity.php index 09ee18a2fc561..44630a1fc51f1 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class SingleIdentEntity +class SingleIntIdEntity { /** @Id @Column(type="integer") */ protected $id; diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/NoToStringSingleIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdNoToStringEntity.php similarity index 94% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/NoToStringSingleIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdNoToStringEntity.php index a5ecb3da27e3f..bcbe7a5f7bdeb 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/NoToStringSingleIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdNoToStringEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class NoToStringSingleIdentEntity +class SingleIntIdNoToStringEntity { /** @Id @Column(type="integer") */ protected $id; diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdEntity.php similarity index 86% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdEntity.php index 50f53b790efb0..258c5a65158e7 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class SingleStringIdentEntity +class SingleStringIdEntity { /** @Id @Column(type="string") */ protected $id; @@ -29,4 +29,9 @@ public function __construct($id, $name) $this->id = $id; $this->name = $name; } + + public function __toString() + { + return $this->name; + } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/User.php similarity index 95% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/User.php index 9d263141f7f56..e59e32c27e82c 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/User.php @@ -17,7 +17,7 @@ use Symfony\Component\Security\Core\User\UserInterface; /** @Entity */ -class CompositeIdentEntity implements UserInterface +class User implements UserInterface { /** @Id @Column(type="integer") */ protected $id1; diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListCompositeIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListCompositeIdTest.php new file mode 100644 index 0000000000000..5980d9c734c54 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListCompositeIdTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity; + +/** + * @author Bernhard Schussek + */ +abstract class AbstractEntityChoiceListCompositeIdTest extends AbstractEntityChoiceListTest +{ + protected function getEntityClass() + { + return 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'; + } + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createObjects() + { + return array( + new CompositeIntIdEntity(10, 11, 'A'), + new CompositeIntIdEntity(20, 21, 'B'), + new CompositeIntIdEntity(30, 31, 'C'), + new CompositeIntIdEntity(40, 41, 'D'), + ); + } + + protected function getChoices() + { + return array(0 => $this->obj1, 1 => $this->obj2, 2 => $this->obj3, 3 => $this->obj4); + } + + protected function getLabels() + { + return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D'); + } + + protected function getValues() + { + return array(0 => '0', 1 => '1', 2 => '2', 3 => '3'); + } + + protected function getIndices() + { + return array(0, 1, 2, 3); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleIntIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleIntIdTest.php new file mode 100644 index 0000000000000..74af66db360e2 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleIntIdTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity; + +/** + * @author Bernhard Schussek + */ +abstract class AbstractEntityChoiceListSingleIntIdTest extends AbstractEntityChoiceListTest +{ + protected function getEntityClass() + { + return 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'; + } + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createObjects() + { + return array( + new SingleIntIdEntity(-10, 'A'), + new SingleIntIdEntity(10, 'B'), + new SingleIntIdEntity(20, 'C'), + new SingleIntIdEntity(30, 'D'), + ); + } + + protected function getChoices() + { + return array('_10' => $this->obj1, 10 => $this->obj2, 20 => $this->obj3, 30 => $this->obj4); + } + + protected function getLabels() + { + return array('_10' => 'A', 10 => 'B', 20 => 'C', 30 => 'D'); + } + + protected function getValues() + { + return array('_10' => '-10', 10 => '10', 20 => '20', 30 => '30'); + } + + protected function getIndices() + { + return array('_10', 10, 20, 30); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleStringIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleStringIdTest.php new file mode 100644 index 0000000000000..56b4c21319826 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleStringIdTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity; + +/** + * @author Bernhard Schussek + */ +abstract class AbstractEntityChoiceListSingleStringIdTest extends AbstractEntityChoiceListTest +{ + protected function getEntityClass() + { + return 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity'; + } + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createObjects() + { + return array( + new SingleStringIdEntity('a', 'A'), + new SingleStringIdEntity('b', 'B'), + new SingleStringIdEntity('c', 'C'), + new SingleStringIdEntity('d', 'D'), + ); + } + + protected function getChoices() + { + return array(0 => $this->obj1, 1 => $this->obj2, 2 => $this->obj3, 3 => $this->obj4); + } + + protected function getLabels() + { + return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D'); + } + + protected function getValues() + { + return array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'); + } + + protected function getIndices() + { + return array(0, 1, 2, 3); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListTest.php new file mode 100644 index 0000000000000..a56123a2a1f04 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListTest.php @@ -0,0 +1,100 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; +use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity; +use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; +use Doctrine\ORM\Tools\SchemaTool; +use Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest; + +/** + * @author Bernhard Schussek + */ +abstract class AbstractEntityChoiceListTest extends AbstractChoiceListTest +{ + /** + * @var \Doctrine\ORM\EntityManager + */ + protected $em; + + protected $obj1; + + protected $obj2; + + protected $obj3; + + protected $obj4; + + protected function setUp() + { + if (!class_exists('Symfony\Component\Form\Form')) { + $this->markTestSkipped('The "Form" component is not available'); + } + + if (!class_exists('Doctrine\DBAL\Platforms\MySqlPlatform')) { + $this->markTestSkipped('Doctrine DBAL is not available.'); + } + + if (!class_exists('Doctrine\Common\Version')) { + $this->markTestSkipped('Doctrine Common is not available.'); + } + + if (!class_exists('Doctrine\ORM\EntityManager')) { + $this->markTestSkipped('Doctrine ORM is not available.'); + } + + $this->em = DoctrineTestHelper::createTestEntityManager(); + + $schemaTool = new SchemaTool($this->em); + $classes = array($this->em->getClassMetadata($this->getEntityClass())); + + try { + $schemaTool->dropSchema($classes); + } catch (\Exception $e) { + } + + try { + $schemaTool->createSchema($classes); + } catch (\Exception $e) { + } + + list($this->obj1, $this->obj2, $this->obj3, $this->obj4) = $this->createObjects(); + + $this->em->persist($this->obj1); + $this->em->persist($this->obj2); + $this->em->persist($this->obj3); + $this->em->persist($this->obj4); + $this->em->flush(); + + parent::setUp(); + } + + protected function tearDown() + { + parent::tearDown(); + + $this->em = null; + } + + abstract protected function getEntityClass(); + + abstract protected function createObjects(); + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + return new EntityChoiceList($this->em, $this->getEntityClass()); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php deleted file mode 100644 index a3ce7503436cd..0000000000000 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php +++ /dev/null @@ -1,358 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; - -use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader; -use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase; -use Symfony\Bridge\Doctrine\Tests\Fixtures\ItemGroupEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\NoToStringSingleIdentEntity; -use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; -use Symfony\Component\Form\Extension\Core\View\ChoiceView; -use Doctrine\ORM\Tools\SchemaTool; - -class EntityChoiceListTest extends DoctrineOrmTestCase -{ - const ITEM_GROUP_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\ItemGroupEntity'; - - const SINGLE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity'; - - const SINGLE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdentEntity'; - - const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity'; - - private $em; - - protected function setUp() - { - if (!class_exists('Symfony\Component\Form\Form')) { - $this->markTestSkipped('The "Form" component is not available'); - } - - parent::setUp(); - - $this->em = $this->createTestEntityManager(); - - $schemaTool = new SchemaTool($this->em); - $classes = array( - $this->em->getClassMetadata(self::ITEM_GROUP_CLASS), - $this->em->getClassMetadata(self::SINGLE_IDENT_CLASS), - $this->em->getClassMetadata(self::SINGLE_STRING_IDENT_CLASS), - $this->em->getClassMetadata(self::COMPOSITE_IDENT_CLASS), - ); - - try { - $schemaTool->dropSchema($classes); - } catch (\Exception $e) { - } - - try { - $schemaTool->createSchema($classes); - } catch (\Exception $e) { - } - } - - protected function tearDown() - { - parent::tearDown(); - - $this->em = null; - } - - /** - * @expectedException \Symfony\Component\Form\Exception\FormException - * @expectedMessage Entity "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity" passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option). - */ - public function testEntitiesMustHaveAToStringMethod() - { - $entity1 = new NoToStringSingleIdentEntity(1, 'Foo'); - $entity2 = new NoToStringSingleIdentEntity(2, 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - null, - null, - array( - $entity1, - $entity2, - ) - ); - - $choiceList->getValues(); - } - - /** - * @expectedException \Symfony\Component\Form\Exception\FormException - */ - public function testChoicesMustBeManaged() - { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - - // no persist here! - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name', - null, - array( - $entity1, - $entity2, - ) - ); - - // triggers loading -> exception - $choiceList->getChoices(); - } - - public function testFlattenedChoicesAreManaged() - { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name', - null, - array( - $entity1, - $entity2, - ) - ); - - $this->assertSame(array(1 => $entity1, 2 => $entity2), $choiceList->getChoices()); - } - - public function testEmptyChoicesAreManaged() - { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name', - null, - array() - ); - - $this->assertSame(array(), $choiceList->getChoices()); - } - - public function testNestedChoicesAreManaged() - { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - - // Oh yeah, we're persisting with fire now! - $this->em->persist($entity1); - $this->em->persist($entity2); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name', - null, - array( - 'group1' => array($entity1), - 'group2' => array($entity2), - ), - array() - ); - - $this->assertSame(array(1 => $entity1, 2 => $entity2), $choiceList->getChoices()); - $this->assertEquals(array( - 'group1' => array(1 => new ChoiceView($entity1, '1', 'Foo')), - 'group2' => array(2 => new ChoiceView($entity2, '2', 'Bar')) - ), $choiceList->getRemainingViews()); - } - - public function testGroupBySupportsString() - { - $item1 = new ItemGroupEntity(1, 'Foo', 'Group1'); - $item2 = new ItemGroupEntity(2, 'Bar', 'Group1'); - $item3 = new ItemGroupEntity(3, 'Baz', 'Group2'); - $item4 = new ItemGroupEntity(4, 'Boo!', null); - - $this->em->persist($item1); - $this->em->persist($item2); - $this->em->persist($item3); - $this->em->persist($item4); - - $choiceList = new EntityChoiceList( - $this->em, - self::ITEM_GROUP_CLASS, - 'name', - null, - array( - $item1, - $item2, - $item3, - $item4, - ), - array(), - 'groupName' - ); - - $this->assertEquals(array(1 => $item1, 2 => $item2, 3 => $item3, 4 => $item4), $choiceList->getChoices()); - $this->assertEquals(array( - 'Group1' => array(1 => new ChoiceView($item1, '1', 'Foo'), 2 => new ChoiceView($item2, '2', 'Bar')), - 'Group2' => array(3 => new ChoiceView($item3, '3', 'Baz')), - 4 => new ChoiceView($item4, '4', 'Boo!') - ), $choiceList->getRemainingViews()); - } - - public function testGroupByInvalidPropertyPathReturnsFlatChoices() - { - $item1 = new ItemGroupEntity(1, 'Foo', 'Group1'); - $item2 = new ItemGroupEntity(2, 'Bar', 'Group1'); - - $this->em->persist($item1); - $this->em->persist($item2); - - $choiceList = new EntityChoiceList( - $this->em, - self::ITEM_GROUP_CLASS, - 'name', - null, - array( - $item1, - $item2, - ), - array(), - 'child.that.does.not.exist' - ); - - $this->assertEquals(array( - 1 => $item1, - 2 => $item2 - ), $choiceList->getChoices()); - } - - public function testPossibleToProvideShorthandEntityName() - { - $shorthandName = 'SymfonyTestsDoctrine:SingleIdentEntity'; - - $item1 = new SingleIdentEntity(1, 'Foo'); - $item2 = new SingleIdentEntity(2, 'Bar'); - - $this->em->persist($item1); - $this->em->persist($item2); - - $choiceList = new EntityChoiceList( - $this->em, - $shorthandName, - null, - null, - null, - array(), - null - ); - - $this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2))); - $this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2))); - } - - // Ticket #3446 - public function testGetEmptyArrayChoicesForEmptyValues() - { - $qb = $this->em->createQueryBuilder()->select('s')->from(self::SINGLE_IDENT_CLASS, 's'); - $entityLoader = new ORMQueryBuilderLoader($qb); - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - null, - $entityLoader - ); - - $this->assertEquals(array(), $choiceList->getChoicesForValues(array())); - } - - // https://github.com/symfony/symfony/issues/3635 - public function testSingleNonIntIdFallsBackToGeneration() - { - $entity1 = new SingleStringIdentEntity('Id 1', 'Foo'); - $entity2 = new SingleStringIdentEntity('Id 2', 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - $this->em->flush(); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_STRING_IDENT_CLASS, - 'name' - ); - - $this->assertSame(array(0 => $entity1, 1 => $entity2), $choiceList->getChoices()); - } - - public function testMinusReplacedByUnderscoreInNegativeIntIds() - { - $entity1 = new SingleIdentEntity(-1, 'Foo'); - $entity2 = new SingleIdentEntity(1, 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - $this->em->flush(); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name' - ); - - $this->assertSame(array('_1' => $entity1, 1 => $entity2), $choiceList->getChoices()); - $this->assertSame(array('_1', 1), $choiceList->getIndicesForChoices(array($entity1, $entity2))); - $this->assertSame(array('_1', 1), $choiceList->getIndicesForValues(array('-1', '1'))); - } - - public function testMinusReplacedByUnderscoreIfNotLoaded() - { - $entity1 = new SingleIdentEntity(-1, 'Foo'); - $entity2 = new SingleIdentEntity(1, 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - $this->em->flush(); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name' - ); - - // no getChoices()! - - $this->assertSame(array('_1', 1), $choiceList->getIndicesForChoices(array($entity1, $entity2))); - $this->assertSame(array('_1', 1), $choiceList->getIndicesForValues(array('-1', '1'))); - } -} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/GenericEntityChoiceListTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/GenericEntityChoiceListTest.php new file mode 100644 index 0000000000000..fec65b8245d01 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/GenericEntityChoiceListTest.php @@ -0,0 +1,286 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; +use Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity; +use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; +use Symfony\Component\Form\Extension\Core\View\ChoiceView; +use Doctrine\ORM\Tools\SchemaTool; + +class GenericEntityChoiceListTest extends \PHPUnit_Framework_TestCase +{ + const SINGLE_INT_ID_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'; + + const SINGLE_STRING_ID_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity'; + + const COMPOSITE_ID_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'; + + const GROUPABLE_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity'; + + /** + * @var \Doctrine\ORM\EntityManager + */ + private $em; + + protected function setUp() + { + if (!class_exists('Symfony\Component\Form\Form')) { + $this->markTestSkipped('The "Form" component is not available'); + } + + if (!class_exists('Doctrine\DBAL\Platforms\MySqlPlatform')) { + $this->markTestSkipped('Doctrine DBAL is not available.'); + } + + if (!class_exists('Doctrine\Common\Version')) { + $this->markTestSkipped('Doctrine Common is not available.'); + } + + if (!class_exists('Doctrine\ORM\EntityManager')) { + $this->markTestSkipped('Doctrine ORM is not available.'); + } + + $this->em = DoctrineTestHelper::createTestEntityManager(); + + $schemaTool = new SchemaTool($this->em); + $classes = array( + $this->em->getClassMetadata(self::SINGLE_INT_ID_CLASS), + $this->em->getClassMetadata(self::SINGLE_STRING_ID_CLASS), + $this->em->getClassMetadata(self::COMPOSITE_ID_CLASS), + $this->em->getClassMetadata(self::GROUPABLE_CLASS), + ); + + try { + $schemaTool->dropSchema($classes); + } catch (\Exception $e) { + } + + try { + $schemaTool->createSchema($classes); + } catch (\Exception $e) { + } + + parent::setUp(); + } + + protected function tearDown() + { + parent::tearDown(); + + $this->em = null; + } + + /** + * @expectedException \Symfony\Component\Form\Exception\StringCastException + * @expectedMessage Entity "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity" passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option). + */ + public function testEntitiesMustHaveAToStringMethod() + { + $entity1 = new SingleIntIdNoToStringEntity(1, 'Foo'); + $entity2 = new SingleIntIdNoToStringEntity(2, 'Bar'); + + // Persist for managed state + $this->em->persist($entity1); + $this->em->persist($entity2); + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_INT_ID_CLASS, + null, + null, + array( + $entity1, + $entity2, + ) + ); + + $choiceList->getValues(); + } + + /** + * @expectedException \Symfony\Component\Form\Exception\FormException + */ + public function testChoicesMustBeManaged() + { + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + + // no persist here! + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_INT_ID_CLASS, + 'name', + null, + array( + $entity1, + $entity2, + ) + ); + + // triggers loading -> exception + $choiceList->getChoices(); + } + + public function testInitExplicitChoices() + { + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + + // Persist for managed state + $this->em->persist($entity1); + $this->em->persist($entity2); + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_INT_ID_CLASS, + 'name', + null, + array( + $entity1, + $entity2, + ) + ); + + $this->assertSame(array(1 => $entity1, 2 => $entity2), $choiceList->getChoices()); + } + + public function testInitEmptyChoices() + { + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + + // Persist for managed state + $this->em->persist($entity1); + $this->em->persist($entity2); + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_INT_ID_CLASS, + 'name', + null, + array() + ); + + $this->assertSame(array(), $choiceList->getChoices()); + } + + public function testInitNestedChoices() + { + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + + // Oh yeah, we're persisting with fire now! + $this->em->persist($entity1); + $this->em->persist($entity2); + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_INT_ID_CLASS, + 'name', + null, + array( + 'group1' => array($entity1), + 'group2' => array($entity2), + ), + array() + ); + + $this->assertSame(array(1 => $entity1, 2 => $entity2), $choiceList->getChoices()); + $this->assertEquals(array( + 'group1' => array(1 => new ChoiceView($entity1, '1', 'Foo')), + 'group2' => array(2 => new ChoiceView($entity2, '2', 'Bar')) + ), $choiceList->getRemainingViews()); + } + + public function testGroupByPropertyPath() + { + $item1 = new GroupableEntity(1, 'Foo', 'Group1'); + $item2 = new GroupableEntity(2, 'Bar', 'Group1'); + $item3 = new GroupableEntity(3, 'Baz', 'Group2'); + $item4 = new GroupableEntity(4, 'Boo!', null); + + $this->em->persist($item1); + $this->em->persist($item2); + $this->em->persist($item3); + $this->em->persist($item4); + + $choiceList = new EntityChoiceList( + $this->em, + self::GROUPABLE_CLASS, + 'name', + null, + array( + $item1, + $item2, + $item3, + $item4, + ), + array(), + 'groupName' + ); + + $this->assertEquals(array(1 => $item1, 2 => $item2, 3 => $item3, 4 => $item4), $choiceList->getChoices()); + $this->assertEquals(array( + 'Group1' => array(1 => new ChoiceView($item1, '1', 'Foo'), 2 => new ChoiceView($item2, '2', 'Bar')), + 'Group2' => array(3 => new ChoiceView($item3, '3', 'Baz')), + 4 => new ChoiceView($item4, '4', 'Boo!') + ), $choiceList->getRemainingViews()); + } + + public function testGroupByInvalidPropertyPathReturnsFlatChoices() + { + $item1 = new GroupableEntity(1, 'Foo', 'Group1'); + $item2 = new GroupableEntity(2, 'Bar', 'Group1'); + + $this->em->persist($item1); + $this->em->persist($item2); + + $choiceList = new EntityChoiceList( + $this->em, + self::GROUPABLE_CLASS, + 'name', + null, + array( + $item1, + $item2, + ), + array(), + 'child.that.does.not.exist' + ); + + $this->assertEquals(array( + 1 => $item1, + 2 => $item2 + ), $choiceList->getChoices()); + } + + public function testInitShorthandEntityName() + { + $item1 = new SingleIntIdEntity(1, 'Foo'); + $item2 = new SingleIntIdEntity(2, 'Bar'); + + $this->em->persist($item1); + $this->em->persist($item2); + + $choiceList = new EntityChoiceList( + $this->em, + 'SymfonyTestsDoctrine:SingleIntIdEntity' + ); + + $this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2))); + $this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2))); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListCompositeIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListCompositeIdTest.php new file mode 100644 index 0000000000000..90cbf1d7c8b31 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListCompositeIdTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class LoadedEntityChoiceListCompositeIdTest extends AbstractEntityChoiceListCompositeIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $list = parent::createChoiceList(); + + // load list + $list->getChoices(); + + return $list; + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleIntIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleIntIdTest.php new file mode 100644 index 0000000000000..52d04c38798a5 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleIntIdTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class LoadedEntityChoiceListSingleIntIdTest extends AbstractEntityChoiceListSingleIntIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $list = parent::createChoiceList(); + + // load list + $list->getChoices(); + + return $list; + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleStringIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleStringIdTest.php new file mode 100644 index 0000000000000..690d4b3d2300c --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleStringIdTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class LoadedEntityChoiceListSingleStringIdTest extends AbstractEntityChoiceListSingleStringIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $list = parent::createChoiceList(); + + // load list + $list->getChoices(); + + return $list; + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdTest.php new file mode 100644 index 0000000000000..5740a2ff9434d --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListCompositeIdTest extends AbstractEntityChoiceListCompositeIdTest +{ + public function testGetIndicesForValuesIgnoresNonExistingValues() + { + $this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.'); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdWithQueryBuilderTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdWithQueryBuilderTest.php new file mode 100644 index 0000000000000..9c72ccccd91a4 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdWithQueryBuilderTest.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; +use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListCompositeIdWithQueryBuilderTest extends UnloadedEntityChoiceListCompositeIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $qb = $this->em->createQueryBuilder()->select('s')->from($this->getEntityClass(), 's'); + $loader = new ORMQueryBuilderLoader($qb); + + return new EntityChoiceList($this->em, $this->getEntityClass(), null, $loader); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdTest.php new file mode 100644 index 0000000000000..a87687841510b --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListSingleIntIdTest extends AbstractEntityChoiceListSingleIntIdTest +{ + public function testGetIndicesForValuesIgnoresNonExistingValues() + { + $this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.'); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdWithQueryBuilderTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdWithQueryBuilderTest.php new file mode 100644 index 0000000000000..fa5bb80ae7be8 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdWithQueryBuilderTest.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; +use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListSingleIntIdWithQueryBuilderTest extends UnloadedEntityChoiceListSingleIntIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $qb = $this->em->createQueryBuilder()->select('s')->from($this->getEntityClass(), 's'); + $loader = new ORMQueryBuilderLoader($qb); + + return new EntityChoiceList($this->em, $this->getEntityClass(), null, $loader); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdTest.php new file mode 100644 index 0000000000000..5b25b49a710bf --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListSingleStringIdTest extends AbstractEntityChoiceListSingleStringIdTest +{ + public function testGetIndicesForValuesIgnoresNonExistingValues() + { + $this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.'); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdWithQueryBuilderTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdWithQueryBuilderTest.php new file mode 100644 index 0000000000000..9fba5b9295a08 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdWithQueryBuilderTest.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; +use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListSingleStringIdWithQueryBuilderTest extends UnloadedEntityChoiceListSingleStringIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $qb = $this->em->createQueryBuilder()->select('s')->from($this->getEntityClass(), 's'); + $loader = new ORMQueryBuilderLoader($qb); + + return new EntityChoiceList($this->em, $this->getEntityClass(), null, $loader); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php index 1fc6ae378e8a7..1505bf8215e09 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php @@ -89,7 +89,7 @@ protected function setUp() foreach ($ids as $id) { $name = 65 + chr($id % 57); - $this->em->persist(new SingleIdentEntity($id, $name)); + $this->em->persist(new SingleIntIdEntity($id, $name)); } $this->em->flush(); diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php index 4d23db4ef827b..f44f70f017b96 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php @@ -11,26 +11,26 @@ namespace Symfony\Bridge\Doctrine\Tests\Form\Type; +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; use Symfony\Component\Form\Exception\UnexpectedTypeException; -use Symfony\Component\Form\Tests\Extension\Core\Type\TypeTestCase; -use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase; -use Symfony\Bridge\Doctrine\Tests\Fixtures\ItemGroupEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdentEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdEntity; use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension; use Doctrine\ORM\Tools\SchemaTool; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\Form\Extension\Core\View\ChoiceView; +use Symfony\Component\Form\Tests\Extension\Core\Type\TypeTestCase; class EntityTypeTest extends TypeTestCase { - const ITEM_GROUP_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\ItemGroupEntity'; - const SINGLE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity'; - const SINGLE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdentEntity'; - const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity'; - const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdentEntity'; + const ITEM_GROUP_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity'; + const SINGLE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'; + const SINGLE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity'; + const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'; + const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdEntity'; /** * @var \Doctrine\ORM\EntityManager @@ -60,7 +60,7 @@ protected function setUp() $this->markTestSkipped('Doctrine ORM is not available.'); } - $this->em = DoctrineOrmTestCase::createTestEntityManager(); + $this->em = DoctrineTestHelper::createTestEntityManager(); $this->emRegistry = $this->createRegistryMock('default', $this->em); parent::setUp(); @@ -121,8 +121,8 @@ public function testClassOptionIsRequired() public function testSetDataToUninitializedEntityWithNonRequired() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); $this->persist(array($entity1, $entity2)); @@ -138,8 +138,8 @@ public function testSetDataToUninitializedEntityWithNonRequired() public function testSetDataToUninitializedEntityWithNonRequiredToString() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); $this->persist(array($entity1, $entity2)); @@ -154,8 +154,8 @@ public function testSetDataToUninitializedEntityWithNonRequiredToString() public function testSetDataToUninitializedEntityWithNonRequiredQueryBuilder() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); $this->persist(array($entity1, $entity2)); $qb = $this->em->createQueryBuilder()->select('e')->from(self::SINGLE_IDENT_CLASS, 'e'); @@ -283,8 +283,8 @@ public function testSubmitMultipleNull() public function testSubmitSingleNonExpandedSingleIdentifier() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); $this->persist(array($entity1, $entity2)); @@ -305,8 +305,8 @@ public function testSubmitSingleNonExpandedSingleIdentifier() public function testSubmitSingleNonExpandedCompositeIdentifier() { - $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); - $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); + $entity1 = new CompositeIntIdEntity(10, 20, 'Foo'); + $entity2 = new CompositeIntIdEntity(30, 40, 'Bar'); $this->persist(array($entity1, $entity2)); @@ -328,9 +328,9 @@ public function testSubmitSingleNonExpandedCompositeIdentifier() public function testSubmitMultipleNonExpandedSingleIdentifier() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -353,9 +353,9 @@ public function testSubmitMultipleNonExpandedSingleIdentifier() public function testSubmitMultipleNonExpandedSingleIdentifierForExistingData() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -384,9 +384,9 @@ public function testSubmitMultipleNonExpandedSingleIdentifierForExistingData() public function testSubmitMultipleNonExpandedCompositeIdentifier() { - $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); - $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); - $entity3 = new CompositeIdentEntity(50, 60, 'Baz'); + $entity1 = new CompositeIntIdEntity(10, 20, 'Foo'); + $entity2 = new CompositeIntIdEntity(30, 40, 'Bar'); + $entity3 = new CompositeIntIdEntity(50, 60, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -410,9 +410,9 @@ public function testSubmitMultipleNonExpandedCompositeIdentifier() public function testSubmitMultipleNonExpandedCompositeIdentifierExistingData() { - $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); - $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); - $entity3 = new CompositeIdentEntity(50, 60, 'Baz'); + $entity1 = new CompositeIntIdEntity(10, 20, 'Foo'); + $entity2 = new CompositeIntIdEntity(30, 40, 'Bar'); + $entity3 = new CompositeIntIdEntity(50, 60, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -441,8 +441,8 @@ public function testSubmitMultipleNonExpandedCompositeIdentifierExistingData() public function testSubmitSingleExpanded() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); $this->persist(array($entity1, $entity2)); @@ -466,9 +466,9 @@ public function testSubmitSingleExpanded() public function testSubmitMultipleExpanded() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Bar'); $this->persist(array($entity1, $entity2, $entity3)); @@ -496,9 +496,9 @@ public function testSubmitMultipleExpanded() public function testOverrideChoices() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -520,10 +520,10 @@ public function testOverrideChoices() public function testGroupByChoices() { - $item1 = new ItemGroupEntity(1, 'Foo', 'Group1'); - $item2 = new ItemGroupEntity(2, 'Bar', 'Group1'); - $item3 = new ItemGroupEntity(3, 'Baz', 'Group2'); - $item4 = new ItemGroupEntity(4, 'Boo!', null); + $item1 = new GroupableEntity(1, 'Foo', 'Group1'); + $item2 = new GroupableEntity(2, 'Bar', 'Group1'); + $item3 = new GroupableEntity(3, 'Baz', 'Group2'); + $item4 = new GroupableEntity(4, 'Boo!', null); $this->persist(array($item1, $item2, $item3, $item4)); @@ -547,9 +547,9 @@ public function testGroupByChoices() public function testPreferredChoices() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -566,9 +566,9 @@ public function testPreferredChoices() public function testOverrideChoicesWithPreferredChoices() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -586,9 +586,9 @@ public function testOverrideChoicesWithPreferredChoices() public function testDisallowChoicesThatAreNotIncludedChoicesSingleIdentifier() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -607,9 +607,9 @@ public function testDisallowChoicesThatAreNotIncludedChoicesSingleIdentifier() public function testDisallowChoicesThatAreNotIncludedChoicesCompositeIdentifier() { - $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); - $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); - $entity3 = new CompositeIdentEntity(50, 60, 'Baz'); + $entity1 = new CompositeIntIdEntity(10, 20, 'Foo'); + $entity2 = new CompositeIntIdEntity(30, 40, 'Bar'); + $entity3 = new CompositeIntIdEntity(50, 60, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -628,9 +628,9 @@ public function testDisallowChoicesThatAreNotIncludedChoicesCompositeIdentifier( public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleIdentifier() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -652,9 +652,9 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleIdentifie public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureSingleIdentifier() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -676,9 +676,9 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureSingle public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureCompositeIdentifier() { - $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); - $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); - $entity3 = new CompositeIdentEntity(50, 60, 'Baz'); + $entity1 = new CompositeIntIdEntity(10, 20, 'Foo'); + $entity2 = new CompositeIntIdEntity(30, 40, 'Bar'); + $entity3 = new CompositeIntIdEntity(50, 60, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -700,7 +700,7 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureCompos public function testSubmitSingleStringIdentifier() { - $entity1 = new SingleStringIdentEntity('foo', 'Foo'); + $entity1 = new SingleStringIdEntity('foo', 'Foo'); $this->persist(array($entity1)); @@ -721,7 +721,7 @@ public function testSubmitSingleStringIdentifier() public function testSubmitCompositeStringIdentifier() { - $entity1 = new CompositeStringIdentEntity('foo1', 'foo2', 'Foo'); + $entity1 = new CompositeStringIdEntity('foo1', 'foo2', 'Foo'); $this->persist(array($entity1)); diff --git a/src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php b/src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php index 3df1be5365be8..f791f97f01ff0 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php @@ -11,12 +11,12 @@ namespace Symfony\Bridge\Doctrine\Tests\Security\User; -use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase; -use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity; +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; +use Symfony\Bridge\Doctrine\Tests\Fixtures\User; use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider; use Doctrine\ORM\Tools\SchemaTool; -class EntityUserProviderTest extends DoctrineOrmTestCase +class EntityUserProviderTest extends \PHPUnit_Framework_TestCase { protected function setUp() { @@ -29,17 +29,17 @@ protected function setUp() public function testRefreshUserGetsUserByPrimaryKey() { - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); - $user1 = new CompositeIdentEntity(1, 1, 'user1'); - $user2 = new CompositeIdentEntity(1, 2, 'user2'); + $user1 = new User(1, 1, 'user1'); + $user2 = new User(1, 2, 'user2'); $em->persist($user1); $em->persist($user2); $em->flush(); - $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity', 'name'); + $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name'); // try to change the user identity $user1->name = 'user2'; @@ -49,10 +49,10 @@ public function testRefreshUserGetsUserByPrimaryKey() public function testRefreshUserRequiresId() { - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); - $user1 = new CompositeIdentEntity(null, null, 'user1'); - $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity', 'name'); + $user1 = new User(null, null, 'user1'); + $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name'); $this->setExpectedException( 'InvalidArgumentException', @@ -63,17 +63,17 @@ public function testRefreshUserRequiresId() public function testRefreshInvalidUser() { - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); - $user1 = new CompositeIdentEntity(1, 1, 'user1'); + $user1 = new User(1, 1, 'user1'); $em->persist($user1); $em->flush(); - $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity', 'name'); + $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name'); - $user2 = new CompositeIdentEntity(1, 2, 'user2'); + $user2 = new User(1, 2, 'user2'); $this->setExpectedException( 'Symfony\Component\Security\Core\Exception\UsernameNotFoundException', 'User with id {"id1":1,"id2":2} not found' @@ -83,18 +83,18 @@ public function testRefreshInvalidUser() public function testSupportProxy() { - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); - $user1 = new CompositeIdentEntity(1, 1, 'user1'); + $user1 = new User(1, 1, 'user1'); $em->persist($user1); $em->flush(); $em->clear(); - $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity', 'name'); + $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name'); - $user2 = $em->getReference('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity', array('id1' => 1, 'id2' => 1)); + $user2 = $em->getReference('Symfony\Bridge\Doctrine\Tests\Fixtures\User', array('id1' => 1, 'id2' => 1)); $this->assertTrue($provider->supportsClass(get_class($user2))); } @@ -113,7 +113,7 @@ private function createSchema($em) { $schemaTool = new SchemaTool($em); $schemaTool->createSchema(array( - $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity'), + $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\User'), )); } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php index 2c5af404a266d..e10f799d4c4ff 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php @@ -11,12 +11,12 @@ namespace Symfony\Bridge\Doctrine\Tests\Validator\Constraints; -use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase; +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; +use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity; use Symfony\Component\Validator\DefaultTranslator; use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory; -use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity; use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator; @@ -24,7 +24,7 @@ use Symfony\Component\Validator\Validator; use Doctrine\ORM\Tools\SchemaTool; -class UniqueValidatorTest extends DoctrineOrmTestCase +class UniqueValidatorTest extends \PHPUnit_Framework_TestCase { protected function setUp() { @@ -109,7 +109,7 @@ protected function createValidatorFactory($uniqueValidator) public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null, $repositoryMethod = 'findBy', $ignoreNull = true) { if (!$validateClass) { - $validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity'; + $validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'; } if (!$uniqueFields) { $uniqueFields = array('name'); @@ -140,9 +140,9 @@ private function createSchema($em) { $schemaTool = new SchemaTool($em); $schemaTool->createSchema(array( - $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity'), - $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity'), - $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity'), + $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'), + $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'), + $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'), $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'), )); } @@ -153,11 +153,11 @@ private function createSchema($em) public function testValidateUniqueness() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em); - $entity1 = new SingleIdentEntity(1, 'Foo'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), "No violations found on entity before it is saved to the database."); @@ -167,7 +167,7 @@ public function testValidateUniqueness() $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), "No violations found on entity after it was saved to the database."); - $entity2 = new SingleIdentEntity(2, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Foo'); $violationsList = $validator->validate($entity2); $this->assertEquals(1, $violationsList->count(), "Violation found on entity with conflicting entity existing in the database."); @@ -181,16 +181,16 @@ public function testValidateUniqueness() public function testValidateCustomErrorPath() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em, null, null, 'bar'); - $entity1 = new SingleIdentEntity(1, 'Foo'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); $em->persist($entity1); $em->flush(); - $entity2 = new SingleIdentEntity(2, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Foo'); $violationsList = $validator->validate($entity2); $this->assertEquals(1, $violationsList->count(), "Violation found on entity with conflicting entity existing in the database."); @@ -204,12 +204,12 @@ public function testValidateCustomErrorPath() public function testValidateUniquenessWithNull() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em); - $entity1 = new SingleIdentEntity(1, null); - $entity2 = new SingleIdentEntity(2, null); + $entity1 = new SingleIntIdEntity(1, null); + $entity2 = new SingleIntIdEntity(2, null); $em->persist($entity1); $em->persist($entity2); @@ -222,12 +222,12 @@ public function testValidateUniquenessWithNull() public function testValidateUniquenessWithIgnoreNull() { $entityManagerName = "foo"; - $validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity'; - $em = $this->createTestEntityManager(); + $validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'; + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em, $validateClass, array('name', 'name2'), 'bar', 'findby', false); - $entity1 = new DoubleIdentEntity(1, 'Foo', null); + $entity1 = new DoubleNameEntity(1, 'Foo', null); $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), "No violations found on entity before it is saved to the database."); @@ -237,7 +237,7 @@ public function testValidateUniquenessWithIgnoreNull() $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), "No violations found on entity after it was saved to the database."); - $entity2 = new DoubleIdentEntity(2, 'Foo', null); + $entity2 = new DoubleNameEntity(2, 'Foo', null); $violationsList = $validator->validate($entity2); $this->assertEquals(1, $violationsList->count(), "Violation found on entity with conflicting entity existing in the database."); @@ -251,12 +251,12 @@ public function testValidateUniquenessWithIgnoreNull() public function testValidateUniquenessAfterConsideringMultipleQueryResults() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em); - $entity1 = new SingleIdentEntity(1, 'foo'); - $entity2 = new SingleIdentEntity(2, 'foo'); + $entity1 = new SingleIntIdEntity(1, 'foo'); + $entity2 = new SingleIntIdEntity(2, 'foo'); $em->persist($entity1); $em->persist($entity2); @@ -280,7 +280,7 @@ public function testValidateUniquenessUsingCustomRepositoryMethod() $em = $this->createEntityManagerMock($repository); $validator = $this->createValidator($entityManagerName, $em, null, array(), null, 'findByCustom'); - $entity1 = new SingleIdentEntity(1, 'foo'); + $entity1 = new SingleIntIdEntity(1, 'foo'); $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), 'Violation is using custom repository method.'); @@ -288,7 +288,7 @@ public function testValidateUniquenessUsingCustomRepositoryMethod() public function testValidateUniquenessWithUnrewoundArray() { - $entity = new SingleIdentEntity(1, 'foo'); + $entity = new SingleIntIdEntity(1, 'foo'); $entityManagerName = 'foo'; $repository = $this->createRepositoryMock(); @@ -318,11 +318,11 @@ public function testValidateUniquenessWithUnrewoundArray() public function testAssociatedEntity() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em, 'Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity', array('single')); - $entity1 = new SingleIdentEntity(1, 'foo'); + $entity1 = new SingleIntIdEntity(1, 'foo'); $associated = new AssociationEntity(); $associated->single = $entity1; @@ -349,11 +349,11 @@ public function testAssociatedEntity() public function testAssociatedCompositeEntity() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em, 'Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity', array('composite')); - $composite = new CompositeIdentEntity(1, 1, "test"); + $composite = new CompositeIntIdEntity(1, 1, "test"); $associated = new AssociationEntity(); $associated->composite = $composite; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy