mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-08-20 01:53:11 +00:00
first commit
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Tests;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Api;
|
||||
use BushlanovDev\MaxMessengerBot\Client;
|
||||
use BushlanovDev\MaxMessengerBot\ClientApiInterface;
|
||||
use BushlanovDev\MaxMessengerBot\ModelFactory;
|
||||
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\Attributes\UsesClass;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionClass;
|
||||
|
||||
#[CoversClass(Api::class)]
|
||||
#[UsesClass(BotInfo::class)]
|
||||
#[UsesClass(Client::class)]
|
||||
final class ApiTest extends TestCase
|
||||
{
|
||||
private MockObject&ClientApiInterface $clientMock;
|
||||
private MockObject&ModelFactory $modelFactoryMock;
|
||||
private Api $api;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->clientMock = $this->createMock(ClientApiInterface::class);
|
||||
$this->modelFactoryMock = $this->createMock(ModelFactory::class);
|
||||
|
||||
$this->api = new Api('fake-token', $this->clientMock, $this->modelFactoryMock);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function constructorCanCreateDefaultDependencies(): void
|
||||
{
|
||||
$api = new Api('some-token');
|
||||
|
||||
$reflection = new ReflectionClass($api);
|
||||
|
||||
$clientProp = $reflection->getProperty('client');
|
||||
$clientProp->setAccessible(true);
|
||||
$this->assertInstanceOf(ClientApiInterface::class, $clientProp->getValue($api));
|
||||
|
||||
$factoryProp = $reflection->getProperty('modelFactory');
|
||||
$factoryProp->setAccessible(true);
|
||||
$this->assertInstanceOf(ModelFactory::class, $factoryProp->getValue($api));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getBotInfoCallsClientAndFactoryCorrectly(): void
|
||||
{
|
||||
$rawResponseData = ['user_id' => 123, 'first_name' => 'ApiTestBot'];
|
||||
|
||||
$expectedBotInfo = new BotInfo(
|
||||
123,
|
||||
'ApiTestBot',
|
||||
null, null, true, 0, null, null, null, null,
|
||||
);
|
||||
|
||||
$this->clientMock
|
||||
->expects($this->once())
|
||||
->method('request')
|
||||
->with('GET', '/me')
|
||||
->willReturn($rawResponseData);
|
||||
|
||||
$this->modelFactoryMock
|
||||
->expects($this->once())
|
||||
->method('createBotInfo')
|
||||
->with($rawResponseData)
|
||||
->willReturn($expectedBotInfo);
|
||||
|
||||
$result = $this->api->getBotInfo();
|
||||
|
||||
$this->assertSame($expectedBotInfo, $result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user