imbo / behat-api-extension

API extension for Behat, used to ease testing of JSON-based APIs
MIT License
107 stars 42 forks source link

Creating custom Matcher does not allow us to throw an Exception #86

Closed dFayet closed 4 years ago

dFayet commented 4 years ago

Hello,

First of all, thank you for this nice extension.

I have only worked with it recently, so this might be a question from a beginner.

After reading https://behat-api-extension.readthedocs.io/en/latest/guide/extending-the-extension.html#register-custom-matcher-functions

I've created a new matcher:

// MyContext.php

use myMatcher;
public function setArrayContainsComparator(ArrayContainsComparator $comparator)
{
    $comparator->addFunction('myMatcher', new MyMatcher());

    return parent::setArrayContainsComparator($comparator);
}
// MyMatcher.php
use InvalidArgumentException;

class MyMatcher
{
    public function __invoke($value) {
        if (!is_string($value)) {
            throw new InvalidArgumentException(sprintf(
                'Expected the value to be a string, got: %s.',
                gettype($value)
            ));
        }
    }

}

But when I run a test with an invalid value I receive the message The object in needle was not found in the object elements in the haystack. Due to https://github.com/imbo/behat-api-extension/blob/develop/src/ArrayContainsComparator.php#L252-L258

This is not that bad as the test fails. But I was expecting to see the Matcher's exception.

Is it the awaited bahavior? Did I miss something?

If no, and this is a bug/new feature, I would be more than happy to do a PR :-)

christeredvartsen commented 4 years ago

Could you paste the test from the .feature-file as well that triggers your custom matcher? That way I can add it as an actual test to the testsuite to verify the behaviour.

Btw, if you just want to test that a value in your JSON is a string value, you can use an already built in matcher:

Then the response body contains JSON:
    """
    {
      "string value": "@variableType(string)"
    }
    """

That matcher also supports other variable types: https://behat-api-extension.readthedocs.io/en/latest/guide/verify-server-response.html#variable-type-variabletype

dFayet commented 4 years ago

Here is an example

    Scenario: Example of the use case
        Given I am authenticated as "user@demo.com"
        And the "Content-Type" request header is "application/json"
        When I request "/my-awesome-endpoint" using HTTP GET
        Then the response code is 200
        And the response body contains JSON:
        """
        {
            "message": "@myMatcher()"
        }
        """

I know the variableType matcher but it allows empty ("") string. (This part is missing from my example)

christeredvartsen commented 4 years ago

Check out https://github.com/imbo/behat-api-extension/pull/87 for some more information on this.