Skip to content

Commit

Permalink
Change FailResult::$errorCode type to int|null (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Feb 10, 2025
1 parent c5d0aeb commit 1a73d86
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
- Enh #138: Remove unused private properties in `PsrTransport` class.
- New #139: Add `RawValue` value processor.
- Chg #139: Change result of `MethodInterface::getResultType()` to `ValueProcessorInterface`.
- Enh #140: Extract core code from `TelegramBotApi` to internal `Api` class.
- Enh #140: Extract core code from `TelegramBotApi` to internal `Api` class.
- Chg #143: Change `FailResult::$errorCode` type to `int|null`.

## 0.6.0 January 24, 2025

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
"php-http/multipart-stream-builder": "^1.4.2",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.1",
"psr/http-message": "^1.1|^2.0"
"psr/http-message": "^1.1 || ^2.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.2",
"httpsoft/http-message": "^1.1.6",
"php-http/curl-client": "^2.3.3",
"phpunit/phpunit": "^10.5.44",
"phpunit/phpunit": "^10.5.45",
"psr/log": "^3.0.2",
"yiisoft/test-support": "^3.0.1"
},
Expand Down
5 changes: 4 additions & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function array_key_exists;
use function is_array;
use function is_bool;
use function is_int;
use function is_string;
use function json_decode;

Expand Down Expand Up @@ -146,7 +147,9 @@ private function prepareFailResult(
? $decodedBody['description']
: null,
ResponseParameters::fromDecodedBody($decodedBody),
$decodedBody['error_code'] ?? null,
(isset($decodedBody['error_code']) && is_int($decodedBody['error_code']))
? $decodedBody['error_code']
: null,
);
}
}
2 changes: 1 addition & 1 deletion src/FailResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function __construct(
public ApiResponse $response,
public ?string $description = null,
public ?ResponseParameters $parameters = null,
public mixed $errorCode = null,
public ?int $errorCode = null,
) {}
}
19 changes: 17 additions & 2 deletions tests/TelegramBotApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,14 @@ public function testCallFail(bool $useLogger): void
{
$logger = $useLogger ? new SimpleLogger() : null;
$method = new GetMe();
$response = new ApiResponse(200, '{"ok":false,"description":"test error"}');
$response = new ApiResponse(200, '{"ok":false,"description":"test error","error_code":400}');
$api = $this->createApi($response, logger: $logger);

$result = $api->call($method);

$this->assertInstanceOf(FailResult::class, $result);
$this->assertSame('test error', $result->description);
$this->assertSame(400, $result->errorCode);

if ($useLogger) {
$this->assertSame(
Expand All @@ -174,12 +175,13 @@ public function testCallFail(bool $useLogger): void
'message' => 'On "getMe" request Telegram Bot API returned fail result.',
'context' => [
'type' => 3,
'payload' => '{"ok":false,"description":"test error"}',
'payload' => '{"ok":false,"description":"test error","error_code":400}',
'method' => $method,
'response' => $response,
'decodedResponse' => [
'ok' => false,
'description' => 'test error',
'error_code' => 400,
],
],
],
Expand All @@ -188,6 +190,7 @@ public function testCallFail(bool $useLogger): void
);
}
}

public function testCallFailWithInvalidDescription(): void
{
$method = new GetMe();
Expand All @@ -200,6 +203,18 @@ public function testCallFailWithInvalidDescription(): void
$this->assertNull($result->description);
}

public function testCallFailWithInvalidErrorCode(): void
{
$method = new GetMe();
$response = new ApiResponse(200, '{"ok":false,"error_code":"2"}');
$api = $this->createApi($response);

$result = $api->call($method);

$this->assertInstanceOf(FailResult::class, $result);
$this->assertNull($result->errorCode);
}

public function testSuccessResponseWithoutResult(): void
{
$api = $this->createApi([
Expand Down
23 changes: 20 additions & 3 deletions tests/Type/InputFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use HttpSoft\Message\StreamFactory;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Throwable;
use Vjik\TelegramBot\Api\Type\InputFile;

final class InputFileTest extends TestCase
Expand Down Expand Up @@ -47,8 +48,24 @@ public function testFromLocalFileWithName(): void

public function testFromLocalNotExistsFile(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unable to open file "not-exists".');
@InputFile::fromLocalFile('not-exists');
$errorMessage = null;
set_error_handler(
static function (int $code, string $message) use (&$errorMessage): bool {
$errorMessage = $message;
return true;
},
);

$exception = null;
try {
InputFile::fromLocalFile('not-exists');
} catch (Throwable $exception) {
}

restore_error_handler();

$this->assertSame('fopen(not-exists): Failed to open stream: No such file or directory', $errorMessage);
$this->assertInstanceOf(RuntimeException::class, $exception);
$this->assertSame('Unable to open file "not-exists".', $exception->getMessage());
}
}
2 changes: 1 addition & 1 deletion tools/composer-require-checker/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require-dev": {
"maglnet/composer-require-checker": "^4.15"
"maglnet/composer-require-checker": "^4.16.1"
},
"config": {
"bump-after-update": "dev"
Expand Down
2 changes: 1 addition & 1 deletion tools/psalm/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require-dev": {
"vimeo/psalm": "^6.3"
"vimeo/psalm": "^6.5.1"
},
"config": {
"bump-after-update": "dev"
Expand Down

0 comments on commit 1a73d86

Please sign in to comment.