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-18 11:07:32 +00:00
|
|
|
|
namespace AtolOnlineTests;
|
2021-11-18 04:24:30 +00:00
|
|
|
|
|
2020-04-17 12:10:50 +00:00
|
|
|
|
use AtolOnline\Entities\Entity;
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class BasicTestCase
|
|
|
|
|
*/
|
|
|
|
|
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-18 04:24:30 +00:00
|
|
|
|
$this->assertIsObject($entity);
|
|
|
|
|
$this->assertIsObject($entity->jsonSerialize());
|
|
|
|
|
$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
|
|
|
|
{
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
is_object($expected) ? $expected::class : $expected,
|
|
|
|
|
is_object($actual) ? $actual::class : $actual
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует наследование класса (объекта) от указанных классов
|
|
|
|
|
*
|
|
|
|
|
* @param string[] $parents
|
|
|
|
|
* @param object|string $actual
|
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function assertExtendsClasses(array $parents, object|string $actual): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
|
|
|
|
$this->checkClassesIntersection($parents, $actual, 'class_parents');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует имплементацию классом (объектом) указанных интерфейсов
|
|
|
|
|
*
|
|
|
|
|
* @param string[] $parents
|
|
|
|
|
* @param object|string $actual
|
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function assertImplementsInterfaces(array $parents, object|string $actual): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
|
|
|
|
$this->checkClassesIntersection($parents, $actual, 'class_implements');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Тестирует использование классом (объектом) указанных трейтов
|
|
|
|
|
*
|
|
|
|
|
* @param string[] $parents
|
|
|
|
|
* @param object|string $actual
|
|
|
|
|
*/
|
2021-11-20 15:38:19 +00:00
|
|
|
|
public function assertUsesTraits(array $parents, object|string $actual): void
|
2021-11-18 11:07:32 +00:00
|
|
|
|
{
|
|
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
}
|
2020-01-11 06:30:25 +00:00
|
|
|
|
}
|