2020-01-11 06:30:25 +00:00
|
|
|
|
<?php
|
2021-11-18 04:24:30 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
|
2020-01-11 06:30:25 +00:00
|
|
|
|
*
|
|
|
|
|
* This code is licensed under MIT.
|
|
|
|
|
* Этот код распространяется по лицензии MIT.
|
|
|
|
|
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
|
|
|
|
|
*/
|
|
|
|
|
|
2021-11-18 04:24:30 +00:00
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
2021-11-23 17:30:54 +00:00
|
|
|
|
namespace AtolOnline\Tests;
|
2021-11-18 04:24:30 +00:00
|
|
|
|
|
2021-12-06 08:14:19 +00:00
|
|
|
|
use AtolOnline\Collections\EntityCollection;
|
2020-04-17 12:10:50 +00:00
|
|
|
|
use AtolOnline\Entities\Entity;
|
2021-12-07 12:04:03 +00:00
|
|
|
|
use AtolOnline\Entities\Item;
|
|
|
|
|
use AtolOnline\Entities\Payment;
|
|
|
|
|
use AtolOnline\Entities\Vat;
|
|
|
|
|
use AtolOnline\Enums\PaymentTypes;
|
|
|
|
|
use AtolOnline\Enums\VatTypes;
|
|
|
|
|
use AtolOnline\Exceptions\EmptyItemNameException;
|
|
|
|
|
use AtolOnline\Exceptions\InvalidEnumValueException;
|
|
|
|
|
use AtolOnline\Exceptions\NegativeItemPriceException;
|
|
|
|
|
use AtolOnline\Exceptions\NegativeItemQuantityException;
|
|
|
|
|
use AtolOnline\Exceptions\NegativePaymentSumException;
|
|
|
|
|
use AtolOnline\Exceptions\TooHighItemPriceException;
|
|
|
|
|
use AtolOnline\Exceptions\TooHighPaymentSumException;
|
|
|
|
|
use AtolOnline\Exceptions\TooLongItemNameException;
|
|
|
|
|
use AtolOnline\Exceptions\TooManyException;
|
2021-11-22 06:51:10 +00:00
|
|
|
|
use AtolOnline\Helpers;
|
2021-12-06 08:14:19 +00:00
|
|
|
|
use Exception;
|
2021-11-18 11:07:32 +00:00
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
|
|
|
use Illuminate\Support\Collection;
|
2020-01-11 06:30:25 +00:00
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-23 17:30:54 +00:00
|
|
|
|
* Базовый класс для тестов
|
2020-01-11 06:30:25 +00:00
|
|
|
|
*/
|
|
|
|
|
class BasicTestCase extends TestCase
|
|
|
|
|
{
|
2021-12-07 12:04:03 +00:00
|
|
|
|
//------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
// Методы для управления тестами, использующими тестовый АТОЛ API
|
|
|
|
|
//------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
2020-01-11 06:30:25 +00:00
|
|
|
|
/**
|
2021-11-18 11:07:32 +00:00
|
|
|
|
* Проверяет наличие подключения к ресурсу по URL
|
|
|
|
|
*
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @param int $code
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function ping(string $url, int $code): bool
|
|
|
|
|
{
|
2021-11-19 10:42:14 +00:00
|
|
|
|
try {
|
|
|
|
|
$result = (new Client([
|
|
|
|
|
'http_errors' => false,
|
|
|
|
|
'timeout' => 3,
|
|
|
|
|
]))->request('GET', $url);
|
|
|
|
|
} catch (GuzzleException) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-11-18 11:07:32 +00:00
|
|
|
|
return $result->getStatusCode() === $code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Проверяет доступность API мониторинга
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
2020-01-11 06:30:25 +00:00
|
|
|
|
*/
|
2021-11-18 11:07:32 +00:00
|
|
|
|
protected function isMonitoringOnline(): bool
|
2020-01-11 06:30:25 +00:00
|
|
|
|
{
|
2021-11-18 11:07:32 +00:00
|
|
|
|
return $this->ping('https://testonline.atol.ru/api/auth/v1/gettoken', 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-20 15:38:19 +00:00
|
|
|
|
* Пропускает текущий тест если API мониторинга недоступен
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
|
|
|
|
protected function skipIfMonitoringIsOffline(): void
|
|
|
|
|
{
|
|
|
|
|
if (!$this->isMonitoringOnline()) {
|
|
|
|
|
$this->markTestSkipped($this->getName() . ': Monitoring API is inaccessible. Skipping test.');
|
|
|
|
|
}
|
2020-01-11 06:30:25 +00:00
|
|
|
|
}
|
2021-11-18 04:24:30 +00:00
|
|
|
|
|
2021-12-07 12:04:03 +00:00
|
|
|
|
//------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
// Дополнительные ассерты
|
|
|
|
|
//------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
2020-01-11 06:30:25 +00:00
|
|
|
|
/**
|
2021-11-18 04:24:30 +00:00
|
|
|
|
* Тестирует является ли объект приводимым к json-строке согласно схеме АТОЛ Онлайн
|
|
|
|
|
*
|
2021-12-06 08:14:19 +00:00
|
|
|
|
* @param Entity|EntityCollection $entity
|
2021-11-27 09:59:50 +00:00
|
|
|
|
* @param array|null $json_structure
|
2021-11-18 04:24:30 +00:00
|
|
|
|
* @covers \AtolOnline\Entities\Entity::__toString
|
2021-12-12 03:09:12 +00:00
|
|
|
|
* @covers \AtolOnline\Entities\Entity::toArray
|
2021-12-06 08:14:19 +00:00
|
|
|
|
* @covers \AtolOnline\Entities\Entity::jsonSerialize
|
|
|
|
|
* @covers \AtolOnline\Collections\EntityCollection::jsonSerialize
|
|
|
|
|
* @throws Exception
|
2020-01-11 06:30:25 +00:00
|
|
|
|
*/
|
2021-12-07 12:04:03 +00:00
|
|
|
|
public function assertIsAtolable(Entity|EntityCollection $entity, ?array $json_structure = null): void
|
2020-01-11 06:30:25 +00:00
|
|
|
|
{
|
2021-11-23 17:30:54 +00:00
|
|
|
|
$this->assertIsArray($entity->jsonSerialize());
|
2021-12-12 03:09:12 +00:00
|
|
|
|
$this->assertIsArray($entity->toArray());
|
|
|
|
|
$this->assertEquals($entity->jsonSerialize(), $entity->toArray());
|
2021-11-18 04:24:30 +00:00
|
|
|
|
$this->assertIsString((string)$entity);
|
2020-01-11 06:30:25 +00:00
|
|
|
|
$this->assertJson((string)$entity);
|
2021-11-27 09:59:50 +00:00
|
|
|
|
if (!is_null($json_structure)) {
|
2021-11-18 04:24:30 +00:00
|
|
|
|
$this->assertEquals(json_encode($json_structure), (string)$entity);
|
|
|
|
|
}
|
2020-01-11 06:30:25 +00:00
|
|
|
|
}
|
2021-11-18 04:24:30 +00:00
|
|
|
|
|
2021-12-07 12:04:03 +00:00
|
|
|
|
//------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
// Ассерты проверки наследования
|
2021-12-06 06:15:47 +00:00
|
|
|
|
//------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
2021-11-18 11:07:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* Тестирует идентичность двух классов
|
|
|
|
|
*
|
2021-12-06 06:15:47 +00:00
|
|
|
|
* @param object|string $expected Ожидаемый класс
|
|
|
|
|
* @param object|string $actual Фактический класс
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function assertIsSameClass(object|string $expected, object|string $actual): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
2021-12-06 06:15:47 +00:00
|
|
|
|
$this->assertTrue($this->checkisSameClass($expected, $actual));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Проверяет идентичность двух классов
|
|
|
|
|
*
|
|
|
|
|
* @param object|string $class1
|
|
|
|
|
* @param object|string $class2
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function checkisSameClass(object|string $class1, object|string $class2): bool
|
|
|
|
|
{
|
|
|
|
|
return (is_object($class1) ? $class1::class : $class1)
|
|
|
|
|
=== (is_object($class2) ? $class2::class : $class2);
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует наследование класса (объекта) от указанных классов
|
|
|
|
|
*
|
2021-12-06 06:15:47 +00:00
|
|
|
|
* @param array $expected Массив ожидаемых имён классов-родителей
|
|
|
|
|
* @param object|string $actual Объект или имя класса для проверки
|
|
|
|
|
*/
|
|
|
|
|
public function assertExtendsClasses(array $expected, object|string $actual): void
|
|
|
|
|
{
|
|
|
|
|
$this->assertTrue($this->checkExtendsClasses($expected, $actual));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Проверяет наследование класса (объекта) от указанных классов
|
|
|
|
|
*
|
|
|
|
|
* @param string[] $parents Имена классов-родителей
|
|
|
|
|
* @param object|string $class Объект или имя класса для проверки
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-12-06 06:15:47 +00:00
|
|
|
|
private function checkExtendsClasses(array $parents, object|string $class): bool
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
2021-12-06 06:15:47 +00:00
|
|
|
|
return !empty(array_intersect($parents, is_object($class) ? class_parents($class) : [$class]));
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует имплементацию классом (объектом) указанных интерфейсов
|
|
|
|
|
*
|
2021-12-06 06:15:47 +00:00
|
|
|
|
* @param string[] $expected Массив ожидаемых имён интерфейсов
|
|
|
|
|
* @param object|string $actual Объект или имя класса для проверки
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-12-06 06:15:47 +00:00
|
|
|
|
public function assertImplementsInterfaces(array $expected, object|string $actual): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
2021-12-06 06:15:47 +00:00
|
|
|
|
$this->assertTrue($this->checkImplementsInterfaces($expected, $actual));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Проверяет имплементацию классом (объектом) указанных интерфейсов
|
|
|
|
|
*
|
|
|
|
|
* @param string[] $interfaces Имена классов-интерфейсов
|
|
|
|
|
* @param object|string $class Объект или имя класса для проверки
|
|
|
|
|
* @see https://www.php.net/manual/ru/function.class-implements.php
|
|
|
|
|
*/
|
|
|
|
|
private function checkImplementsInterfaces(array $interfaces, object|string $class): bool
|
|
|
|
|
{
|
|
|
|
|
return !empty(array_intersect($interfaces, is_object($class) ? class_implements($class) : [$class]));
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует использование классом (объектом) указанных трейтов
|
|
|
|
|
*
|
2021-12-06 06:15:47 +00:00
|
|
|
|
* @param string[] $expected Массив ожидаемых имён трейтов
|
|
|
|
|
* @param object|string $actual Объект или имя класса для проверки
|
|
|
|
|
*/
|
|
|
|
|
public function assertUsesTraits(array $expected, object|string $actual): void
|
|
|
|
|
{
|
|
|
|
|
$this->assertTrue($this->checkUsesTraits($expected, $actual));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Проверяет использование классом (объектом) указанных трейтов (исключает родителей)
|
|
|
|
|
*
|
|
|
|
|
* @param string[] $traits Массив ожидаемых имён трейтов
|
|
|
|
|
* @param object|string $class Объект или имя класса для проверки
|
|
|
|
|
* @return bool
|
|
|
|
|
* @see https://www.php.net/manual/ru/function.class-uses.php#110752
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-12-06 06:15:47 +00:00
|
|
|
|
private function checkUsesTraits(array $traits, object|string $class): bool
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
2021-12-06 06:15:47 +00:00
|
|
|
|
$found_traits = [];
|
|
|
|
|
$check_class = is_object($class) ? $class::class : $class;
|
|
|
|
|
do {
|
|
|
|
|
$found_traits = array_merge(class_uses($check_class, true), $found_traits);
|
|
|
|
|
} while ($check_class = get_parent_class($check_class));
|
|
|
|
|
foreach ($found_traits as $trait => $same) {
|
|
|
|
|
$found_traits = array_merge(class_uses($trait, true), $found_traits);
|
|
|
|
|
}
|
|
|
|
|
return !empty(array_intersect(array_unique($found_traits), $traits));
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует, является ли объект коллекцией
|
|
|
|
|
*
|
2021-12-06 06:15:47 +00:00
|
|
|
|
* @param mixed $value
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-12-06 06:15:47 +00:00
|
|
|
|
public function assertIsCollection(mixed $value): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
2021-12-06 06:15:47 +00:00
|
|
|
|
$this->assertIsObject($value);
|
|
|
|
|
$this->assertIsIterable($value);
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
$this->checkisSameClass(Collection::class, $value) ||
|
|
|
|
|
$this->checkExtendsClasses([Collection::class], $value)
|
|
|
|
|
);
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-07 12:04:03 +00:00
|
|
|
|
//------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
// Провайдеры данных для прогона тестов
|
2021-11-23 17:30:54 +00:00
|
|
|
|
//------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Провайдер строк, которые приводятся к null
|
|
|
|
|
*
|
2021-12-06 08:14:19 +00:00
|
|
|
|
* @return array
|
2021-11-23 17:30:54 +00:00
|
|
|
|
*/
|
|
|
|
|
public function providerNullableStrings(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
[''],
|
|
|
|
|
[' '],
|
|
|
|
|
[null],
|
|
|
|
|
["\n\r\t"],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 06:30:25 +00:00
|
|
|
|
/**
|
2021-11-18 04:24:30 +00:00
|
|
|
|
* Провайдер валидных телефонов
|
2020-01-11 06:30:25 +00:00
|
|
|
|
*
|
2021-11-18 04:24:30 +00:00
|
|
|
|
* @return array<array<string, string>>
|
2020-01-11 06:30:25 +00:00
|
|
|
|
*/
|
2021-11-18 04:24:30 +00:00
|
|
|
|
public function providerValidPhones(): array
|
2020-01-11 06:30:25 +00:00
|
|
|
|
{
|
2021-11-18 04:24:30 +00:00
|
|
|
|
return [
|
|
|
|
|
['+79991234567', '+79991234567'],
|
|
|
|
|
['79991234567', '+79991234567'],
|
|
|
|
|
['89991234567', '+89991234567'],
|
|
|
|
|
['+7 999 123 45 67', '+79991234567'],
|
|
|
|
|
['+7 (999) 123-45-67', '+79991234567'],
|
|
|
|
|
["+7 %(?9:9\"9')abc\r123\n45\t67\0", '+79991234567'],
|
|
|
|
|
];
|
2020-01-11 06:30:25 +00:00
|
|
|
|
}
|
2021-11-18 04:24:30 +00:00
|
|
|
|
|
2021-11-23 17:30:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* Провайдер телефонов, которые приводятся к null
|
|
|
|
|
*
|
|
|
|
|
* @return array<array<string>>
|
|
|
|
|
*/
|
|
|
|
|
public function providerNullablePhones(): array
|
|
|
|
|
{
|
|
|
|
|
return array_merge(
|
|
|
|
|
$this->providerNullableStrings(),
|
|
|
|
|
[
|
|
|
|
|
[Helpers::randomStr(10, false)],
|
|
|
|
|
["asdfgvs \n\rtt\t*/(*&%^*$%"],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 14:24:15 +00:00
|
|
|
|
/**
|
2021-11-18 04:24:30 +00:00
|
|
|
|
* Провайдер валидных email-ов
|
2020-05-29 14:24:15 +00:00
|
|
|
|
*
|
2021-11-18 04:24:30 +00:00
|
|
|
|
* @return array<array<string>>
|
2020-05-29 14:24:15 +00:00
|
|
|
|
*/
|
2021-11-18 04:24:30 +00:00
|
|
|
|
public function providerValidEmails(): array
|
2020-05-29 14:24:15 +00:00
|
|
|
|
{
|
2021-11-18 04:24:30 +00:00
|
|
|
|
return [
|
|
|
|
|
['abc@mail.com'],
|
|
|
|
|
['abc-d@mail.com'],
|
|
|
|
|
['abc.def@mail.com'],
|
|
|
|
|
['abc.def@mail.org'],
|
|
|
|
|
['abc.def@mail-archive.com'],
|
|
|
|
|
];
|
2020-05-29 14:24:15 +00:00
|
|
|
|
}
|
2021-11-23 17:30:54 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Провайдер невалидных email-ов
|
|
|
|
|
*
|
|
|
|
|
* @return array<array<string>>
|
|
|
|
|
*/
|
|
|
|
|
public function providerInvalidEmails(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
['@example'],
|
|
|
|
|
[Helpers::randomStr(15)],
|
|
|
|
|
['@example.com'],
|
|
|
|
|
['abc.def@mail'],
|
|
|
|
|
['.abc@mail.com'],
|
|
|
|
|
['example@example'],
|
|
|
|
|
['abc..def@mail.com'],
|
|
|
|
|
['abc.def@mail..com'],
|
|
|
|
|
['abc.def@mail#archive.com'],
|
|
|
|
|
];
|
|
|
|
|
}
|
2021-12-07 12:04:03 +00:00
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
// Генераторы тестовых объектов
|
|
|
|
|
//------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Генерирует массив тестовых объектов предметов расчёта
|
|
|
|
|
*
|
|
|
|
|
* @param int $count
|
|
|
|
|
* @return Item[]
|
|
|
|
|
* @throws EmptyItemNameException
|
|
|
|
|
* @throws NegativeItemPriceException
|
|
|
|
|
* @throws NegativeItemQuantityException
|
|
|
|
|
* @throws TooHighItemPriceException
|
|
|
|
|
* @throws TooLongItemNameException
|
|
|
|
|
* @throws TooManyException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function generateItemObjects(int $count = 1): array
|
|
|
|
|
{
|
|
|
|
|
$result = [];
|
|
|
|
|
for ($i = 0; $i < abs($count); ++$i) {
|
|
|
|
|
$result[] = new Item(Helpers::randomStr(), random_int(1, 100), random_int(1, 10));
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Генерирует массив тестовых объектов оплаты
|
|
|
|
|
*
|
|
|
|
|
* @param int $count
|
|
|
|
|
* @return Payment[]
|
|
|
|
|
* @throws InvalidEnumValueException
|
|
|
|
|
* @throws NegativePaymentSumException
|
|
|
|
|
* @throws TooHighPaymentSumException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function generatePaymentObjects(int $count = 1): array
|
|
|
|
|
{
|
|
|
|
|
$types = PaymentTypes::toArray();
|
|
|
|
|
$result = [];
|
|
|
|
|
for ($i = 0; $i < abs($count); ++$i) {
|
|
|
|
|
$result[] = new Payment(
|
|
|
|
|
array_values($types)[random_int(min($types), max($types))],
|
|
|
|
|
random_int(1, 100) * 2 / 3
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Генерирует массив тестовых объектов ставок НДС
|
|
|
|
|
*
|
|
|
|
|
* @param int $count
|
|
|
|
|
* @return Vat[]
|
|
|
|
|
* @throws InvalidEnumValueException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function generateVatObjects(int $count = 1): array
|
|
|
|
|
{
|
|
|
|
|
$types = VatTypes::toArray();
|
|
|
|
|
$result = [];
|
|
|
|
|
for ($i = 0; $i < abs($count); ++$i) {
|
|
|
|
|
$result[] = new Vat(
|
|
|
|
|
array_values($types)[random_int(0, count($types) - 1)],
|
|
|
|
|
random_int(1, 100) * 2 / 3
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2021-11-23 17:30:54 +00:00
|
|
|
|
}
|