phpspec / prophecy

Highly opinionated mocking framework for PHP 5.3+
MIT License
8.53k stars 241 forks source link

More argument tokens or integration with Hamcrest #212

Open aik099 opened 9 years ago

aik099 commented 9 years ago

The are some built-in argument tokens (see https://github.com/phpspec/prophecy/tree/master/src/Prophecy/Argument/Token), but this isn't sufficient for complex matching (e.g. regex). Maybe there is a way to integrate with Hamcrest (see https://github.com/hamcrest/hamcrest-php).

At least some cool matchers from PhpSpec (see https://github.com/phpspec/phpspec/tree/master/src/PhpSpec/Matcher) can be copied to Prophecy.

stof commented 9 years ago

What are the token you would need ?

aik099 commented 9 years ago

Right now the one, that does preg_match on the value. I had to write my own token to make this happen. Situation that potentially in every project I will be needing to drag along my custom tokens from other project to make some checks doesn't sound correctly.

stof commented 9 years ago

@aik099 or you could just use Argument::that($callback). do you often need regex-based argument matching ?

aik099 commented 9 years ago

In that particular test case quite often, because I knew approximate argument value, but not exactly.

I indeed started with

Argument::that(function ($value) use ($regex) {
    return preg_match($regex, $value);
})

solution, but that soon became not very handy to use because:

Then I've created my own token to do the matching for me and to show nice matches(regex_here) when something isn't matched in Prophecy exception message, which quite cool as well.

aik099 commented 9 years ago

The callback tokens are indeed powerful, but it takes much more code to write them and their error message is totally useless.

aik099 commented 9 years ago

Maybe a new token can be created, that would accept Hamcrest matcher. And corresponding shortcut method, e.g. Argument::hamcrestMatcher(...) that uses that token as well.

minterior commented 8 years ago

+1 to add, at least, the StringRegexMatcher.php as Argument::regex, Argument::regexString, Argument::withRegex or similar

aik099 commented 8 years ago

Here is what I'm currently using: