Туча доработок

- класс `PayingAgent`, покрытый тестами
- новые константы для тегов ФФД 1.05 `Ffd105Tags`
- `Entity::jsonSerialize()` object -> array (again)
- `TooManyException::$max` int -> float
- тесты по psr-4, потому что почему бы и нет
- некоторые провайдеры вынесены в `BasicTestCase`
- улучшен тест покупателя
This commit is contained in:
2021-11-24 01:30:54 +08:00
parent b5a01debd2
commit 42d194116f
40 changed files with 622 additions and 192 deletions

View File

@@ -7,7 +7,7 @@
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
namespace AtolOnlineTests;
namespace AtolOnline\Tests\Api;
use AtolOnline\Api\AtolClient;
use AtolOnline\Api\KktMonitor;
@@ -22,6 +22,7 @@ use AtolOnline\Exceptions\TooLongLoginException;
use AtolOnline\Exceptions\TooLongPasswordException;
use AtolOnline\Helpers;
use AtolOnline\TestEnvParams;
use AtolOnline\Tests\BasicTestCase;
use GuzzleHttp\Exception\GuzzleException;
/**

View File

@@ -9,7 +9,7 @@
declare(strict_types = 1);
namespace AtolOnlineTests;
namespace AtolOnline\Tests;
use AtolOnline\Entities\Entity;
use AtolOnline\Helpers;
@@ -19,7 +19,7 @@ use Illuminate\Support\Collection;
use PHPUnit\Framework\TestCase;
/**
* Class BasicTestCase
* Базовый класс для тестов
*/
class BasicTestCase extends TestCase
{
@@ -73,8 +73,7 @@ class BasicTestCase extends TestCase
*/
public function assertAtolable(Entity $entity, array $json_structure = []): void
{
$this->assertIsObject($entity);
$this->assertIsObject($entity->jsonSerialize());
$this->assertIsArray($entity->jsonSerialize());
$this->assertIsString((string)$entity);
$this->assertJson((string)$entity);
if ($json_structure) {
@@ -138,6 +137,23 @@ class BasicTestCase extends TestCase
$this->assertIsSameClass($expected, Collection::class);
}
//------------------------------------------------------------------------------------------------------------------
/**
* Провайдер строк, которые приводятся к null
*
* @return array<array<string|null>>
*/
public function providerNullableStrings(): array
{
return [
[''],
[' '],
[null],
["\n\r\t"],
];
}
/**
* Провайдер валидных телефонов
*
@@ -155,6 +171,22 @@ class BasicTestCase extends TestCase
];
}
/**
* Провайдер телефонов, которые приводятся к null
*
* @return array<array<string>>
*/
public function providerNullablePhones(): array
{
return array_merge(
$this->providerNullableStrings(),
[
[Helpers::randomStr(10, false)],
["asdfgvs \n\rtt\t*/(*&%^*$%"],
]
);
}
/**
* Провайдер валидных email-ов
*
@@ -170,4 +202,24 @@ class BasicTestCase extends TestCase
['abc.def@mail-archive.com'],
];
}
}
/**
* Провайдер невалидных 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'],
];
}
}

View File

@@ -7,7 +7,7 @@
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
namespace AtolOnlineTests;
namespace AtolOnline\Tests\Entities;
use AtolOnline\{
Entities\Client,
@@ -16,91 +16,28 @@ use AtolOnline\{
Exceptions\TooLongClientContactException,
Exceptions\TooLongClientNameException,
Exceptions\TooLongEmailException,
Exceptions\TooLongItemNameException,
Helpers};
Helpers,
Tests\BasicTestCase
};
/**
* Набор тестов для проверки работы класс покупателя
* Набор тестов для проверки работы класса покупателя
*/
class ClientTest extends BasicTestCase
{
/**
* Провайдер строк, которые приводятся к null
*
* @return array<array<string|null>>
*/
public function providerNullableStrings(): array
{
return [
[''],
[' '],
[null],
["\n\r\t"],
];
}
/**
* Провайдер телефонов, которые приводятся к null
*
* @return array<array<string>>
*/
public function providerNullablePhones(): array
{
return array_merge(
$this->providerNullableStrings(),
[
[Helpers::randomStr(10, false)],
["asdfgvs \n\rtt\t*/(*&%^*$%"],
]
);
}
/**
* Провайдер невалидных 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'],
];
}
//------------------------------------------------------------------------------------------------------------------
/**
* Тестирует приведение покупателя к json
*
* @covers \AtolOnline\Entities\Client
* @covers \AtolOnline\Entities\Client::jsonSerialize
*/
public function testAtolable(): void
{
$this->assertAtolable(new Client());
}
/**
* Тестирует конструктор покупателя без передачи значений
* Тестирует конструктор без передачи значений и приведение к json
*
* @covers \AtolOnline\Entities\Client
* @covers \AtolOnline\Entities\Client::jsonSerialize
*/
public function testConstructorWithoutArgs(): void
{
$this->assertEquals('{}', (string)(new Client()));
$this->assertEquals('[]', (string)(new Client()));
}
/**
* Тестирует конструктор с передачей значений (внутри работают сеттеры)
* Тестирует конструктор с передачей значений и приведение к json
*
* @covers \AtolOnline\Entities\Client
* @covers \AtolOnline\Entities\Client::jsonSerialize
@@ -108,16 +45,23 @@ class ClientTest extends BasicTestCase
* @covers \AtolOnline\Entities\Client::setPhone
* @covers \AtolOnline\Entities\Client::setEmail
* @covers \AtolOnline\Entities\Client::setInn
* @covers \AtolOnline\Entities\Client::getName
* @covers \AtolOnline\Entities\Client::getPhone
* @covers \AtolOnline\Entities\Client::getEmail
* @covers \AtolOnline\Entities\Client::getInn
*/
public function testConstructorWithArgs(): void
{
$customer = new Client(
$this->assertAtolable(new Client('John Doe'), ['name' => 'John Doe']);
$this->assertAtolable(new Client(email: 'john@example.com'), ['email' => 'john@example.com']);
$this->assertAtolable(new Client(phone: '+1/22/99*73s dsdas654 5s6'), ['phone' => '+122997365456']);
$this->assertAtolable(new Client(inn: '+fasd3\qe3fs_=nac99013928czc'), ['inn' => '3399013928']);
$this->assertAtolable(new Client(
'John Doe',
'john@example.com',
'+1/22/99*73s dsdas654 5s6', // +122997365456
'+fasd3\qe3fs_=nac99013928czc' // 3399013928
);
$this->assertAtolable($customer, [
), [
'name' => 'John Doe',
'email' => 'john@example.com',
'phone' => '+122997365456',
@@ -125,10 +69,8 @@ class ClientTest extends BasicTestCase
]);
}
//------------------------------------------------------------------------------------------------------------------
/**
* Тестирует установку имён покупателя, которые приводятся к null
* Тестирует установку имён, которые приводятся к null
*
* @param mixed $name
* @dataProvider providerNullableStrings
@@ -139,27 +81,25 @@ class ClientTest extends BasicTestCase
*/
public function testNullableNames(mixed $name): void
{
$customer = (new Client())->setName($name);
$this->assertNull($customer->getName());
$this->assertNull((new Client())->setName($name)->getName());
}
/**
* Тестирует установку валидного имени покупателя
* Тестирует установку валидного имени
*
* @covers \AtolOnline\Entities\Client
* @covers \AtolOnline\Entities\Client::setName
* @covers \AtolOnline\Entities\Client::getName
* @throws TooLongItemNameException
* @throws TooLongClientNameException
*/
public function testValidName(): void
{
$name = Helpers::randomStr();
$customer = (new Client())->setName($name);
$this->assertEquals($name, $customer->getName());
$this->assertEquals($name, (new Client())->setName($name)->getName());
}
/**
* Тестирует установку невалидного имени покупателя
* Тестирует установку невалидного имени
*
* @covers \AtolOnline\Entities\Client
* @covers \AtolOnline\Entities\Client::setName
@@ -174,7 +114,7 @@ class ClientTest extends BasicTestCase
//------------------------------------------------------------------------------------------------------------------
/**
* Тестирует установку телефонов покупателя, которые приводятся к null
* Тестирует установку телефонов, которые приводятся к null
*
* @param mixed $phone
* @dataProvider providerNullablePhones
@@ -185,13 +125,13 @@ class ClientTest extends BasicTestCase
*/
public function testNullablePhones(mixed $phone): void
{
$customer = (new Client())->setPhone($phone);
$this->assertNull($customer->getPhone());
$this->assertNull((new Client())->setPhone($phone)->getPhone());
}
/**
* Тестирует установку валидного телефона покупателя
* Тестирует установку валидного телефона
*
* @todo актуализировать при доработатанной валидации
* @dataProvider providerValidPhones
* @covers \AtolOnline\Entities\Client
* @covers \AtolOnline\Entities\Client::setPhone
@@ -200,13 +140,13 @@ class ClientTest extends BasicTestCase
*/
public function testValidPhone(string $input, string $output): void
{
$customer = (new Client())->setPhone($input);
$this->assertEquals($output, $customer->getPhone());
$this->assertEquals($output, (new Client())->setPhone($input)->getPhone());
}
/**
* Тестирует установку невалидного телефона покупателя
* Тестирует установку невалидного телефона
*
* @todo актуализировать при доработатанной валидации
* @covers \AtolOnline\Entities\Client
* @covers \AtolOnline\Entities\Client::setPhone
* @covers \AtolOnline\Exceptions\TooLongClientContactException
@@ -221,7 +161,7 @@ class ClientTest extends BasicTestCase
//------------------------------------------------------------------------------------------------------------------
/**
* Тестирует установку валидных email-ов покупателя
* Тестирует установку валидных email-ов
*
* @param mixed $email
* @dataProvider providerValidEmails
@@ -233,12 +173,11 @@ class ClientTest extends BasicTestCase
*/
public function testValidEmails(mixed $email): void
{
$customer = (new Client())->setEmail($email);
$this->assertEquals($email, $customer->getEmail());
$this->assertEquals($email, (new Client())->setEmail($email)->getEmail());
}
/**
* Тестирует установку слишком длинного email покупателя
* Тестирует установку слишком длинного email
*
* @covers \AtolOnline\Entities\Client
* @covers \AtolOnline\Entities\Client::setEmail
@@ -253,7 +192,7 @@ class ClientTest extends BasicTestCase
}
/**
* Тестирует установку невалидного email покупателя
* Тестирует установку невалидного email
*
* @param mixed $email
* @dataProvider providerInvalidEmails
@@ -281,10 +220,8 @@ class ClientTest extends BasicTestCase
*/
public function testValidInn(): void
{
$customer = (new Client())->setInn('1234567890');
$this->assertEquals('1234567890', $customer->getInn());
$customer = $customer->setInn('123456789012');
$this->assertEquals('123456789012', $customer->getInn());
$this->assertEquals('1234567890', (new Client())->setInn('1234567890')->getInn());
$this->assertEquals('123456789012', (new Client())->setInn('123456789012')->getInn());
}
/**

View File

@@ -7,7 +7,7 @@
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
namespace AtolOnlineTests;
namespace AtolOnline\Tests\Entities;
use AtolOnline\{
Entities\Company,
@@ -18,7 +18,9 @@ use AtolOnline\{
Exceptions\InvalidPaymentAddressException,
Exceptions\TooLongEmailException,
Exceptions\TooLongPaymentAddressException,
Helpers};
Helpers,
Tests\BasicTestCase
};
/**
* Набор тестов для проверки работы класс продавца
@@ -131,4 +133,4 @@ class CompanyTest extends BasicTestCase
$this->expectException(InvalidPaymentAddressException::class);
new Company('company@example.com', SnoTypes::OSN, '1234567890', '');
}
}
}

View File

@@ -9,11 +9,12 @@
declare(strict_types = 1);
namespace AtolOnlineTests;
namespace AtolOnline\Tests\Entities;
use AtolOnline\Entities\Kkt;
use AtolOnline\Exceptions\EmptyMonitorDataException;
use AtolOnline\Exceptions\NotEnoughMonitorDataException;
use AtolOnline\Tests\BasicTestCase;
use DateTime;
use Exception;

View File

@@ -0,0 +1,145 @@
<?php
/*
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
*
* This code is licensed under MIT.
* Этот код распространяется по лицензии MIT.
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
namespace AtolOnline\Tests\Entities;
use AtolOnline\{
Entities\PayingAgent,
Exceptions\InvalidPhoneException,
Exceptions\TooLongPayingAgentOperationException,
Helpers,
Tests\BasicTestCase
};
/**
* Набор тестов для проверки работы класса платёжного агента
*/
class PayingAgentTest extends BasicTestCase
{
/**
* Тестирует конструктор без передачи значений и корректное приведение к json
*
* @covers \AtolOnline\Entities\PayingAgent
* @covers \AtolOnline\Entities\PayingAgent::jsonSerialize
*/
public function testConstructorWithoutArgs(): void
{
$this->assertEquals('[]', (string)(new PayingAgent()));
}
/**
* Тестирует конструктор с передачей значений и корректное приведение к json
*
* @covers \AtolOnline\Entities\PayingAgent
* @covers \AtolOnline\Entities\PayingAgent::jsonSerialize
* @covers \AtolOnline\Entities\PayingAgent::setOperation
* @covers \AtolOnline\Entities\PayingAgent::setPhones
* @covers \AtolOnline\Entities\PayingAgent::getOperation
* @covers \AtolOnline\Entities\PayingAgent::getPhones
* @throws InvalidPhoneException
* @throws TooLongPayingAgentOperationException
*/
public function testConstructorWithArgs(): void
{
$operation = Helpers::randomStr();
$this->assertAtolable(new PayingAgent(
$operation,
['+122997365456'],
), [
'operation' => $operation,
'phones' => ['+122997365456'],
]);
$this->assertAtolable(
new PayingAgent($operation),
['operation' => $operation]
);
$this->assertAtolable(
new PayingAgent(phones: ['+122997365456']),
['phones' => ['+122997365456']]
);
}
/**
* Тестирует установку операций, которые приводятся к null
*
* @param mixed $name
* @dataProvider providerNullableStrings
* @covers \AtolOnline\Entities\PayingAgent
* @covers \AtolOnline\Entities\PayingAgent::setOperation
* @covers \AtolOnline\Entities\PayingAgent::getOperation
* @throws TooLongPayingAgentOperationException
*/
public function testNullableOperations(mixed $name): void
{
$this->assertNull((new PayingAgent())->setOperation($name)->getOperation());
}
/**
* Тестирует установку невалидного имени покупателя
*
* @covers \AtolOnline\Entities\PayingAgent
* @covers \AtolOnline\Entities\PayingAgent::setOperation
* @covers \AtolOnline\Exceptions\TooLongPayingAgentOperationException
*/
public function testTooLongPayingAgentOperationException(): void
{
$this->expectException(TooLongPayingAgentOperationException::class);
(new PayingAgent())->setOperation(Helpers::randomStr(25));
}
/**
* Провайдер массивов телефонов, которые приводятся к null
*
* @return array<array<mixed>>
*/
public function providerNullablePhonesArrays(): array
{
return [
[[]],
[null],
[collect()],
];
}
/**
* Тестирует установку пустых телефонов
*
* @dataProvider providerNullablePhonesArrays
* @covers \AtolOnline\Entities\PayingAgent
* @covers \AtolOnline\Entities\PayingAgent::setPhones
* @covers \AtolOnline\Entities\PayingAgent::getPhones
* @throws InvalidPhoneException
* @throws TooLongPayingAgentOperationException
*/
public function testNullablePhones(mixed $phones): void
{
$agent = new PayingAgent(phones: $phones);
$this->assertIsCollection($agent->getPhones());
$this->assertTrue($agent->getPhones()->isEmpty());
}
/**
* Тестирует установку невалидных телефонов
*
* @covers \AtolOnline\Entities\PayingAgent
* @covers \AtolOnline\Entities\PayingAgent::setPhones
* @covers \AtolOnline\Exceptions\InvalidPhoneException
* @throws InvalidPhoneException
*/
public function testInvalidPhoneException(): void
{
$this->expectException(InvalidPhoneException::class);
(new PayingAgent())->setPhones([
'12345678901234567', // good
'+123456789012345678', // good
'12345678901234567890', // bad
'+12345678901234567890', // bad
]);
}
}

View File

@@ -7,7 +7,7 @@
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
namespace AtolOnlineTests;
namespace AtolOnline\Tests;
use AtolOnline\Helpers;
@@ -120,4 +120,4 @@ class HelpersTest extends BasicTestCase
$this->assertEquals($output, strlen($result));
// тестировать на наличие цифр быссмысленно
}
}
}