Skip to content

Add callback request matcher #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions spec/RequestMatcher/CallbackRequestMatcherSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace spec\Http\Message\RequestMatcher;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;

class CallbackRequestMatcherSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith(function () {});
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\RequestMatcher\CallbackRequestMatcher');
}

function it_is_a_request_matcher()
{
$this->shouldImplement('Http\Message\RequestMatcher');
}

function it_matches_a_request(RequestInterface $request)
{
$callback = function () { return true; };

$this->beConstructedWith($callback);

$this->matches($request)->shouldReturn(true);
}

function it_does_not_match_a_request(RequestInterface $request)
{
$callback = function () { return false; };

$this->beConstructedWith($callback);

$this->matches($request)->shouldReturn(false);
}
}
35 changes: 35 additions & 0 deletions src/RequestMatcher/CallbackRequestMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Http\Message\RequestMatcher;

use Http\Message\RequestMatcher;
use Psr\Http\Message\RequestInterface;

/**
* Match a request with a callback.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
final class CallbackRequestMatcher implements RequestMatcher
{
/**
* @var callable
*/
private $callback;

/**
* @param callable $callback
*/
public function __construct(callable $callback)
{
$this->callback = $callback;
}

/**
* {@inheritdoc}
*/
public function matches(RequestInterface $request)
{
return (bool) call_user_func($this->callback, $request);
}
}