Skip to content

Commit

Permalink
Fixes and tests (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jun 5, 2024
1 parent 9fc2a7b commit eb56d35
Show file tree
Hide file tree
Showing 278 changed files with 12,114 additions and 698 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
on:
pull_request:
push:
branches: [ 'master' ]

name: build

jobs:
tests:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- ubuntu-latest
- windows-latest

php:
- "8.2"
- "8.3"

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install PHP with extensions.
uses: shivammathur/setup-php@v2
env:
update: true
with:
php-version: ${{ matrix.php }}
ini-values: date.timezone='UTC'
coverage: xdebug
tools: composer:v2

- name: Determine composer cache directory on Linux.
if: matrix.os == 'ubuntu-latest'
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV

- name: Determine composer cache directory on Windows.
if: matrix.os == 'windows-latest'
run: echo "COMPOSER_CACHE_DIR=~\AppData\Local\Composer" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

- name: Cache dependencies installed with composer.
uses: actions/cache@v4
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
restore-keys: |
php${{ matrix.php }}-composer-
- name: Update composer.
run: composer self-update

- name: Install dependencies with composer.
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run tests with phpunit.
run: vendor/bin/phpunit --colors=always --coverage-clover=coverage.xml

- name: Upload coverage to Codecov.
if: matrix.os == 'ubuntu-latest'
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Vjik Telegram Bot API

[![Latest Stable Version](https://poser.pugx.org/vjik/_____/v/stable.png)](https://packagist.org/packages/vjik/_____)
[![Total Downloads](https://poser.pugx.org/vjik/_____/downloads.png)](https://packagist.org/packages/vjik/_____)
[![Build status](https://github.com/vjik/_____/workflows/build/badge.svg)](https://github.com/vjik/_____/actions?query=workflow%3Abuild)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fvjik%2F_____%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/vjik/_____/master)
[![type-coverage](https://shepherd.dev/github/vjik/_____/coverage.svg)](https://shepherd.dev/github/vjik/_____)
[![static analysis](https://github.com/vjik/_____/workflows/static%20analysis/badge.svg)](https://github.com/vjik/_____/actions?query=workflow%3A%22static+analysis%22)
[![psalm-level](https://shepherd.dev/github/vjik/_____/level.svg)](https://shepherd.dev/github/vjik/_____)
[![Latest Stable Version](https://poser.pugx.org/vjik/telegram-bot-api/v/stable.png)](https://packagist.org/packages/vjik/telegram-bot-api)
[![Total Downloads](https://poser.pugx.org/vjik/telegram-bot-api/downloads.png)](https://packagist.org/packages/vjik/telegram-bot-api)
[![Build status](https://github.com/vjik/php-telegram-bot-api/workflows/build/badge.svg)](https://github.com/vjik/php-telegram-bot-api/actions?query=workflow%3Abuild)
[![Code coverage](https://codecov.io/gh/vjik/php-telegram-bot-api/graph/badge.svg?token=5SV9NWKMQZ)](https://codecov.io/gh/vjik/php-telegram-bot-api)
[![type-coverage](https://shepherd.dev/github/vjik/php-telegram-bot-api/coverage.svg)](https://shepherd.dev/github/vjik/php-telegram-bot-api)
[![static analysis](https://github.com/vjik/php-telegram-bot-api/workflows/static%20analysis/badge.svg)](https://github.com/vjik/php-telegram-bot-api/actions?query=workflow%3A%22static+analysis%22)
[![psalm-level](https://shepherd.dev/github/vjik/php-telegram-bot-api/level.svg)](https://shepherd.dev/github/vjik/php-telegram-bot-api)

The package ...

Expand Down
6 changes: 6 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@
<directory suffix=".php">./src</directory>
</include>
</source>

<groups>
<exclude>
<group>realApi</group>
</exclude>
</groups>
</phpunit>
3 changes: 3 additions & 0 deletions src/Client/ApiUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Vjik\TelegramBot\Api\Client;

/**
* @see https://core.telegram.org/bots/api#making-requests
*/
final readonly class ApiUrlGenerator
{
private string $baseUrl;
Expand Down
11 changes: 8 additions & 3 deletions src/Client/PsrTelegramClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ private function createPostRequest(TelegramRequestInterface $request): HttpReque
$streamBuilder->addResource($key, json_encode($value, JSON_THROW_ON_ERROR));
}
foreach ($files as $key => $file) {
$streamBuilder->addResource($key, $file->resource, array_filter([
'filename' => $file->filename,
]));
$streamBuilder->addResource(
$key,
$file->resource,
array_filter(
['filename' => $file->filename],
static fn(mixed $value): bool => $value !== null,
)
);
}
$body = $streamBuilder->build();
$contentType = 'multipart/form-data; boundary=' . $streamBuilder->getBoundary() . '; charset=utf-8';
Expand Down
6 changes: 3 additions & 3 deletions src/FailResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
final readonly class FailResult
{
public function __construct(
public ?string $description,
public mixed $error_code,
public ?ResponseParameters $parameters,
public TelegramRequestInterface $request,
public TelegramResponse $response,
public ?string $description = null,
public ?ResponseParameters $parameters = null,
public mixed $error_code = null,
) {
}
}
11 changes: 7 additions & 4 deletions src/Method/DeleteMyCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ public function getApiMethod(): string

public function getData(): array
{
return array_filter([
'scope' => $this->scope?->toRequestArray(),
'language_code' => $this->languageCode,
]);
return array_filter(
[
'scope' => $this->scope?->toRequestArray(),
'language_code' => $this->languageCode,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): true
Expand Down
9 changes: 6 additions & 3 deletions src/Method/GetChatMenuButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ public function getApiMethod(): string

public function getData(): array
{
return array_filter([
'chat_id' => $this->chatId,
]);
return array_filter(
[
'chat_id' => $this->chatId,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): MenuButton
Expand Down
11 changes: 7 additions & 4 deletions src/Method/GetMyCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ public function getApiMethod(): string

public function getData(): array
{
return array_filter([
'scope' => $this->scope?->toRequestArray(),
'language_code' => $this->languageCode,
]);
return array_filter(
[
'scope' => $this->scope?->toRequestArray(),
'language_code' => $this->languageCode,
],
static fn(mixed $value): bool => $value !== null,
);
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/Method/GetMyDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ public function getApiMethod(): string

public function getData(): array
{
return array_filter([
'language_code' => $this->languageCode,
]);
return array_filter(
[
'language_code' => $this->languageCode,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): BotDescription
Expand Down
9 changes: 6 additions & 3 deletions src/Method/GetMyName.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ public function getApiMethod(): string

public function getData(): array
{
return array_filter([
'language_code' => $this->languageCode,
]);
return array_filter(
[
'language_code' => $this->languageCode,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): BotName
Expand Down
9 changes: 6 additions & 3 deletions src/Method/GetMyShortDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ public function getApiMethod(): string

public function getData(): array
{
return array_filter([
'language_code' => $this->languageCode,
]);
return array_filter(
[
'language_code' => $this->languageCode,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): BotShortDescription
Expand Down
35 changes: 19 additions & 16 deletions src/Method/SendLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,25 @@ public function getApiMethod(): string

public function getData(): array
{
return array_filter([
'business_connection_id' => $this->businessConnectionId,
'chat_id' => $this->chatId,
'message_thread_id' => $this->messageThreadId,
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'horizontal_accuracy' => $this->horizontalAccuracy,
'live_period' => $this->livePeriod,
'heading' => $this->heading,
'proximity_alert_radius' => $this->proximityAlertRadius,
'disable_notification' => $this->disableNotification,
'protect_content' => $this->protectContent,
'message_effect_id' => $this->messageEffectId,
'reply_parameters' => $this->replyParameters?->toRequestArray(),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
]);
return array_filter(
[
'business_connection_id' => $this->businessConnectionId,
'chat_id' => $this->chatId,
'message_thread_id' => $this->messageThreadId,
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'horizontal_accuracy' => $this->horizontalAccuracy,
'live_period' => $this->livePeriod,
'heading' => $this->heading,
'proximity_alert_radius' => $this->proximityAlertRadius,
'disable_notification' => $this->disableNotification,
'protect_content' => $this->protectContent,
'message_effect_id' => $this->messageEffectId,
'reply_parameters' => $this->replyParameters?->toRequestArray(),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): Message
Expand Down
37 changes: 20 additions & 17 deletions src/Method/SendMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,26 @@ public function getApiMethod(): string

public function getData(): array
{
return array_filter([
'business_connection_id' => $this->businessConnectionId,
'chat_id' => $this->chatId,
'message_thread_id' => $this->messageThreadId,
'text' => $this->text,
'parse_mode' => $this->parseMode,
'entities' => $this->entities === null ? null : array_map(
static fn(MessageEntity $entity) => $entity->toRequestArray(),
$this->entities,
),
'link_preview_options' => $this->linkPreviewOptions?->toRequestArray(),
'disable_notification' => $this->disableNotification,
'protect_content' => $this->protectContent,
'message_effect_id' => $this->messageEffectId,
'reply_parameters' => $this->replyParameters?->toRequestArray(),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
]);
return array_filter(
[
'business_connection_id' => $this->businessConnectionId,
'chat_id' => $this->chatId,
'message_thread_id' => $this->messageThreadId,
'text' => $this->text,
'parse_mode' => $this->parseMode,
'entities' => $this->entities === null ? null : array_map(
static fn(MessageEntity $entity) => $entity->toRequestArray(),
$this->entities,
),
'link_preview_options' => $this->linkPreviewOptions?->toRequestArray(),
'disable_notification' => $this->disableNotification,
'protect_content' => $this->protectContent,
'message_effect_id' => $this->messageEffectId,
'reply_parameters' => $this->replyParameters?->toRequestArray(),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): Message
Expand Down
50 changes: 28 additions & 22 deletions src/Method/SendPhoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,38 @@ public function getApiMethod(): string

public function getData(): array
{
return array_filter([
'chat_id' => $this->chatId,
'photo' => is_string($this->photo) ? $this->photo : null,
'business_connection_id' => $this->businessConnectionId,
'message_thread_id' => $this->messageThreadId,
'caption' => $this->caption,
'parse_mode' => $this->parseMode,
'caption_entities' => $this->captionEntities === null ? null : array_map(
static fn(MessageEntity $entity) => $entity->toRequestArray(),
$this->captionEntities,
),
'show_caption_above_media' => $this->showCaptionAboveMedia,
'has_spoiler' => $this->hasSpoiler,
'disable_notification' => $this->disableNotification,
'protect_content' => $this->protectContent,
'message_effect_id' => $this->messageEffectId,
'reply_parameters' => $this->replyParameters?->toRequestArray(),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
]);
return array_filter(
[
'chat_id' => $this->chatId,
'photo' => is_string($this->photo) ? $this->photo : null,
'business_connection_id' => $this->businessConnectionId,
'message_thread_id' => $this->messageThreadId,
'caption' => $this->caption,
'parse_mode' => $this->parseMode,
'caption_entities' => $this->captionEntities === null ? null : array_map(
static fn(MessageEntity $entity) => $entity->toRequestArray(),
$this->captionEntities,
),
'show_caption_above_media' => $this->showCaptionAboveMedia,
'has_spoiler' => $this->hasSpoiler,
'disable_notification' => $this->disableNotification,
'protect_content' => $this->protectContent,
'message_effect_id' => $this->messageEffectId,
'reply_parameters' => $this->replyParameters?->toRequestArray(),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
],
static fn(mixed $value): bool => $value !== null,
);
}

public function getFiles(): array
{
return array_filter([
'photo' => $this->photo instanceof InputFile ? $this->photo : null,
]);
return array_filter(
[
'photo' => $this->photo instanceof InputFile ? $this->photo : null,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): Message
Expand Down
Loading

0 comments on commit eb56d35

Please sign in to comment.