8000 [HttpClient] fix chaining promises returned by HttplugClient · symfony/symfony@75043a1 · GitHub
[go: up one dir, main page]

Skip to content
Sign in

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 75043a1

Browse files
CthulhuDennicolas-grekas
authored andcommitted
[HttpClient] fix chaining promises returned by HttplugClient
1 parent 1382001 commit 75043a1

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/Symfony/Component/HttpClient/Response/HttplugPromise.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpClient\Response;
1313

14+
use function GuzzleHttp\Promise\promise_for;
1415
use GuzzleHttp\Promise\PromiseInterface as GuzzlePromiseInterface;
1516
use Http\Promise\Promise as HttplugPromiseInterface;
1617
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
@@ -31,7 +32,10 @@ public function __construct(GuzzlePromiseInterface $promise)
3132

3233
public function then(callable $onFulfilled = null, callable $onRejected = null): self
3334
{
34-
return new self($this->promise->then($onFulfilled, $onRejected));
35+
return new self($this->promise->then(
36+
$this->wrapThenCallback($onFulfilled),
37+
$this->wrapThenCallback($onRejected)
38+
));
3539
}
3640

3741
public function cancel(): void
@@ -62,4 +66,15 @@ public function wait($unwrap = true)
6266

6367
return $result;
6468
}
69+
70+
private function wrapThenCallback(?callable $callback): ?callable
71+
{
72+
if (null === $callback) {
73+
return null;
74+
}
75+
76+
return static function ($value) use ($callback) {
77+
return promise_for($callback($value));
78+
};
79+
}
6580
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Tests\Response;
13+
14+
use GuzzleHttp\Promise\Promise;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\HttpClient\Response\HttplugPromise;
17+
18+
class HttplugPromiseTest extends TestCase
19+
{
20+
public function testComplexNesting()
21+
{
22+
$mkPromise = function ($result): HttplugPromise {
23+
$guzzlePromise = new Promise(function () use (&$guzzlePromise, $result) {
24+
$guzzlePromise->resolve($result);
25+
});
26+
27+
return new HttplugPromise($guzzlePromise);
28+
};
29+
30+
$promise1 = $mkPromise('result');
31+
$promise2 = $promise1->then($mkPromise);
32+
$promise3 = $promise2->then(function ($result) { return $result; });
33+
34+
$this->assertSame('result', $promise3->wait());
35+
}
36+
}

0 commit comments

Comments
 (0)
0