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

fix(): Fix the missing resolving or references in simple string arrays #984

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Parser\TokenParser\Chainable;

use Nelmio\Alice\Definition\Value\ArrayValue;
use Nelmio\Alice\Definition\ValueInterface;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\ParserInterface;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Token;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\TokenType;
Expand All @@ -36,15 +38,15 @@ public function canParse(Token $token): bool
*
* {@inheritdoc}
*/
public function parse(Token $token): array
public function parse(Token $token): ValueInterface
{
parent::parse($token);

$value = $token->getValue();
try {
$elements = substr($value, 1, strlen($value) - 2);

return $this->parseElements($this->parser, $elements);
return new ArrayValue($this->parseElements($this->parser, $elements));
} catch (\TypeError $error) {
throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token, 0, $error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public function provideValues()
new FunctionCallValue(
'function',
[
['foo', 'bar'],
new ArrayValue(['foo', 'bar']),
]
),
];
Expand Down Expand Up @@ -607,7 +607,7 @@ public function provideValues()
'10x [@user->name, @group->getName()]',
new DynamicArrayValue(
10,
[
new ArrayValue([
new FixturePropertyValue(
new FixtureReferenceValue('user'),
'name'
Expand All @@ -616,7 +616,7 @@ public function provideValues()
new FixtureReferenceValue('group'),
new FunctionCallValue('getName')
),
]
])
),
];
yield '[Array] escaped array' => [
Expand Down Expand Up @@ -645,7 +645,7 @@ public function provideValues()
];
yield '[Array] simple string array' => [
'[@user->name, @group->getName()]',
[
new ArrayValue([
new FixturePropertyValue(
new FixtureReferenceValue('user'),
'name'
Expand All @@ -654,7 +654,7 @@ public function provideValues()
new FixtureReferenceValue('group'),
new FunctionCallValue('getName')
),
],
]),
];

// Optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Parser\TokenParser\Chainable;

use Nelmio\Alice\Definition\Value\ArrayValue;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Parser\ChainableTokenParserInterface;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Parser\FakeParser;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\ParserInterface;
Expand Down Expand Up @@ -88,7 +89,7 @@ public function testParsesEachArrayElementAndReturnsTheConstructedArray()
/** @var ParserInterface $decoratedParser */
$decoratedParser = $decoratedParserProphecy->reveal();

$expected = ['parsed_val1', 'parsed_val2'];
$expected = new ArrayValue(['parsed_val1', 'parsed_val2']);

$parser = new StringArrayTokenParser($decoratedParser);
$actual = $parser->parse($token);
Expand All @@ -107,7 +108,7 @@ public function testIsAbleToParseEmptyArrays()
/** @var ParserInterface $decoratedParser */
$decoratedParser = $decoratedParserProphecy->reveal();

$expected = [];
$expected = new ArrayValue([]);

$parser = new StringArrayTokenParser($decoratedParser);
$actual = $parser->parse($token);
Expand All @@ -125,7 +126,7 @@ public function testTrimsEachArgumentValueBeforePassingThemToTheDecoratedParser(
/** @var ParserInterface $decoratedParser */
$decoratedParser = $decoratedParserProphecy->reveal();

$expected = ['parsed_val1', 'parsed_val2'];
$expected = new ArrayValue(['parsed_val1', 'parsed_val2']);

$parser = new StringArrayTokenParser($decoratedParser);
$actual = $parser->parse($token);
Expand Down
24 changes: 24 additions & 0 deletions tests/Loader/LoaderIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2632,6 +2632,30 @@ public function provideFixturesToGenerate()
],
];

yield 'string array value' => [
[
stdClass::class => [
'dummy' => [
'foo' => 'bar',
],
'another_dummy' => [
'dummies' => '[@dummy, @dummy, @dummy]',
],
],
],
[
'parameters' => [],
'objects' => [
'dummy' => $dummy = StdClassFactory::create([
'foo' => 'bar',
]),
'another_dummy' => StdClassFactory::create([
'dummies' => [$dummy, $dummy, $dummy]
]),
],
],
];

yield 'array value' => [
[
stdClass::class => [
Expand Down