Skip to content

[ExpressionLanguage] Allow passing any iterable as $providers list #57685

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

Merged
merged 1 commit into from
Jul 9, 2024
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
1 change: 1 addition & 0 deletions src/Symfony/Component/ExpressionLanguage/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add support for null-coalescing unknown variables
* Add support for comments using `/*` & `*/`
* Allow passing any iterable as `$providers` list to `ExpressionLanguage` constructor

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class ExpressionLanguage
protected array $functions = [];

/**
* @param ExpressionFunctionProviderInterface[] $providers
* @param iterable<ExpressionFunctionProviderInterface> $providers
*/
public function __construct(?CacheItemPoolInterface $cache = null, array $providers = [])
public function __construct(?CacheItemPoolInterface $cache = null, iterable $providers = [])
{
$this->cache = $cache ?? new ArrayAdapter();
$this->registerFunctions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ public function testCompiledEnumFunctionWithBackedEnum()
$this->assertSame(FooBackedEnum::Bar, $result);
}

public function testProviders()
/**
* @dataProvider providerTestCases
*/
public function testProviders(iterable $providers)
{
$expressionLanguage = new ExpressionLanguage(null, [new TestProvider()]);
$expressionLanguage = new ExpressionLanguage(null, $providers);
$this->assertEquals('foo', $expressionLanguage->evaluate('identity("foo")'));
$this->assertEquals('"foo"', $expressionLanguage->compile('identity("foo")'));
$this->assertEquals('FOO', $expressionLanguage->evaluate('strtoupper("foo")'));
Comment on lines 146 to 148
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while on it, can we use assertSame here and in other places of the test class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't have time to respond yesterday, but I see it's been taken care of in #57693

Expand All @@ -150,6 +153,14 @@ public function testProviders()
$this->assertEquals('\Symfony\Component\ExpressionLanguage\Tests\Fixtures\fn_namespaced()', $expressionLanguage->compile('fn_namespaced()'));
}

public static function providerTestCases(): iterable
{
yield 'array' => [[new TestProvider()]];
yield 'Traversable' => [(function () {
yield new TestProvider();
})()];
}

/**
* @dataProvider shortCircuitProviderEvaluate
*/
Expand Down
Loading