Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 8660a5d

Browse files
committed
Fix #66
This patch removes the change introduced in 2.7.1's `Zend\Form\Factory::create()` method that aggregated options and passed them to the `FormElementManager::get()` call. This usage was incorrect, as it creates an order-of-operations problem: most instances call their own `setOptions()` method from the constructor if options are present, and many of the actions this executes assume things like a populated `FormElementManager`, using a default instance if none is found! As such, reverting those changes to pre-2.7.1 usage fixes the issues reported in #66. Since the changes were done under an erroneous assumption, a test related to the incorrect functionality was also removed.
1 parent 60de7d8 commit 8660a5d

File tree

5 files changed

+4
-39
lines changed

5 files changed

+4
-39
lines changed

src/Factory.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,7 @@ public function create($spec)
108108
$spec = $this->validateSpecification($spec, __METHOD__);
109109
$type = isset($spec['type']) ? $spec['type'] : Element::class;
110110

111-
$creationOptions = [];
112-
if (isset($spec['options']) && is_array($spec['options'])) {
113-
$creationOptions = $spec['options'];
114-
}
115-
116-
$element = $this->getFormElementManager()->get($type, $creationOptions);
111+
$element = $this->getFormElementManager()->get($type);
117112

118113
if ($element instanceof FormInterface) {
119114
return $this->configureForm($element, $spec);

test/Annotation/AnnotationBuilderFactoryTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Interop\Container\ContainerInterface;
1111
use PHPUnit_Framework_TestCase as TestCase;
12-
use Prophecy\Argument;
1312
use ReflectionProperty;
1413
use stdClass;
1514
use Zend\EventManager\EventManagerInterface;

test/FactoryTest.php

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
namespace ZendTest\Form;
1111

1212
use PHPUnit_Framework_TestCase as TestCase;
13-
use DateTime;
1413
use Zend\Filter;
1514
use Zend\Form;
1615
use Zend\Form\Factory as FormFactory;
1716
use Zend\Form\FormElementManager;
18-
use Zend\Form\FormInterface;
1917
use Zend\ServiceManager\ServiceManager;
2018
use Zend\Hydrator\HydratorPluginManager;
2119

@@ -758,32 +756,6 @@ public function testCanCreateFormWithNullElements()
758756
$this->assertTrue($form->has('bat'));
759757
}
760758

761-
public function testOptionsArePassedAsCreationOptionsToFactories()
762-
{
763-
$formManager = $this->factory->getFormElementManager();
764-
$formManager->setFactory('customCreatedForm', TestAsset\CustomCreatedFormFactory::class);
765-
766-
/* @var $form TestAsset\CustomCreatedForm */
767-
$form = $this->factory->create([
768-
'name' => 'some_name',
769-
'type' => 'customCreatedForm',
770-
'options' => [
771-
'created' => '2016-02-19',
772-
'some_other_option' => 1234
773-
]
774-
]);
775-
776-
$this->assertInstanceOf(FormInterface::class, $form);
777-
$this->assertInstanceOf(TestAsset\CustomCreatedForm::class, $form);
778-
$this->assertSame('some_name', $form->getName());
779-
$this->assertSame(1234, $form->getOption('some_other_option'));
780-
781-
/* @var $created DateTime */
782-
$created = $form->getCreated();
783-
$this->assertInstanceOf(DateTime::class, $created);
784-
$this->assertSame('2016-02-19', $created->format('Y-m-d'));
785-
}
786-
787759
public function testCanCreateWithConstructionLogicInOptions()
788760
{
789761
$formManager = $this->factory->getFormElementManager();
@@ -802,6 +774,7 @@ public function testCanCreateWithConstructionLogicInOptions()
802774
$this->assertInstanceOf(Form\Element\Collection::class, $collection);
803775

804776
$targetElement = $collection->getTargetElement();
777+
805778
$this->assertInstanceOf(TestAsset\FieldsetWithDependency::class, $targetElement);
806779
$this->assertInstanceOf(TestAsset\InputFilter::class, $targetElement->getDependency());
807780
}

test/TestAsset/FieldsetWithDependency.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class FieldsetWithDependency extends Fieldset
1919

2020
public function __construct($name = null, $options = [])
2121
{
22-
parent::__construct('fielset_with_dependency', $options);
22+
parent::__construct('fieldset_with_dependency', $options);
2323
}
2424

2525
public function init()

test/TestAsset/FieldsetWithDependencyFactory.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public function __invoke(ContainerInterface $container, $name, array $options =
2626
}
2727

2828
$form = new FieldsetWithDependency($name, $options);
29-
$dependency = new InputFilter();
30-
31-
$form->setDependency($dependency);
29+
$form->setDependency(new InputFilter());
3230

3331
return $form;
3432
}

0 commit comments

Comments
 (0)