mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-08-17 13:24:17 +00:00
Added tests
This commit is contained in:
+5
-1
@@ -106,7 +106,11 @@ class Api
|
||||
self::METHOD_POST,
|
||||
self::ACTION_SUBSCRIPTIONS,
|
||||
[],
|
||||
compact('url', 'secret', 'update_types'),
|
||||
[
|
||||
'url' => $url,
|
||||
'secret' => $secret,
|
||||
'update_types' => $update_types ? array_map(fn($type) => $type->value, $update_types) : null,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ use BushlanovDev\MaxMessengerBot\ClientApiInterface;
|
||||
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
||||
use BushlanovDev\MaxMessengerBot\ModelFactory;
|
||||
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Result;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Subscription;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
@@ -20,6 +21,7 @@ use PHPUnit\Framework\TestCase;
|
||||
use ReflectionClass;
|
||||
|
||||
#[CoversClass(Api::class)]
|
||||
#[UsesClass(Result::class)]
|
||||
#[UsesClass(Client::class)]
|
||||
#[UsesClass(BotInfo::class)]
|
||||
#[UsesClass(Subscription::class)]
|
||||
@@ -124,4 +126,92 @@ final class ApiTest extends TestCase
|
||||
$this->assertInstanceOf(Subscription::class, $result[0]);
|
||||
$this->assertSame(UpdateType::MessageCreated, $result[0]->update_types[0]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subscribeCallsClientWithAllParameters(): void
|
||||
{
|
||||
$url = 'https://example.com/webhook';
|
||||
$secret = 'secure';
|
||||
$updateTypes = [UpdateType::MessageCreated, UpdateType::BotStarted];
|
||||
$updateTypesAsStrings = array_map(fn($type) => $type->value, $updateTypes);
|
||||
|
||||
$expectedBody = [
|
||||
'url' => $url,
|
||||
'secret' => $secret,
|
||||
'update_types' => $updateTypesAsStrings,
|
||||
];
|
||||
|
||||
$rawClientResponse = ['success' => true];
|
||||
$expectedResultObject = new Result(true, null);
|
||||
|
||||
$this->clientMock
|
||||
->expects($this->once())
|
||||
->method('request')
|
||||
->with('POST', '/subscriptions', [], $expectedBody)
|
||||
->willReturn($rawClientResponse);
|
||||
|
||||
$this->modelFactoryMock
|
||||
->expects($this->once())
|
||||
->method('createResult')
|
||||
->with($rawClientResponse)
|
||||
->willReturn($expectedResultObject);
|
||||
|
||||
$result = $this->api->subscribe($url, $secret, $updateTypes);
|
||||
$this->assertSame($expectedResultObject, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subscribeHandlesOptionalParametersAsNull(): void
|
||||
{
|
||||
$url = 'https://example.com/webhook';
|
||||
|
||||
$expectedBody = [
|
||||
'url' => $url,
|
||||
'secret' => null,
|
||||
'update_types' => null,
|
||||
];
|
||||
|
||||
$rawClientResponse = ['success' => true];
|
||||
$expectedResultObject = new Result(true, null);
|
||||
|
||||
$this->clientMock
|
||||
->expects($this->once())
|
||||
->method('request')
|
||||
->with('POST', '/subscriptions', [], $expectedBody)
|
||||
->willReturn($rawClientResponse);
|
||||
|
||||
$this->modelFactoryMock
|
||||
->expects($this->once())
|
||||
->method('createResult')
|
||||
->with($rawClientResponse)
|
||||
->willReturn($expectedResultObject);
|
||||
|
||||
$result = $this->api->subscribe($url);
|
||||
$this->assertSame($expectedResultObject, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function unsubscribeCallsClientWithCorrectParameters(): void
|
||||
{
|
||||
$url = 'https://example.com/webhook';
|
||||
$expectedQueryParams = ['url' => $url];
|
||||
|
||||
$rawClientResponse = ['success' => true];
|
||||
$expectedResultObject = new Result(true, null);
|
||||
|
||||
$this->clientMock
|
||||
->expects($this->once())
|
||||
->method('request')
|
||||
->with('DELETE', '/subscriptions', $expectedQueryParams, [])
|
||||
->willReturn($rawClientResponse);
|
||||
|
||||
$this->modelFactoryMock
|
||||
->expects($this->once())
|
||||
->method('createResult')
|
||||
->with($rawClientResponse)
|
||||
->willReturn($expectedResultObject);
|
||||
|
||||
$result = $this->api->unsubscribe($url);
|
||||
$this->assertSame($expectedResultObject, $result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +116,69 @@ final class ClientTest extends TestCase
|
||||
$this->assertSame($responsePayload, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function successfulPostRequestWithJsonBody(): void
|
||||
{
|
||||
$uri = '/subscriptions';
|
||||
$requestBody = [
|
||||
'subscriptions' => [
|
||||
[
|
||||
'url' => 'https://example.com/webhook',
|
||||
'time' => 1678886400000,
|
||||
'update_types' => ['message_created'],
|
||||
'version' => '0.0.1',
|
||||
],
|
||||
],
|
||||
];
|
||||
$responsePayload = ['success' => true];
|
||||
|
||||
$expectedUrl = self::API_BASE_URL . $uri . '?' . http_build_query([
|
||||
'access_token' => self::FAKE_TOKEN,
|
||||
'v' => self::API_VERSION
|
||||
]);
|
||||
|
||||
$this->requestFactoryMock
|
||||
->expects($this->once())
|
||||
->method('createRequest')
|
||||
->with('POST', $expectedUrl)
|
||||
->willReturn($this->requestMock);
|
||||
|
||||
$this->streamFactoryMock
|
||||
->expects($this->once())
|
||||
->method('createStream')
|
||||
->with(json_encode($requestBody))
|
||||
->willReturn($this->streamMock);
|
||||
|
||||
$this->requestMock
|
||||
->expects($this->once())
|
||||
->method('withBody')
|
||||
->with($this->streamMock)
|
||||
->willReturn($this->requestMock);
|
||||
|
||||
$this->requestMock
|
||||
->expects($this->once())
|
||||
->method('withHeader')
|
||||
->with('Content-Type', 'application/json; charset=utf-8')
|
||||
->willReturn($this->requestMock);
|
||||
|
||||
$this->responseMock->method('getStatusCode')->willReturn(200);
|
||||
$this->streamMock->method('__toString')->willReturn(json_encode($responsePayload));
|
||||
|
||||
$result = $this->client->request('POST', $uri, [], $requestBody);
|
||||
$this->assertSame($responsePayload, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function handlesEmptySuccessfulResponse(): void
|
||||
{
|
||||
$this->responseMock->method('getStatusCode')->willReturn(200);
|
||||
$this->streamMock->method('__toString')->willReturn('');
|
||||
|
||||
$result = $this->client->request('DELETE', '/subscriptions');
|
||||
|
||||
$this->assertSame(['success' => true], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsNetworkExceptionOnClientError(): void
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user