Skip to content

Commit

Permalink
enhanced behat json context type matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mmadariaga committed Aug 11, 2023
1 parent e43d7f4 commit 1a93713
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Behat/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Mink\Exception\ExpectationException;
use Behatch\Context\BaseContext;
use Behatch\HttpCall\HttpCallResultPool;
use Behatch\HttpCall\Request;
Expand Down Expand Up @@ -123,7 +124,7 @@ public function theJsonShouldBeLike(PyStringNode $content)
} catch (\Exception $e) {
$this->assert(
false,
"The json is equal to:\n". $actual->encode()
"The json is equal to:\n". $actual->encode() . "\nbut\n" . $e->getMessage()
);
}
}
Expand Down Expand Up @@ -208,11 +209,26 @@ private function applyMatcher(string $matcher, $expected, $actual)
{
switch($matcher) {
case 'type':

$actualType = gettype($actual);
$castedExpectedVariable = $expected;
settype($castedExpectedVariable, gettype($actual));

if ($actualType !== gettype($castedExpectedVariable)) {
$succeeded = settype(
$castedExpectedVariable,
$actualType
);

if (!$succeeded) {
throw new ExpectationException(
'Unable to cast ' . $expected . ' to ' . $actualType,
$this->getSession()->getDriver()
);
}
}

$this->assert(
$castedExpectedVariable === $actual,
gettype($castedExpectedVariable) === gettype($actual),
"Type of element '$actual' is not equal to '$expected'"
);
break;
Expand Down

0 comments on commit 1a93713

Please sign in to comment.