Skip to content

Commit b3ba0ac

Browse files
committed
Merge pull request #34 from php-http/callback_request_matcher
Add callback request matcher
2 parents 75531c4 + 662f2c9 commit b3ba0ac

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace spec\Http\Message\RequestMatcher;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use Psr\Http\Message\RequestInterface;
8+
9+
class CallbackRequestMatcherSpec extends ObjectBehavior
10+
{
11+
function let()
12+
{
13+
$this->beConstructedWith(function () {});
14+
}
15+
16+
function it_is_initializable()
17+
{
18+
$this->shouldHaveType('Http\Message\RequestMatcher\CallbackRequestMatcher');
19+
}
20+
21+
function it_is_a_request_matcher()
22+
{
23+
$this->shouldImplement('Http\Message\RequestMatcher');
24+
}
25+
26+
function it_matches_a_request(RequestInterface $request)
27+
{
28+
$callback = function () { return true; };
29+
30+
$this->beConstructedWith($callback);
31+
32+
$this->matches($request)->shouldReturn(true);
33+
}
34+
35+
function it_does_not_match_a_request(RequestInterface $request)
36+
{
37+
$callback = function () { return false; };
38+
39+
$this->beConstructedWith($callback);
40+
41+
$this->matches($request)->shouldReturn(false);
42+
}
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Http\Message\RequestMatcher;
4+
5+
use Http\Message\RequestMatcher;
6+
use Psr\Http\Message\RequestInterface;
7+
8+
/**
9+
* Match a request with a callback.
10+
*
11+
* @author Márk Sági-Kazár <[email protected]>
12+
*/
13+
final class CallbackRequestMatcher implements RequestMatcher
14+
{
15+
/**
16+
* @var callable
17+
*/
18+
private $callback;
19+
20+
/**
21+
* @param callable $callback
22+
*/
23+
public function __construct(callable $callback)
24+
{
25+
$this->callback = $callback;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function matches(RequestInterface $request)
32+
{
33+
return (bool) call_user_func($this->callback, $request);
34+
}
35+
}

0 commit comments

Comments
 (0)