Skip to content

[DomCrawler] Fix conversion to int on GetPhpFiles #24141

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

Merged
merged 1 commit into from
Sep 11, 2017

Conversation

MaraBlaga
Copy link
Contributor

@MaraBlaga MaraBlaga commented Sep 8, 2017

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

We've encountered that when using the DomCrawler with the UploadedFile, everything gets converted into strings. We've addressed the issue, and made sure that the attributes in the UploadedFile respects their type.

The code below demonstrates the problem.

require 'vendor/autoload.php';
require 'app/AppKernel.php';
$crawler = new \Symfony\Component\DomCrawler\Crawler(
    '<form method="post"><input type="file" name="image"/></form>',
    'http://www.example.com'
);
$form = $crawler->filter('form')->form();
$form['image'] = new \Symfony\Component\HttpFoundation\File\UploadedFile(
    'path/to/file',
    'foo',
    'text/plain',
    100
);

$client = new \Symfony\Bundle\FrameworkBundle\Client(new AppKernel('test', true));
$crawler = $client->submit($form);

var_dump($client->getRequest()->files->get('image')->getClientSize());  //returns string, not int
echo 'Done.' . PHP_EOL;

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

thanks, but tests are red currently :)

array_walk_recursive(
$expandedValue,
function (&$value, $key) {
if (is_numeric($value) && in_array($key, ['size', 'error'], true)) {
Copy link
Member

@nicolas-grekas nicolas-grekas Sep 9, 2017

Choose a reason for hiding this comment

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

is_numeric works with floats also, so better use ctype_digit here if we handle only integers, isn't it?
also, please use array(...) instead of [...], or better:
('size' === $key || 'error' === $key)

@MaraBlaga MaraBlaga force-pushed the Convert-to-int-on-GetPhpFiles branch 3 times, most recently from 10fb11c to 7618799 Compare September 11, 2017 13:53
@MaraBlaga MaraBlaga force-pushed the Convert-to-int-on-GetPhpFiles branch from 7618799 to 122da5a Compare September 11, 2017 14:11
@MaraBlaga
Copy link
Contributor Author

The PR is now updated, based on the comment above :). Thank you!

@fabpot
Copy link
Member

fabpot commented Sep 11, 2017

Thank you @MaraBlaga.

@fabpot fabpot merged commit 122da5a into symfony:2.7 Sep 11, 2017
fabpot added a commit that referenced this pull request Sep 11, 2017
This PR was merged into the 2.7 branch.

Discussion
----------

[DomCrawler] Fix conversion to int on GetPhpFiles

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

We've encountered that when using the DomCrawler with the UploadedFile, everything gets converted into strings. We've addressed the issue, and made sure that the attributes in the UploadedFile respects their type.

The code below demonstrates the problem.

```<?php
require 'vendor/autoload.php';
require 'app/AppKernel.php';
$crawler = new \Symfony\Component\DomCrawler\Crawler(
    '<form method="post"><input type="file" name="image"/></form>',
    'http://www.example.com'
);
$form = $crawler->filter('form')->form();
$form['image'] = new \Symfony\Component\HttpFoundation\File\UploadedFile(
    'path/to/file',
    'foo',
    'text/plain',
    100
);

$client = new \Symfony\Bundle\FrameworkBundle\Client(new AppKernel('test', true));
$crawler = $client->submit($form);

var_dump($client->getRequest()->files->get('image')->getClientSize());  //returns string, not int
echo 'Done.' . PHP_EOL;

Commits
-------

122da5a [DomCrawler] Fix conversion to int on GetPhpFiles
@fabpot fabpot mentioned this pull request Sep 11, 2017
This was referenced Oct 5, 2017
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