Skip to content

Remove PSR-7 dependency #9

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
Jan 25, 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
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
"email": "mark.sagikazar@gmail.com"
}
],
"require": {
"psr/http-message": "^1.0"
},
"require-dev": {
"phpspec/phpspec": "^2.4",
"henrikbjorn/phpspec-code-coverage" : "^1.0"
Expand Down
34 changes: 14 additions & 20 deletions spec/FulfilledPromiseSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
namespace spec\Http\Promise;

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

class FulfilledPromiseSpec extends ObjectBehavior
{
function let(ResponseInterface $response)
function let()
{
$this->beConstructedWith($response);
$this->beConstructedWith('result');
}

function it_is_initializable(ResponseInterface $response)
function it_is_initializable()
{
$this->shouldHaveType('Http\Promise\FulfilledPromise');
}
Expand All @@ -25,28 +23,24 @@ function it_is_a_promise()
$this->shouldImplement('Http\Promise\Promise');
}

function it_returns_a_fulfilled_promise(ResponseInterface $response)
function it_returns_a_fulfilled_promise()
{
$promise = $this->then(function (ResponseInterface $responseReceived) use ($response) {
if (Argument::is($responseReceived)->scoreArgument($response->getWrappedObject())) {
return $response->getWrappedObject();
}
$promise = $this->then(function ($result) {
return $result;
});

$promise->shouldHaveType('Http\Promise\Promise');
$promise->shouldHaveType('Http\Promise\FulfilledPromise');
$promise->getState()->shouldReturn(Promise::FULFILLED);
$promise->wait()->shouldReturn($response);
$promise->wait()->shouldReturn('result');
}

function it_returns_a_rejected_promise(RequestInterface $request, ResponseInterface $response)
function it_returns_a_rejected_promise()
{
$exception = new \Exception();

$promise = $this->then(function (ResponseInterface $responseReceived) use ($response, $exception) {
if (Argument::is($responseReceived)->scoreArgument($response->getWrappedObject())) {
throw $exception;
}
$promise = $this->then(function () use ($exception) {
throw $exception;
});

$promise->shouldHaveType('Http\Promise\Promise');
Expand All @@ -65,13 +59,13 @@ function it_is_in_fulfilled_state()
$this->getState()->shouldReturn(Promise::FULFILLED);
}

function it_has_a_response(ResponseInterface $response)
function it_has_a_result()
{
$this->wait()->shouldReturn($response);
$this->wait()->shouldReturn('result');
}

function it_does_not_unwrap_a_value(ResponseInterface $response)
function it_does_not_unwrap_a_value()
{
$this->wait(false)->shouldNotReturn($response);
$this->wait(false)->shouldNotReturn('result');
}
}
13 changes: 5 additions & 8 deletions spec/RejectedPromiseSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace spec\Http\Promise;

use Http\Promise\Promise;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

Expand All @@ -24,21 +23,19 @@ function it_is_a_promise()
$this->shouldImplement('Http\Promise\Promise');
}

function it_returns_a_fulfilled_promise(ResponseInterface $response)
function it_returns_a_fulfilled_promise()
{
$exception = new \Exception();
$this->beConstructedWith($exception);

$promise = $this->then(null, function (\Exception $exceptionReceived) use($exception, $response) {
if (Argument::is($exceptionReceived)->scoreArgument($exception)) {
return $response->getWrappedObject();
}
$promise = $this->then(null, function (\Exception $exceptionReceived) use($exception) {
return 'result';
});

$promise->shouldHaveType('Http\Promise\Promise');
$promise->shouldHaveType('Http\Promise\FulfilledPromise');
$promise->getState()->shouldReturn(Promise::FULFILLED);
$promise->wait()->shouldReturn($response);
$promise->wait()->shouldReturn('result');
}

function it_returns_a_rejected_promise()
Expand Down Expand Up @@ -76,7 +73,7 @@ function it_returns_an_exception()
$this->shouldThrow($exception)->duringWait();
}

function it_does_not_unwrap_a_value(ResponseInterface $response)
function it_does_not_unwrap_a_value()
{
$this->shouldNotThrow('Exception')->duringWait(false);
}
Expand Down
16 changes: 7 additions & 9 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Http\Promise;

use Psr\Http\Message\ResponseInterface;

/**
* A promise already fulfilled.
*
Expand All @@ -12,16 +10,16 @@
final class FulfilledPromise implements Promise
{
/**
* @var ResponseInterface
* @var mixed
*/
private $response;
private $result;

/**
* @param ResponseInterface $response
* @param $result
*/
public function __construct(ResponseInterface $response)
public function __construct($result)
{
$this->response = $response;
$this->result = $result;
}

/**
Expand All @@ -34,7 +32,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
}

try {
return new self($onFulfilled($this->response));
return new self($onFulfilled($this->result));
} catch (\Exception $e) {
return new RejectedPromise($e);
}
Expand All @@ -54,7 +52,7 @@ public function getState()
public function wait($unwrap = true)
{
if ($unwrap) {
return $this->response;
return $this->result;
}
}
}
4 changes: 1 addition & 3 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Http\Promise;

use Psr\Http\Message\ResponseInterface;

/**
* Promise represents a value that may not be available yet, but will be resolved at some point in future.
* It acts like a proxy to the actual value.
Expand Down Expand Up @@ -63,7 +61,7 @@ public function getState();
*
* @param bool $unwrap Whether to return resolved value / throw reason or not
*
* @return ResponseInterface|null Resolved value, null if $unwrap is set to false
* @return mixed Resolved value, null if $unwrap is set to false
*
* @throws \Exception The rejection reason if $unwrap is set to true and the request failed.
*/
Expand Down