Skip to content

Commit

Permalink
Fix usage of functions in reference names (#901)
Browse files Browse the repository at this point in the history
Closes #889
  • Loading branch information
theofidry authored Mar 17, 2018
1 parent 772b352 commit 34ee003
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/FixtureBuilder/ExpressionLanguage/Parser/SimpleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Parser;

use function key;
use Nelmio\Alice\Definition\Value\FixtureReferenceValue;
use Nelmio\Alice\Definition\Value\FunctionCallValue;
use Nelmio\Alice\Definition\Value\ListValue;
use Nelmio\Alice\Definition\Value\NestedValue;
use Nelmio\Alice\Definition\ValueInterface;
Expand Down Expand Up @@ -75,6 +78,24 @@ public function parse(string $value)
private function parseToken(array $parsedTokens, TokenParserInterface $parser, Token $token): array
{
$parsedToken = $parser->parse($token);

if ($parsedToken instanceof FunctionCallValue) {
// Check if the pattern was something like @value_<func()> in which case the function call should be part of the reference
// name
$lastParsedToken = end($parsedTokens);

if ($lastParsedToken instanceof FixtureReferenceValue) {
$parsedTokens[key($parsedTokens)] = new FixtureReferenceValue(
new ListValue([
$lastParsedToken->getValue(),
$parsedToken,
])
);

return $parsedTokens;
}
}

$parsedToken = ($parsedToken instanceof NestedValue)
? $parsedToken->getValue()
: [$parsedToken]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,13 @@ public function provideValues()
new Token('@user0', new TokenType(TokenType::SIMPLE_REFERENCE_TYPE)),
],
];
yield '[Reference] with function call' => [
'@user0_<current()>',
[
new Token('@user0_', new TokenType(TokenType::SIMPLE_REFERENCE_TYPE)),
new Token('<aliceTokenizedFunction(FUNCTION_START__current__IDENTITY_OR_FUNCTION_END)>', new TokenType(TokenType::FUNCTION_TYPE)),
],
];
yield '[Reference] escaped alone with strings' => [
'\@user0',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,18 @@ public function provideValues()
'@user0',
new FixtureReferenceValue('user0'),
];
yield '[Reference] with function call' => [
'@user0_<current()>',
new FixtureReferenceValue(
new ListValue([
'user0_',
new FunctionCallValue(
'current',
[new ValueForCurrentValue()]
)
])
),
];
yield '[Reference] escaped alone with strings' => [
'\@user0',
'@user0',
Expand Down

0 comments on commit 34ee003

Please sign in to comment.