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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
the original error message.
- Enh #229: Replace Psalm with PHPStan for static analysis.
- Enh #230: Add PHP 8.6 support.
- Enh #231: Use `http_get_last_response_headers()` in `NativeTransport` on PHP 8.4+.
- Bug #227: Throw `InvalidArgumentException` on serialization of rich text that contains an element other than
a string, a `RichText` object or an array of them, instead of failing with `TypeError` or silently casting scalars
to strings.
Expand Down
15 changes: 12 additions & 3 deletions src/Transport/NativeTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ static function (int $errorNumber, string $errorString): bool {
*/
private function send(string $url, array $options): ApiResponse
{
global $http_response_header;
if (PHP_VERSION_ID < 80400) {
// The variable name is dynamic to avoid the deprecation of `$http_response_header` in PHP 8.5+.
$headersVariableName = 'http_response_header';
global ${$headersVariableName};
}

$options['ignore_errors'] = true;

Expand All @@ -114,12 +118,17 @@ static function (int $errorNumber, string $errorString): bool {
}

/**
* @var non-empty-list<string> $http_response_header
* @var non-empty-list<string> $headers We throw an exception on error, so the response is received and headers
* contain at least the status line.
* @see https://www.php.net/manual/function.http-get-last-response-headers.php
* @see https://www.php.net/manual/reserved.variables.httpresponseheader.php
*/
$headers = PHP_VERSION_ID < 80400
? ${$headersVariableName}
: http_get_last_response_headers();

return new ApiResponse(
$this->parseStatusCode($http_response_header),
$this->parseStatusCode($headers),
$body,
);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Transport/NativeTransport/NativeTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use function PHPUnit\Framework\assertTrue;
use function sprintf;

require_once __DIR__ . '/StreamMock/functions.php';

final class NativeTransportTest extends TestCase
{
public function testGet(): void
Expand Down
19 changes: 17 additions & 2 deletions tests/Transport/NativeTransport/StreamMock/StreamMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ final class StreamMock

private int $position = 0;

/**
* @var list<string>|null
*/
private static ?array $lastResponseHeaders = null;

/**
* @var resource
*/
Expand Down Expand Up @@ -73,6 +78,7 @@ public static function enable(array $responseHeaders = [], string $responseBody
if (file_exists(self::REQUEST_FILE)) {
unlink(self::REQUEST_FILE);
}
self::$lastResponseHeaders = null;

stream_wrapper_unregister('http');
stream_wrapper_register('http', self::class, STREAM_IS_URL);
Expand All @@ -84,6 +90,7 @@ public static function enable(array $responseHeaders = [], string $responseBody
public static function disable(): ?array
{
stream_wrapper_restore('http');
self::$lastResponseHeaders = null;

if (!file_exists(self::REQUEST_FILE)) {
return null;
Expand Down Expand Up @@ -143,10 +150,18 @@ public function stream_stat(): array
return [];
}

/**
* @return list<string>|null
*/
public static function getLastResponseHeaders(): ?array
{
return self::$lastResponseHeaders;
}

public function stream_close(): void
{
global $http_response_header;
$http_response_header = $this->config['responseHeaders'];
self::$lastResponseHeaders = $this->config['responseHeaders'];
$GLOBALS['http_response_header'] = $this->config['responseHeaders'];
}

private static function readFile(string $path): string
Expand Down
18 changes: 18 additions & 0 deletions tests/Transport/NativeTransport/StreamMock/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Phptg\BotApi\Transport;

use Phptg\BotApi\Tests\Transport\NativeTransport\StreamMock\StreamMock;

/**
* Overrides `http_get_last_response_headers()` for `NativeTransport` (PHP 8.4+), because a userland stream wrapper
* can't set the last response headers.
*
* @return list<string>|null
*/
function http_get_last_response_headers(): ?array
{
return StreamMock::getLastResponseHeaders();
}
Loading