Skip to content
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

DataProvider can use default values of target method #123

Conversation

MartinMajor
Copy link

This pull request allows to use named parameters in dataProvider. You can now also use default values of method's parameters.

ApiTests.php:

    /**
     * Tests one API request
     * @dataProvider api-calls.ini
     */
    public function testRequest($url, $schema, $method = 'GET', $authenticated = FALSE, $headers = [])
    {
        // validates API response using schema
    }

api-calls.ini

[search]
url = "/search/?q=hitchhiker%27s+guide+to+the+galaxy"
schema = search.json

[buy]
url = "/buy/42"
schema = buy.json
authenticated = TRUE

@JanTvrdik
Copy link
Contributor

👍 But the implementation seems too complicated

@JanTvrdik
Copy link
Contributor

  1. It should be $defaultParameters not $defaultParametr.

  2. What about putting in $defaultParametr only parameters with default value and then $item += $defaultParameters; + wrap the array walk with if ($defaultParameters)?

  3. And I'm old-fashioned and use foreach instead of array_walk.

foreach ($method->getParameters() as $parameter) {
    if ($parameter->isDefaultValueAvailable()) {
        $defaults[$parameter->getName()] = $parameter->getDefaultValue();
    }
}

if (isset($defaults)) {
    foreach ($parameters as $name => $value) {
        $parameters[$name] += $defaults;
    }
}

@MartinMajor
Copy link
Author

Thanks for review!

  1. Sure, it was a typo. I'll change it.
  2. Parameters must be in same order as method's parameters so we cannot use $item += $defaultParameters;.
  3. Me too, but I tried to be new-fashioned :)

@JanTvrdik
Copy link
Contributor

 Parameters must be in same order as method parameters

I didn't know that. It that case I would fix the method invocation so that the order would be irrelevant as I would expected from named parameters.


Edit: or maybe not. You would need to reiterate the parameters in method reflection which does not make much sense.

@dg dg closed this in 128e82c Apr 23, 2014
@MartinMajor
Copy link
Author

Thanks, that was fast :)

@MartinMajor MartinMajor deleted the feature/dataProviderDefaultValues branch April 23, 2014 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants