Compare commits

..

2 Commits

Author SHA1 Message Date
b4af189292 Обновление зависимостей 2021-12-11 15:54:42 +08:00
6787ce3ad7 Класс документа коррекции Correction с покрытием и всякая мелочёвка
- финализация Receipt + Payment
- фиксы phpdoc
2021-12-11 15:53:57 +08:00
9 changed files with 387 additions and 40 deletions

View File

@ -74,6 +74,6 @@
}, },
"scripts": { "scripts": {
"test": "vendor/bin/phpunit --colors=always", "test": "vendor/bin/phpunit --colors=always",
"test-cov": "php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-html .coverage-report" "coverage": "php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-html .coverage"
} }
} }

12
composer.lock generated
View File

@ -1935,16 +1935,16 @@
}, },
{ {
"name": "phpspec/prophecy", "name": "phpspec/prophecy",
"version": "1.14.0", "version": "v1.15.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/phpspec/prophecy.git", "url": "https://github.com/phpspec/prophecy.git",
"reference": "d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e" "reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/phpspec/prophecy/zipball/d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e", "url": "https://api.github.com/repos/phpspec/prophecy/zipball/bbcd7380b0ebf3961ee21409db7b38bc31d69a13",
"reference": "d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e", "reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -1996,9 +1996,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/phpspec/prophecy/issues", "issues": "https://github.com/phpspec/prophecy/issues",
"source": "https://github.com/phpspec/prophecy/tree/1.14.0" "source": "https://github.com/phpspec/prophecy/tree/v1.15.0"
}, },
"time": "2021-09-10T09:02:12+00:00" "time": "2021-12-08T12:19:24+00:00"
}, },
{ {
"name": "phpunit/php-code-coverage", "name": "phpunit/php-code-coverage",

View File

@ -20,7 +20,7 @@ use Illuminate\Support\Collection;
abstract class EntityCollection extends Collection abstract class EntityCollection extends Collection
{ {
/** /**
* @return array * @inheritDoc
* @throws InvalidEntityInCollectionException * @throws InvalidEntityInCollectionException
*/ */
public function jsonSerialize(): array public function jsonSerialize(): array

View File

@ -11,18 +11,14 @@ declare(strict_types = 1);
namespace AtolOnline\Entities; namespace AtolOnline\Entities;
use AtolOnline\{ use AtolOnline\{Constants\Constraints, Enums\SnoTypes, Traits\HasEmail, Traits\HasInn};
Constants\Constraints, use AtolOnline\Exceptions\{InvalidEmailException,
Enums\SnoTypes,
Traits\HasEmail,
Traits\HasInn};
use AtolOnline\Exceptions\{
InvalidEmailException,
InvalidEnumValueException, InvalidEnumValueException,
InvalidInnLengthException, InvalidInnLengthException,
InvalidPaymentAddressException, InvalidPaymentAddressException,
TooLongEmailException, TooLongEmailException,
TooLongPaymentAddressException}; TooLongPaymentAddressException
};
use JetBrains\PhpStorm\ArrayShape; use JetBrains\PhpStorm\ArrayShape;
/** /**
@ -59,8 +55,8 @@ final class Company extends Entity
* @throws TooLongPaymentAddressException * @throws TooLongPaymentAddressException
*/ */
public function __construct( public function __construct(
string $email, string $email, //TODO сделать необязательным здесь
string $sno, string $sno, //TODO сделать необязательным здесь
string $inn, string $inn,
string $payment_address, string $payment_address,
) { ) {

215
src/Entities/Correction.php Normal file
View File

@ -0,0 +1,215 @@
<?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\Entities;
use AtolOnline\Collections\{Payments, Vats};
use AtolOnline\Constants\Constraints;
use AtolOnline\Exceptions\{InvalidEntityInCollectionException, TooLongCashierException};
use Exception;
use JetBrains\PhpStorm\ArrayShape;
/**
* Класс, описывающий документ коррекции
*
* @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 35
*/
class Correction extends Entity
{
/**
* @var Company Продавец
*/
protected Company $company;
/**
* @var string|null ФИО кассира
* @todo вынести в трейт
*/
protected ?string $cashier = null;
/**
* @var CorrectionInfo Данные коррекции
*/
protected CorrectionInfo $correction_info;
/**
* @var Payments Коллекция оплат
*/
protected Payments $payments;
/**
* @var Vats Коллекция ставок НДС
*/
protected Vats $vats;
/**
* Конструктор
*
* @param Company $company
* @param CorrectionInfo $correction_info
* @param Payments $payments
* @param Vats $vats
* @throws InvalidEntityInCollectionException
* @throws Exception
*/
public function __construct(
Company $company,
CorrectionInfo $correction_info,
Payments $payments,
Vats $vats,
) {
$this->setCompany($company)->setCorrectionInfo($correction_info)->setPayments($payments)->setVats($vats);
}
/**
* Возвращает установленного продавца
*
* @return Company
*/
public function getCompany(): Company
{
return $this->company;
}
/**
* Устанаваливает продавца
*
* @param Company $company
* @return $this
*/
public function setCompany(Company $company): self
{
$this->company = $company;
return $this;
}
/**
* Возвращает установленного кассира
*
* @return string|null
*/
public function getCashier(): ?string
{
return $this->cashier;
}
/**
* Устанаваливает кассира
*
* @param string|null $cashier
* @return $this
* @throws TooLongCashierException
*/
public function setCashier(?string $cashier): self
{
if (is_string($cashier)) {
$cashier = trim($cashier);
if (mb_strlen($cashier) > Constraints::MAX_LENGTH_CASHIER_NAME) {
throw new TooLongCashierException($cashier);
}
}
$this->cashier = $cashier ?: null;
return $this;
}
/**
* Возвращает установленные данные коррекции
*
* @return CorrectionInfo
*/
public function getCorrectionInfo(): CorrectionInfo
{
return $this->correction_info;
}
/**
* Устанавливает данные коррекции
*
* @param CorrectionInfo $correction_info
* @return Correction
*/
public function setCorrectionInfo(CorrectionInfo $correction_info): Correction
{
$this->correction_info = $correction_info;
return $this;
}
/**
* Возвращает установленную коллекцию оплат
*
* @return Payments
*/
public function getPayments(): Payments
{
return $this->payments;
}
/**
* Устанаваливает коллекцию оплат
*
* @param Payments $payments
* @return $this
* @throws InvalidEntityInCollectionException
*/
public function setPayments(Payments $payments): self
{
$payments->checkCount();
$payments->checkItemsClasses();
$this->payments = $payments;
return $this;
}
/**
* Возвращает установленную коллекцию ставок НДС
*
* @return Vats|null
*/
public function getVats(): ?Vats
{
return $this->vats ?? new Vats();
}
/**
* Устанаваливает коллекцию ставок НДС
*
* @param Vats|null $vats
* @return $this
* @throws Exception
*/
public function setVats(?Vats $vats): self
{
$vats->checkCount();
$vats->checkItemsClasses();
$this->vats = $vats;
return $this;
}
/**
* @inheritDoc
* @throws InvalidEntityInCollectionException
*/
#[ArrayShape([
'company' => "\AtolOnline\Entities\Company",
'correction_info' => "\AtolOnline\Entities\CorrectionInfo",
'payments' => "array",
'vats' => "\AtolOnline\Collections\Vats|null",
'cashier' => "null|string"
])]
public function jsonSerialize(): array
{
$json = [
'company' => $this->getCompany(),
'correction_info' => $this->getCorrectionInfo(),
'payments' => $this->getPayments()->jsonSerialize(),
'vats' => $this->getVats(),
];
!is_null($this->getCashier()) && $json['cashier'] = $this->getCashier();
return $json;
}
}

View File

@ -13,20 +13,15 @@ namespace AtolOnline\Entities;
use AtolOnline\Constants\Constraints; use AtolOnline\Constants\Constraints;
use AtolOnline\Enums\PaymentTypes; use AtolOnline\Enums\PaymentTypes;
use AtolOnline\Exceptions\{ use AtolOnline\Exceptions\{InvalidEnumValueException, NegativePaymentSumException, TooHighPaymentSumException,};
InvalidEnumValueException, use JetBrains\PhpStorm\{ArrayShape, Pure};
NegativePaymentSumException,
TooHighPaymentSumException,};
use JetBrains\PhpStorm\{
ArrayShape,
Pure};
/** /**
* Класс, описывающий оплату * Класс, описывающий оплату
* *
* @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 30 * @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 30
*/ */
class Payment extends Entity final class Payment extends Entity
{ {
/** /**
* @var int Тип оплаты * @var int Тип оплаты

View File

@ -26,7 +26,7 @@ use Exception;
* *
* @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 17 * @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 17
*/ */
class Receipt extends Entity final class Receipt extends Entity
{ {
/** /**
* @var Client Покупатель * @var Client Покупатель
@ -35,6 +35,7 @@ class Receipt extends Entity
/** /**
* @var Company Продавец * @var Company Продавец
* @todo вынести в трейт
*/ */
protected Company $company; protected Company $company;
@ -55,6 +56,7 @@ class Receipt extends Entity
/** /**
* @var Payments Коллекция оплат * @var Payments Коллекция оплат
* @todo вынести в трейт
*/ */
protected Payments $payments; protected Payments $payments;
@ -70,6 +72,7 @@ class Receipt extends Entity
/** /**
* @var string|null ФИО кассира * @var string|null ФИО кассира
* @todo вынести в трейт
*/ */
protected ?string $cashier = null; protected ?string $cashier = null;
@ -112,7 +115,7 @@ class Receipt extends Entity
* Устанаваливает покупателя * Устанаваливает покупателя
* *
* @param Client $client * @param Client $client
* @return Receipt * @return $this
*/ */
public function setClient(Client $client): self public function setClient(Client $client): self
{ {
@ -134,7 +137,7 @@ class Receipt extends Entity
* Устанаваливает продавца * Устанаваливает продавца
* *
* @param Company $company * @param Company $company
* @return Receipt * @return $this
*/ */
public function setCompany(Company $company): self public function setCompany(Company $company): self
{ {
@ -156,7 +159,7 @@ class Receipt extends Entity
* Устанаваливает агента * Устанаваливает агента
* *
* @param AgentInfo|null $agent_info * @param AgentInfo|null $agent_info
* @return Receipt * @return $this
*/ */
public function setAgentInfo(?AgentInfo $agent_info): self public function setAgentInfo(?AgentInfo $agent_info): self
{ {
@ -178,7 +181,7 @@ class Receipt extends Entity
* Поставщика * Поставщика
* *
* @param Supplier|null $supplier * @param Supplier|null $supplier
* @return Receipt * @return $this
*/ */
public function setSupplier(?Supplier $supplier): self public function setSupplier(?Supplier $supplier): self
{ {
@ -199,12 +202,12 @@ class Receipt extends Entity
/** /**
* Устанаваливает коллекцию предметов расчёта * Устанаваливает коллекцию предметов расчёта
* *
* @todo исключение при пустой коллекции
* @param Items $items * @param Items $items
* @return Receipt * @return $this
* @throws EmptyItemsException * @throws EmptyItemsException
* @throws InvalidEntityInCollectionException * @throws InvalidEntityInCollectionException
* @throws Exception * @throws Exception
* @todo исключение при пустой коллекции
*/ */
public function setItems(Items $items): self public function setItems(Items $items): self
{ {
@ -230,7 +233,7 @@ class Receipt extends Entity
* Устанаваливает коллекцию оплат * Устанаваливает коллекцию оплат
* *
* @param Payments $payments * @param Payments $payments
* @return Receipt * @return $this
* @throws InvalidEntityInCollectionException * @throws InvalidEntityInCollectionException
*/ */
public function setPayments(Payments $payments): self public function setPayments(Payments $payments): self
@ -255,7 +258,7 @@ class Receipt extends Entity
* Устанаваливает коллекцию ставок НДС * Устанаваливает коллекцию ставок НДС
* *
* @param Vats|null $vats * @param Vats|null $vats
* @return Receipt * @return $this
* @throws Exception * @throws Exception
*/ */
public function setVats(?Vats $vats): self public function setVats(?Vats $vats): self
@ -292,7 +295,7 @@ class Receipt extends Entity
* Устанаваливает кассира * Устанаваливает кассира
* *
* @param string|null $cashier * @param string|null $cashier
* @return Receipt * @return $this
* @throws TooLongCashierException * @throws TooLongCashierException
*/ */
public function setCashier(?string $cashier): self public function setCashier(?string $cashier): self
@ -321,7 +324,7 @@ class Receipt extends Entity
* Устанаваливает дополнительный реквизит чека * Устанаваливает дополнительный реквизит чека
* *
* @param string|null $add_check_props * @param string|null $add_check_props
* @return Receipt * @return $this
* @throws TooLongAddCheckPropException * @throws TooLongAddCheckPropException
*/ */
public function setAddCheckProps(?string $add_check_props): self public function setAddCheckProps(?string $add_check_props): self
@ -350,7 +353,7 @@ class Receipt extends Entity
* Устанаваливает дополнительный реквизит пользователя * Устанаваливает дополнительный реквизит пользователя
* *
* @param AdditionalUserProps|null $add_user_props * @param AdditionalUserProps|null $add_user_props
* @return Receipt * @return $this
*/ */
public function setAddUserProps(?AdditionalUserProps $add_user_props): self public function setAddUserProps(?AdditionalUserProps $add_user_props): self
{ {

View File

@ -17,12 +17,12 @@ namespace AtolOnline\Enums;
final class DocumentTypes extends Enum final class DocumentTypes extends Enum
{ {
/** /**
* Чек прихода, возврата прихода, расхода, возврата расхода * Документ прихода, возврата прихода, расхода, возврата расхода
*/ */
const RECEIPT = 'receipt'; const RECEIPT = 'receipt';
/** /**
* Чек коррекции * Документ коррекции
*/ */
const CORRECTION = 'correction'; const CORRECTION = 'correction';

View File

@ -0,0 +1,138 @@
<?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\{Constants\Constraints, Helpers, Tests\BasicTestCase};
use AtolOnline\Collections\{Payments, Vats,};
use AtolOnline\Entities\{Company, Correction, CorrectionInfo};
use AtolOnline\Enums\{CorrectionTypes, SnoTypes};
use AtolOnline\Exceptions\{EmptyCorrectionNumberException,
InvalidCorrectionDateException,
InvalidEntityInCollectionException,
InvalidEnumValueException,
NegativePaymentSumException,
TooHighPaymentSumException,
TooLongCashierException
};
use Exception;
/**
* Набор тестов для проверки работы класса чека коррекции
*/
class CorrectionTest extends BasicTestCase
{
/**
* Тестирует конструктор и корректное приведение к json
*
* @return void
* @covers \AtolOnline\Entities\Correction
* @covers \AtolOnline\Entities\Correction::setCompany
* @covers \AtolOnline\Entities\Correction::setCorrectionInfo
* @covers \AtolOnline\Entities\Correction::setPayments
* @covers \AtolOnline\Entities\Correction::setVats
* @covers \AtolOnline\Entities\Correction::jsonSerialize
* @throws EmptyCorrectionNumberException
* @throws InvalidCorrectionDateException
* @throws InvalidEntityInCollectionException
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws Exception
*/
public function testConstructor(): void
{
$correction = $this->newCorrection();
$this->assertIsAtolable($correction);
}
/**
* Тестирует установку валидного кассира
*
* @return void
* @covers \AtolOnline\Entities\Correction::setCashier
* @covers \AtolOnline\Entities\Correction::getCashier
* @covers \AtolOnline\Entities\Correction::jsonSerialize
* @throws EmptyCorrectionNumberException
* @throws InvalidCorrectionDateException
* @throws InvalidEntityInCollectionException
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws TooLongCashierException
*/
public function testCashier(): void
{
$correction = $this->newCorrection()->setCashier(Helpers::randomStr());
$this->assertArrayHasKey('cashier', $correction->jsonSerialize());
$this->assertEquals($correction->getCashier(), $correction->jsonSerialize()['cashier']);
}
/**
* Тестирует обнуление кассира
*
* @param mixed $param
* @return void
* @dataProvider providerNullableStrings
* @covers \AtolOnline\Entities\Correction::setCashier
* @covers \AtolOnline\Entities\Correction::getCashier
* @throws EmptyCorrectionNumberException
* @throws InvalidCorrectionDateException
* @throws InvalidEntityInCollectionException
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws TooLongCashierException
*/
public function testNullableCashier(mixed $param): void
{
$this->assertNull($this->newCorrection()->setCashier($param)->getCashier());
}
/**
* Тестирует выброс исключения при установке слишком длинного кассира (лол)
*
* @return void
* @covers \AtolOnline\Entities\Correction::setCashier
* @covers \AtolOnline\Exceptions\TooLongCashierException
* @throws EmptyCorrectionNumberException
* @throws InvalidCorrectionDateException
* @throws InvalidEntityInCollectionException
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws TooLongCashierException
*/
public function testTooLongCashierException(): void
{
$this->expectException(TooLongCashierException::class);
$this->newCorrection()->setCashier(Helpers::randomStr(Constraints::MAX_LENGTH_CASHIER_NAME + 1));
}
/**
* Возвращает валидный тестовый объект чека
*
* @return Correction
* @throws InvalidEntityInCollectionException
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws EmptyCorrectionNumberException
* @throws InvalidCorrectionDateException
*/
protected function newCorrection(): Correction
{
return new Correction(
new Company('company@example.com', SnoTypes::OSN, '1234567890', 'https://example.com'),
new CorrectionInfo(CorrectionTypes::SELF, '01.01.2021', Helpers::randomStr()),
new Payments($this->generatePaymentObjects(2)),
new Vats($this->generateVatObjects(2)),
);
}
}