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

Make parameters accessible in identity #698

Merged
merged 4 commits into from
Mar 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ documentation for 2.x, head [here](https://github.com/nelmio/alice/tree/2.x)**.
1. [PHP](doc/complete-reference.md#php)
1. [Fixture Ranges](doc/complete-reference.md#fixture-ranges)
1. [Calling Methods](doc/complete-reference.md#calling-methods)
1. [Method arguments with flags](doc/complete-reference.md#method-arguments-with-flags)
1. [Method arguments with parameters](doc/complete-reference.md#method-arguments-with-parameters)
1. [Specifying Constructor Arguments](doc/complete-reference.md#specifying-constructor-arguments)
1. [Using a factory](doc/complete-reference.md#using-a-factory)
1. [Optional Data](doc/complete-reference.md#optional-data)
Expand All @@ -50,6 +52,11 @@ documentation for 2.x, head [here](https://github.com/nelmio/alice/tree/2.x)**.
1. [Including files](doc/fixtures-refactoring.md#including-files)
1. [Variables](doc/fixtures-refactoring.md#variables)
1. [Parameters](doc/fixtures-refactoring.md#parameters)
1. [Static parameters](doc/fixtures-refactoring.md#static-parameters)
1. [Dynamic parameters](doc/fixtures-refactoring.md#dynamic-parameters)
1. [Composite parameters](doc/fixtures-refactoring.md#composite-parameters)
1. [Usage with functions (constructor included)](doc/fixtures-refactoring.md#usage-with-functions-constructor-included)
1. [Inject external parameters](#inject-external-parameters)
1. [Customize Data Generation](doc/customizing-data-generation.md)
1. [Faker Data](doc/customizing-data-generation.md#faker-data)
1. [Localized Fake Data](doc/customizing-data-generation.md#localized-fake-data) **TODO: port that change to v2**
Expand Down
10 changes: 6 additions & 4 deletions bin/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
# file that was distributed with this source code.
#

export INFO_COLOR="\e[34m"
export NO_COLOR="\e[0m"
INFO_COLOR="\e[34m"
NO_COLOR="\e[0m"
PHPUNIT="bin/phpunit"

log() {
local message=$1;
Expand All @@ -20,9 +21,10 @@ log() {
set -e

log "Core library"
vendor/bin/phpunit -c phpunit.xml.dist
$PHPUNIT -c phpunit.xml.dist

log "Symfony bridge"
rm -rf fixtures/Bridge/Symfony/Application/cache/*
PHPUNIT=vendor-bin/symfony/bin/phpunit

vendor-bin/symfony/bin/phpunit -c phpunit_symfony.xml.dist
$PHPUNIT -c phpunit_symfony.xml.dist
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"php-mock/php-mock": "^1.0",
"phpspec/prophecy": "^1.6",
"phpunit/phpunit": "^5.5",
"symfony/phpunit-bridge": "^3.1"
"symfony/phpunit-bridge": "^3.1",
"symfony/var-dumper": "^3.2"
},
"scripts": {
"post-install-cmd": ["@composer bin all install --ansi"]
Expand All @@ -45,6 +46,9 @@
}
},
"autoload-dev": {
"files": [
"vendor/symfony/var-dumper/Resources/functions/dump.php"
],
"psr-4": {
"Nelmio\\Alice\\": [
"fixtures",
Expand Down
55 changes: 55 additions & 0 deletions doc/complete-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
1. [PHP](#php)
1. [Fixture Ranges](#fixture-ranges)
1. [Calling Methods](#calling-methods)
1. [Method arguments with flags](#method-arguments-with-flags)
1. [Method arguments with parameters](#method-arguments-with-parameters)
1. [Specifying Constructor Arguments](#specifying-constructor-arguments)
1. [Using a factory](#using-a-factory)
1. [Optional Data](#optional-data)
Expand Down Expand Up @@ -128,6 +130,59 @@ Nelmio\Entity\User:
- setLocation: [40.689269, -74.044737]
```

### Method arguments with flags

You can specify a flag on a specific argument like so:

```yaml
Nelmio\Entity\User:
user{1..10}:
username: '<username()>'
__calls:
- setLocation:
0 (unique): '<latitude()>'
1 (unique): '<longitude()>'
```

### Method arguments with parameters

```yaml
parameters:
foo: bar

Nelmio\Entity\Dummy:
dummy{1..10}:
__calls:
- setLocation:
arg0: '<{foo}>'
arg1: '$arg0' # will be resolved info 'bar'
3: 500 # the numerical key here is just a random number as in YAML you cannot mix keys with array values
4: '$3' # `3` here refers to the *third* argument, i.e. 500
```

**Note**: as you can see, arguments can be used as parameters as you go. They however will only in the scope of that
function, i.e. in the above the parameter `$arg0` is usable only within the `__construct` declaration above.

The case above can be a bit confusing in YAML, in PHP it would be the following:

```php
[
'parameters' => 'bar',
Nelmio\Entity\Dummy::class => [
'dummy{1..10}' => [
'__calls' => [
[
'arg0' => '<{foo}>',
'arg1' => '$arg0',
500,
'$3',
],
],
],
],
],
```


## Specifying Constructor Arguments

Expand Down
95 changes: 91 additions & 4 deletions doc/fixtures-refactoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
1. [Including files](#including-files)
1. [Variables](#variables)
1. [Parameters](#parameters)
1. [Static parameters](#static-parameters)
1. [Dynamic parameters](#dynamic-parameters)
1. [Composite parameters](#composite-parameters)
1. [Usage with functions (constructor included)](#usage-with-functions-constructor-included)
1. [Inject external parameters](#inject-external-parameters)


## Fixture inheritance
Expand Down Expand Up @@ -134,21 +139,103 @@ date and the current time, which ensure the data will look real enough.

## Parameters

When using the Yaml loader, you can also set global parameters that will be
When using the YAML loader, you can also set global parameters that will be
inserted everywhere those values are used to help with readability. For example:

### Static parameters

```yaml
parameters:
ebay_domain_name: ebay.us

Nelmio\Entity\Shop:
shop1:
shop{1..10}:
domain: '<{ebay_domain_name}>'
# or
domain: '<($ebay_domain_name)>'
```

**Note**: parameters are resolved only one time in 3.x, which means if you have the following:

```yaml
parameters:
shop_id: '<uniqid()>'

Nelmio\Entity\Shop:
shop{1..10}:
id: '<{shop_id}>'
```

Then `shop1`, `shop2`, ... `shop10` will all have the sale value for `id`


### Dynamic parameters

```yaml
parameters:
username_alice: Alice
username_bob: Bob

Nelmio\Entity\User:
user_{alice, bob}:
username: '<{username_<current()>}>' # Will be 'Alice' for 'user_alice' and 'Bob' for 'user_bob'
```

**TODO**: add doc regarding dynamic parameters cf. #354

Additionally, you can pass in a list of defined parameters as the second
### Composite parameters

```yaml
parameters:
key1: NaN
key2: Bat
composite: '<{key1}> <{key2>!'

Nelmio\Entity\User:
user0:
username: '<{composite}>' # 'NaN Bat!'
```


### Usage with functions (constructor included)

```yaml
parameters:
foo: bar

Nelmio\Entity\Dummy:
dummy{1..10}:
__construct:
arg0: '<{foo}>'
arg1: '$arg0' # will be resolved info 'bar'
3: 500 # the numerical key here is just a random number as in YAML you cannot mix keys with array values
4: '$3' # `3` here refers to the *third* argument, i.e. 500
```

**Note**: as you can see, arguments can be used as parameters as you go. They however will only in the scope of that
function, i.e. in the above the parameter `$arg0` is usable only within the `__construct` declaration above.

The case above can be a bit confusing in YAML, in PHP it would be the following:

```php
[
'parameters' => 'bar',
Nelmio\Entity\Dummy::class => [
'dummy{1..10}' => [
'__construct' => [
'arg0' => '<{foo}>',
'arg1' => '$arg0',
500,
'$3',
],
],
],
],
```


### Inject external parameters

You can pass in a list of defined parameters as the second
argument of `{File,Data}LoaderInterface::load{File,Data}()`.


Expand Down
24 changes: 24 additions & 0 deletions fixtures/Entity/DummyWithVariadicConstructorParam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Nelmio\Alice\Entity;

class DummyWithVariadicConstructorParam
{
public $val;

public function __construct(...$val)
{
$this->val = $val;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ public function denormalize(
$arguments = [];
foreach ($unparsedArguments as $unparsedIndex => $argument) {
$argumentFlags = (is_string($unparsedIndex)) ? $parser->parse($unparsedIndex) : null;
$arguments[] = $this->valueDenormalizer->denormalize($scope, $argumentFlags, $argument);
$denormalizedArgument = $this->valueDenormalizer->denormalize($scope, $argumentFlags, $argument);

if (null === $argumentFlags) {
$arguments[] = $denormalizedArgument;
} else {
$arguments[$argumentFlags->getKey()] = $denormalizedArgument;
}
}

return $arguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class SubPatternsLexer implements LexerInterface
'/^(@[^\ @\<]+\{.*\})/' => self::REFERENCE_LEXER, // Range or list
'/^(@[^\ @\{\<]+)/' => self::REFERENCE_LEXER,
'/^(@)<\S+\(.*\)>/' => self::REFERENCE_LEXER,
'/^(\$[\p{L}_]+)/' => TokenType::VARIABLE_TYPE,
'/^(\$[\p{L}_\d]+)/' => TokenType::VARIABLE_TYPE,
'/^([^\\\<>\[\d\%\$@\]]+)/' => TokenType::STRING_TYPE,
'/^([^\\\<>\[\%\$@\]]+)/' => TokenType::STRING_TYPE,
];
Expand Down
6 changes: 3 additions & 3 deletions src/Generator/Hydrator/SimpleHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public function hydrate(
$fixture = $fixtureSet->getFixtures()->get($object->getId());
$properties = $fixture->getSpecs()->getProperties();

$scope = [
'_instances' => $fixtureSet->getObjects()->toArray(),
];
$scope = $fixtureSet->getParameters()->toArray();
$scope['_instances'] = $fixtureSet->getObjects()->toArray();

foreach ($properties as $property) {
/** @var Property $property */
$propertyValue = $property->getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public function canInstantiate(FixtureInterface $fixture): bool
*/
protected function createInstance(FixtureInterface $fixture)
{
list($class, $arguments) = [$fixture->getClassName(), $fixture->getSpecs()->getConstructor()->getArguments()];
list($class, $arguments) = [
$fixture->getClassName(),
array_values($fixture->getSpecs()->getConstructor()->getArguments())
];

return new $class(...$arguments);
}
Expand Down
18 changes: 15 additions & 3 deletions src/Generator/Instantiator/InstantiatorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,28 @@ private function resolveArguments(
GenerationContext $context
): array
{
$scope = $fixtureSet->getParameters()->toArray();

$argumentPosition = 1;
foreach ($arguments as $index => $argument) {
if ($argument instanceof ValueInterface) {
try {
$result = $resolver->resolve($argument, $fixture, $fixtureSet, [], $context);
$result = $resolver->resolve($argument, $fixture, $fixtureSet, $scope, $context);
} catch (ResolutionThrowable $throwable) {
throw UnresolvableValueDuringGenerationExceptionFactory::createFromResolutionThrowable($throwable);
}

$fixtureSet = $result->getSet();
$arguments[$index] = $result->getValue();
list($fixtureSet, $value) = [$result->getSet(), $result->getValue()];

$arguments[$index] = $value;

if (is_int($index)) {
$scope[$argumentPosition] = $value;
} else {
$scope[$index] = $value;
}

$argumentPosition++;
}
}

Expand Down
Loading