Compare commits

..

1 Commits

7 changed files with 19 additions and 80 deletions
+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.1';
public const string API_VERSION = '1.2.5';
@@ -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.'
);
}
}
@@ -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);
}
}