Skip to content
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
42 changes: 37 additions & 5 deletions Http/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Psr\Log\LoggerAwareTrait;
use React\Promise\Deferred;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -397,7 +398,7 @@ public function attachFile($key, $filepath, $mimetype = null, $clientName = null
*/
public function onSuccess(callable $callable)
{
$this->getPromise()->then($callable);
$this->consumePromise($this->getPromise()->then($callable));
return $this;
}

Expand All @@ -410,7 +411,7 @@ public function onSuccess(callable $callable)
*/
public function onError(callable $callable)
{
$this->getPromise()->then(null, $callable);
$this->consumePromise($this->getPromise()->then(null, $callable));
return $this;
}

Expand All @@ -423,7 +424,22 @@ public function onError(callable $callable)
*/
public function onProgress(callable $callable)
{
$this->getPromise()->then(null, null, $callable);
$promise = $this->getPromise();

if (method_exists(object_or_class: $promise, method: 'progress')) {
$progressPromise = $promise->progress($callable);

if ($progressPromise instanceof PromiseInterface) {
$this->consumePromise($progressPromise);
}

return $this;
}

$this->logger?->warning(
'Statement::onProgress() is ignored because the installed react/promise runtime does not support progress callbacks.'
);

return $this;
}

Expand All @@ -437,13 +453,29 @@ public function onProgress(callable $callable)
public function onFinish(callable $callable)
{
if (method_exists(object_or_class: $this->getPromise(), method: 'finally')) {
$this->getPromise()->finally($callable);
$this->consumePromise($this->getPromise()->finally($callable));
} else {
$this->getPromise()->always($callable);
$this->consumePromise($this->getPromise()->always($callable));
}
return $this;
}

/**
* React Promise v3 reports unhandled rejections on discarded child promises.
*/
private function consumePromise(PromiseInterface $promise): void
{
if (method_exists(object_or_class: $promise, method: 'catch')) {
$promise->catch(static function (): void {
});

return;
}

$promise->otherwise(static function (): void {
});
}


/**
* @param string $consumerKey
Expand Down
93 changes: 93 additions & 0 deletions Tests/Unit/StatementPromiseHandlersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace evaisse\SimpleHttpBundle\Tests\Unit;

use evaisse\SimpleHttpBundle\Http\Statement;
use PHPUnit\Framework\TestCase;
use Psr\Log\AbstractLogger;
use ReflectionProperty;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class StatementPromiseHandlersTest extends TestCase
{
private function createLogger(): AbstractLogger
{
return new class extends AbstractLogger {
/** @var string[] */
public array $warnings = [];

public function log($level, $message, array $context = []): void
{
if ($level === 'warning') {
$this->warnings[] = (string) $message;
}
}
};
}

public function testProxiedHandlersConsumeRejectedChildPromises(): void
{
$logger = $this->createLogger();

$statement = new Statement(
Request::create('/status/404', 'GET'),
new EventDispatcher(),
);
$statement->setLogger($logger);

$events = [];

$statement
->onSuccess(static function () use (&$events): void {
$events[] = 'success';
})
->onError(static function () use (&$events): void {
$events[] = 'error';
})
->onFinish(static function () use (&$events): void {
$events[] = 'done';
});

$statement->setError(new NotFoundHttpException('Not found'));

self::assertSame(['error', 'done'], $events);
self::assertSame([], $logger->warnings);
}

public function testOnProgressIsCompatibleAcrossPromiseVersions(): void
{
$logger = $this->createLogger();
$statement = new Statement(
Request::create('/status/200', 'GET'),
new EventDispatcher(),
);
$statement->setLogger($logger);

$progressEvents = [];

self::assertSame(
$statement,
$statement->onProgress(static function ($payload) use (&$progressEvents): void {
$progressEvents[] = $payload;
})
);

$deferredProperty = new ReflectionProperty(Statement::class, 'deferred');
$deferred = $deferredProperty->getValue($statement);

if (method_exists($statement->getPromise(), 'progress') && method_exists($deferred, 'notify')) {
$deferred->notify('tick');

self::assertSame(['tick'], $progressEvents);
self::assertSame([], $logger->warnings);

return;
}

self::assertSame([], $progressEvents);
self::assertCount(1, $logger->warnings);
self::assertStringContainsString('onProgress() is ignored', $logger->warnings[0]);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"homepage": "https://github.com/evaisse/SimpleHttpBundle",
"require": {
"php": "^8.1",
"react/promise": "^2.11",
"react/promise": "^2.2 || ^3.0",
"twig/twig": "^3.27",
"symfony/http-foundation": "^5.4 || ^6.0 || ^7.0",
"symfony/browser-kit": "^5.4 || ^6.0 || ^7.0",
Expand Down
Loading