2021-11-17 16:01:53 +00:00
|
|
|
|
<?php
|
2021-11-20 16:17:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
|
|
|
|
|
*
|
|
|
|
|
* This code is licensed under MIT.
|
|
|
|
|
* Этот код распространяется по лицензии MIT.
|
|
|
|
|
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
|
|
|
|
|
*/
|
2021-11-17 16:01:53 +00:00
|
|
|
|
|
2021-11-23 17:30:54 +00:00
|
|
|
|
namespace AtolOnline\Tests\Api;
|
2021-11-17 16:01:53 +00:00
|
|
|
|
|
2021-11-18 11:07:32 +00:00
|
|
|
|
use AtolOnline\Api\AtolClient;
|
|
|
|
|
use AtolOnline\Api\KktMonitor;
|
2021-11-19 05:42:51 +00:00
|
|
|
|
use AtolOnline\Api\KktResponse;
|
2021-11-20 15:38:19 +00:00
|
|
|
|
use AtolOnline\Entities\Kkt;
|
2021-11-19 05:42:51 +00:00
|
|
|
|
use AtolOnline\Exceptions\AuthFailedException;
|
2021-11-18 11:07:32 +00:00
|
|
|
|
use AtolOnline\Exceptions\EmptyLoginException;
|
2021-11-20 15:38:19 +00:00
|
|
|
|
use AtolOnline\Exceptions\EmptyMonitorDataException;
|
2021-11-18 11:07:32 +00:00
|
|
|
|
use AtolOnline\Exceptions\EmptyPasswordException;
|
2021-11-20 15:38:19 +00:00
|
|
|
|
use AtolOnline\Exceptions\NotEnoughMonitorDataException;
|
2021-11-18 11:07:32 +00:00
|
|
|
|
use AtolOnline\Exceptions\TooLongLoginException;
|
|
|
|
|
use AtolOnline\Exceptions\TooLongPasswordException;
|
|
|
|
|
use AtolOnline\Helpers;
|
2021-11-19 05:42:51 +00:00
|
|
|
|
use AtolOnline\TestEnvParams;
|
2021-11-23 17:30:54 +00:00
|
|
|
|
use AtolOnline\Tests\BasicTestCase;
|
2021-11-19 05:42:51 +00:00
|
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2021-11-17 16:01:53 +00:00
|
|
|
|
|
2021-11-18 11:07:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* Набор тестов для проверки работы API-клиента на примере монитора ККТ
|
|
|
|
|
*/
|
|
|
|
|
class KktMonitorTest extends BasicTestCase
|
2021-11-17 16:01:53 +00:00
|
|
|
|
{
|
2021-11-20 15:38:19 +00:00
|
|
|
|
/**
|
|
|
|
|
* Возвращает объект клиента для тестирования с тестовым API АТОЛ
|
|
|
|
|
*
|
2021-11-20 16:17:40 +00:00
|
|
|
|
* @return KktMonitor
|
2021-11-20 15:38:19 +00:00
|
|
|
|
* @throws EmptyLoginException
|
|
|
|
|
* @throws EmptyPasswordException
|
|
|
|
|
* @throws TooLongLoginException
|
|
|
|
|
* @throws TooLongPasswordException
|
|
|
|
|
*/
|
|
|
|
|
private function newTestClient(): KktMonitor
|
|
|
|
|
{
|
|
|
|
|
$credentials = TestEnvParams::FFD105();
|
|
|
|
|
return (new KktMonitor(true))
|
|
|
|
|
->setLogin($credentials['login'])
|
|
|
|
|
->setPassword($credentials['password']);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 11:07:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* Тестирует успешное создание объекта монитора без аргументов конструктора
|
|
|
|
|
*
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::__construct
|
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testConstructorWithoutArgs(): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
|
|
|
|
$client = new KktMonitor();
|
|
|
|
|
$this->assertIsObject($client);
|
|
|
|
|
$this->assertIsSameClass(KktMonitor::class, $client);
|
2021-12-06 06:15:47 +00:00
|
|
|
|
$this->assertExtendsClasses([AtolClient::class], $client);
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует успешное создание объекта монитора с аргументами конструктора
|
|
|
|
|
*
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::__construct
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::setLogin
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::getLogin
|
2021-11-19 05:42:51 +00:00
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::setPassword
|
2021-11-18 11:07:32 +00:00
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::getPassword
|
2021-11-19 10:42:14 +00:00
|
|
|
|
* @throws EmptyLoginException
|
|
|
|
|
* @throws EmptyPasswordException
|
|
|
|
|
* @throws TooLongLoginException
|
|
|
|
|
* @throws TooLongPasswordException
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testConstructorWithArgs(): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
|
|
|
|
$client = new KktMonitor(false, 'login', 'password', []);
|
|
|
|
|
$this->assertIsObject($client);
|
2021-11-22 06:51:10 +00:00
|
|
|
|
$this->assertIsSameClass($client, KktMonitor::class);
|
2021-12-06 06:15:47 +00:00
|
|
|
|
$this->assertExtendsClasses([AtolClient::class], $client);
|
2021-11-19 05:42:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует установку и возврат логина
|
|
|
|
|
*
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::__construct
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::getLogin
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::setLogin
|
2021-11-19 10:42:14 +00:00
|
|
|
|
* @throws EmptyLoginException
|
|
|
|
|
* @throws TooLongLoginException
|
2021-11-19 05:42:51 +00:00
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testLogin(): void
|
2021-11-19 05:42:51 +00:00
|
|
|
|
{
|
|
|
|
|
$client = new KktMonitor(login: 'login');
|
|
|
|
|
$this->assertEquals('login', $client->getLogin());
|
|
|
|
|
|
|
|
|
|
$client = new KktMonitor();
|
|
|
|
|
$this->assertNull($client->getLogin());
|
|
|
|
|
|
|
|
|
|
$client->setLogin('login');
|
|
|
|
|
$this->assertEquals('login', $client->getLogin());
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует исключение при попытке передать пустой логин в конструктор
|
|
|
|
|
*
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::__construct
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::setLogin
|
2021-11-19 05:42:51 +00:00
|
|
|
|
* @covers \AtolOnline\Exceptions\EmptyLoginException
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testEmptyLoginException(): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
|
|
|
|
$this->expectException(EmptyLoginException::class);
|
|
|
|
|
new KktMonitor(login: '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует исключение при попытке передать слишком длинный логин в конструктор
|
|
|
|
|
*
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::__construct
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::setLogin
|
2021-11-19 05:42:51 +00:00
|
|
|
|
* @covers \AtolOnline\Exceptions\TooLongLoginException
|
2021-11-19 10:42:14 +00:00
|
|
|
|
* @throws EmptyLoginException
|
|
|
|
|
* @throws EmptyPasswordException
|
|
|
|
|
* @throws TooLongLoginException
|
|
|
|
|
* @throws TooLongPasswordException
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testTooLongLoginException(): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
|
|
|
|
$this->expectException(TooLongLoginException::class);
|
|
|
|
|
new KktMonitor(login: Helpers::randomStr(101));
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 05:42:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Тестирует установку и возврат пароля
|
|
|
|
|
*
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::__construct
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::getPassword
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::setPassword
|
2021-11-19 10:42:14 +00:00
|
|
|
|
* @throws EmptyPasswordException
|
|
|
|
|
* @throws TooLongPasswordException
|
2021-11-19 05:42:51 +00:00
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testPassword(): void
|
2021-11-19 05:42:51 +00:00
|
|
|
|
{
|
|
|
|
|
$client = new KktMonitor(password: 'password');
|
|
|
|
|
$this->assertEquals('password', $client->getPassword());
|
|
|
|
|
|
|
|
|
|
$client = new KktMonitor();
|
|
|
|
|
$this->assertNull($client->getPassword());
|
|
|
|
|
|
|
|
|
|
$client->setPassword('password');
|
|
|
|
|
$this->assertEquals('password', $client->getPassword());
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 11:07:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* Тестирует исключение при попытке передать пустой пароль в конструктор
|
|
|
|
|
*
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::__construct
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::setPassword
|
2021-11-19 05:42:51 +00:00
|
|
|
|
* @covers \AtolOnline\Exceptions\EmptyPasswordException
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testEmptyPasswordException(): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
|
|
|
|
$this->expectException(EmptyPasswordException::class);
|
|
|
|
|
new KktMonitor(password: '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует исключение при попытке передать слишком длинный пароль в конструктор
|
|
|
|
|
*
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::__construct
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::setPassword
|
2021-11-19 10:42:14 +00:00
|
|
|
|
* @throws EmptyLoginException
|
|
|
|
|
* @throws EmptyPasswordException
|
|
|
|
|
* @throws TooLongLoginException
|
|
|
|
|
* @throws TooLongPasswordException
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testConstructorWithLongPassword(): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
|
|
|
|
$this->expectException(TooLongPasswordException::class);
|
|
|
|
|
new KktMonitor(password: Helpers::randomStr(101));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует установку тестового режима
|
|
|
|
|
*
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::__construct
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::isTestMode
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::setTestMode
|
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testTestMode(): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
|
|
|
|
$client = new KktMonitor();
|
|
|
|
|
$this->assertTrue($client->isTestMode());
|
|
|
|
|
|
|
|
|
|
$client = new KktMonitor(true);
|
|
|
|
|
$this->assertTrue($client->isTestMode());
|
|
|
|
|
|
|
|
|
|
$client = new KktMonitor(false);
|
|
|
|
|
$this->assertFalse($client->isTestMode());
|
|
|
|
|
|
|
|
|
|
$client = (new KktMonitor())->setTestMode();
|
|
|
|
|
$this->assertTrue($client->isTestMode());
|
|
|
|
|
|
|
|
|
|
$client = (new KktMonitor())->setTestMode(true);
|
|
|
|
|
$this->assertTrue($client->isTestMode());
|
|
|
|
|
|
|
|
|
|
$client = (new KktMonitor())->setTestMode(false);
|
|
|
|
|
$this->assertFalse($client->isTestMode());
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 05:42:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Тестирует авторизацию
|
|
|
|
|
*
|
|
|
|
|
* @covers \AtolOnline\Api\AtolClient::getHeaders
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::sendRequest
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::getAuthEndpoint
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::doAuth
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::auth
|
2021-11-20 15:38:19 +00:00
|
|
|
|
* @covers \AtolOnline\Exceptions\AuthFailedException
|
2021-11-19 05:42:51 +00:00
|
|
|
|
* @throws EmptyLoginException
|
|
|
|
|
* @throws EmptyPasswordException
|
|
|
|
|
* @throws TooLongLoginException
|
|
|
|
|
* @throws TooLongPasswordException
|
|
|
|
|
* @throws AuthFailedException
|
|
|
|
|
* @throws GuzzleException
|
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testAuth(): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
2021-11-19 05:42:51 +00:00
|
|
|
|
$this->skipIfMonitoringIsOffline();
|
|
|
|
|
$result = $this->newTestClient()->auth();
|
|
|
|
|
$this->assertTrue($result);
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
2021-11-17 16:01:53 +00:00
|
|
|
|
|
2021-11-19 05:42:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Тестирует возврат токена после авторизации
|
|
|
|
|
*
|
2021-11-19 10:42:14 +00:00
|
|
|
|
* @depends testAuth
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::setToken
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::getToken
|
2021-11-20 15:38:19 +00:00
|
|
|
|
* @covers \AtolOnline\Exceptions\AuthFailedException
|
2021-11-19 05:42:51 +00:00
|
|
|
|
* @throws AuthFailedException
|
|
|
|
|
* @throws EmptyLoginException
|
|
|
|
|
* @throws EmptyPasswordException
|
|
|
|
|
* @throws GuzzleException
|
|
|
|
|
* @throws TooLongLoginException
|
|
|
|
|
* @throws TooLongPasswordException
|
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testGetToken(): void
|
2021-11-17 16:01:53 +00:00
|
|
|
|
{
|
2021-11-19 05:42:51 +00:00
|
|
|
|
$client = new KktMonitor();
|
|
|
|
|
$this->assertNull($client->getToken());
|
2021-11-17 16:01:53 +00:00
|
|
|
|
|
2021-11-19 05:42:51 +00:00
|
|
|
|
$this->skipIfMonitoringIsOffline();
|
|
|
|
|
$client = $this->newTestClient();
|
|
|
|
|
$client->auth();
|
|
|
|
|
$this->assertIsString($client->getToken());
|
2021-11-17 16:01:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 05:42:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Тестирует возврат объекта последнего ответа от API
|
|
|
|
|
*
|
2021-11-19 10:42:14 +00:00
|
|
|
|
* @depends testAuth
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::getResponse
|
2021-11-20 15:38:19 +00:00
|
|
|
|
* @covers \AtolOnline\Exceptions\AuthFailedException
|
2021-11-19 05:42:51 +00:00
|
|
|
|
* @throws AuthFailedException
|
|
|
|
|
* @throws EmptyLoginException
|
|
|
|
|
* @throws EmptyPasswordException
|
|
|
|
|
* @throws GuzzleException
|
|
|
|
|
* @throws TooLongLoginException
|
|
|
|
|
* @throws TooLongPasswordException
|
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testGetResponse(): void
|
2021-11-17 16:01:53 +00:00
|
|
|
|
{
|
2021-11-19 05:42:51 +00:00
|
|
|
|
$this->skipIfMonitoringIsOffline();
|
|
|
|
|
$client = $this->newTestClient();
|
|
|
|
|
$client->auth();
|
|
|
|
|
$this->assertIsSameClass(KktResponse::class, $client->getResponse());
|
2021-11-17 16:01:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 10:42:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* [Мониторинг] Тестирует получение данных о всех ККТ
|
|
|
|
|
*
|
|
|
|
|
* @depends testAuth
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::getMainEndpoint
|
|
|
|
|
* @covers \AtolOnline\Api\AtolClient::getUrlToMethod
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::fetchAll
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::getAll
|
2021-11-20 15:38:19 +00:00
|
|
|
|
* @covers \AtolOnline\Exceptions\AuthFailedException
|
2021-11-19 10:42:14 +00:00
|
|
|
|
* @throws AuthFailedException
|
|
|
|
|
* @throws EmptyLoginException
|
|
|
|
|
* @throws EmptyPasswordException
|
|
|
|
|
* @throws GuzzleException
|
|
|
|
|
* @throws TooLongLoginException
|
|
|
|
|
* @throws TooLongPasswordException
|
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function testMonitorGetAll(): void
|
2021-11-17 16:01:53 +00:00
|
|
|
|
{
|
2021-11-19 10:42:14 +00:00
|
|
|
|
$this->skipIfMonitoringIsOffline();
|
|
|
|
|
$client = $this->newTestClient();
|
|
|
|
|
$client->auth();
|
|
|
|
|
$kkts = $client->getAll();
|
|
|
|
|
$this->assertNotEmpty($client->getResponse()->getContent());
|
|
|
|
|
$this->assertIsCollection($kkts);
|
|
|
|
|
$this->assertTrue($kkts->count() > 0);
|
2021-11-20 15:38:19 +00:00
|
|
|
|
$this->assertIsSameClass(Kkt::class, $kkts->random());
|
2021-11-17 16:01:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 10:42:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* [Мониторинг] Тестирует получение данных о конкретной ККТ
|
|
|
|
|
*
|
|
|
|
|
* @depends testAuth
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::getMainEndpoint
|
|
|
|
|
* @covers \AtolOnline\Api\AtolClient::getUrlToMethod
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::fetchOne
|
|
|
|
|
* @covers \AtolOnline\Api\KktMonitor::getOne
|
2021-11-20 15:38:19 +00:00
|
|
|
|
* @covers \AtolOnline\Entities\Kkt::__construct
|
|
|
|
|
* @covers \AtolOnline\Entities\Kkt::__get
|
|
|
|
|
* @covers \AtolOnline\Entities\Kkt::jsonSerialize
|
|
|
|
|
* @covers \AtolOnline\Entities\Kkt::__toString
|
2021-11-19 10:42:14 +00:00
|
|
|
|
* @throws AuthFailedException
|
|
|
|
|
* @throws EmptyLoginException
|
|
|
|
|
* @throws EmptyPasswordException
|
|
|
|
|
* @throws GuzzleException
|
|
|
|
|
* @throws TooLongLoginException
|
|
|
|
|
* @throws TooLongPasswordException
|
2021-11-20 15:38:19 +00:00
|
|
|
|
* @throws EmptyMonitorDataException
|
|
|
|
|
* @throws NotEnoughMonitorDataException
|
2021-11-19 10:42:14 +00:00
|
|
|
|
*/
|
2021-11-20 16:17:40 +00:00
|
|
|
|
public function testMonitorGetOne(): void
|
2021-11-17 16:01:53 +00:00
|
|
|
|
{
|
2021-11-19 10:42:14 +00:00
|
|
|
|
$this->skipIfMonitoringIsOffline();
|
|
|
|
|
$client = $this->newTestClient();
|
|
|
|
|
$client->auth();
|
2021-11-20 16:17:40 +00:00
|
|
|
|
$serial_number = $client->getAll()->first()->serialNumber;
|
2021-11-20 15:38:19 +00:00
|
|
|
|
$kkt = $client->getOne($serial_number);
|
2021-11-19 10:42:14 +00:00
|
|
|
|
$this->assertNotEmpty($client->getResponse());
|
2021-11-20 15:38:19 +00:00
|
|
|
|
$this->assertIsSameClass(Kkt::class, $kkt);
|
|
|
|
|
$this->assertAtolable($kkt);
|
|
|
|
|
$this->assertNotNull($kkt->serialNumber);
|
|
|
|
|
$this->assertEquals($serial_number, $kkt->serialNumber);
|
2021-11-17 16:01:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|