diff --git a/composer.json b/composer.json index c9438f6..be3ec00 100644 --- a/composer.json +++ b/composer.json @@ -58,7 +58,7 @@ }, "autoload-dev": { "psr-4": { - "AtolOnline\\Tests\\": "tests/" + "AtolOnlineTests\\": "tests/" } }, "scripts": { diff --git a/src/Api/AtolClient.php b/src/Api/AtolClient.php index 81e58f0..213e790 100644 --- a/src/Api/AtolClient.php +++ b/src/Api/AtolClient.php @@ -29,7 +29,7 @@ abstract class AtolClient /** * @var bool Флаг тестового режима */ - protected bool $test_mode = true; + protected bool $test_mode; /** * @var Client HTTP-клиент для работы с API @@ -59,6 +59,7 @@ abstract class AtolClient /** * Конструктор * + * @param bool $test_mode * @param string|null $login * @param string|null $password * @param array $config @@ -69,6 +70,7 @@ abstract class AtolClient * @see https://guzzle.readthedocs.io/en/latest/request-options.html Допустимые параметры для $config */ public function __construct( + bool $test_mode = true, ?string $login = null, ?string $password = null, array $config = [] @@ -76,8 +78,9 @@ abstract class AtolClient $this->http = new Client(array_merge($config, [ 'http_errors' => $config['http_errors'] ?? false, ])); - $login && $this->setLogin($login); - $password && $this->setPassword($password); + $this->setTestMode($test_mode); + !is_null($login) && $this->setLogin($login); + !is_null($password) && $this->setPassword($password); } /** @@ -96,7 +99,7 @@ abstract class AtolClient * @param bool $test_mode * @return $this */ - public function setTestMode(bool $test_mode): self + public function setTestMode(bool $test_mode = true): self { $this->test_mode = $test_mode; return $this; @@ -139,7 +142,7 @@ abstract class AtolClient * * @return string|null */ - protected function getLogin(): ?string + public function getLogin(): ?string { return $this->login; } @@ -169,7 +172,7 @@ abstract class AtolClient * * @return string|null */ - protected function getPassword(): ?string + public function getPassword(): ?string { return $this->password; } diff --git a/tests/AtolClientTest.php b/tests/AtolClientTest.php deleted file mode 100644 index b5f387d..0000000 --- a/tests/AtolClientTest.php +++ /dev/null @@ -1,48 +0,0 @@ - false]))->request('GET', $url); + //$this->assertEquals(200, $result->getStatusCode()); + return $result->getStatusCode() === $code; + } + + /** + * Проверяет доступность API мониторинга + * + * @return bool + * @throws GuzzleException + */ + protected function isMonitoringOnline(): bool + { + return $this->ping('https://testonline.atol.ru/api/auth/v1/gettoken', 400); + } + + /** + * Пропускает текущий тест если API мониторинга недоступно + * + * @throws GuzzleException + */ + protected function skipIfMonitoringIsOffline(): void + { + if (!$this->isMonitoringOnline()) { + $this->markTestSkipped($this->getName() . ': Monitoring API is inaccessible. Skipping test.'); + } } /** @@ -50,6 +79,79 @@ class BasicTestCase extends TestCase } } + /** + * Тестирует идентичность двух классов + * + * @param object|string $expected + * @param object|string $actual + */ + public function assertIsSameClass(object|string $expected, object|string $actual) + { + $this->assertEquals( + is_object($expected) ? $expected::class : $expected, + is_object($actual) ? $actual::class : $actual + ); + } + + /** + * Тестирует наследование класса (объекта) от указанных классов + * + * @param string[] $parents + * @param object|string $actual + */ + public function assertExtendsClasses(array $parents, object|string $actual) + { + $this->checkClassesIntersection($parents, $actual, 'class_parents'); + } + + /** + * Тестирует имплементацию классом (объектом) указанных интерфейсов + * + * @param string[] $parents + * @param object|string $actual + */ + public function assertImplementsInterfaces(array $parents, object|string $actual) + { + $this->checkClassesIntersection($parents, $actual, 'class_implements'); + } + + /** + * Тестирует использование классом (объектом) указанных трейтов + * + * @param string[] $parents + * @param object|string $actual + */ + public function assertUsesTraits(array $parents, object|string $actual) + { + $this->checkClassesIntersection($parents, $actual, 'class_uses'); + } + + /** + * Проверяет пересечение классов указанной функцией SPL + * + * @param object|string $class Класс для проверки на вхождение, или объект, класс коего нужно проверить + * @param array $classes Массив классов, вхождение в который нужно проверить + * @param string $function class_parents|class_implements|class_uses + */ + protected function checkClassesIntersection(array $classes, object|string $class, string $function): void + { + $actual_classes = is_object($class) ? $function($class) : [$class::class]; + $this->assertIsArray($actual_classes); + $this->assertNotEmpty(array_intersect($classes, $actual_classes)); + } + + /** + * Тестирует, является ли объект коллекцией + * + * @param mixed $expected + */ + public function assertIsCollection(mixed $expected): void + { + $this->assertIsObject($expected); + $this->assertIsIterable($expected); + $this->assertIsSameClass($expected, Collection::class); + } + /** * Провайдер валидных телефонов * diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 8447c27..be2ce7a 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -7,7 +7,7 @@ * https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE */ -namespace AtolOnline\Tests; +namespace AtolOnlineTests; use AtolOnline\{ Entities\Client, @@ -16,7 +16,8 @@ use AtolOnline\{ Exceptions\TooLongEmailException, Exceptions\TooLongNameException, Exceptions\TooLongPhoneException, - Helpers}; + Helpers +}; /** * Набор тестов для проверки работы класс покупателя diff --git a/tests/CompanyTest.php b/tests/CompanyTest.php index cad4c86..7b8e20c 100644 --- a/tests/CompanyTest.php +++ b/tests/CompanyTest.php @@ -7,7 +7,7 @@ * https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE */ -namespace AtolOnline\Tests; +namespace AtolOnlineTests; use AtolOnline\{ Constants\SnoTypes, diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index 1259886..7715516 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -7,7 +7,7 @@ * https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE */ -namespace AtolOnline\Tests; +namespace AtolOnlineTests; use AtolOnline\Helpers; diff --git a/tests/ItemTest_todo.php b/tests/ItemTest_todo.php index 59a8288..88a4088 100644 --- a/tests/ItemTest_todo.php +++ b/tests/ItemTest_todo.php @@ -7,7 +7,7 @@ * https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE */ -namespace AtolOnline\Tests; +namespace AtolOnlineTests; use AtolOnline\{ Constants\PaymentMethods, diff --git a/tests/KktMonitorTest.php b/tests/KktMonitorTest.php index 4d5fe7c..fbedd50 100644 --- a/tests/KktMonitorTest.php +++ b/tests/KktMonitorTest.php @@ -1,40 +1,163 @@ assertIsObject($client); + $this->assertIsSameClass(KktMonitor::class, $client); + $this->assertExtendsClasses([AtolClient::class], $client); + $this->assertNull($client->getLogin()); + $this->assertNull($client->getPassword()); + } - public function testSetToken() + /** + * Тестирует успешное создание объекта монитора с аргументами конструктора + * + * @covers \AtolOnline\Api\KktMonitor::__construct + * @covers \AtolOnline\Api\KktMonitor::setLogin + * @covers \AtolOnline\Api\KktMonitor::setPassword + * @covers \AtolOnline\Api\KktMonitor::getLogin + * @covers \AtolOnline\Api\KktMonitor::getPassword + */ + public function testConstructorWithArgs() + { + $client = new KktMonitor(false, 'login', 'password', []); + $this->assertIsObject($client); + $this->assertIsSameClass(KktMonitor::class, $client); + $this->assertExtendsClasses([AtolClient::class], $client); + //$this->assertFalse($client->isTestMode()); + //$this->assertEquals('login', $client->getLogin()); + //$this->assertEquals('password', $client->getPassword()); + } + + /** + * Тестирует исключение при попытке передать пустой логин в конструктор + * + * @covers \AtolOnline\Api\KktMonitor::__construct + * @covers \AtolOnline\Api\KktMonitor::setLogin + */ + public function testConstructorWithShortLogin() + { + $this->expectException(EmptyLoginException::class); + new KktMonitor(login: ''); + } + + /** + * Тестирует исключение при попытке передать слишком длинный логин в конструктор + * + * @covers \AtolOnline\Api\KktMonitor::__construct + * @covers \AtolOnline\Api\KktMonitor::setLogin + */ + public function testConstructorWithLongLogin() + { + $this->expectException(TooLongLoginException::class); + new KktMonitor(login: Helpers::randomStr(101)); + } + + /** + * Тестирует исключение при попытке передать пустой пароль в конструктор + * + * @covers \AtolOnline\Api\KktMonitor::__construct + * @covers \AtolOnline\Api\KktMonitor::setPassword + */ + public function testConstructorWithShortPassword() + { + $this->expectException(EmptyPasswordException::class); + new KktMonitor(password: ''); + } + + /** + * Тестирует исключение при попытке передать слишком длинный пароль в конструктор + * + * @covers \AtolOnline\Api\KktMonitor::__construct + * @covers \AtolOnline\Api\KktMonitor::setPassword + */ + public function testConstructorWithLongPassword() + { + $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 + */ + public function testTestMode() + { + $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()); + } + + public function todo_testGetToken() { } - public function testGetResponse() + public function todo_testGetResponse() + { + //$this->skipIfMonitoringIsOffline(); + } + + public function todo_testSetPassword() { } - public function testSetLogin() + public function todo_testAuth() { } - public function testAuth() + public function todo_testGetAll() { } - public function testGetToken() + public function todo_testSetToken() { } - public function testSetPassword() + public function todo_testGetOne() + { + } + + public function todo_testSetLogin() { } } diff --git a/tests/VatTest_todo.php b/tests/VatTest_todo.php index 5810438..b65fee9 100644 --- a/tests/VatTest_todo.php +++ b/tests/VatTest_todo.php @@ -7,11 +7,12 @@ * https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE */ -namespace AtolOnline\Tests; +namespace AtolOnlineTests; use AtolOnline\{ Constants\VatTypes, - Entities\Vat}; + Entities\Vat +}; /** * Class VatTest