Skip to content

Commit

Permalink
Added matchers to allow value comparisons like:
Browse files Browse the repository at this point in the history
match:type(asd adasd  asd)
match:type(5)
match:regexp(/\d{2}\/\d{2}\/\d{4}/)
  • Loading branch information
mmadariaga committed Aug 10, 2023
1 parent 3daf275 commit e43d7f4
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion Behat/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Behatch\HttpCall\Request;
use Behatch\Json\Json;
use Behatch\Json\JsonInspector;
use http\Exception\RuntimeException;

/**
* Defines application features from the specific context.
Expand Down Expand Up @@ -174,7 +175,19 @@ private function assertAlike($expected, $actual)
return;
}

if ($expected !== '~') {
$matchingRules = preg_match(
'/match:(\w+)\((.+)\)/', //'/match:(\w)\((\w)\)/',
$expected,
$matches
);

if ($matchingRules) {
[, $matcher, $value] = $matches;
$this->applyMatcher($matcher, $value, $actual);

return;

} elseif ($expected !== '~') {
$this->assert(
$expected === $actual,
"The element '$actual' is not equal to '$expected'"
Expand All @@ -190,4 +203,27 @@ private function assertAlike($expected, $actual)
);
}
}

private function applyMatcher(string $matcher, $expected, $actual)
{
switch($matcher) {
case 'type':
$castedExpectedVariable = $expected;
settype($castedExpectedVariable, gettype($actual));

$this->assert(
$castedExpectedVariable === $actual,
"Type of element '$actual' is not equal to '$expected'"
);
break;
case 'regexp':
$this->assert(
preg_match($expected, (string) $actual),
"The element '$actual' does not match regexp '$expected'"
);
break;
default:
throw new RuntimeException('Unknown matcher type ' . $matcher);
}
}
}

0 comments on commit e43d7f4

Please sign in to comment.