diff --git a/ButtonBuilder.php b/ButtonBuilder.php index ed41b2147d..5e1106ad05 100644 --- a/ButtonBuilder.php +++ b/ButtonBuilder.php @@ -27,7 +27,7 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface /** * @var bool */ - private $disabled; + private $disabled = false; /** * @var ResolvedFormTypeInterface diff --git a/Extension/Core/Type/ButtonType.php b/Extension/Core/Type/ButtonType.php index b05dcc018d..ba2f8dfe0e 100644 --- a/Extension/Core/Type/ButtonType.php +++ b/Extension/Core/Type/ButtonType.php @@ -26,6 +26,7 @@ class ButtonType extends BaseType implements ButtonTypeInterface */ public function getParent() { + return null; } /** diff --git a/Extension/Core/Type/FormType.php b/Extension/Core/Type/FormType.php index 72b14035e5..a31c171f0e 100644 --- a/Extension/Core/Type/FormType.php +++ b/Extension/Core/Type/FormType.php @@ -190,6 +190,7 @@ public function configureOptions(OptionsResolver $resolver) */ public function getParent() { + return null; } /** diff --git a/FormError.php b/FormError.php index 98a1e29a83..078060c9f2 100644 --- a/FormError.php +++ b/FormError.php @@ -127,7 +127,7 @@ public function setOrigin(FormInterface $origin) /** * Returns the form that caused this error. * - * @return FormInterface The form that caused this error + * @return FormInterface|null The form that caused this error */ public function getOrigin() { diff --git a/FormInterface.php b/FormInterface.php index 5c55bcd795..f8d5c6ea5b 100644 --- a/FormInterface.php +++ b/FormInterface.php @@ -31,7 +31,7 @@ interface FormInterface extends \ArrayAccess, \Traversable, \Countable * @throws Exception\LogicException when trying to set a parent for a form with * an empty name */ - public function setParent(self $parent = null); + public function setParent(FormInterface $parent = null); /** * Returns the parent form. diff --git a/Test/Traits/ValidatorExtensionTrait.php b/Test/Traits/ValidatorExtensionTrait.php index 07dd9e0d55..46bd8b7e57 100644 --- a/Test/Traits/ValidatorExtensionTrait.php +++ b/Test/Traits/ValidatorExtensionTrait.php @@ -13,6 +13,7 @@ use Symfony\Component\Form\Extension\Validator\ValidatorExtension; use Symfony\Component\Form\Test\TypeTestCase; +use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Validator\ValidatorInterface; @@ -37,9 +38,9 @@ protected function getValidatorExtension() } $this->validator = $this->getMockBuilder(ValidatorInterface::class)->getMock(); - $metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->setMethods(['addPropertyConstraint'])->getMock(); + $metadata = $this->getMockBuilder(ClassMetadata::class)->setConstructorArgs([''])->setMethods(['addPropertyConstraint'])->getMock(); $this->validator->expects($this->any())->method('getMetadataFor')->will($this->returnValue($metadata)); - $this->validator->expects($this->any())->method('validate')->will($this->returnValue([])); + $this->validator->expects($this->any())->method('validate')->will($this->returnValue(new ConstraintViolationList())); return new ValidatorExtension($this->validator); } diff --git a/Tests/AbstractRequestHandlerTest.php b/Tests/AbstractRequestHandlerTest.php index f2ee71b342..776c753eca 100644 --- a/Tests/AbstractRequestHandlerTest.php +++ b/Tests/AbstractRequestHandlerTest.php @@ -346,7 +346,7 @@ public function getPostMaxSizeFixtures() [1024, '1K', false], [null, '1K', false], [1024, '', false], - [1024, 0, false], + [1024, '0', false], ]; } diff --git a/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php b/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php index b194d65eee..ba4ae7cd19 100644 --- a/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php +++ b/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php @@ -13,7 +13,9 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Symfony\Component\Form\ChoiceList\ArrayChoiceList; use Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator; +use Symfony\Component\Form\ChoiceList\View\ChoiceListView; /** * @author Bernhard Schussek @@ -38,7 +40,7 @@ protected function setUp() public function testCreateFromChoicesEmpty() { - $list = new \stdClass(); + $list = new ArrayChoiceList([]); $this->decoratedFactory->expects($this->once()) ->method('createListFromChoices') @@ -54,7 +56,7 @@ public function testCreateFromChoicesComparesTraversableChoicesAsArray() // The top-most traversable is converted to an array $choices1 = new \ArrayIterator(['A' => 'a']); $choices2 = ['A' => 'a']; - $list = new \stdClass(); + $list = new ArrayChoiceList([]); $this->decoratedFactory->expects($this->once()) ->method('createListFromChoices') @@ -69,8 +71,8 @@ public function testCreateFromChoicesGroupedChoices() { $choices1 = ['key' => ['A' => 'a']]; $choices2 = ['A' => 'a']; - $list1 = new \stdClass(); - $list2 = new \stdClass(); + $list1 = new ArrayChoiceList([]); + $list2 = new ArrayChoiceList([]); $this->decoratedFactory->expects($this->at(0)) ->method('createListFromChoices') @@ -92,7 +94,7 @@ public function testCreateFromChoicesSameChoices($choice1, $choice2) { $choices1 = [$choice1]; $choices2 = [$choice2]; - $list = new \stdClass(); + $list = new ArrayChoiceList([]); $this->decoratedFactory->expects($this->once()) ->method('createListFromChoices') @@ -110,8 +112,8 @@ public function testCreateFromChoicesDifferentChoices($choice1, $choice2) { $choices1 = [$choice1]; $choices2 = [$choice2]; - $list1 = new \stdClass(); - $list2 = new \stdClass(); + $list1 = new ArrayChoiceList([]); + $list2 = new ArrayChoiceList([]); $this->decoratedFactory->expects($this->at(0)) ->method('createListFromChoices') @@ -129,7 +131,7 @@ public function testCreateFromChoicesDifferentChoices($choice1, $choice2) public function testCreateFromChoicesSameValueClosure() { $choices = [1]; - $list = new \stdClass(); + $list = new ArrayChoiceList([]); $closure = function () {}; $this->decoratedFactory->expects($this->once()) @@ -144,8 +146,8 @@ public function testCreateFromChoicesSameValueClosure() public function testCreateFromChoicesDifferentValueClosure() { $choices = [1]; - $list1 = new \stdClass(); - $list2 = new \stdClass(); + $list1 = new ArrayChoiceList([]); + $list2 = new ArrayChoiceList([]); $closure1 = function () {}; $closure2 = function () {}; @@ -165,7 +167,7 @@ public function testCreateFromChoicesDifferentValueClosure() public function testCreateFromLoaderSameLoader() { $loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock(); - $list = new \stdClass(); + $list = new ArrayChoiceList([]); $this->decoratedFactory->expects($this->once()) ->method('createListFromLoader') @@ -180,8 +182,8 @@ public function testCreateFromLoaderDifferentLoader() { $loader1 = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock(); $loader2 = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock(); - $list1 = new \stdClass(); - $list2 = new \stdClass(); + $list1 = new ArrayChoiceList([]); + $list2 = new ArrayChoiceList([]); $this->decoratedFactory->expects($this->at(0)) ->method('createListFromLoader') @@ -199,7 +201,7 @@ public function testCreateFromLoaderDifferentLoader() public function testCreateFromLoaderSameValueClosure() { $loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock(); - $list = new \stdClass(); + $list = new ArrayChoiceList([]); $closure = function () {}; $this->decoratedFactory->expects($this->once()) @@ -214,8 +216,8 @@ public function testCreateFromLoaderSameValueClosure() public function testCreateFromLoaderDifferentValueClosure() { $loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock(); - $list1 = new \stdClass(); - $list2 = new \stdClass(); + $list1 = new ArrayChoiceList([]); + $list2 = new ArrayChoiceList([]); $closure1 = function () {}; $closure2 = function () {}; @@ -236,7 +238,7 @@ public function testCreateViewSamePreferredChoices() { $preferred = ['a']; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view = new \stdClass(); + $view = new ChoiceListView(); $this->decoratedFactory->expects($this->once()) ->method('createView') @@ -252,8 +254,8 @@ public function testCreateViewDifferentPreferredChoices() $preferred1 = ['a']; $preferred2 = ['b']; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view1 = new \stdClass(); - $view2 = new \stdClass(); + $view1 = new ChoiceListView(); + $view2 = new ChoiceListView(); $this->decoratedFactory->expects($this->at(0)) ->method('createView') @@ -272,7 +274,7 @@ public function testCreateViewSamePreferredChoicesClosure() { $preferred = function () {}; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view = new \stdClass(); + $view = new ChoiceListView(); $this->decoratedFactory->expects($this->once()) ->method('createView') @@ -288,8 +290,8 @@ public function testCreateViewDifferentPreferredChoicesClosure() $preferred1 = function () {}; $preferred2 = function () {}; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view1 = new \stdClass(); - $view2 = new \stdClass(); + $view1 = new ChoiceListView(); + $view2 = new ChoiceListView(); $this->decoratedFactory->expects($this->at(0)) ->method('createView') @@ -308,7 +310,7 @@ public function testCreateViewSameLabelClosure() { $labels = function () {}; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view = new \stdClass(); + $view = new ChoiceListView(); $this->decoratedFactory->expects($this->once()) ->method('createView') @@ -324,8 +326,8 @@ public function testCreateViewDifferentLabelClosure() $labels1 = function () {}; $labels2 = function () {}; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view1 = new \stdClass(); - $view2 = new \stdClass(); + $view1 = new ChoiceListView(); + $view2 = new ChoiceListView(); $this->decoratedFactory->expects($this->at(0)) ->method('createView') @@ -344,7 +346,7 @@ public function testCreateViewSameIndexClosure() { $index = function () {}; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view = new \stdClass(); + $view = new ChoiceListView(); $this->decoratedFactory->expects($this->once()) ->method('createView') @@ -360,8 +362,8 @@ public function testCreateViewDifferentIndexClosure() $index1 = function () {}; $index2 = function () {}; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view1 = new \stdClass(); - $view2 = new \stdClass(); + $view1 = new ChoiceListView(); + $view2 = new ChoiceListView(); $this->decoratedFactory->expects($this->at(0)) ->method('createView') @@ -380,7 +382,7 @@ public function testCreateViewSameGroupByClosure() { $groupBy = function () {}; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view = new \stdClass(); + $view = new ChoiceListView(); $this->decoratedFactory->expects($this->once()) ->method('createView') @@ -396,8 +398,8 @@ public function testCreateViewDifferentGroupByClosure() $groupBy1 = function () {}; $groupBy2 = function () {}; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view1 = new \stdClass(); - $view2 = new \stdClass(); + $view1 = new ChoiceListView(); + $view2 = new ChoiceListView(); $this->decoratedFactory->expects($this->at(0)) ->method('createView') @@ -416,7 +418,7 @@ public function testCreateViewSameAttributes() { $attr = ['class' => 'foobar']; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view = new \stdClass(); + $view = new ChoiceListView(); $this->decoratedFactory->expects($this->once()) ->method('createView') @@ -432,8 +434,8 @@ public function testCreateViewDifferentAttributes() $attr1 = ['class' => 'foobar1']; $attr2 = ['class' => 'foobar2']; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view1 = new \stdClass(); - $view2 = new \stdClass(); + $view1 = new ChoiceListView(); + $view2 = new ChoiceListView(); $this->decoratedFactory->expects($this->at(0)) ->method('createView') @@ -452,7 +454,7 @@ public function testCreateViewSameAttributesClosure() { $attr = function () {}; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view = new \stdClass(); + $view = new ChoiceListView(); $this->decoratedFactory->expects($this->once()) ->method('createView') @@ -468,8 +470,8 @@ public function testCreateViewDifferentAttributesClosure() $attr1 = function () {}; $attr2 = function () {}; $list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock(); - $view1 = new \stdClass(); - $view2 = new \stdClass(); + $view1 = new ChoiceListView(); + $view2 = new ChoiceListView(); $this->decoratedFactory->expects($this->at(0)) ->method('createView') diff --git a/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php b/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php index 725a09df08..043989a983 100644 --- a/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php +++ b/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php @@ -13,7 +13,9 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Symfony\Component\Form\ChoiceList\ArrayChoiceList; use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator; +use Symfony\Component\Form\ChoiceList\View\ChoiceListView; use Symfony\Component\PropertyAccess\PropertyPath; /** @@ -45,10 +47,10 @@ public function testCreateFromChoicesPropertyPath() ->method('createListFromChoices') ->with($choices, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($choices, $callback) { - return array_map($callback, $choices); + return new ArrayChoiceList(array_map($callback, $choices)); }); - $this->assertSame(['value'], $this->factory->createListFromChoices($choices, 'property')); + $this->assertSame(['value' => 'value'], $this->factory->createListFromChoices($choices, 'property')->getChoices()); } public function testCreateFromChoicesPropertyPathInstance() @@ -59,10 +61,10 @@ public function testCreateFromChoicesPropertyPathInstance() ->method('createListFromChoices') ->with($choices, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($choices, $callback) { - return array_map($callback, $choices); + return new ArrayChoiceList(array_map($callback, $choices)); }); - $this->assertSame(['value'], $this->factory->createListFromChoices($choices, new PropertyPath('property'))); + $this->assertSame(['value' => 'value'], $this->factory->createListFromChoices($choices, new PropertyPath('property'))->getChoices()); } /** @@ -88,10 +90,10 @@ public function testCreateFromLoaderPropertyPath() ->method('createListFromLoader') ->with($loader, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($loader, $callback) { - return $callback((object) ['property' => 'value']); + return new ArrayChoiceList((array) $callback((object) ['property' => 'value'])); }); - $this->assertSame('value', $this->factory->createListFromLoader($loader, 'property')); + $this->assertSame(['value' => 'value'], $this->factory->createListFromLoader($loader, 'property')->getChoices()); } /** @@ -118,10 +120,10 @@ public function testCreateFromChoicesAssumeNullIfValuePropertyPathUnreadable() ->method('createListFromChoices') ->with($choices, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($choices, $callback) { - return array_map($callback, $choices); + return new ArrayChoiceList(array_map($callback, $choices)); }); - $this->assertSame([null], $this->factory->createListFromChoices($choices, 'property')); + $this->assertSame([null], $this->factory->createListFromChoices($choices, 'property')->getChoices()); } // https://github.com/symfony/symfony/issues/5494 @@ -133,10 +135,10 @@ public function testCreateFromChoiceLoaderAssumeNullIfValuePropertyPathUnreadabl ->method('createListFromLoader') ->with($loader, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($loader, $callback) { - return $callback(null); + return new ArrayChoiceList((array) $callback(null)); }); - $this->assertNull($this->factory->createListFromLoader($loader, 'property')); + $this->assertSame([], $this->factory->createListFromLoader($loader, 'property')->getChoices()); } public function testCreateFromLoaderPropertyPathInstance() @@ -147,10 +149,10 @@ public function testCreateFromLoaderPropertyPathInstance() ->method('createListFromLoader') ->with($loader, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($loader, $callback) { - return $callback((object) ['property' => 'value']); + return new ArrayChoiceList((array) $callback((object) ['property' => 'value'])); }); - $this->assertSame('value', $this->factory->createListFromLoader($loader, new PropertyPath('property'))); + $this->assertSame(['value' => 'value'], $this->factory->createListFromLoader($loader, new PropertyPath('property'))->getChoices()); } public function testCreateViewPreferredChoicesAsPropertyPath() @@ -161,13 +163,10 @@ public function testCreateViewPreferredChoicesAsPropertyPath() ->method('createView') ->with($list, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred) { - return $preferred((object) ['property' => true]); + return new ChoiceListView((array) $preferred((object) ['property' => true])); }); - $this->assertTrue($this->factory->createView( - $list, - 'property' - )); + $this->assertSame([true], $this->factory->createView($list, 'property')->choices); } /** @@ -196,13 +195,10 @@ public function testCreateViewPreferredChoicesAsPropertyPathInstance() ->method('createView') ->with($list, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred) { - return $preferred((object) ['property' => true]); + return new ChoiceListView((array) $preferred((object) ['property' => true])); }); - $this->assertTrue($this->factory->createView( - $list, - new PropertyPath('property') - )); + $this->assertSame([true], $this->factory->createView($list, 'property')->choices); } // https://github.com/symfony/symfony/issues/5494 @@ -214,13 +210,10 @@ public function testCreateViewAssumeNullIfPreferredChoicesPropertyPathUnreadable ->method('createView') ->with($list, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred) { - return $preferred((object) ['category' => null]); + return new ChoiceListView((array) $preferred((object) ['category' => null])); }); - $this->assertFalse($this->factory->createView( - $list, - 'category.preferred' - )); + $this->assertSame([false], $this->factory->createView($list, 'category.preferred')->choices); } public function testCreateViewLabelsAsPropertyPath() @@ -231,14 +224,10 @@ public function testCreateViewLabelsAsPropertyPath() ->method('createView') ->with($list, null, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred, $label) { - return $label((object) ['property' => 'label']); + return new ChoiceListView((array) $label((object) ['property' => 'label'])); }); - $this->assertSame('label', $this->factory->createView( - $list, - null, // preferred choices - 'property' - )); + $this->assertSame(['label'], $this->factory->createView($list, null, 'property')->choices); } /** @@ -268,14 +257,10 @@ public function testCreateViewLabelsAsPropertyPathInstance() ->method('createView') ->with($list, null, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred, $label) { - return $label((object) ['property' => 'label']); + return new ChoiceListView((array) $label((object) ['property' => 'label'])); }); - $this->assertSame('label', $this->factory->createView( - $list, - null, // preferred choices - new PropertyPath('property') - )); + $this->assertSame(['label'], $this->factory->createView($list, null, new PropertyPath('property'))->choices); } public function testCreateViewIndicesAsPropertyPath() @@ -286,15 +271,10 @@ public function testCreateViewIndicesAsPropertyPath() ->method('createView') ->with($list, null, null, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred, $label, $index) { - return $index((object) ['property' => 'index']); + return new ChoiceListView((array) $index((object) ['property' => 'index'])); }); - $this->assertSame('index', $this->factory->createView( - $list, - null, // preferred choices - null, // label - 'property' - )); + $this->assertSame(['index'], $this->factory->createView($list, null, null, 'property')->choices); } /** @@ -325,15 +305,10 @@ public function testCreateViewIndicesAsPropertyPathInstance() ->method('createView') ->with($list, null, null, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred, $label, $index) { - return $index((object) ['property' => 'index']); + return new ChoiceListView((array) $index((object) ['property' => 'index'])); }); - $this->assertSame('index', $this->factory->createView( - $list, - null, // preferred choices - null, // label - new PropertyPath('property') - )); + $this->assertSame(['index'], $this->factory->createView($list, null, null, new PropertyPath('property'))->choices); } public function testCreateViewGroupsAsPropertyPath() @@ -344,16 +319,10 @@ public function testCreateViewGroupsAsPropertyPath() ->method('createView') ->with($list, null, null, null, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred, $label, $index, $groupBy) { - return $groupBy((object) ['property' => 'group']); + return new ChoiceListView((array) $groupBy((object) ['property' => 'group'])); }); - $this->assertSame('group', $this->factory->createView( - $list, - null, // preferred choices - null, // label - null, // index - 'property' - )); + $this->assertSame(['group'], $this->factory->createView($list, null, null, null, 'property')->choices); } /** @@ -385,16 +354,10 @@ public function testCreateViewGroupsAsPropertyPathInstance() ->method('createView') ->with($list, null, null, null, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred, $label, $index, $groupBy) { - return $groupBy((object) ['property' => 'group']); + return new ChoiceListView((array) $groupBy((object) ['property' => 'group'])); }); - $this->assertSame('group', $this->factory->createView( - $list, - null, // preferred choices - null, // label - null, // index - new PropertyPath('property') - )); + $this->assertSame(['group'], $this->factory->createView($list, null, null, null, new PropertyPath('property'))->choices); } // https://github.com/symfony/symfony/issues/5494 @@ -406,16 +369,10 @@ public function testCreateViewAssumeNullIfGroupsPropertyPathUnreadable() ->method('createView') ->with($list, null, null, null, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred, $label, $index, $groupBy) { - return $groupBy((object) ['group' => null]); + return new ChoiceListView((array) $groupBy((object) ['group' => null])); }); - $this->assertNull($this->factory->createView( - $list, - null, // preferred choices - null, // label - null, // index - 'group.name' - )); + $this->assertSame([], $this->factory->createView($list, null, null, null, 'group.name')->choices); } public function testCreateViewAttrAsPropertyPath() @@ -426,17 +383,10 @@ public function testCreateViewAttrAsPropertyPath() ->method('createView') ->with($list, null, null, null, null, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred, $label, $index, $groupBy, $attr) { - return $attr((object) ['property' => 'attr']); + return new ChoiceListView((array) $attr((object) ['property' => 'attr'])); }); - $this->assertSame('attr', $this->factory->createView( - $list, - null, // preferred choices - null, // label - null, // index - null, // groups - 'property' - )); + $this->assertSame(['attr'], $this->factory->createView($list, null, null, null, null, 'property')->choices); } /** @@ -469,16 +419,9 @@ public function testCreateViewAttrAsPropertyPathInstance() ->method('createView') ->with($list, null, null, null, null, $this->isInstanceOf('\Closure')) ->willReturnCallback(function ($list, $preferred, $label, $index, $groupBy, $attr) { - return $attr((object) ['property' => 'attr']); + return new ChoiceListView((array) $attr((object) ['property' => 'attr'])); }); - $this->assertSame('attr', $this->factory->createView( - $list, - null, // preferred choices - null, // label - null, // index - null, // groups - new PropertyPath('property') - )); + $this->assertSame(['attr'], $this->factory->createView($list, null, null, null, null, new PropertyPath('property'))->choices); } } diff --git a/Tests/ChoiceList/LazyChoiceListTest.php b/Tests/ChoiceList/LazyChoiceListTest.php index 5888038370..aa6b6b5d49 100644 --- a/Tests/ChoiceList/LazyChoiceListTest.php +++ b/Tests/ChoiceList/LazyChoiceListTest.php @@ -56,10 +56,10 @@ public function testGetChoiceLoadersLoadsLoadedListOnFirstCall() // The same list is returned by the loader $this->loadedList->expects($this->exactly(2)) ->method('getChoices') - ->willReturn('RESULT'); + ->willReturn(['RESULT']); - $this->assertSame('RESULT', $this->list->getChoices()); - $this->assertSame('RESULT', $this->list->getChoices()); + $this->assertSame(['RESULT'], $this->list->getChoices()); + $this->assertSame(['RESULT'], $this->list->getChoices()); } /** @@ -96,10 +96,10 @@ public function testGetValuesLoadsLoadedListOnFirstCall() // The same list is returned by the loader $this->loadedList->expects($this->exactly(2)) ->method('getValues') - ->willReturn('RESULT'); + ->willReturn(['RESULT']); - $this->assertSame('RESULT', $this->list->getValues()); - $this->assertSame('RESULT', $this->list->getValues()); + $this->assertSame(['RESULT'], $this->list->getValues()); + $this->assertSame(['RESULT'], $this->list->getValues()); } public function testGetStructuredValuesLoadsLoadedListOnFirstCall() @@ -112,10 +112,10 @@ public function testGetStructuredValuesLoadsLoadedListOnFirstCall() // The same list is returned by the loader $this->loadedList->expects($this->exactly(2)) ->method('getStructuredValues') - ->willReturn('RESULT'); + ->willReturn(['RESULT']); - $this->assertSame('RESULT', $this->list->getStructuredValues()); - $this->assertSame('RESULT', $this->list->getStructuredValues()); + $this->assertSame(['RESULT'], $this->list->getStructuredValues()); + $this->assertSame(['RESULT'], $this->list->getStructuredValues()); } public function testGetOriginalKeysLoadsLoadedListOnFirstCall() @@ -128,10 +128,10 @@ public function testGetOriginalKeysLoadsLoadedListOnFirstCall() // The same list is returned by the loader $this->loadedList->expects($this->exactly(2)) ->method('getOriginalKeys') - ->willReturn('RESULT'); + ->willReturn(['RESULT']); - $this->assertSame('RESULT', $this->list->getOriginalKeys()); - $this->assertSame('RESULT', $this->list->getOriginalKeys()); + $this->assertSame(['RESULT'], $this->list->getOriginalKeys()); + $this->assertSame(['RESULT'], $this->list->getOriginalKeys()); } public function testGetChoicesForValuesForwardsCallIfListNotLoaded() @@ -139,10 +139,10 @@ public function testGetChoicesForValuesForwardsCallIfListNotLoaded() $this->loader->expects($this->exactly(2)) ->method('loadChoicesForValues') ->with(['a', 'b']) - ->willReturn('RESULT'); + ->willReturn(['RESULT']); - $this->assertSame('RESULT', $this->list->getChoicesForValues(['a', 'b'])); - $this->assertSame('RESULT', $this->list->getChoicesForValues(['a', 'b'])); + $this->assertSame(['RESULT'], $this->list->getChoicesForValues(['a', 'b'])); + $this->assertSame(['RESULT'], $this->list->getChoicesForValues(['a', 'b'])); } public function testGetChoicesForValuesUsesLoadedList() @@ -160,13 +160,13 @@ public function testGetChoicesForValuesUsesLoadedList() $this->loadedList->expects($this->exactly(2)) ->method('getChoicesForValues') ->with(['a', 'b']) - ->willReturn('RESULT'); + ->willReturn(['RESULT']); // load choice list $this->list->getChoices(); - $this->assertSame('RESULT', $this->list->getChoicesForValues(['a', 'b'])); - $this->assertSame('RESULT', $this->list->getChoicesForValues(['a', 'b'])); + $this->assertSame(['RESULT'], $this->list->getChoicesForValues(['a', 'b'])); + $this->assertSame(['RESULT'], $this->list->getChoicesForValues(['a', 'b'])); } /** @@ -198,12 +198,12 @@ public function testGetValuesForChoicesUsesLoadedList() $this->loadedList->expects($this->exactly(2)) ->method('getValuesForChoices') ->with(['a', 'b']) - ->willReturn('RESULT'); + ->willReturn(['RESULT']); // load choice list $this->list->getChoices(); - $this->assertSame('RESULT', $this->list->getValuesForChoices(['a', 'b'])); - $this->assertSame('RESULT', $this->list->getValuesForChoices(['a', 'b'])); + $this->assertSame(['RESULT'], $this->list->getValuesForChoices(['a', 'b'])); + $this->assertSame(['RESULT'], $this->list->getValuesForChoices(['a', 'b'])); } } diff --git a/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php b/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php index 232fd52b46..ea7fc5ff50 100644 --- a/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php +++ b/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php @@ -142,7 +142,7 @@ public function testGenerateCsrfTokenUsesFormNameAsIntentionByDefault() $this->tokenManager->expects($this->once()) ->method('getToken') ->with('FORM_NAME') - ->willReturn('token'); + ->willReturn(new CsrfToken('TOKEN_ID', 'token')); $view = $this->factory ->createNamed('FORM_NAME', 'Symfony\Component\Form\Extension\Core\Type\FormType', null, [ @@ -160,7 +160,7 @@ public function testGenerateCsrfTokenUsesTypeClassAsIntentionIfEmptyFormName() $this->tokenManager->expects($this->once()) ->method('getToken') ->with('Symfony\Component\Form\Extension\Core\Type\FormType') - ->willReturn('token'); + ->willReturn(new CsrfToken('TOKEN_ID', 'token')); $view = $this->factory ->createNamed('', 'Symfony\Component\Form\Extension\Core\Type\FormType', null, [ diff --git a/Tests/Extension/DataCollector/FormDataExtractorTest.php b/Tests/Extension/DataCollector/FormDataExtractorTest.php index 6d897c711f..4b61223773 100644 --- a/Tests/Extension/DataCollector/FormDataExtractorTest.php +++ b/Tests/Extension/DataCollector/FormDataExtractorTest.php @@ -16,6 +16,7 @@ use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\Form\Extension\DataCollector\FormDataExtractor; +use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormView; @@ -57,7 +58,7 @@ public function testExtractConfiguration() $type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock(); $type->expects($this->any()) ->method('getInnerType') - ->willReturn(new \stdClass()); + ->willReturn(new HiddenType()); $form = $this->createBuilder('name') ->setType($type) @@ -66,7 +67,7 @@ public function testExtractConfiguration() $this->assertSame([ 'id' => 'name', 'name' => 'name', - 'type_class' => 'stdClass', + 'type_class' => HiddenType::class, 'synchronized' => true, 'passed_options' => [], 'resolved_options' => [], @@ -78,7 +79,7 @@ public function testExtractConfigurationSortsPassedOptions() $type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock(); $type->expects($this->any()) ->method('getInnerType') - ->willReturn(new \stdClass()); + ->willReturn(new HiddenType()); $options = [ 'b' => 'foo', @@ -96,7 +97,7 @@ public function testExtractConfigurationSortsPassedOptions() $this->assertSame([ 'id' => 'name', 'name' => 'name', - 'type_class' => 'stdClass', + 'type_class' => HiddenType::class, 'synchronized' => true, 'passed_options' => [ 'a' => 'bar', @@ -112,7 +113,7 @@ public function testExtractConfigurationSortsResolvedOptions() $type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock(); $type->expects($this->any()) ->method('getInnerType') - ->willReturn(new \stdClass()); + ->willReturn(new HiddenType()); $options = [ 'b' => 'foo', @@ -127,7 +128,7 @@ public function testExtractConfigurationSortsResolvedOptions() $this->assertSame([ 'id' => 'name', 'name' => 'name', - 'type_class' => 'stdClass', + 'type_class' => HiddenType::class, 'synchronized' => true, 'passed_options' => [], 'resolved_options' => [ @@ -143,7 +144,7 @@ public function testExtractConfigurationBuildsIdRecursively() $type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock(); $type->expects($this->any()) ->method('getInnerType') - ->willReturn(new \stdClass()); + ->willReturn(new HiddenType()); $grandParent = $this->createBuilder('grandParent') ->setCompound(true) @@ -163,7 +164,7 @@ public function testExtractConfigurationBuildsIdRecursively() $this->assertSame([ 'id' => 'grandParent_parent_name', 'name' => 'name', - 'type_class' => 'stdClass', + 'type_class' => HiddenType::class, 'synchronized' => true, 'passed_options' => [], 'resolved_options' => [], diff --git a/Tests/FormFactoryTest.php b/Tests/FormFactoryTest.php index ddd5b4bb72..79421be635 100644 --- a/Tests/FormFactoryTest.php +++ b/Tests/FormFactoryTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\Form\FormFactory; +use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormTypeGuesserChain; use Symfony\Component\Form\Guess\Guess; use Symfony\Component\Form\Guess\TypeGuess; @@ -193,11 +194,13 @@ public function testCreateUsesBlockPrefixIfTypeGivenAsString() ->method('buildForm') ->with($this->builder, $resolvedOptions); + $form = $this->createMock(FormInterface::class); + $this->builder->expects($this->once()) ->method('getForm') - ->willReturn('FORM'); + ->willReturn($form); - $this->assertSame('FORM', $this->factory->create('TYPE', null, $options)); + $this->assertSame($form, $this->factory->create('TYPE', null, $options)); } public function testCreateNamed() @@ -224,11 +227,13 @@ public function testCreateNamed() ->method('buildForm') ->with($this->builder, $resolvedOptions); + $form = $this->createMock(FormInterface::class); + $this->builder->expects($this->once()) ->method('getForm') - ->willReturn('FORM'); + ->willReturn($form); - $this->assertSame('FORM', $this->factory->createNamed('name', 'type', null, $options)); + $this->assertSame($form, $this->factory->createNamed('name', 'type', null, $options)); } public function testCreateBuilderForPropertyWithoutTypeGuesser() @@ -242,11 +247,11 @@ public function testCreateBuilderForPropertyWithoutTypeGuesser() $factory->expects($this->once()) ->method('createNamedBuilder') ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, []) - ->willReturn('builderInstance'); + ->willReturn($this->builder); $this->builder = $factory->createBuilderForProperty('Application\Author', 'firstName'); - $this->assertEquals('builderInstance', $this->builder); + $this->assertSame($this->builder, $this->builder); } public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence() @@ -274,11 +279,11 @@ public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence() $factory->expects($this->once()) ->method('createNamedBuilder') ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\PasswordType', null, ['attr' => ['maxlength' => 7]]) - ->willReturn('builderInstance'); + ->willReturn($this->builder); $this->builder = $factory->createBuilderForProperty('Application\Author', 'firstName'); - $this->assertEquals('builderInstance', $this->builder); + $this->assertSame($this->builder, $this->builder); } public function testCreateBuilderCreatesTextFormIfNoGuess() @@ -293,11 +298,11 @@ public function testCreateBuilderCreatesTextFormIfNoGuess() $factory->expects($this->once()) ->method('createNamedBuilder') ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType') - ->willReturn('builderInstance'); + ->willReturn($this->builder); $this->builder = $factory->createBuilderForProperty('Application\Author', 'firstName'); - $this->assertEquals('builderInstance', $this->builder); + $this->assertSame($this->builder, $this->builder); } public function testOptionsCanBeOverridden() @@ -316,7 +321,7 @@ public function testOptionsCanBeOverridden() $factory->expects($this->once()) ->method('createNamedBuilder') ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, ['attr' => ['class' => 'foo', 'maxlength' => 11]]) - ->willReturn('builderInstance'); + ->willReturn($this->builder); $this->builder = $factory->createBuilderForProperty( 'Application\Author', @@ -325,7 +330,7 @@ public function testOptionsCanBeOverridden() ['attr' => ['maxlength' => 11]] ); - $this->assertEquals('builderInstance', $this->builder); + $this->assertSame($this->builder, $this->builder); } public function testCreateBuilderUsesMaxLengthIfFound() @@ -351,14 +356,14 @@ public function testCreateBuilderUsesMaxLengthIfFound() $factory->expects($this->once()) ->method('createNamedBuilder') ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, ['attr' => ['maxlength' => 20]]) - ->willReturn('builderInstance'); + ->willReturn($this->builder); $this->builder = $factory->createBuilderForProperty( 'Application\Author', 'firstName' ); - $this->assertEquals('builderInstance', $this->builder); + $this->assertSame($this->builder, $this->builder); } public function testCreateBuilderUsesMaxLengthAndPattern() @@ -384,7 +389,7 @@ public function testCreateBuilderUsesMaxLengthAndPattern() $factory->expects($this->once()) ->method('createNamedBuilder') ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, ['attr' => ['maxlength' => 20, 'pattern' => '.{5,}', 'class' => 'tinymce']]) - ->willReturn('builderInstance'); + ->willReturn($this->builder); $this->builder = $factory->createBuilderForProperty( 'Application\Author', @@ -393,7 +398,7 @@ public function testCreateBuilderUsesMaxLengthAndPattern() ['attr' => ['class' => 'tinymce']] ); - $this->assertEquals('builderInstance', $this->builder); + $this->assertSame($this->builder, $this->builder); } public function testCreateBuilderUsesRequiredSettingWithHighestConfidence() @@ -419,14 +424,14 @@ public function testCreateBuilderUsesRequiredSettingWithHighestConfidence() $factory->expects($this->once()) ->method('createNamedBuilder') ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, ['required' => false]) - ->willReturn('builderInstance'); + ->willReturn($this->builder); $this->builder = $factory->createBuilderForProperty( 'Application\Author', 'firstName' ); - $this->assertEquals('builderInstance', $this->builder); + $this->assertSame($this->builder, $this->builder); } public function testCreateBuilderUsesPatternIfFound() @@ -452,14 +457,14 @@ public function testCreateBuilderUsesPatternIfFound() $factory->expects($this->once()) ->method('createNamedBuilder') ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, ['attr' => ['pattern' => '[a-zA-Z]']]) - ->willReturn('builderInstance'); + ->willReturn($this->builder); $this->builder = $factory->createBuilderForProperty( 'Application\Author', 'firstName' ); - $this->assertEquals('builderInstance', $this->builder); + $this->assertSame($this->builder, $this->builder); } private function getMockFactory(array $methods = [])