-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Browserkit][Form][Tests] Add submitWithAdditionalValues() method in Symfony\Component\BrowserK… #18330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
4c62292
to
38e5d55
Compare
I don't know why tests are failing, on AppVeyor the error is not related to my changes and on Travis CI I don't know what is |
38e5d55
to
582a494
Compare
Failures are unrelated :) |
Actually travis fails because #18275 has not been reverted in 3.0 and master yet. |
It has been reverted in 3.0 |
and in master as well now. |
582a494
to
36542cc
Compare
I have force-pushed the commit to launch CI tests again. |
ad02c38
to
31a542f
Compare
@alexislefebvre thanks for sending this proposal. I'm not against it ... but adding a new method always "complicates" things (people must learn a new method, Symfony developers need to maintain that method forever, etc.) I'd like to ask some Symfony Form experts, like @HeahDude, if they think this is absolutely necessary or if the problem to solve (which I don't understand) can be solved in a simpler way. Thanks! |
@javiereguiluz Please don't say I'm an expert :) "involved" seems more appropriate. For me it's a 👍 (I've already add one it a few days ago on the description). |
@HeahDude could you please explain the problem to solve to people like me, not involved in the Form component? @alexislefebvre said: "I want to submit additional values in my tests but Symfony doesn't allow me to do that". Why does he want to submit additional values? Why doesn't Symfony allow to do that? Why should be "force" Symfony yo allow something it doesn't allow? Thanks. |
@javiereguiluz because a form view in a template (like used in a tested page with a crawler) might contain no fields. Think of a Currently the This PR fixes it by allowing to submit extra fields. |
@HeahDude thanks for the explanation. I think the "right" solution would be to allow the crawler to add nodes dynamically and then submit the form ... but my guess is that they want to maintain the DomCrawler as "read-only", so we can't add anything. Therefore, the proposed solution seems the way to go. Thanks! |
Maybe this one could have a |
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms'); | ||
|
||
// The field "foo[0]" have been converted to an array "foo => 0". | ||
$this->assertEquals( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this test would be more readable assigning variables:
$client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
$crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
$additionalValues = array('foo[0]' => 'bar');
$expectedParameters = array('foo' => array('0' => 'bar'));
$client->submitWithAdditionalValues($crawler->filter('input')->form(), array(), $additionalValues);
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
$this->assertEquals($expectedParameters, $client->getRequest()->getParameters(), 'parameters have not been added');
Thanks @HeahDude for your explanation to @javiereguiluz. :) Upon further reflection, one drawback of this If you're not OK to add a new method for a not so common case, I suggest another approach:
|
ping @jakzal :) |
31a542f
to
d18e89b
Compare
d18e89b
to
6cf66b1
Compare
Can someone please give some feedback about my last comment? Thanks. |
@@ -263,6 +263,31 @@ public function submit(Form $form, array $values = array()) | |||
} | |||
|
|||
/** | |||
* Submits a form. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with additional values ?
c39e500
to
ea5d7de
Compare
Looks good :) |
Since the method is in |
or |
Tests are failing because you need to update the |
…it\Client Extract code that converts fields to arrays in a new method convertFieldsToArray()
ea5d7de
to
3823aeb
Compare
After further reflection, I don't like this |
I'll work on the documentation instead, these changes are not necessary to test a collection of fields. |
Thanks to @HeahDude and the others for your help and sorry for the inconvenience! I opened a PR for the documentation, I hope that it will be more useful to document how to test a form collection for all the Symfony versions instead of adding a method that will be usable only with the last version of Symfony. |
This PR adds a
submitWithAdditionalValues()
method which you can use in order to submit a form with additional data during tests.For example, when you have a
CollectionType
with the'allow_add' => true,
option, you can't change the values with$form[FIELD_NAME][…] = 'foo'
, it triggers an error.With the
submitWithAdditionalValues()
method you can add some fields to the request:Where
array('foo[0]' => 'bar'),
is an array which values won't be checked (Symfony won't check if the field exist).