Open aik099 opened 9 years ago
What are the token you would need ?
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.
@aik099 or you could just use Argument::that($callback)
. do you often need regex-based argument matching ?
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:
exact(callback)
or similar was totally unhelpful, when something wasn't matching 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.
The callback tokens are indeed powerful, but it takes much more code to write them and their error message is totally useless.
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.
+1 to add, at least, the StringRegexMatcher.php as Argument::regex
, Argument::regexString
, Argument::withRegex
or similar
Here is what I'm currently using:
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.