-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delegate matching function expressions in strings to a dedicated test…
…able class
- Loading branch information
Showing
5 changed files
with
105 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/ExpressionLanguage/ExpressionMatcher/FunctionExpressionMatcher.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
tests/Unit/ExpressionLanguage/ExpressionMatcher/FunctionExpressionMatcherTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()>', | ||
]; | ||
} | ||
} |