Compare commits

..

2 Commits

11 changed files with 62 additions and 87 deletions
+1 -6
View File
@@ -44,12 +44,7 @@ jobs:
coverage: xdebug
- name: Install composer dependencies
run: |
if [[ "${{ matrix.php-versions }}" == "8.5" ]]; then
composer install --prefer-dist --no-interaction --ignore-platform-req=php
else
composer install --prefer-dist --no-interaction
fi
run: composer install --prefer-dist --no-interaction
- name: PHPUnit
run: ./vendor/bin/phpunit --configuration=phpunit.xml --coverage-text
+1 -1
View File
@@ -48,7 +48,7 @@ use RuntimeException;
*/
class Api
{
public const string LIBRARY_VERSION = '1.4.0';
public const string LIBRARY_VERSION = '1.4.2';
public const string API_VERSION = '1.2.5';
+22 -1
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot;
use BushlanovDev\MaxMessengerBot\Exceptions\AttachmentNotReadyException;
use BushlanovDev\MaxMessengerBot\Exceptions\ForbiddenException;
use BushlanovDev\MaxMessengerBot\Exceptions\ClientApiException;
use BushlanovDev\MaxMessengerBot\Exceptions\MethodNotAllowedException;
@@ -252,7 +253,27 @@ final readonly class Client implements ClientApiInterface
404 => new NotFoundException($errorMessage, $errorCode, $response),
405 => new MethodNotAllowedException($errorMessage, $errorCode, $response),
429 => new RateLimitExceededException($errorMessage, $errorCode, $response),
default => new ClientApiException($errorMessage, $errorCode, $response, $statusCode),
default => $this->mapErrorCodeToException($errorMessage, $errorCode, $response, $statusCode),
};
}
/**
* @param string $message
* @param string $errorCode
* @param ResponseInterface $response
* @param int|null $httpStatusCode
*
* @return ClientApiException
*/
private function mapErrorCodeToException(
string $message,
string $errorCode,
ResponseInterface $response,
?int $httpStatusCode = null,
): ClientApiException {
return match ($errorCode) {
'attachment.not.ready' => new AttachmentNotReadyException($message, $errorCode, $response, $httpStatusCode),
default => new ClientApiException($message, $errorCode, $response, $httpStatusCode),
};
}
}
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Exceptions;
/**
* Exception thrown when an attachment is not yet ready for use.
* This typically occurs when trying to use an attachment that is still being processed.
*/
class AttachmentNotReadyException extends ClientApiException
{
}
@@ -23,9 +23,9 @@ final readonly class PhotoAttachmentRequestPayload extends AbstractAttachmentReq
#[ArrayOf(PhotoToken::class)]
public ?array $photos = null,
) {
if (count(array_filter([$this->url, $this->token, $this->photos])) !== 1) {
if ($this->url === null && $this->token === null && $this->photos === null) {
throw new InvalidArgumentException(
'Provide exactly one of "url", "token", or "photos" for PhotoAttachmentRequestPayload.'
'Provide one of "url", "token", or "photos" for PhotoAttachmentRequestPayload.'
);
}
}
@@ -19,9 +19,9 @@ final readonly class ShareAttachmentRequestPayload extends AbstractAttachmentReq
public ?string $url = null,
public ?string $token = null,
) {
if (count(array_filter([$this->url, $this->token])) !== 1) {
if ($this->url === null && $this->token === null) {
throw new InvalidArgumentException(
'Provide exactly one of "url" or "token" for ShareAttachmentRequestPayload.'
'Provide one of "url" or "token" for ShareAttachmentRequestPayload.'
);
}
}
+7
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests;
use BushlanovDev\MaxMessengerBot\Client;
use BushlanovDev\MaxMessengerBot\Exceptions\AttachmentNotReadyException;
use BushlanovDev\MaxMessengerBot\Exceptions\ClientApiException;
use BushlanovDev\MaxMessengerBot\Exceptions\ForbiddenException;
use BushlanovDev\MaxMessengerBot\Exceptions\MethodNotAllowedException;
@@ -242,6 +243,12 @@ final class ClientTest extends TestCase
public static function apiErrorProvider(): array
{
return [
'400 Attachment Not Ready' => [
400,
AttachmentNotReadyException::class,
'attachment.not.ready',
'Key: errors.process.attachment.file.not.processed',
],
'400 Bad Request' => [400, ClientApiException::class, 'bad.request', 'Invalid parameters'],
'401 Unauthorized' => [401, UnauthorizedException::class, 'verify.token', 'Invalid access_token'],
'403 Forbidden' => [403, ForbiddenException::class, 'access.denied', 'You don\'t have permissions'],
@@ -9,7 +9,6 @@ use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequ
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoToken;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
@@ -77,32 +76,12 @@ final class PhotoAttachmentRequestPayloadTest extends TestCase
$this->assertEquals($expectedArray, $payload->toArray());
}
/**
* Data provider for invalid constructor arguments.
*
* @return array<string, array{0: string|null, 1: string|null, 2: array|null}>
*/
public static function invalidPayloadProvider(): array
{
return [
'all null (no arguments)' => [null, null, null],
'url and token provided' => ['https://a.com', 'token123', null],
'url and photos provided' => ['https://a.com', null, [new PhotoToken('t')]],
'token and photos provided' => [null, 'token123', [new PhotoToken('t')]],
'all three arguments provided' => ['https://a.com', 'token123', [new PhotoToken('t')]],
];
}
#[Test]
#[DataProvider('invalidPayloadProvider')]
public function constructorThrowsExceptionForInvalidArguments(
?string $url,
?string $token,
?array $photos
): void {
public function constructorThrowsExceptionForInvalidArguments(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Provide exactly one of "url", "token", or "photos" for PhotoAttachmentRequestPayload.');
$this->expectExceptionMessage('Provide one of "url", "token", or "photos" for PhotoAttachmentRequestPayload.');
new PhotoAttachmentRequestPayload($url, $token, $photos);
new PhotoAttachmentRequestPayload(null, null, null);
}
}
@@ -38,24 +38,12 @@ final class ShareAttachmentRequestPayloadTest extends TestCase
$this->assertEquals($expectedArray, $payload->toArray());
}
/**
* @return array<string, array{0: ?string, 1: ?string}>
*/
public static function invalidPayloadProvider(): array
{
return [
'both null' => [null, null],
'both set' => ['https://a.com', 'token123'],
];
}
#[Test]
#[DataProvider('invalidPayloadProvider')]
public function constructorThrowsExceptionForInvalidArguments(?string $url, ?string $token): void
public function constructorThrowsExceptionForInvalidArguments(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Provide exactly one of "url" or "token" for ShareAttachmentRequestPayload.');
$this->expectExceptionMessage('Provide one of "url" or "token" for ShareAttachmentRequestPayload.');
new ShareAttachmentRequestPayload($url, $token);
new ShareAttachmentRequestPayload(null, null);
}
}
@@ -98,29 +98,12 @@ final class PhotoAttachmentRequestTest extends TestCase
$this->assertEquals($expectedArray, $request->toArray());
}
/**
* @return array<string, array{0: string|null, 1: string|null, 2: array|null}>
*/
public static function invalidPayloadProvider(): array
{
return [
'no arguments' => [null, null, null],
'url and token' => ['http://a.com', 'token123', null],
'token and photos' => [null, 'token123', [new PhotoToken('t')]],
'all arguments' => ['http://a.com', 'token123', [new PhotoToken('t')]],
];
}
#[Test]
#[DataProvider('invalidPayloadProvider')]
public function payloadThrowsExceptionWhenNotExactlyOneArgumentIsProvided(
?string $url,
?string $token,
?array $photos
): void {
public function payloadThrowsExceptionWhenNotExactlyOneArgumentIsProvided(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Provide exactly one of "url", "token", or "photos" for PhotoAttachmentRequestPayload.');
$this->expectExceptionMessage('Provide one of "url", "token", or "photos" for PhotoAttachmentRequestPayload.');
new PhotoAttachmentRequestPayload($url, $token, $photos);
new PhotoAttachmentRequestPayload(null, null, null);
}
}
@@ -9,7 +9,6 @@ use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\ShareAttachmentRequ
use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\ShareAttachmentRequest;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
@@ -42,21 +41,11 @@ final class ShareAttachmentRequestTest extends TestCase
$this->assertEquals($expected, $request->toArray());
}
/** @return array<string, array{0: ?string, 1: ?string}> */
public static function invalidPayloadProvider(): array
{
return [
'both null' => [null, null],
'both set' => ['https://a.com', 'token123'],
];
}
#[Test]
#[DataProvider('invalidPayloadProvider')]
public function payloadThrowsExceptionForInvalidArguments(?string $url, ?string $token): void
public function payloadThrowsExceptionForInvalidArguments(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Provide exactly one of "url" or "token" for ShareAttachmentRequestPayload.');
new ShareAttachmentRequestPayload($url, $token);
$this->expectExceptionMessage('Provide one of "url" or "token" for ShareAttachmentRequestPayload.');
new ShareAttachmentRequestPayload(null, null);
}
}