diff --git a/src/Entities/Client.php b/src/Entities/Client.php index 983233b..6a1e10b 100644 --- a/src/Entities/Client.php +++ b/src/Entities/Client.php @@ -17,12 +17,10 @@ use AtolOnline\Exceptions\{ InvalidInnLengthException, InvalidPhoneException, TooLongClientNameException, - TooLongEmailException -}; + TooLongEmailException}; use AtolOnline\Traits\{ HasEmail, - HasInn -}; + HasInn}; use JetBrains\PhpStorm\Pure; /** @@ -94,7 +92,7 @@ final class Client extends Entity throw new TooLongClientNameException($name); } } - $this->name = empty($name) ? null : $name; + $this->name = $name ?: null; return $this; } diff --git a/src/Entities/PayingAgent.php b/src/Entities/PayingAgent.php index 4b6c028..7620382 100644 --- a/src/Entities/PayingAgent.php +++ b/src/Entities/PayingAgent.php @@ -14,8 +14,7 @@ namespace AtolOnline\Entities; use AtolOnline\Constants\Constraints; use AtolOnline\Exceptions\{ InvalidPhoneException, - TooLongPayingAgentOperationException -}; + TooLongPayingAgentOperationException}; use AtolOnline\Traits\HasPhones; use Illuminate\Support\Collection; @@ -64,7 +63,7 @@ final class PayingAgent extends Entity throw new TooLongPayingAgentOperationException($operation); } } - $this->operation = empty($operation) ? null : $operation; + $this->operation = $operation ?: null; return $this; } diff --git a/src/Entities/Receipt.php b/src/Entities/Receipt.php index db18db9..beb8b68 100644 --- a/src/Entities/Receipt.php +++ b/src/Entities/Receipt.php @@ -340,7 +340,7 @@ class Receipt extends Entity throw new TooLongAddCheckPropException($add_check_props); } } - $this->add_check_props = empty($add_check_props) ? null : $add_check_props; + $this->add_check_props = $add_check_props ?: null; return $this; } diff --git a/src/Traits/HasEmail.php b/src/Traits/HasEmail.php index e72b736..661902c 100644 --- a/src/Traits/HasEmail.php +++ b/src/Traits/HasEmail.php @@ -41,7 +41,7 @@ trait HasEmail throw new InvalidEmailException($email); } } - $this->email = empty($email) ? null : $email; + $this->email = $email ?: null; return $this; } diff --git a/src/Traits/HasInn.php b/src/Traits/HasInn.php index 01ae7d3..8160ac4 100644 --- a/src/Traits/HasInn.php +++ b/src/Traits/HasInn.php @@ -37,7 +37,7 @@ trait HasInn throw new InvalidInnLengthException($inn); } } - $this->inn = empty($inn) ? null : $inn; + $this->inn = $inn ?: null; return $this; } diff --git a/tests/AtolOnline/Tests/Entities/ReceiptTest.php b/tests/AtolOnline/Tests/Entities/ReceiptTest.php index 3075b3d..b4cc014 100644 --- a/tests/AtolOnline/Tests/Entities/ReceiptTest.php +++ b/tests/AtolOnline/Tests/Entities/ReceiptTest.php @@ -10,22 +10,28 @@ namespace AtolOnline\Tests\Entities; use AtolOnline\{ - Collections\Items, - Collections\Payments, - Collections\Vats, - Entities\AgentInfo, - Entities\Client, - Entities\Company, - Entities\Item, - Entities\MoneyTransferOperator, - Entities\PayingAgent, - Entities\Receipt, - Entities\ReceivePaymentsOperator, - Entities\Supplier, - Entities\Vat, - Enums\AgentTypes, - Enums\SnoTypes, + Constants\Constraints, + Helpers, Tests\BasicTestCase}; +use AtolOnline\Collections\{ + Items, + Payments, + Vats,}; +use AtolOnline\Entities\{ + AdditionalUserProps, + AgentInfo, + Client, + Company, + Item, + MoneyTransferOperator, + PayingAgent, + Receipt, + ReceivePaymentsOperator, + Supplier, + Vat,}; +use AtolOnline\Enums\{ + AgentTypes, + SnoTypes,}; use AtolOnline\Exceptions\{ EmptyItemNameException, EmptyItemsException, @@ -41,6 +47,8 @@ use AtolOnline\Exceptions\{ TooHighItemPriceException, TooHighItemSumException, TooHighPaymentSumException, + TooLongAddCheckPropException, + TooLongCashierException, TooLongItemNameException, TooLongPayingAgentOperationException, TooManyException}; @@ -83,7 +91,6 @@ class ReceiptTest extends BasicTestCase { $receipt = $this->newReceipt(); $this->assertIsAtolable($receipt); - $receipt->getItems(); } /** @@ -363,7 +370,201 @@ class ReceiptTest extends BasicTestCase /** @var Vat $vat */ $receipt->setVats(new Vats($this->generateVatObjects(2)))->getVats() - ->each(fn ($vat) => $this->assertEquals($items_total, $vat->getSum())); + ->each(fn($vat) => $this->assertEquals($items_total, $vat->getSum())); + } + + /** + * Тестирует установку валидного кассира + * + * @return void + * @covers \AtolOnline\Entities\Receipt::setCashier + * @covers \AtolOnline\Entities\Receipt::getCashier + * @covers \AtolOnline\Entities\Receipt::jsonSerialize + * @throws EmptyItemNameException + * @throws EmptyItemsException + * @throws EmptyPaymentsException + * @throws InvalidEntityInCollectionException + * @throws InvalidEnumValueException + * @throws NegativeItemPriceException + * @throws NegativeItemQuantityException + * @throws NegativePaymentSumException + * @throws TooHighItemPriceException + * @throws TooHighPaymentSumException + * @throws TooLongItemNameException + * @throws TooManyException + * @throws TooLongCashierException + * @throws Exception + */ + public function testCashier(): void + { + $receipt = $this->newReceipt()->setCashier(Helpers::randomStr()); + $this->assertArrayHasKey('cashier', $receipt->jsonSerialize()); + $this->assertEquals($receipt->getCashier(), $receipt->jsonSerialize()['cashier']); + } + + /** + * Тестирует обнуление кассира + * + * @param mixed $param + * @return void + * @dataProvider providerNullableStrings + * @covers \AtolOnline\Entities\Receipt::setCashier + * @covers \AtolOnline\Entities\Receipt::getCashier + * @throws EmptyItemNameException + * @throws EmptyItemsException + * @throws EmptyPaymentsException + * @throws InvalidEntityInCollectionException + * @throws InvalidEnumValueException + * @throws NegativeItemPriceException + * @throws NegativeItemQuantityException + * @throws NegativePaymentSumException + * @throws TooHighItemPriceException + * @throws TooHighPaymentSumException + * @throws TooLongCashierException + * @throws TooLongItemNameException + * @throws TooManyException + */ + public function testNullableCashier(mixed $param): void + { + $this->assertNull($this->newReceipt()->setCashier($param)->getCashier()); + } + + /** + * Тестирует выброс исключения при установке слишком длинного кассира (лол) + * + * @return void + * @covers \AtolOnline\Entities\Receipt::setCashier + * @covers \AtolOnline\Exceptions\TooLongCashierException + * @throws EmptyItemNameException + * @throws EmptyItemsException + * @throws EmptyPaymentsException + * @throws InvalidEntityInCollectionException + * @throws InvalidEnumValueException + * @throws NegativeItemPriceException + * @throws NegativeItemQuantityException + * @throws NegativePaymentSumException + * @throws TooHighItemPriceException + * @throws TooHighPaymentSumException + * @throws TooLongCashierException + * @throws TooLongItemNameException + * @throws TooManyException + */ + public function testTooLongCashierException(): void + { + $this->expectException(TooLongCashierException::class); + $this->newReceipt()->setCashier(Helpers::randomStr(Constraints::MAX_LENGTH_CASHIER_NAME + 1)); + } + + /** + * Тестирует установку дополнительного реквизита чека + * + * @return void + * @covers \AtolOnline\Entities\Receipt::setAddCheckProps + * @covers \AtolOnline\Entities\Receipt::getAddCheckProps + * @covers \AtolOnline\Entities\Receipt::jsonSerialize + * @throws EmptyItemNameException + * @throws EmptyItemsException + * @throws EmptyPaymentsException + * @throws InvalidEntityInCollectionException + * @throws InvalidEnumValueException + * @throws NegativeItemPriceException + * @throws NegativeItemQuantityException + * @throws NegativePaymentSumException + * @throws TooHighItemPriceException + * @throws TooHighPaymentSumException + * @throws TooLongItemNameException + * @throws TooManyException + * @throws TooLongAddCheckPropException + * @throws Exception + */ + public function testAddCheckProps(): void + { + $receipt = $this->newReceipt()->setAddCheckProps(Helpers::randomStr()); + $this->assertArrayHasKey('additional_check_props', $receipt->jsonSerialize()); + $this->assertEquals($receipt->getAddCheckProps(), $receipt->jsonSerialize()['additional_check_props']); + } + + /** + * Тестирует обнуление дополнительного реквизита чека + * + * @param mixed $param + * @return void + * @dataProvider providerNullableStrings + * @covers \AtolOnline\Entities\Receipt::setAddCheckProps + * @covers \AtolOnline\Entities\Receipt::getAddCheckProps + * @throws EmptyItemNameException + * @throws EmptyItemsException + * @throws EmptyPaymentsException + * @throws InvalidEntityInCollectionException + * @throws InvalidEnumValueException + * @throws NegativeItemPriceException + * @throws NegativeItemQuantityException + * @throws NegativePaymentSumException + * @throws TooHighItemPriceException + * @throws TooHighPaymentSumException + * @throws TooLongAddCheckPropException + * @throws TooLongItemNameException + * @throws TooManyException + */ + public function testNullableAddCheckProps(mixed $param): void + { + $this->assertNull($this->newReceipt()->setAddCheckProps($param)->getAddCheckProps()); + } + + /** + * Тестирует выброс исключения при установке слишком длинного дополнительного реквизита чека + * + * @return void + * @covers \AtolOnline\Entities\Receipt::setAddCheckProps + * @covers \AtolOnline\Exceptions\TooLongAddCheckPropException + * @throws EmptyItemNameException + * @throws EmptyItemsException + * @throws EmptyPaymentsException + * @throws InvalidEntityInCollectionException + * @throws InvalidEnumValueException + * @throws NegativeItemPriceException + * @throws NegativeItemQuantityException + * @throws NegativePaymentSumException + * @throws TooHighItemPriceException + * @throws TooHighPaymentSumException + * @throws TooLongAddCheckPropException + * @throws TooLongItemNameException + * @throws TooManyException + */ + public function testTooLongAddCheckPropException(): void + { + $this->expectException(TooLongAddCheckPropException::class); + $this->newReceipt()->setAddCheckProps(Helpers::randomStr(Constraints::MAX_LENGTH_ADD_CHECK_PROP + 1)); + } + + /** + * Тестирует установку дополнительного реквизита пользователя + * + * @return void + * @covers \AtolOnline\Entities\Receipt::setAddUserProps + * @covers \AtolOnline\Entities\Receipt::getAddUserProps + * @covers \AtolOnline\Entities\Receipt::jsonSerialize + * @throws EmptyItemNameException + * @throws EmptyItemsException + * @throws EmptyPaymentsException + * @throws InvalidEntityInCollectionException + * @throws InvalidEnumValueException + * @throws NegativeItemPriceException + * @throws NegativeItemQuantityException + * @throws NegativePaymentSumException + * @throws TooHighItemPriceException + * @throws TooHighPaymentSumException + * @throws TooLongItemNameException + * @throws TooManyException + * @throws Exception + */ + public function testAdditionalUserProps(): void + { + $aup = new AdditionalUserProps('name', 'value'); + $receipt = $this->newReceipt()->setAddUserProps($aup); + $this->assertArrayHasKey('additional_user_props', $receipt->jsonSerialize()); + $this->assertEquals($receipt->getAddUserProps(), $receipt->jsonSerialize()['additional_user_props']); + $this->assertArrayNotHasKey('additional_user_props', $receipt->setAddUserProps(null)->jsonSerialize()); } /**