Skip to content

DataProvider can use default values of target method#123

Closed
MartinMajor wants to merge 1 commit into
nette:masterfrom
MartinMajor:feature/dataProviderDefaultValues
Closed

DataProvider can use default values of target method#123
MartinMajor wants to merge 1 commit into
nette:masterfrom
MartinMajor:feature/dataProviderDefaultValues

Conversation

@MartinMajor

Copy link
Copy Markdown

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
Copy Markdown
Contributor

👍 But the implementation seems too complicated

@JanTvrdik

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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