diff --git a/Form.php b/Form.php index 1ab5d146ef..1822cf6e18 100644 --- a/Form.php +++ b/Form.php @@ -532,9 +532,11 @@ public function submit($submittedData, $clearMissing = true) $submittedData = null; } elseif (is_scalar($submittedData)) { $submittedData = (string) $submittedData; - } elseif (!$this->config->getOption('allow_file_upload') && $this->config->getRequestHandler()->isFileUpload($submittedData)) { - $submittedData = null; - $this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, file upload given.'); + } elseif ($this->config->getRequestHandler()->isFileUpload($submittedData)) { + if (!$this->config->getOption('allow_file_upload')) { + $submittedData = null; + $this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, file upload given.'); + } } elseif (\is_array($submittedData) && !$this->config->getCompound() && !$this->config->hasOption('multiple')) { $submittedData = null; $this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.'); diff --git a/Tests/AbstractFormTest.php b/Tests/AbstractFormTest.php index 235aa3fa98..00d9e0fbd4 100644 --- a/Tests/AbstractFormTest.php +++ b/Tests/AbstractFormTest.php @@ -65,26 +65,6 @@ protected function getBuilder($name = 'name', EventDispatcherInterface $dispatch return new FormBuilder($name, $dataClass, $dispatcher ?: $this->dispatcher, $this->factory, $options); } - /** - * @param string $name - * - * @return \PHPUnit_Framework_MockObject_MockObject - */ - protected function getMockForm($name = 'name') - { - $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); - $config = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')->getMock(); - - $form->expects($this->any()) - ->method('getName') - ->will($this->returnValue($name)); - $form->expects($this->any()) - ->method('getConfig') - ->will($this->returnValue($config)); - - return $form; - } - /** * @return \PHPUnit_Framework_MockObject_MockObject */ diff --git a/Tests/AbstractRequestHandlerTest.php b/Tests/AbstractRequestHandlerTest.php index 2c3de1184b..dd73dcde23 100644 --- a/Tests/AbstractRequestHandlerTest.php +++ b/Tests/AbstractRequestHandlerTest.php @@ -12,6 +12,10 @@ namespace Symfony\Component\Form\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; +use Symfony\Component\Form\Form; +use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormFactory; use Symfony\Component\Form\Forms; @@ -66,17 +70,16 @@ public function methodProvider() */ public function testSubmitIfNameInRequest($method) { - $form = $this->getMockForm('param1', $method); + $form = $this->createForm('param1', $method); $this->setRequestData($method, [ 'param1' => 'DATA', ]); - $form->expects($this->once()) - ->method('submit') - ->with('DATA', 'PATCH' !== $method); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertTrue($form->isSubmitted()); + $this->assertSame('DATA', $form->getData()); } /** @@ -84,7 +87,7 @@ public function testSubmitIfNameInRequest($method) */ public function testDoNotSubmitIfWrongRequestMethod($method) { - $form = $this->getMockForm('param1', $method); + $form = $this->createForm('param1', $method); $otherMethod = 'POST' === $method ? 'PUT' : 'POST'; @@ -92,10 +95,9 @@ public function testDoNotSubmitIfWrongRequestMethod($method) 'param1' => 'DATA', ]); - $form->expects($this->never()) - ->method('submit'); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertFalse($form->isSubmitted()); } /** @@ -103,16 +105,15 @@ public function testDoNotSubmitIfWrongRequestMethod($method) */ public function testDoNoSubmitSimpleFormIfNameNotInRequestAndNotGetRequest($method) { - $form = $this->getMockForm('param1', $method, false); + $form = $this->createForm('param1', $method, false); $this->setRequestData($method, [ 'paramx' => [], ]); - $form->expects($this->never()) - ->method('submit'); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertFalse($form->isSubmitted()); } /** @@ -120,30 +121,28 @@ public function testDoNoSubmitSimpleFormIfNameNotInRequestAndNotGetRequest($meth */ public function testDoNotSubmitCompoundFormIfNameNotInRequestAndNotGetRequest($method) { - $form = $this->getMockForm('param1', $method, true); + $form = $this->createForm('param1', $method, true); $this->setRequestData($method, [ 'paramx' => [], ]); - $form->expects($this->never()) - ->method('submit'); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertFalse($form->isSubmitted()); } public function testDoNotSubmitIfNameNotInRequestAndGetRequest() { - $form = $this->getMockForm('param1', 'GET'); + $form = $this->createForm('param1', 'GET'); $this->setRequestData('GET', [ 'paramx' => [], ]); - $form->expects($this->never()) - ->method('submit'); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertFalse($form->isSubmitted()); } /** @@ -151,24 +150,28 @@ public function testDoNotSubmitIfNameNotInRequestAndGetRequest() */ public function testSubmitFormWithEmptyNameIfAtLeastOneFieldInRequest($method) { - $form = $this->getMockForm('', $method); - $form->expects($this->any()) - ->method('all') - ->will($this->returnValue([ - 'param1' => $this->getMockForm('param1'), - 'param2' => $this->getMockForm('param2'), - ])); + $form = $this->createForm('', $method, true); + $form->add($this->createForm('param1')); + $form->add($this->createForm('param2')); $this->setRequestData($method, $requestData = [ 'param1' => 'submitted value', 'paramx' => 'submitted value', ]); - $form->expects($this->once()) - ->method('submit') - ->with($requestData, 'PATCH' !== $method); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertTrue($form->isSubmitted()); + $this->assertTrue($form->get('param1')->isSubmitted()); + $this->assertSame('submitted value', $form->get('param1')->getData()); + + if ('PATCH' === $method) { + $this->assertFalse($form->get('param2')->isSubmitted()); + } else { + $this->assertTrue($form->get('param2')->isSubmitted()); + } + + $this->assertNull($form->get('param2')->getData()); } /** @@ -176,22 +179,17 @@ public function testSubmitFormWithEmptyNameIfAtLeastOneFieldInRequest($method) */ public function testDoNotSubmitFormWithEmptyNameIfNoFieldInRequest($method) { - $form = $this->getMockForm('', $method); - $form->expects($this->any()) - ->method('all') - ->will($this->returnValue([ - 'param1' => $this->getMockForm('param1'), - 'param2' => $this->getMockForm('param2'), - ])); + $form = $this->createForm('', $method, true); + $form->add($this->createForm('param1')); + $form->add($this->createForm('param2')); $this->setRequestData($method, [ 'paramx' => 'submitted value', ]); - $form->expects($this->never()) - ->method('submit'); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertFalse($form->isSubmitted()); } /** @@ -199,7 +197,9 @@ public function testDoNotSubmitFormWithEmptyNameIfNoFieldInRequest($method) */ public function testMergeParamsAndFiles($method) { - $form = $this->getMockForm('param1', $method); + $form = $this->createForm('param1', $method, true); + $form->add($this->createForm('field1')); + $form->add($this->createBuilder('field2', false, ['allow_file_upload' => true])->getForm()); $file = $this->getMockFile(); $this->setRequestData($method, [ @@ -212,14 +212,11 @@ public function testMergeParamsAndFiles($method) ], ]); - $form->expects($this->once()) - ->method('submit') - ->with([ - 'field1' => 'DATA', - 'field2' => $file, - ], 'PATCH' !== $method); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertTrue($form->isSubmitted()); + $this->assertSame('DATA', $form->get('field1')->getData()); + $this->assertSame($file, $form->get('field2')->getData()); } /** @@ -227,7 +224,7 @@ public function testMergeParamsAndFiles($method) */ public function testParamTakesPrecedenceOverFile($method) { - $form = $this->getMockForm('param1', $method); + $form = $this->createForm('param1', $method); $file = $this->getMockFile(); $this->setRequestData($method, [ @@ -236,11 +233,10 @@ public function testParamTakesPrecedenceOverFile($method) 'param1' => $file, ]); - $form->expects($this->once()) - ->method('submit') - ->with('DATA', 'PATCH' !== $method); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertTrue($form->isSubmitted()); + $this->assertSame('DATA', $form->getData()); } /** @@ -248,7 +244,9 @@ public function testParamTakesPrecedenceOverFile($method) */ public function testSubmitFileIfNoParam($method) { - $form = $this->getMockForm('param1', $method); + $form = $this->createBuilder('param1', false, ['allow_file_upload' => true]) + ->setMethod($method) + ->getForm(); $file = $this->getMockFile(); $this->setRequestData($method, [ @@ -257,11 +255,10 @@ public function testSubmitFileIfNoParam($method) 'param1' => $file, ]); - $form->expects($this->once()) - ->method('submit') - ->with($file, 'PATCH' !== $method); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertTrue($form->isSubmitted()); + $this->assertSame($file, $form->getData()); } /** @@ -269,7 +266,9 @@ public function testSubmitFileIfNoParam($method) */ public function testSubmitMultipleFiles($method) { - $form = $this->getMockForm('param1', $method); + $form = $this->createBuilder('param1', false, ['allow_file_upload' => true]) + ->setMethod($method) + ->getForm(); $file = $this->getMockFile(); $this->setRequestData($method, [ @@ -280,32 +279,10 @@ public function testSubmitMultipleFiles($method) 'param3' => $this->getMockFile('3'), ]); - $form->expects($this->once()) - ->method('submit') - ->with($file, 'PATCH' !== $method); - $this->requestHandler->handleRequest($form, $this->request); - } - - /** - * @dataProvider methodExceptGetProvider - */ - public function testSubmitFileWithNamelessForm($method) - { - $form = $this->getMockForm(null, $method); - $file = $this->getMockFile(); - - $this->setRequestData($method, [ - '' => null, - ], [ - '' => $file, - ]); - - $form->expects($this->once()) - ->method('submit') - ->with($file, 'PATCH' !== $method); - $this->requestHandler->handleRequest($form, $this->request); + $this->assertTrue($form->isSubmitted()); + $this->assertSame($file, $form->getData()); } /** @@ -371,24 +348,26 @@ abstract protected function getMockFile($suffix = ''); abstract protected function getInvalidFile(); - protected function getMockForm($name, $method = null, $compound = true) + protected function createForm($name, $method = null, $compound = false) + { + $config = $this->createBuilder($name, $compound); + + if (null !== $method) { + $config->setMethod($method); + } + + return new Form($config); + } + + protected function createBuilder($name, $compound = false, array $options = []) { - $config = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')->getMock(); - $config->expects($this->any()) - ->method('getMethod') - ->will($this->returnValue($method)); - $config->expects($this->any()) - ->method('getCompound') - ->will($this->returnValue($compound)); - - $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); - $form->expects($this->any()) - ->method('getName') - ->will($this->returnValue($name)); - $form->expects($this->any()) - ->method('getConfig') - ->will($this->returnValue($config)); - - return $form; + $builder = new FormBuilder($name, null, new EventDispatcher(), $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock(), $options); + $builder->setCompound($compound); + + if ($compound) { + $builder->setDataMapper(new PropertyPathMapper()); + } + + return $builder; } } diff --git a/Tests/CompoundFormTest.php b/Tests/CompoundFormTest.php index 85c7431bcf..ba4f26ecc9 100644 --- a/Tests/CompoundFormTest.php +++ b/Tests/CompoundFormTest.php @@ -11,9 +11,12 @@ namespace Symfony\Component\Form\Tests; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler; use Symfony\Component\Form\FormError; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\Forms; use Symfony\Component\Form\FormView; use Symfony\Component\Form\SubmitButtonBuilder; @@ -69,40 +72,35 @@ public function testDisabledFormsValidEvenIfChildrenInvalid() public function testSubmitForwardsNullIfNotClearMissingButValueIsExplicitlyNull() { - $child = $this->getMockForm('firstName'); + $child = $this->createForm('firstName', false); $this->form->add($child); - $child->expects($this->once()) - ->method('submit') - ->with($this->equalTo(null)); - $this->form->submit(['firstName' => null], false); + + $this->assertNull($this->form->get('firstName')->getData()); } public function testSubmitForwardsNullIfValueIsMissing() { - $child = $this->getMockForm('firstName'); + $child = $this->createForm('firstName', false); $this->form->add($child); - $child->expects($this->once()) - ->method('submit') - ->with($this->equalTo(null)); - $this->form->submit([]); + + $this->assertNull($this->form->get('firstName')->getData()); } public function testSubmitDoesNotForwardNullIfNotClearMissing() { - $child = $this->getMockForm('firstName'); + $child = $this->createForm('firstName', false); $this->form->add($child); - $child->expects($this->never()) - ->method('submit'); - $this->form->submit([], false); + + $this->assertFalse($child->isSubmitted()); } public function testSubmitDoesNotAddExtraFieldForNullValues() @@ -120,15 +118,22 @@ public function testSubmitDoesNotAddExtraFieldForNullValues() public function testClearMissingFlagIsForwarded() { - $child = $this->getMockForm('firstName'); + $personForm = $this->createForm('person'); - $this->form->add($child); + $firstNameForm = $this->createForm('firstName', false); + $personForm->add($firstNameForm); - $child->expects($this->once()) - ->method('submit') - ->with($this->equalTo('foo'), false); + $lastNameForm = $this->createForm('lastName', false); + $lastNameForm->setData('last name'); + $personForm->add($lastNameForm); - $this->form->submit(['firstName' => 'foo'], false); + $this->form->add($personForm); + $this->form->submit(['person' => ['firstName' => 'foo']], false); + + $this->assertTrue($firstNameForm->isSubmitted()); + $this->assertSame('foo', $firstNameForm->getData()); + $this->assertFalse($lastNameForm->isSubmitted()); + $this->assertSame('last name', $lastNameForm->getData()); } public function testCloneChildren() @@ -145,10 +150,8 @@ public function testCloneChildren() public function testNotEmptyIfChildNotEmpty() { - $child = $this->getMockForm(); - $child->expects($this->once()) - ->method('isEmpty') - ->will($this->returnValue(false)); + $child = $this->createForm('name', false); + $child->setData('foo'); $this->form->setData(null); $this->form->add($child); @@ -400,29 +403,25 @@ public function testSetDataSupportsDynamicAdditionAndRemovalOfChildren() ->setDataMapper(new PropertyPathMapper()) ->getForm(); - $child = $this->getMockForm('child'); - $childToBeRemoved = $this->getMockForm('removed'); - $childToBeAdded = $this->getMockForm('added'); - - $form->add($child); - $form->add($childToBeRemoved); - - $child->expects($this->once()) - ->method('setData') - ->will($this->returnCallback(function () use ($form, $childToBeAdded) { + $childToBeRemoved = $this->createForm('removed', false); + $childToBeAdded = $this->createForm('added', false); + $child = $this->getBuilder('child', new EventDispatcher()) + ->setCompound(true) + ->setDataMapper(new PropertyPathMapper()) + ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($form, $childToBeAdded) { $form->remove('removed'); $form->add($childToBeAdded); - })); - - $childToBeRemoved->expects($this->never()) - ->method('setData'); + }) + ->getForm(); - // once when it it is created, once when it is added - $childToBeAdded->expects($this->exactly(2)) - ->method('setData'); + $form->add($child); + $form->add($childToBeRemoved); // pass NULL to all children $form->setData([]); + + $this->assertFalse($form->has('removed')); + $this->assertTrue($form->has('added')); } public function testSetDataMapsViewDataToChildren() @@ -453,30 +452,25 @@ public function testSetDataMapsViewDataToChildren() public function testSubmitSupportsDynamicAdditionAndRemovalOfChildren() { - $child = $this->getMockForm('child'); - $childToBeRemoved = $this->getMockForm('removed'); - $childToBeAdded = $this->getMockForm('added'); - - $this->form->add($child); - $this->form->add($childToBeRemoved); - $form = $this->form; - $child->expects($this->once()) - ->method('submit') - ->will($this->returnCallback(function () use ($form, $childToBeAdded) { + $childToBeRemoved = $this->createForm('removed'); + $childToBeAdded = $this->createForm('added'); + $child = $this->getBuilder('child') + ->addEventListener(FormEvents::PRE_SUBMIT, function () use ($form, $childToBeAdded) { $form->remove('removed'); $form->add($childToBeAdded); - })); - - $childToBeRemoved->expects($this->never()) - ->method('submit'); + }) + ->getForm(); - $childToBeAdded->expects($this->once()) - ->method('submit'); + $this->form->add($child); + $this->form->add($childToBeRemoved); // pass NULL to all children $this->form->submit([]); + + $this->assertFalse($childToBeRemoved->isSubmitted()); + $this->assertTrue($childToBeAdded->isSubmitted()); } public function testSubmitMapsSubmittedChildrenOntoExistingViewData() @@ -880,12 +874,24 @@ public function testGetErrorsDeepRecursive() public function testCreateViewWithChildren() { $type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock(); + $type1 = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock(); + $type2 = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock(); $options = ['a' => 'Foo', 'b' => 'Bar']; - $field1 = $this->getMockForm('foo'); - $field2 = $this->getMockForm('bar'); + $field1 = $this->getBuilder('foo') + ->setType($type1) + ->getForm(); + $field2 = $this->getBuilder('bar') + ->setType($type2) + ->getForm(); $view = new FormView(); $field1View = new FormView(); + $type1 + ->method('createView') + ->will($this->returnValue($field1View)); $field2View = new FormView(); + $type2 + ->method('createView') + ->will($this->returnValue($field2View)); $this->form = $this->getBuilder('form', null, null, $options) ->setCompound(true) @@ -912,24 +918,8 @@ public function testCreateViewWithChildren() ->with($view, $this->form, $options) ->will($this->returnCallback($assertChildViewsEqual([]))); - // Then add the first child form - $field1->expects($this->once()) - ->method('createView') - ->will($this->returnValue($field1View)); - - // Then the second child form - $field2->expects($this->once()) - ->method('createView') - ->will($this->returnValue($field2View)); - - // Again build the view for the form itself. This time the child views - // exist. - $type->expects($this->once()) - ->method('finishView') - ->with($view, $this->form, $options) - ->will($this->returnCallback($assertChildViewsEqual(['foo' => $field1View, 'bar' => $field2View]))); - $this->assertSame($view, $this->form->createView()); + $this->assertSame(['foo' => $field1View, 'bar' => $field2View], $view->children); } public function testNoClickedButtonBeforeSubmission() @@ -1022,7 +1012,7 @@ public function testDisabledButtonIsNotSubmitted() ->getForm(); $form = $this->createForm() - ->add($this->getBuilder('text')->getForm()) + ->add($this->createForm('text', false)) ->add($submit) ; @@ -1067,11 +1057,17 @@ public function testFileUpload() $this->assertNull($this->form->get('bar')->getData()); } - protected function createForm() + protected function createForm($name = 'name', $compound = true) { - return $this->getBuilder() - ->setCompound(true) - ->setDataMapper($this->getDataMapper()) - ->getForm(); + $builder = $this->getBuilder($name); + + if ($compound) { + $builder + ->setCompound(true) + ->setDataMapper($this->getDataMapper()) + ; + } + + return $builder->getForm(); } } diff --git a/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php b/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php index 7861e12197..0182aa23d3 100644 --- a/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php +++ b/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php @@ -13,6 +13,8 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener; +use Symfony\Component\Form\Form; +use Symfony\Component\Form\FormConfigInterface; use Symfony\Component\Form\FormEvent; class FixUrlProtocolListenerTest extends TestCase @@ -20,7 +22,7 @@ class FixUrlProtocolListenerTest extends TestCase public function testFixHttpUrl() { $data = 'www.symfony.com'; - $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); + $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); $event = new FormEvent($form, $data); $filter = new FixUrlProtocolListener('http'); @@ -32,7 +34,7 @@ public function testFixHttpUrl() public function testSkipKnownUrl() { $data = 'http://www.symfony.com'; - $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); + $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); $event = new FormEvent($form, $data); $filter = new FixUrlProtocolListener('http'); @@ -57,7 +59,7 @@ public function provideUrlsWithSupportedProtocols() */ public function testSkipOtherProtocol($url) { - $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); + $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); $event = new FormEvent($form, $url); $filter = new FixUrlProtocolListener('http'); diff --git a/Tests/Extension/Core/EventListener/MergeCollectionListenerTest.php b/Tests/Extension/Core/EventListener/MergeCollectionListenerTest.php index 41b7d18bd5..98cd78385a 100644 --- a/Tests/Extension/Core/EventListener/MergeCollectionListenerTest.php +++ b/Tests/Extension/Core/EventListener/MergeCollectionListenerTest.php @@ -44,11 +44,6 @@ protected function getForm($name = 'name', $propertyPath = null) return $this->getBuilder($name)->setAttribute('property_path', $propertyPath)->getForm(); } - protected function getMockForm() - { - return $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); - } - public function getBooleanMatrix1() { return [ diff --git a/Tests/Extension/Core/EventListener/ResizeFormListenerTest.php b/Tests/Extension/Core/EventListener/ResizeFormListenerTest.php index daa3a8de09..84a8c1db88 100644 --- a/Tests/Extension/Core/EventListener/ResizeFormListenerTest.php +++ b/Tests/Extension/Core/EventListener/ResizeFormListenerTest.php @@ -58,11 +58,6 @@ private function getDataMapper() return $this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock(); } - protected function getMockForm() - { - return $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); - } - public function testPreSetDataResizesForm() { $this->form->add($this->getForm('0')); diff --git a/Tests/Extension/Core/EventListener/TrimListenerTest.php b/Tests/Extension/Core/EventListener/TrimListenerTest.php index 1ecd3ad705..78dae1bd82 100644 --- a/Tests/Extension/Core/EventListener/TrimListenerTest.php +++ b/Tests/Extension/Core/EventListener/TrimListenerTest.php @@ -13,6 +13,8 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Form\Extension\Core\EventListener\TrimListener; +use Symfony\Component\Form\Form; +use Symfony\Component\Form\FormConfigInterface; use Symfony\Component\Form\FormEvent; class TrimListenerTest extends TestCase @@ -20,7 +22,7 @@ class TrimListenerTest extends TestCase public function testTrim() { $data = ' Foo! '; - $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); + $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); $event = new FormEvent($form, $data); $filter = new TrimListener(); @@ -32,7 +34,7 @@ public function testTrim() public function testTrimSkipNonStrings() { $data = 1234; - $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); + $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); $event = new FormEvent($form, $data); $filter = new TrimListener(); diff --git a/Tests/Extension/Csrf/EventListener/CsrfValidationListenerTest.php b/Tests/Extension/Csrf/EventListener/CsrfValidationListenerTest.php index 5bad588884..43e9acad05 100644 --- a/Tests/Extension/Csrf/EventListener/CsrfValidationListenerTest.php +++ b/Tests/Extension/Csrf/EventListener/CsrfValidationListenerTest.php @@ -51,11 +51,6 @@ protected function getDataMapper() return $this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock(); } - protected function getMockForm() - { - return $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); - } - // https://github.com/symfony/symfony/pull/5838 public function testStringFormData() { diff --git a/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php b/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php index f414a511b0..5597e83554 100644 --- a/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php +++ b/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php @@ -26,7 +26,7 @@ class HttpFoundationRequestHandlerTest extends AbstractRequestHandlerTest */ public function testRequestShouldNotBeNull() { - $this->requestHandler->handleRequest($this->getMockForm('name', 'GET')); + $this->requestHandler->handleRequest($this->createForm('name', 'GET')); } /** @@ -34,7 +34,7 @@ public function testRequestShouldNotBeNull() */ public function testRequestShouldBeInstanceOfRequest() { - $this->requestHandler->handleRequest($this->getMockForm('name', 'GET'), new \stdClass()); + $this->requestHandler->handleRequest($this->createForm('name', 'GET'), new \stdClass()); } protected function setRequestData($method, $data, $files = []) diff --git a/Tests/Extension/Validator/EventListener/ValidationListenerTest.php b/Tests/Extension/Validator/EventListener/ValidationListenerTest.php index 5cda7eeaf7..855a90d2e3 100644 --- a/Tests/Extension/Validator/EventListener/ValidationListenerTest.php +++ b/Tests/Extension/Validator/EventListener/ValidationListenerTest.php @@ -12,10 +12,14 @@ namespace Symfony\Component\Form\Tests\Extension\Validator\EventListener; use PHPUnit\Framework\TestCase; -use Symfony\Component\Form\Extension\Validator\Constraints\Form; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; +use Symfony\Component\Form\Extension\Validator\Constraints\Form as FormConstraint; use Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener; +use Symfony\Component\Form\Form; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\PropertyAccess\PropertyPath; use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationList; @@ -67,7 +71,7 @@ protected function setUp() private function getConstraintViolation($code = null) { - return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code, new Form()); + return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code, new FormConstraint()); } private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null) @@ -86,9 +90,16 @@ private function getForm($name = 'name', $propertyPath = null, $dataClass = null return $this->getBuilder($name, $propertyPath, $dataClass)->getForm(); } - private function getMockForm() + private function createForm($name = '', $compound = false) { - return $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); + $config = new FormBuilder($name, null, $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), $this->getMockBuilder(FormFactoryInterface::class)->getMock()); + $config->setCompound($compound); + + if ($compound) { + $config->setDataMapper(new PropertyPathMapper()); + } + + return new Form($config); } // More specific mapping tests can be found in ViolationMapperTest @@ -110,7 +121,7 @@ public function testMapViolation() public function testMapViolationAllowsNonSyncIfInvalid() { - $violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR); + $violation = $this->getConstraintViolation(FormConstraint::NOT_SYNCHRONIZED_ERROR); $form = $this->getForm('street'); $this->validator->expects($this->once()) @@ -127,10 +138,10 @@ public function testMapViolationAllowsNonSyncIfInvalid() public function testValidateIgnoresNonRoot() { - $form = $this->getMockForm(); - $form->expects($this->once()) - ->method('isRoot') - ->will($this->returnValue(false)); + $childForm = $this->createForm('child'); + + $form = $this->createForm('', true); + $form->add($childForm); $this->validator->expects($this->never()) ->method('validate'); @@ -138,15 +149,12 @@ public function testValidateIgnoresNonRoot() $this->violationMapper->expects($this->never()) ->method('mapViolation'); - $this->listener->validateForm(new FormEvent($form, null)); + $this->listener->validateForm(new FormEvent($childForm, null)); } public function testValidateWithEmptyViolationList() { - $form = $this->getMockForm(); - $form->expects($this->once()) - ->method('isRoot') - ->will($this->returnValue(true)); + $form = $this->createForm(); $this->validator ->expects($this->once()) diff --git a/Tests/NativeRequestHandlerTest.php b/Tests/NativeRequestHandlerTest.php index db4902e34b..077f477d4a 100644 --- a/Tests/NativeRequestHandlerTest.php +++ b/Tests/NativeRequestHandlerTest.php @@ -53,12 +53,12 @@ protected function tearDown() */ public function testRequestShouldBeNull() { - $this->requestHandler->handleRequest($this->getMockForm('name', 'GET'), 'request'); + $this->requestHandler->handleRequest($this->createForm('name', 'GET'), 'request'); } public function testMethodOverrideHeaderTakesPrecedenceIfPost() { - $form = $this->getMockForm('param1', 'PUT'); + $form = $this->createForm('param1', 'PUT'); $this->setRequestData('POST', [ 'param1' => 'DATA', @@ -66,16 +66,15 @@ public function testMethodOverrideHeaderTakesPrecedenceIfPost() $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT'; - $form->expects($this->once()) - ->method('submit') - ->with('DATA'); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertTrue($form->isSubmitted()); + $this->assertSame('DATA', $form->getData()); } public function testConvertEmptyUploadedFilesToNull() { - $form = $this->getMockForm('param1', 'POST', false); + $form = $this->createForm('param1', 'POST', false); $this->setRequestData('POST', [], ['param1' => [ 'name' => '', @@ -85,16 +84,17 @@ public function testConvertEmptyUploadedFilesToNull() 'size' => 0, ]]); - $form->expects($this->once()) - ->method('submit') - ->with($this->identicalTo(null)); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertTrue($form->isSubmitted()); + $this->assertNull($form->getData()); } public function testFixBuggyFilesArray() { - $form = $this->getMockForm('param1', 'POST', false); + $form = $this->createForm('param1', 'POST', true); + $fieldForm = $this->createBuilder('field', false, ['allow_file_upload' => true])->getForm(); + $form->add($fieldForm); $this->setRequestData('POST', [], ['param1' => [ 'name' => [ @@ -114,24 +114,25 @@ public function testFixBuggyFilesArray() ], ]]); - $form->expects($this->once()) - ->method('submit') - ->with([ - 'field' => [ - 'name' => 'upload.txt', - 'type' => 'text/plain', - 'tmp_name' => 'owfdskjasdfsa', - 'error' => UPLOAD_ERR_OK, - 'size' => 100, - ], - ]); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertTrue($form->isSubmitted()); + $this->assertEquals([ + 'name' => 'upload.txt', + 'type' => 'text/plain', + 'tmp_name' => 'owfdskjasdfsa', + 'error' => UPLOAD_ERR_OK, + 'size' => 100, + ], $fieldForm->getData()); } public function testFixBuggyNestedFilesArray() { - $form = $this->getMockForm('param1', 'POST'); + $form = $this->createForm('param1', 'POST', true); + $fieldForm = $this->createForm('field', null, true); + $form->add($fieldForm); + $subfieldForm = $this->createBuilder('subfield', false, ['allow_file_upload' => true])->getForm(); + $fieldForm->add($subfieldForm); $this->setRequestData('POST', [], ['param1' => [ 'name' => [ @@ -151,26 +152,21 @@ public function testFixBuggyNestedFilesArray() ], ]]); - $form->expects($this->once()) - ->method('submit') - ->with([ - 'field' => [ - 'subfield' => [ - 'name' => 'upload.txt', - 'type' => 'text/plain', - 'tmp_name' => 'owfdskjasdfsa', - 'error' => UPLOAD_ERR_OK, - 'size' => 100, - ], - ], - ]); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertTrue($subfieldForm->isSubmitted()); + $this->assertEquals([ + 'name' => 'upload.txt', + 'type' => 'text/plain', + 'tmp_name' => 'owfdskjasdfsa', + 'error' => UPLOAD_ERR_OK, + 'size' => 100, + ], $subfieldForm->getData()); } public function testMethodOverrideHeaderIgnoredIfNotPost() { - $form = $this->getMockForm('param1', 'POST'); + $form = $this->createForm('param1', 'POST'); $this->setRequestData('GET', [ 'param1' => 'DATA', @@ -178,10 +174,9 @@ public function testMethodOverrideHeaderIgnoredIfNotPost() $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT'; - $form->expects($this->never()) - ->method('submit'); - $this->requestHandler->handleRequest($form, $this->request); + + $this->assertFalse($form->isSubmitted()); } protected function setRequestData($method, $data, $files = []) diff --git a/Tests/ResolvedFormTypeTest.php b/Tests/ResolvedFormTypeTest.php index cdbb79ae3e..32bb8bb788 100644 --- a/Tests/ResolvedFormTypeTest.php +++ b/Tests/ResolvedFormTypeTest.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Form\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\Form\Form; +use Symfony\Component\Form\FormConfigInterface; use Symfony\Component\Form\FormTypeExtensionInterface; use Symfony\Component\Form\FormTypeInterface; use Symfony\Component\Form\ResolvedFormType; @@ -220,7 +222,7 @@ public function testBuildForm() public function testCreateView() { - $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); + $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); $view = $this->resolvedType->createView($form); @@ -230,7 +232,7 @@ public function testCreateView() public function testCreateViewWithParent() { - $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); + $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); $parentView = $this->getMockBuilder('Symfony\Component\Form\FormView')->getMock(); $view = $this->resolvedType->createView($form, $parentView); @@ -242,7 +244,7 @@ public function testCreateViewWithParent() public function testBuildView() { $options = ['a' => '1', 'b' => '2']; - $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); + $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); $view = $this->getMockBuilder('Symfony\Component\Form\FormView')->getMock(); $i = 0; @@ -284,7 +286,7 @@ public function testBuildView() public function testFinishView() { $options = ['a' => '1', 'b' => '2']; - $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); + $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); $view = $this->getMockBuilder('Symfony\Component\Form\FormView')->getMock(); $i = 0; diff --git a/Tests/SimpleFormTest.php b/Tests/SimpleFormTest.php index ebc2ee0364..214ff3beab 100644 --- a/Tests/SimpleFormTest.php +++ b/Tests/SimpleFormTest.php @@ -218,14 +218,14 @@ public function getDisabledStates() public function testGetRootReturnsRootOfParent() { - $parent = $this->getMockForm(); - $parent->expects($this->once()) - ->method('getRoot') - ->will($this->returnValue('ROOT')); + $root = $this->createForm(); + + $parent = $this->createForm(); + $parent->setParent($root); $this->form->setParent($parent); - $this->assertEquals('ROOT', $this->form->getRoot()); + $this->assertSame($root, $this->form->getRoot()); } public function testGetRootReturnsSelfIfNoParent() @@ -720,12 +720,13 @@ public function testCreateViewWithParent() { $type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock(); $view = $this->getMockBuilder('Symfony\Component\Form\FormView')->getMock(); - $parentForm = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); + $parentType = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock(); + $parentForm = $this->getBuilder()->setType($parentType)->getForm(); $parentView = $this->getMockBuilder('Symfony\Component\Form\FormView')->getMock(); $form = $this->getBuilder()->setType($type)->getForm(); $form->setParent($parentForm); - $parentForm->expects($this->once()) + $parentType->expects($this->once()) ->method('createView') ->will($this->returnValue($parentView));