Skip to content

Commit

Permalink
Delegate matching function expressions in strings to a dedicated test…
Browse files Browse the repository at this point in the history
…able class
  • Loading branch information
J-Ben87 committed Jun 25, 2023
1 parent d83181f commit a27455b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/Adapter/ConstantAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Presta\BehatEvaluator\Adapter;

use Presta\BehatEvaluator\ExpressionLanguage\ExpressionLanguage;
use Presta\BehatEvaluator\ExpressionLanguage\ExpressionMatcher\FunctionExpressionMatcher;

final class ConstantAdapter implements AdapterInterface
{
Expand All @@ -18,9 +19,9 @@ public function __invoke(mixed $value): mixed
return $value;
}

preg_match_all('/<(?<expression>constant\([^)]+\))>/', $value, $matches);
$match = new FunctionExpressionMatcher();

foreach ($matches['expression'] as $expression) {
foreach ($match('constant', $value) as $expression) {
$evaluated = $this->expressionLanguage->evaluate($expression);

// the evaluation did not end up with a transformation
Expand Down
5 changes: 3 additions & 2 deletions src/Adapter/DateTimeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Presta\BehatEvaluator\Adapter;

use Presta\BehatEvaluator\ExpressionLanguage\ExpressionLanguage;
use Presta\BehatEvaluator\ExpressionLanguage\ExpressionMatcher\FunctionExpressionMatcher;

/**
* @example <datetime()>
Expand All @@ -31,9 +32,9 @@ public function __invoke(mixed $value): mixed
return $value;
}

preg_match_all("/<(?<expression>datetime(_immutable)?\([^)]*\))>/", $value, $matches);
$match = new FunctionExpressionMatcher();

foreach ($matches['expression'] as $expression) {
foreach ($match('datetime(_immutable)?', $value) as $expression) {
// surround named parameters arguments by double quotes
// so that the named parameter part is not interpreted by the expression language
// ex. 'format: "Y-m-d"' will be transformed to '"format: \"Y-m-d\""'
Expand Down
5 changes: 3 additions & 2 deletions src/Adapter/FactoryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Presta\BehatEvaluator\Adapter;

use Presta\BehatEvaluator\ExpressionLanguage\ExpressionLanguage;
use Presta\BehatEvaluator\ExpressionLanguage\ExpressionMatcher\FunctionExpressionMatcher;

/**
* @example <factory("user", {"email": "john.doe@prestaconcept.net"})>
Expand All @@ -26,9 +27,9 @@ public function __invoke(mixed $value): mixed
return $value;
}

preg_match_all("/<(?<expression>factory\([^)]*\))>/", $value, $matches);
$match = new FunctionExpressionMatcher();

foreach ($matches['expression'] as $expression) {
foreach ($match('factory', $value) as $expression) {
$evaluated = $this->expressionLanguage->evaluate($expression);
if ("<$expression>" === $value) {
return $evaluated;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Presta\BehatEvaluator\ExpressionLanguage\ExpressionMatcher;

final class FunctionExpressionMatcher
{
/**
* @return list<string>
*/
public function __invoke(string $name, string $text): array
{
preg_match_all("/<(?<expression>$name\(.*\))>/U", $text, $matches);

return $matches['expression'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Presta\BehatEvaluator\Tests\Unit\ExpressionLanguage\ExpressionMatcher;

use PHPUnit\Framework\TestCase;
use Presta\BehatEvaluator\ExpressionLanguage\ExpressionMatcher\FunctionExpressionMatcher;

final class FunctionExpressionMatcherTest extends TestCase
{
/**
* @param array<string> $expected
*
* @dataProvider strings
*/
public function testMatchingAllExpressions(array $expected, string $name, string $text): void
{
$match = new FunctionExpressionMatcher();

self::assertSame($expected, $match($name, $text));
}

/**
* @return iterable<string, array{array<string>, string}>
*/
public function strings(): iterable
{
$simpleFunction = 'foobar()';
$functionWithArguments = 'foobar("foo", "bar")';
$functionWithDelimiters = 'foobar("(<foo", "bar>)")';

yield 'a simple function expression should return the simple function' => [
[$simpleFunction],
'foobar',
"<$simpleFunction>",
];
yield 'a function expression containing arguments should return the function and it\'s arguments' => [
[$functionWithArguments],
'foobar',
"<$functionWithArguments>",
];
yield 'a function expression containing delimiters should return the function including the delimiters' => [
[$functionWithDelimiters],
'foobar',
"<$functionWithDelimiters>",
];
yield 'a simple function expression inside a larger string should return the simple function' => [
[$simpleFunction],
'foobar',
"The expression <$simpleFunction> is inside a string",
];
yield 'a function expression containing arguments inside a larger string'
. ' should return the function and it\'s arguments' => [
[$functionWithArguments],
'foobar',
"The expression <$functionWithArguments> is inside a string",
];
yield 'a function expression containing delimiters inside a larger string'
. ' should return the function including the delimiters' => [
[$functionWithDelimiters],
'foobar',
"The expression <$functionWithDelimiters> is inside a string",
];
yield 'many function expressions inside a string should return the functions' => [
[$simpleFunction, $functionWithArguments, $functionWithDelimiters],
'foobar',
"The expressions <$simpleFunction>, <$functionWithArguments> and <$functionWithDelimiters>"
. 'are inside a string',
];
yield 'a simple function expression and a name being a pattern'
. ' should return the simple function whose name matches the pattern' => [
['foo()'],
'foo(_bar)?',
'<foo()>',
];
}
}

0 comments on commit a27455b

Please sign in to comment.