public function testCreateBuilderWithDataClassOption() { $givenOptions = array('data_class' => 'Foo'); $resolvedOptions = array('data_class' => '\stdClass'); $optionsResolver = $this->getMockBuilder('Symfony\Component\OptionsResolver\OptionsResolver')->getMock(); $this->resolvedType = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormType') ->setConstructorArgs(array($this->type, array($this->extension1, $this->extension2), $this->parentResolvedType)) ->setMethods(array('getOptionsResolver')) ->getMock(); $this->resolvedType->expects($this->once()) ->method('getOptionsResolver') ->will($this->returnValue($optionsResolver)); $optionsResolver->expects($this->once()) ->method('resolve') ->with($givenOptions) ->will($this->returnValue($resolvedOptions)); $factory = $this->getMockFormFactory(); $builder = $this->resolvedType->createBuilder($factory, 'name', $givenOptions); $this->assertSame($this->resolvedType, $builder->getType()); $this->assertSame($resolvedOptions, $builder->getOptions()); $this->assertSame('\stdClass', $builder->getDataClass()); }
public function testCreateBuilder() { $parentType = $this->getMockFormType(); $type = $this->getMockFormType(); $extension1 = $this->getMockFormTypeExtension(); $extension2 = $this->getMockFormTypeExtension(); $parentResolvedType = new ResolvedFormType($parentType); $resolvedType = new ResolvedFormType($type, array($extension1, $extension2), $parentResolvedType); $test = $this; $i = 0; $assertIndex = function ($index) use(&$i, $test) { return function () use(&$i, $test, $index) { /* @var \PHPUnit_Framework_TestCase $test */ $test->assertEquals($index, $i, 'Executed at index ' . $index); ++$i; }; }; $assertIndexAndAddOption = function ($index, $option, $default) use($assertIndex) { $assertIndex = $assertIndex($index); return function (OptionsResolverInterface $resolver) use($assertIndex, $index, $option, $default) { $assertIndex(); $resolver->setDefaults(array($option => $default)); }; }; // First the default options are generated for the super type $parentType->expects($this->once())->method('setDefaultOptions')->will($this->returnCallback($assertIndexAndAddOption(0, 'a', 'a_default'))); // The form type itself $type->expects($this->once())->method('setDefaultOptions')->will($this->returnCallback($assertIndexAndAddOption(1, 'b', 'b_default'))); // And its extensions $extension1->expects($this->once())->method('setDefaultOptions')->will($this->returnCallback($assertIndexAndAddOption(2, 'c', 'c_default'))); $extension2->expects($this->once())->method('setDefaultOptions')->will($this->returnCallback($assertIndexAndAddOption(3, 'd', 'd_default'))); $givenOptions = array('a' => 'a_custom', 'c' => 'c_custom'); $resolvedOptions = array('a' => 'a_custom', 'b' => 'b_default', 'c' => 'c_custom', 'd' => 'd_default'); // Then the form is built for the super type $parentType->expects($this->once())->method('buildForm')->with($this->anything(), $resolvedOptions)->will($this->returnCallback($assertIndex(4))); // Then the type itself $type->expects($this->once())->method('buildForm')->with($this->anything(), $resolvedOptions)->will($this->returnCallback($assertIndex(5))); // Then its extensions $extension1->expects($this->once())->method('buildForm')->with($this->anything(), $resolvedOptions)->will($this->returnCallback($assertIndex(6))); $extension2->expects($this->once())->method('buildForm')->with($this->anything(), $resolvedOptions)->will($this->returnCallback($assertIndex(7))); $factory = $this->getMockFormFactory(); $builder = $resolvedType->createBuilder($factory, 'name', $givenOptions); $this->assertSame($resolvedType, $builder->getType()); }