From 8706f18ea8a0d39297389764f24ae5c5d3636a46 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sat, 24 Aug 2019 18:55:22 +0200 Subject: [PATCH] [Form] fix return type declarations --- src/Symfony/Component/Form/ButtonBuilder.php | 2 +- .../Form/Extension/Core/Type/ButtonType.php | 1 + .../Form/Extension/Core/Type/FormType.php | 1 + src/Symfony/Component/Form/FormError.php | 2 +- src/Symfony/Component/Form/FormInterface.php | 2 +- .../Test/Traits/ValidatorExtensionTrait.php | 5 +- .../Form/Tests/AbstractRequestHandlerTest.php | 2 +- .../Factory/CachingFactoryDecoratorTest.php | 76 +++++----- .../Factory/PropertyAccessDecoratorTest.php | 133 +++++------------- .../Tests/ChoiceList/LazyChoiceListTest.php | 42 +++--- .../Csrf/Type/FormTypeCsrfExtensionTest.php | 4 +- .../DataCollector/FormDataExtractorTest.php | 17 +-- .../Component/Form/Tests/FormFactoryTest.php | 45 +++--- .../PropertyAccess/PropertyPathBuilder.php | 2 +- .../PropertyAccess/PropertyPathInterface.php | 2 +- 15 files changed, 145 insertions(+), 191 deletions(-) diff --git a/src/Symfony/Component/Form/ButtonBuilder.php b/src/Symfony/Component/Form/ButtonBuilder.php index ed41b2147d..5e1106ad05 100644 --- a/src/Symfony/Component/Form/ButtonBuilder.php +++ b/src/Symfony/Component/Form/ButtonBuilder.php @@ -27,7 +27,7 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface /** * @var bool */ - private $disabled; + private $disabled = false; /** * @var ResolvedFormTypeInterface diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ButtonType.php b/src/Symfony/Component/Form/Extension/Core/Type/ButtonType.php index b05dcc018d..ba2f8dfe0e 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ButtonType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ButtonType.php @@ -26,6 +26,7 @@ class ButtonType extends BaseType implements ButtonTypeInterface */ public function getParent() { + return null; } /** diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php index 72b14035e5..a31c171f0e 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php @@ -190,6 +190,7 @@ class FormType extends BaseType */ public function getParent() { + return null; } /** diff --git a/src/Symfony/Component/Form/FormError.php b/src/Symfony/Component/Form/FormError.php index 98a1e29a83..078060c9f2 100644 --- a/src/Symfony/Component/Form/FormError.php +++ b/src/Symfony/Component/Form/FormError.php @@ -127,7 +127,7 @@ class FormError implements \Serializable /** * 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/src/Symfony/Component/Form/FormInterface.php b/src/Symfony/Component/Form/FormInterface.php index 5c55bcd795..f8d5c6ea5b 100644 --- a/src/Symfony/Component/Form/FormInterface.php +++ b/src/Symfony/Component/Form/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/src/Symfony/Component/Form/Test/Traits/ValidatorExtensionTrait.php b/src/Symfony/Component/Form/Test/Traits/ValidatorExtensionTrait.php index 07dd9e0d55..46bd8b7e57 100644 --- a/src/Symfony/Component/Form/Test/Traits/ValidatorExtensionTrait.php +++ b/src/Symfony/Component/Form/Test/Traits/ValidatorExtensionTrait.php @@ -13,6 +13,7 @@ namespace Symfony\Component\Form\Test\Traits; 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 @@ trait ValidatorExtensionTrait } $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/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php index f2ee71b342..776c753eca 100644 --- a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php @@ -346,7 +346,7 @@ abstract class AbstractRequestHandlerTest extends TestCase [1024, '1K', false], [null, '1K', false], [1024, '', false], - [1024, 0, false], + [1024, '0', false], ]; } diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php index b194d65eee..ba4ae7cd19 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php @@ -13,7 +13,9 @@ namespace Symfony\Component\Form\Tests\ChoiceList\Factory; 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 @@ class CachingFactoryDecoratorTest extends TestCase public function testCreateFromChoicesEmpty() { - $list = new \stdClass(); + $list = new ArrayChoiceList([]); $this->decoratedFactory->expects($this->once()) ->method('createListFromChoices') @@ -54,7 +56,7 @@ class CachingFactoryDecoratorTest extends TestCase // 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 @@ class CachingFactoryDecoratorTest extends TestCase { $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 @@ class CachingFactoryDecoratorTest extends TestCase { $choices1 = [$choice1]; $choices2 = [$choice2]; - $list = new \stdClass(); + $list = new ArrayChoiceList([]); $this->decoratedFactory->expects($this->once()) ->method('createListFromChoices') @@ -110,8 +112,8 @@ class CachingFactoryDecoratorTest extends TestCase { $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 @@ class CachingFactoryDecoratorTest extends TestCase public function testCreateFromChoicesSameValueClosure() { $choices = [1]; - $list = new \stdClass(); + $list = new ArrayChoiceList([]); $closure = function () {}; $this->decoratedFactory->expects($this->once()) @@ -144,8 +146,8 @@ class CachingFactoryDecoratorTest extends TestCase 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 @@ class CachingFactoryDecoratorTest extends TestCase 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 @@ class CachingFactoryDecoratorTest extends TestCase { $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 @@ class CachingFactoryDecoratorTest extends TestCase 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 @@ class CachingFactoryDecoratorTest extends TestCase 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 @@ class CachingFactoryDecoratorTest extends TestCase { $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 @@ class CachingFactoryDecoratorTest extends TestCase $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 @@ class CachingFactoryDecoratorTest extends TestCase { $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 @@ class CachingFactoryDecoratorTest extends TestCase $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 @@ class CachingFactoryDecoratorTest extends TestCase { $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 @@ class CachingFactoryDecoratorTest extends TestCase $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 @@ class CachingFactoryDecoratorTest extends TestCase { $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 @@ class CachingFactoryDecoratorTest extends TestCase $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 @@ class CachingFactoryDecoratorTest extends TestCase { $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 @@ class CachingFactoryDecoratorTest extends TestCase $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 @@ class CachingFactoryDecoratorTest extends TestCase { $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 @@ class CachingFactoryDecoratorTest extends TestCase $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 @@ class CachingFactoryDecoratorTest extends TestCase { $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 @@ class CachingFactoryDecoratorTest extends TestCase $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/src/Symfony/Component/Form/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php index 725a09df08..043989a983 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php @@ -13,7 +13,9 @@ namespace Symfony\Component\Form\Tests\ChoiceList\Factory; 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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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 @@ class PropertyAccessDecoratorTest extends TestCase ->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/src/Symfony/Component/Form/Tests/ChoiceList/LazyChoiceListTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/LazyChoiceListTest.php index 5888038370..aa6b6b5d49 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/LazyChoiceListTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/LazyChoiceListTest.php @@ -56,10 +56,10 @@ class LazyChoiceListTest extends TestCase // 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 @@ class LazyChoiceListTest extends TestCase // 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 @@ class LazyChoiceListTest extends TestCase // 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 @@ class LazyChoiceListTest extends TestCase // 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 @@ class LazyChoiceListTest extends TestCase $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 @@ class LazyChoiceListTest extends TestCase $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 @@ class LazyChoiceListTest extends TestCase $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/src/Symfony/Component/Form/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php index 232fd52b46..ea7fc5ff50 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php @@ -142,7 +142,7 @@ class FormTypeCsrfExtensionTest extends TypeTestCase $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 @@ class FormTypeCsrfExtensionTest extends TypeTestCase $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/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataExtractorTest.php b/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataExtractorTest.php index 6d897c711f..4b61223773 100644 --- a/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataExtractorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataExtractorTest.php @@ -16,6 +16,7 @@ use PHPUnit\Framework\TestCase; 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 @@ class FormDataExtractorTest extends TestCase $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 @@ class FormDataExtractorTest extends TestCase $this->assertSame([ 'id' => 'name', 'name' => 'name', - 'type_class' => 'stdClass', + 'type_class' => HiddenType::class, 'synchronized' => true, 'passed_options' => [], 'resolved_options' => [], @@ -78,7 +79,7 @@ class FormDataExtractorTest extends TestCase $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 @@ class FormDataExtractorTest extends TestCase $this->assertSame([ 'id' => 'name', 'name' => 'name', - 'type_class' => 'stdClass', + 'type_class' => HiddenType::class, 'synchronized' => true, 'passed_options' => [ 'a' => 'bar', @@ -112,7 +113,7 @@ class FormDataExtractorTest extends TestCase $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 @@ class FormDataExtractorTest extends TestCase $this->assertSame([ 'id' => 'name', 'name' => 'name', - 'type_class' => 'stdClass', + 'type_class' => HiddenType::class, 'synchronized' => true, 'passed_options' => [], 'resolved_options' => [ @@ -143,7 +144,7 @@ class FormDataExtractorTest extends TestCase $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 @@ class FormDataExtractorTest extends TestCase $this->assertSame([ 'id' => 'grandParent_parent_name', 'name' => 'name', - 'type_class' => 'stdClass', + 'type_class' => HiddenType::class, 'synchronized' => true, 'passed_options' => [], 'resolved_options' => [], diff --git a/src/Symfony/Component/Form/Tests/FormFactoryTest.php b/src/Symfony/Component/Form/Tests/FormFactoryTest.php index ddd5b4bb72..79421be635 100644 --- a/src/Symfony/Component/Form/Tests/FormFactoryTest.php +++ b/src/Symfony/Component/Form/Tests/FormFactoryTest.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Form\Tests; 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 @@ class FormFactoryTest extends TestCase ->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 @@ class FormFactoryTest extends TestCase ->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 @@ class FormFactoryTest extends TestCase $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 @@ class FormFactoryTest extends TestCase $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 @@ class FormFactoryTest extends TestCase $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 @@ class FormFactoryTest extends TestCase $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 @@ class FormFactoryTest extends TestCase ['attr' => ['maxlength' => 11]] ); - $this->assertEquals('builderInstance', $this->builder); + $this->assertSame($this->builder, $this->builder); } public function testCreateBuilderUsesMaxLengthIfFound() @@ -351,14 +356,14 @@ class FormFactoryTest extends TestCase $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 @@ class FormFactoryTest extends TestCase $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 @@ class FormFactoryTest extends TestCase ['attr' => ['class' => 'tinymce']] ); - $this->assertEquals('builderInstance', $this->builder); + $this->assertSame($this->builder, $this->builder); } public function testCreateBuilderUsesRequiredSettingWithHighestConfidence() @@ -419,14 +424,14 @@ class FormFactoryTest extends TestCase $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 @@ class FormFactoryTest extends TestCase $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 = []) diff --git a/src/Symfony/Component/PropertyAccess/PropertyPathBuilder.php b/src/Symfony/Component/PropertyAccess/PropertyPathBuilder.php index b25d70b12e..d09a14b3fc 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyPathBuilder.php +++ b/src/Symfony/Component/PropertyAccess/PropertyPathBuilder.php @@ -193,7 +193,7 @@ class PropertyPathBuilder /** * Returns the current property path. * - * @return PropertyPathInterface The constructed property path + * @return PropertyPathInterface|null The constructed property path */ public function getPropertyPath() { diff --git a/src/Symfony/Component/PropertyAccess/PropertyPathInterface.php b/src/Symfony/Component/PropertyAccess/PropertyPathInterface.php index ecaffac79a..56d70aa529 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyPathInterface.php +++ b/src/Symfony/Component/PropertyAccess/PropertyPathInterface.php @@ -40,7 +40,7 @@ interface PropertyPathInterface extends \Traversable * * If this property path only contains one item, null is returned. * - * @return PropertyPath|null The parent path or null + * @return self|null The parent path or null */ public function getParent();