Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

alexislefebvre
Copy link
Contributor

Q A
Branch master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #3824, #4124
License MIT
Doc PR symfony/symfony-docs#6403

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:

    $client->submitWithAdditionalValues(
        $crawler->filter('input')->form(),
        array(),
        // New values:
        array('foo[0]' => 'bar'),
    );

Where array('foo[0]' => 'bar'), is an array which values won't be checked (Symfony won't check if the field exist).

@alexislefebvre alexislefebvre force-pushed the 3824-test-form-submit-additional-values branch 2 times, most recently from 4c62292 to 38e5d55 Compare March 27, 2016 14:31
@alexislefebvre alexislefebvre changed the title Add submitWithAdditionalValues() method in Symfony\Component\BrowserK… [Tests] Add submitWithAdditionalValues() method in Symfony\Component\BrowserK… Mar 27, 2016
@alexislefebvre
Copy link
Contributor Author

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 deps=low.

@alexislefebvre alexislefebvre force-pushed the 3824-test-form-submit-additional-values branch from 38e5d55 to 582a494 Compare March 27, 2016 14:42
@HeahDude
Copy link
Contributor

Failures are unrelated :)

@HeahDude
Copy link
Contributor

Actually travis fails because #18275 has not been reverted in 3.0 and master yet.

@alexislefebvre alexislefebvre changed the title [Tests] Add submitWithAdditionalValues() method in Symfony\Component\BrowserK… [Form][Tests] Add submitWithAdditionalValues() method in Symfony\Component\BrowserK… Mar 27, 2016
@fabpot
Copy link
Member

fabpot commented Mar 27, 2016

It has been reverted in 3.0

@fabpot
Copy link
Member

fabpot commented Mar 27, 2016

and in master as well now.

@alexislefebvre alexislefebvre force-pushed the 3824-test-form-submit-additional-values branch from 582a494 to 36542cc Compare March 27, 2016 18:08
@alexislefebvre
Copy link
Contributor Author

I have force-pushed the commit to launch CI tests again.

@alexislefebvre alexislefebvre force-pushed the 3824-test-form-submit-additional-values branch 2 times, most recently from ad02c38 to 31a542f Compare March 28, 2016 10:04
@javiereguiluz
Copy link
Member

@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!

@HeahDude
Copy link
Contributor

@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).

@javiereguiluz
Copy link
Member

@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.

@HeahDude
Copy link
Contributor

@javiereguiluz because a form view in a template (like used in a tested page with a crawler) might contain no fields.

Think of a CollectionType with an empty collection. Then it would require to add some fields with javascript to populate the form and submit.

Currently the DomCrawler/BrowserKit components do not allow to submit extra fields (which are not present in the view because they cannot be generated dynamically).

This PR fixes it by allowing to submit extra fields.

@javiereguiluz
Copy link
Member

@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!

@HeahDude
Copy link
Contributor

Maybe this one could have a BrowserKit label instead of Form.

$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(
Copy link
Contributor

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');

@javiereguiluz javiereguiluz changed the title [Form][Tests] Add submitWithAdditionalValues() method in Symfony\Component\BrowserK… [Browserkit][Form][Tests] Add submitWithAdditionalValues() method in Symfony\Component\BrowserK… Mar 29, 2016
@alexislefebvre
Copy link
Contributor Author

Thanks @HeahDude for your explanation to @javiereguiluz. :)

Upon further reflection, one drawback of this submitWithAdditionalValues() method is that it hides what is happening, it can be used to add data, but not to remove data (e.g. remove an existing field from a collection) which is almost as important as adding data.

If you're not OK to add a new method for a not so common case, I suggest another approach:

  1. reduce this PR: implement the convertFieldsToArray() method I added in Symfony/Component/DomCrawler/Form.php and remove the other changes from this PR. It would be useful because it allows to use a syntax close to the other tests (foo[bar] instead of a PHP array)
  2. document how to add or remove data during tests: examples in this PR https://github.com/symfony/symfony-docs/pull/6403/files or on Stack Overflow

@HeahDude
Copy link
Contributor

ping @jakzal :)

@alexislefebvre alexislefebvre force-pushed the 3824-test-form-submit-additional-values branch from 31a542f to d18e89b Compare March 31, 2016 11:54
@alexislefebvre alexislefebvre force-pushed the 3824-test-form-submit-additional-values branch from d18e89b to 6cf66b1 Compare April 1, 2016 21:17
@alexislefebvre
Copy link
Contributor Author

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with additional values ?

@alexislefebvre alexislefebvre force-pushed the 3824-test-form-submit-additional-values branch from c39e500 to ea5d7de Compare April 2, 2016 15:25
@HeahDude
Copy link
Contributor

HeahDude commented Apr 2, 2016

Looks good :)

@HeahDude
Copy link
Contributor

HeahDude commented Apr 2, 2016

Since the method is in Form wouldn't it make sense to call this method submitWithAdditionnalFields ?

@HeahDude
Copy link
Contributor

HeahDude commented Apr 2, 2016

or submitWithExtraFields ?

@HeahDude
Copy link
Contributor

HeahDude commented Apr 2, 2016

Tests are failing because you need to update the DomCrawler dependency of the BrowerKit component to ~3.1

…it\Client

Extract code that converts fields to arrays in a new method convertFieldsToArray()
@alexislefebvre alexislefebvre force-pushed the 3824-test-form-submit-additional-values branch from ea5d7de to 3823aeb Compare April 2, 2016 15:49
@alexislefebvre
Copy link
Contributor Author

After further reflection, I don't like this submitWithAdditionalValues() method, I will remove it in order to have a simpler PR. Do you think it's better to close this PR and open a new one (while referencing this PR) instead of altering this PR?

@alexislefebvre
Copy link
Contributor Author

I'll work on the documentation instead, these changes are not necessary to test a collection of fields.

@alexislefebvre alexislefebvre deleted the 3824-test-form-submit-additional-values branch April 2, 2016 18:30
@alexislefebvre
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants