Skip to content

Commit

Permalink
Matchers can now be used as callables (#40)
Browse files Browse the repository at this point in the history
Matchers can now be used as callables
  • Loading branch information
ascii-soup authored and Alexander Obuhovich committed Jul 22, 2016
1 parent 8bfb401 commit b7a5e18
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions hamcrest/Hamcrest/BaseMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public function __toString()
{
return StringDescription::toString($this);
}

public function __invoke()
{
return call_user_func_array(array($this, 'matches'), func_get_args());
}
}
30 changes: 30 additions & 0 deletions tests/Hamcrest/InvokedMatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace Hamcrest;


class SampleInvokeMatcher extends BaseMatcherTest
{
private $matchAgainst;

public function __construct($matchAgainst)
{
$this->matchAgainst = $matchAgainst;
}

public function matches($item)
{
return $item == $this->matchAgainst;
}

}

class InvokedMatcherTest extends \PHPUnit_Framework_TestCase
{
public function testInvokedMatchersCallMatches()
{
$sampleMatcher = new SampleInvokeMatcher('foo');

$this->assertTrue($sampleMatcher('foo'));
$this->assertFalse($sampleMatcher('bar'));
}
}

0 comments on commit b7a5e18

Please sign in to comment.