Skip to content

Navigation Menu

Sign in
Appearance settings

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

[HttpClient] fix chaining promises returned by HttplugClient #37927

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
Aug 26, 2020
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
17 changes: 16 additions & 1 deletion 17 src/Symfony/Component/HttpClient/Response/HttplugPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpClient\Response;

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

public function then(callable $onFulfilled = null, callable $onRejected = null): self
{
return new self($this->promise->then($onFulfilled, $onRejected));
return new self($this->promise->then(
$this->wrapThenCallback($onFulfilled),
$this->wrapThenCallback($onRejected)
));
}

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

return $result;
}

private function wrapThenCallback(?callable $callback): ?callable
{
if (null === $callback) {
return null;
}

return static function ($value) use ($callback) {
return promise_for($callback($value));
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpClient\Tests\Response;

use GuzzleHttp\Promise\Promise;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Response\HttplugPromise;

class HttplugPromiseTest extends TestCase
{
public function testComplexNesting()
{
$mkPromise = function ($result): HttplugPromise {
$guzzlePromise = new Promise(function () use (&$guzzlePromise, $result) {
$guzzlePromise->resolve($result);
});

return new HttplugPromise($guzzlePromise);
};

$promise1 = $mkPromise('result');
$promise2 = $promise1->then($mkPromise);
$promise3 = $promise2->then(function ($result) { return $result; });

$this->assertSame('result', $promise3->wait());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.