diff --git a/src/Constants/Constraints.php b/src/Constants/Constraints.php index 1606bbd..49dfec6 100644 --- a/src/Constants/Constraints.php +++ b/src/Constants/Constraints.php @@ -147,6 +147,11 @@ final class Constraints */ const CORRECTION_DATE_FORMAT = 'd.m.Y'; + /** + * Максимальная сумма одной оплаты + */ + const MAX_COUNT_PAYMENT_SUM = 99999.999; + /** * Регулярное выражение для валидации строки ИНН * diff --git a/src/Entities/Payment.php b/src/Entities/Payment.php new file mode 100644 index 0000000..17381b4 --- /dev/null +++ b/src/Entities/Payment.php @@ -0,0 +1,119 @@ +setType($type)->setSum($sum); + } + + /** + * Возвращает установленный тип оплаты + * + * @return int + */ + public function getType(): int + { + return $this->type; + } + + /** + * Устанавливает тип оплаты + * + * @param int $type + * @return $this + * @throws InvalidEnumValueException + */ + public function setType(int $type): self + { + PaymentTypes::isValid($type) && $this->type = $type; + return $this; + } + + /** + * Возвращает установленную сумму оплаты + * + * @return float + */ + public function getSum(): float + { + return $this->sum; + } + + /** + * Устанавливает сумму оплаты + * + * @param float $sum + * @return $this + * @throws TooHighPaymentSumException + * @throws NegativePaymentSumException + */ + public function setSum(float $sum): self + { + $sum = round($sum, 2); + if ($sum > Constraints::MAX_COUNT_PAYMENT_SUM) { + throw new TooHighPaymentSumException($sum); + } + if ($sum < 0) { + throw new NegativePaymentSumException($sum); + } + $this->sum = $sum; + return $this; + } + + /** + * @inheritDoc + */ + #[Pure] + #[ArrayShape(['type' => 'int', 'sum' => 'float'])] + public function jsonSerialize(): array + { + return [ + 'type' => $this->getType(), + 'sum' => $this->getSum(), + ]; + } +} diff --git a/src/Entities/todo_Payment.php b/src/Entities/todo_Payment.php deleted file mode 100644 index 6398a42..0000000 --- a/src/Entities/todo_Payment.php +++ /dev/null @@ -1,99 +0,0 @@ -setType($payment_type); - $this->setSum($sum); - } - - /** - * Возвращает тип оплаты. Тег ФФД - 1031, 1081, 1215, 1216, 1217. - * - * @return int - */ - public function getType(): int - { - return $this->type; - } - - /** - * Устанавливает тип оплаты. Тег ФФД - 1031, 1081, 1215, 1216, 1217. - * - * @param int $type - * @return $this - */ - public function setType(int $type): todoPayment - { - $this->type = $type; - return $this; - } - - /** - * Возвращает сумму оплаты - * - * @return float - */ - public function getSum(): float - { - return $this->sum; - } - - /** - * Устанавливает сумму оплаты - * - * @param float $sum - * @return $this - */ - public function setSum(float $sum): todoPayment - { - $this->sum = $sum; - return $this; - } - - /** - * @inheritDoc - */ - public function jsonSerialize() - { - return [ - 'type' => $this->getType(), - 'sum' => $this->getSum(), - ]; - } -} diff --git a/src/Exceptions/NegativePaymentSumException.php b/src/Exceptions/NegativePaymentSumException.php new file mode 100644 index 0000000..1b952ae --- /dev/null +++ b/src/Exceptions/NegativePaymentSumException.php @@ -0,0 +1,38 @@ +assertAtolable( + new Payment(PaymentTypes::ELECTRON, 123.456789), + [ + 'type' => PaymentTypes::ELECTRON, + 'sum' => 123.46, + ] + ); + } + + /** + * Тестирует исключение при некорректном типе + * + * @covers \AtolOnline\Entities\Payment + * @covers \AtolOnline\Entities\Payment::setType + * @covers \AtolOnline\Enums\PaymentTypes::isValid + * @covers \AtolOnline\Exceptions\InvalidEnumValueException + * @return void + * @throws InvalidEnumValueException + * @throws NegativePaymentSumException + * @throws TooHighPaymentSumException + */ + public function testInvalidEnumValueException(): void + { + $this->expectException(InvalidEnumValueException::class); + $this->expectExceptionMessage('Некорректное значение AtolOnline\Enums\PaymentTypes::123'); + new Payment(123, 123.456789); + } + + /** + * Тестирует исключение при слишком большой сумме + * + * @covers \AtolOnline\Entities\Payment + * @covers \AtolOnline\Entities\Payment::setSum + * @covers \AtolOnline\Exceptions\TooHighPaymentSumException + * @return void + * @throws InvalidEnumValueException + * @throws NegativePaymentSumException + */ + public function testTooHighPaymentSumException(): void + { + $this->expectException(TooHighPaymentSumException::class); + new Payment(PaymentTypes::ELECTRON, Constraints::MAX_COUNT_PAYMENT_SUM + 1); + } + + /** + * Тестирует исключение при отрицательной сумме + * + * @covers \AtolOnline\Entities\Payment + * @covers \AtolOnline\Entities\Payment::setSum + * @covers \AtolOnline\Exceptions\NegativePaymentSumException + * @return void + * @throws InvalidEnumValueException + * @throws NegativePaymentSumException + * @throws TooHighPaymentSumException + */ + public function testNegativePaymentSumException(): void + { + $this->expectException(NegativePaymentSumException::class); + new Payment(PaymentTypes::ELECTRON, -1); + } +} diff --git a/tests/AtolOnline/Tests/Entities/VatTest.php b/tests/AtolOnline/Tests/Entities/VatTest.php index ba62404..98b029f 100644 --- a/tests/AtolOnline/Tests/Entities/VatTest.php +++ b/tests/AtolOnline/Tests/Entities/VatTest.php @@ -12,6 +12,7 @@ namespace AtolOnline\Tests\Entities; use AtolOnline\{ Entities\Vat, Enums\VatTypes, + Exceptions\InvalidEnumValueException, Tests\BasicTestCase }; @@ -69,6 +70,7 @@ class VatTest extends BasicTestCase * @covers \AtolOnline\Entities\Vat::getSum * @covers \AtolOnline\Entities\Vat::getCalculated * @covers \AtolOnline\Entities\Vat::jsonSerialize + * @throws InvalidEnumValueException */ public function testConstructor(string $type, float $sum): void { @@ -90,6 +92,7 @@ class VatTest extends BasicTestCase * @param float $after_minus Результат после -20р * @covers \AtolOnline\Entities\Vat::addSum * @covers \AtolOnline\Entities\Vat::getCalculated + * @throws InvalidEnumValueException */ public function testVatAdd(string $type, float $after_plus, float $after_minus) {