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
|
|
|
|
|
2020-04-17 12:10:50 +00:00
|
|
|
|
use AtolOnline\Entities\Entity;
|
2021-11-22 06:51:10 +00:00
|
|
|
|
use AtolOnline\Helpers;
|
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-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
|
|
|
|
|
2020-01-11 06:30:25 +00:00
|
|
|
|
/**
|
2021-11-18 04:24:30 +00:00
|
|
|
|
* Тестирует является ли объект приводимым к json-строке согласно схеме АТОЛ Онлайн
|
|
|
|
|
*
|
2020-04-17 12:10:50 +00:00
|
|
|
|
* @param Entity $entity
|
2021-11-18 04:24:30 +00:00
|
|
|
|
* @param array $json_structure
|
|
|
|
|
* @covers \AtolOnline\Entities\Entity::jsonSerialize
|
|
|
|
|
* @covers \AtolOnline\Entities\Entity::__toString
|
2020-01-11 06:30:25 +00:00
|
|
|
|
*/
|
2021-11-18 04:24:30 +00:00
|
|
|
|
public function assertAtolable(Entity $entity, array $json_structure = []): void
|
2020-01-11 06:30:25 +00:00
|
|
|
|
{
|
2021-11-23 17:30:54 +00:00
|
|
|
|
$this->assertIsArray($entity->jsonSerialize());
|
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-18 04:24:30 +00:00
|
|
|
|
if ($json_structure) {
|
|
|
|
|
$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-11-18 11:07:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* Тестирует идентичность двух классов
|
|
|
|
|
*
|
|
|
|
|
* @param object|string $expected
|
|
|
|
|
* @param object|string $actual
|
|
|
|
|
*/
|
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-11-22 06:51:10 +00:00
|
|
|
|
$this->assertTrue(Helpers::isSameClass($expected, $actual));
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует наследование класса (объекта) от указанных классов
|
|
|
|
|
*
|
2021-11-22 06:51:10 +00:00
|
|
|
|
* @param object|string $class
|
2021-11-18 11:07:32 +00:00
|
|
|
|
* @param string[] $parents
|
|
|
|
|
*/
|
2021-11-22 06:51:10 +00:00
|
|
|
|
public function assertExtendsClasses(object|string $class, array $parents): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
2021-11-22 06:51:10 +00:00
|
|
|
|
$this->assertTrue(Helpers::checkExtendsClasses($class, $parents));
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует имплементацию классом (объектом) указанных интерфейсов
|
|
|
|
|
*
|
2021-11-22 06:51:10 +00:00
|
|
|
|
* @param object|string $class
|
|
|
|
|
* @param string[] $interfaces
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-11-22 06:51:10 +00:00
|
|
|
|
public function assertImplementsInterfaces(object|string $class, array $interfaces): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
2021-11-22 06:51:10 +00:00
|
|
|
|
$this->assertTrue(Helpers::checkImplementsInterfaces($class, $interfaces));
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует использование классом (объектом) указанных трейтов
|
|
|
|
|
*
|
2021-11-22 06:51:10 +00:00
|
|
|
|
* @param object|string $class
|
|
|
|
|
* @param string[] $traits
|
2021-11-18 11:07:32 +00:00
|
|
|
|
*/
|
2021-11-22 06:51:10 +00:00
|
|
|
|
public function assertUsesTraits(object|string $class, array $traits): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
2021-11-22 06:51:10 +00:00
|
|
|
|
$this->assertTrue(Helpers::checkUsesTraits($traits, $class));
|
2021-11-18 11:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует, является ли объект коллекцией
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $expected
|
|
|
|
|
*/
|
|
|
|
|
public function assertIsCollection(mixed $expected): void
|
|
|
|
|
{
|
|
|
|
|
$this->assertIsObject($expected);
|
|
|
|
|
$this->assertIsIterable($expected);
|
|
|
|
|
$this->assertIsSameClass($expected, Collection::class);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 17:30:54 +00:00
|
|
|
|
//------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Провайдер строк, которые приводятся к null
|
|
|
|
|
*
|
|
|
|
|
* @return array<array<string|null>>
|
|
|
|
|
*/
|
|
|
|
|
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'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|