Skip to content

Commit 886b6a5

Browse files
committed
Update async calls with new package and interface
1 parent 47198db commit 886b6a5

File tree

5 files changed

+57
-41
lines changed

5 files changed

+57
-41
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
"require": {
1414
"php": ">=5.5",
1515
"ext-curl": "*",
16-
"php-http/httplug": "^1.0.0-alpha2",
16+
"php-http/httplug": "dev-feature/promise-dep as 1.0.0",
1717
"php-http/message-factory": "^0.4@dev"
1818
},
1919
"require-dev": {
2020
"guzzlehttp/psr7": "^1.0",
21-
"php-http/adapter-integration-tests": "dev-master",
21+
"php-http/adapter-integration-tests": "dev-feature/promise-update",
2222
"php-http/discovery": "^0.3",
2323
"phpunit/phpunit": "^4.8@stable",
2424
"zendframework/zend-diactoros": "^1.0"

src/CurlPromise.php

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
namespace Http\Curl;
33

44
use Http\Client\Exception;
5-
use Http\Client\Promise;
6-
use Psr\Http\Message\ResponseInterface;
5+
use Http\Promise\Promise;
76

87
/**
98
* Promise represents a response that may not be available yet, but will be resolved at some point
@@ -81,41 +80,28 @@ public function getState()
8180
return $this->core->getState();
8281
}
8382

84-
/**
85-
* Return the value of the promise (fulfilled).
86-
*
87-
* @return ResponseInterface Response Object only when the Promise is fulfilled.
88-
*
89-
* @throws \LogicException When the promise is not fulfilled.
90-
*/
91-
public function getResponse()
92-
{
93-
return $this->core->getResponse();
94-
}
95-
96-
/**
97-
* Get the reason why the promise was rejected.
98-
*
99-
* If the exception is an instance of Http\Client\Exception\HttpException it will contain
100-
* the response object with the status code and the http reason.
101-
*
102-
* @return Exception Exception Object only when the Promise is rejected.
103-
*
104-
* @throws \LogicException When the promise is not rejected.
105-
*/
106-
public function getException()
107-
{
108-
return $this->core->getException();
109-
}
110-
11183
/**
11284
* Wait for the promise to be fulfilled or rejected.
11385
*
11486
* When this method returns, the request has been resolved and the appropriate callable has
11587
* terminated.
88+
*
89+
* @param bool $unwrap
90+
*
91+
* @return mixed
92+
*
93+
* @throws \Exception When the rejection reason is an exception.
11694
*/
117-
public function wait()
95+
public function wait($unwrap = true)
11896
{
11997
$this->runner->wait($this->core);
98+
99+
if ($unwrap) {
100+
if ($this->core->getState() == self::REJECTED) {
101+
throw $this->core->getException();
102+
}
103+
104+
return $this->core->getResponse();
105+
}
120106
}
121107
}

src/PromiseCore.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace Http\Curl;
33

44
use Http\Client\Exception;
5-
use Http\Client\Promise;
5+
use Http\Promise\Promise;
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88

@@ -192,7 +192,12 @@ public function reject(Exception $exception)
192192
{
193193
$this->exception = $exception;
194194
$this->state = Promise::REJECTED;
195-
$this->exception = $this->call($this->onRejected, $this->exception);
195+
196+
try {
197+
$this->call($this->onRejected, $this->exception);
198+
} catch (Exception $exception) {
199+
$this->exception = $exception;
200+
}
196201
}
197202

198203
/**

tests/CurlPromiseTest.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22
namespace Http\Curl\Tests;
33

4-
use Http\Client\Promise;
4+
use Http\Client\Exception\TransferException;
5+
use Http\Promise\Promise;
56
use Http\Curl\CurlPromise;
67
use Http\Curl\MultiRunner;
78

@@ -34,14 +35,38 @@ public function testCoreCalls()
3435

3536
$core->expects(static::once())->method('getState')->willReturn('STATE');
3637
static::assertEquals('STATE', $promise->getState());
38+
}
3739

40+
public function testCoreCallWaitFulfilled()
41+
{
42+
$core = $this->createPromiseCore();
43+
$runner = $this->getMockBuilder(MultiRunner::class)->disableOriginalConstructor()
44+
->setMethods(['wait'])->getMock();
45+
/** @var MultiRunner|\PHPUnit_Framework_MockObject_MockObject $runner */
46+
$promise = new CurlPromise($core, $runner);
47+
48+
$runner->expects(static::once())->method('wait')->with($core);
49+
$core->expects(static::once())->method('getState')->willReturn(Promise::FULFILLED);
3850
$core->expects(static::once())->method('getResponse')->willReturn('RESPONSE');
39-
static::assertEquals('RESPONSE', $promise->getResponse());
51+
static::assertEquals('RESPONSE', $promise->wait());
52+
}
4053

41-
$core->expects(static::once())->method('getException')->willReturn('EXCEPTION');
42-
static::assertEquals('EXCEPTION', $promise->getException());
54+
public function testCoreCallWaitRejected()
55+
{
56+
$core = $this->createPromiseCore();
57+
$runner = $this->getMockBuilder(MultiRunner::class)->disableOriginalConstructor()
58+
->setMethods(['wait'])->getMock();
59+
/** @var MultiRunner|\PHPUnit_Framework_MockObject_MockObject $runner */
60+
$promise = new CurlPromise($core, $runner);
4361

4462
$runner->expects(static::once())->method('wait')->with($core);
45-
$promise->wait();
63+
$core->expects(static::once())->method('getState')->willReturn(Promise::REJECTED);
64+
$core->expects(static::once())->method('getException')->willReturn(new TransferException());
65+
66+
try {
67+
$promise->wait();
68+
} catch (TransferException $exception) {
69+
static::assertTrue(true);
70+
}
4671
}
4772
}

tests/PromiseCoreTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use Http\Client\Exception;
55
use Http\Client\Exception\RequestException;
6-
use Http\Client\Promise;
6+
use Http\Promise\Promise;
77
use Http\Curl\PromiseCore;
88
use Psr\Http\Message\ResponseInterface;
99

@@ -56,7 +56,7 @@ public function testOnReject()
5656
$core = new PromiseCore($request, $this->handle);
5757
$core->addOnRejected(
5858
function (RequestException $exception) {
59-
return new RequestException('Foo', $exception->getRequest(), $exception);
59+
throw new RequestException('Foo', $exception->getRequest(), $exception);
6060
}
6161
);
6262

0 commit comments

Comments
 (0)