diff --git a/src/Entities/AgentInfo.php b/src/Entities/AgentInfo.php new file mode 100644 index 0000000..d311c3e --- /dev/null +++ b/src/Entities/AgentInfo.php @@ -0,0 +1,168 @@ +setType($type); + !is_null($paying_agent) && $this->setPayingAgent($paying_agent); + !is_null($receive_payments_operator) && $this->setReceivePaymentsOperator($receive_payments_operator); + !is_null($money_transfer_operator) && $this->setMoneyTransferOperator($money_transfer_operator); + } + + /** + * Возвращает установленный признак оператора + * + * @return string|null + */ + public function getType(): ?string + { + return $this->type; + } + + /** + * Устанавливает признак оператора + * + * @param string|null $type + * @return AgentInfo + */ + public function setType(?string $type): AgentInfo + { + $this->type = $type; + return $this; + } + + /** + * Взвращает установленного платёжного агента + * + * @return PayingAgent|null + */ + public function getPayingAgent(): ?PayingAgent + { + return $this->paying_agent; + } + + /** + * Устанавливает платёжного агента + * + * @param PayingAgent|null $paying_agent + * @return AgentInfo + */ + public function setPayingAgent(?PayingAgent $paying_agent): AgentInfo + { + $this->paying_agent = $paying_agent; + return $this; + } + + /** + * Возвращает установленного оператора по приёму платежей + * + * @return ReceivePaymentsOperator|null + */ + public function getReceivePaymentsOperator(): ?ReceivePaymentsOperator + { + return $this->receive_payments_operator; + } + + /** + * Устанавливает оператора по приёму платежей + * + * @param ReceivePaymentsOperator|null $receive_payments_operator + * @return AgentInfo + */ + public function setReceivePaymentsOperator(?ReceivePaymentsOperator $receive_payments_operator): AgentInfo + { + $this->receive_payments_operator = $receive_payments_operator; + return $this; + } + + /** + * Возвращает установленного оператора перевода + * + * @return MoneyTransferOperator|null + */ + public function getMoneyTransferOperator(): ?MoneyTransferOperator + { + return $this->money_transfer_operator; + } + + /** + * Устанавливает оператора перевода + * + * @param MoneyTransferOperator|null $money_transfer_operator + * @return AgentInfo + */ + public function setMoneyTransferOperator(?MoneyTransferOperator $money_transfer_operator): AgentInfo + { + $this->money_transfer_operator = $money_transfer_operator; + return $this; + } + + /** + * @inheritDoc + */ + public function jsonSerialize(): array + { + $json = []; + $this->getType() && $json['type'] = $this->getType(); + $this->getPayingAgent()?->jsonSerialize() && $json['paying_agent'] = $this + ->getPayingAgent()->jsonSerialize(); + $this->getReceivePaymentsOperator()?->jsonSerialize() && $json['receive_payments_operator'] = $this + ->getReceivePaymentsOperator()->jsonSerialize(); + $this->getMoneyTransferOperator()?->jsonSerialize() && $json['money_transfer_operator'] = $this + ->getMoneyTransferOperator()->jsonSerialize(); + return $json; + } +} diff --git a/tests/AtolOnline/Tests/BasicTestCase.php b/tests/AtolOnline/Tests/BasicTestCase.php index a1e144d..3d1f26d 100644 --- a/tests/AtolOnline/Tests/BasicTestCase.php +++ b/tests/AtolOnline/Tests/BasicTestCase.php @@ -67,16 +67,16 @@ class BasicTestCase extends TestCase * Тестирует является ли объект приводимым к json-строке согласно схеме АТОЛ Онлайн * * @param Entity $entity - * @param array $json_structure + * @param array|null $json_structure * @covers \AtolOnline\Entities\Entity::jsonSerialize * @covers \AtolOnline\Entities\Entity::__toString */ - public function assertAtolable(Entity $entity, array $json_structure = []): void + public function assertAtolable(Entity $entity, ?array $json_structure = null): void { $this->assertIsArray($entity->jsonSerialize()); $this->assertIsString((string)$entity); $this->assertJson((string)$entity); - if ($json_structure) { + if (!is_null($json_structure)) { $this->assertEquals(json_encode($json_structure), (string)$entity); } } diff --git a/tests/AtolOnline/Tests/Entities/AgentInfoTest.php b/tests/AtolOnline/Tests/Entities/AgentInfoTest.php new file mode 100644 index 0000000..4c9e2ac --- /dev/null +++ b/tests/AtolOnline/Tests/Entities/AgentInfoTest.php @@ -0,0 +1,118 @@ +assertAtolable(new AgentInfo(), []); + } + + /** + * Тестирует конструктор с передачей значений и корректное приведение к json + * + * @covers \AtolOnline\Entities\AgentInfo + * @covers \AtolOnline\Entities\AgentInfo::jsonSerialize + * @covers \AtolOnline\Entities\AgentInfo::setType + * @covers \AtolOnline\Entities\AgentInfo::getType + * @covers \AtolOnline\Entities\AgentInfo::setPayingAgent + * @covers \AtolOnline\Entities\AgentInfo::getPayingAgent + * @covers \AtolOnline\Entities\PayingAgent::jsonSerialize + * @covers \AtolOnline\Entities\AgentInfo::setMoneyTransferOperator + * @covers \AtolOnline\Entities\AgentInfo::getMoneyTransferOperator + * @covers \AtolOnline\Entities\MoneyTransferOperator::jsonSerialize + * @covers \AtolOnline\Entities\AgentInfo::setReceivePaymentsOperator + * @covers \AtolOnline\Entities\AgentInfo::getReceivePaymentsOperator + * @covers \AtolOnline\Entities\ReceivePaymentsOperator::jsonSerialize + * @throws InvalidPhoneException + * @throws TooLongPayingAgentOperationException + * @throws InvalidInnLengthException + * @throws InvalidEnumValueException + */ + public function testConstructorWithArgs(): void + { + $this->assertAtolable(new AgentInfo(null), []); + $this->assertAtolable(new AgentInfo(AgentTypes::ANOTHER), ['type' => AgentTypes::ANOTHER]); + $this->assertAtolable(new AgentInfo(paying_agent: new PayingAgent()), []); + $this->assertAtolable(new AgentInfo(money_transfer_operator: new MoneyTransferOperator()), []); + $this->assertAtolable(new AgentInfo(receive_payments_operator: new ReceivePaymentsOperator()), []); + + $this->assertAtolable(new AgentInfo( + AgentTypes::ANOTHER, + new PayingAgent(), + new ReceivePaymentsOperator(), + new MoneyTransferOperator(), + ), ['type' => AgentTypes::ANOTHER]); + + $this->assertAtolable(new AgentInfo( + AgentTypes::ANOTHER, + new PayingAgent('test', ['+79518888888']), + new ReceivePaymentsOperator(['+79519999999']), + new MoneyTransferOperator('MTO Name', '9876543210', 'London', ['+79517777777']), + ), [ + 'type' => AgentTypes::ANOTHER, + 'paying_agent' => [ + 'operation' => 'test', + 'phones' => [ + '+79518888888', + ], + ], + 'receive_payments_operator' => [ + 'phones' => [ + '+79519999999', + ], + ], + 'money_transfer_operator' => [ + 'name' => 'MTO Name', + 'inn' => '9876543210', + 'address' => 'London', + 'phones' => [ + "+79517777777", + ], + ], + ]); + } + + /** + * Тестирует исключение при некорректном типе + * + * @covers \AtolOnline\Entities\AgentInfo + * @covers \AtolOnline\Enums\AgentTypes::isValid + * @covers \AtolOnline\Exceptions\InvalidEnumValueException + */ + public function testInvalidEnumValueException(): void + { + $this->expectException(InvalidEnumValueException::class); + new AgentInfo('qwerty'); + } +}