Skip to content

Commit

Permalink
avoid deprecation notices regarding `Symfony\Component\HttpFoundation…
Browse files Browse the repository at this point in the history
…\InputBag::get()` with Symfony >= 5.1
  • Loading branch information
craue committed Dec 20, 2020
1 parent 23cffa0 commit c83f9ec
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Form/FormFlow.php
Expand Up @@ -706,13 +706,18 @@ public function saveCurrentStepData(FormInterface $form) {
$request = $this->getRequest();
$formName = $form->getName();

$currentStepData = $request->request->get($formName, []);
if (!\class_exists('Symfony\Component\HttpFoundation\InputBag')) {
// TODO remove as soon as Symfony >= 5.1 is required
$currentStepData = $request->request->get($formName, []);
} else {
$currentStepData = $request->request->all($formName);
}

if ($this->handleFileUploads) {
$currentStepData = array_merge_recursive($currentStepData, $request->files->get($formName, []));
}

$stepData[$this->currentStepNumber] = $currentStepData;
$stepData[$this->getCurrentStepNumber()] = $currentStepData;

$this->saveStepData($stepData);
}
Expand Down
66 changes: 66 additions & 0 deletions Tests/Form/FormFlowTest.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraints\GroupSequence;
Expand Down Expand Up @@ -766,4 +767,69 @@ public function dataBooleanSetter() {
];
}

/**
* @dataProvider dataSaveCurrentStepData
*/
public function testSaveCurrentStepData(bool $useInputBag) {
$flow = $this->getFlowWithMockedMethods(['getRequest', 'getCurrentStepNumber', 'retrieveStepData', 'saveStepData']);

$formData = ['aField' => 'aValue'];

$request = Request::create('', 'POST', ['aForm' => $formData]);

if ($useInputBag) {
/*
* This would cause a deprecation notice with Symfony >= 5.1 when calling `$request->request->get()` and
* a) passing a non-string value for the 2nd argument or
* b) retrieving a non-string value.
*/
$request->request = new InputBag($request->request->all());
}

$flow
->method('getRequest')
->will($this->returnValue($request))
;

$flow
->method('getCurrentStepNumber')
->will($this->returnValue(1))
;

$flow
->method('retrieveStepData')
->will($this->returnValue([]))
;

$flow
->method('saveStepData')
->with([1 => $formData])
;

$dispatcher = $this->createMock(EventDispatcherInterface::class);
$factory = Forms::createFormFactoryBuilder()->getFormFactory();
$formBuilder = new FormBuilder('aForm', null, $dispatcher, $factory);

$form = $formBuilder
->setCompound(true)
->setDataMapper($this->createMock(DataMapperInterface::class))
->add('aField', TextType::class)
->setMethod('POST')
->setRequestHandler(new HttpFoundationRequestHandler())
->getForm()
;

$this->assertTrue($flow->isValid($form));

$flow->saveCurrentStepData($form);
}

public function dataSaveCurrentStepData() {
yield [false];

if (class_exists(InputBag::class)) {
yield [true];
}
}

}

0 comments on commit c83f9ec

Please sign in to comment.