From 413a41722edf9de17542c8f7bc1ed2955841a983 Mon Sep 17 00:00:00 2001 From: kouinkouin Date: Tue, 15 Oct 2024 13:25:14 +0200 Subject: [PATCH] fix: only the first option was filled --- src/Bpost/Order/Box/National.php | 50 ++++++++++--------- .../Option/AutomaticSecondPresentation.php | 10 ---- src/Bpost/Order/Box/Option/CashOnDelivery.php | 19 +++++++ src/Bpost/Order/Box/Option/Insured.php | 5 +- src/Bpost/Order/Box/Option/Option.php | 6 +++ src/Bpost/Order/Box/Option/Signed.php | 10 ---- tests/Bpost/Order/Box/NationalTest.php | 31 +++++++++++- tests/Bpost/OrderTest.php | 43 ++++++++++++++-- 8 files changed, 123 insertions(+), 51 deletions(-) diff --git a/src/Bpost/Order/Box/National.php b/src/Bpost/Order/Box/National.php index 5c34db35..e775444a 100644 --- a/src/Bpost/Order/Box/National.php +++ b/src/Bpost/Order/Box/National.php @@ -4,11 +4,14 @@ use Bpost\BpostApiClient\Bpost; use Bpost\BpostApiClient\Bpost\Order\Box\OpeningHour\Day; +use Bpost\BpostApiClient\Bpost\Order\Box\Option\CashOnDelivery; use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging; use Bpost\BpostApiClient\Bpost\Order\Box\Option\Option; use Bpost\BpostApiClient\BpostException; use Bpost\BpostApiClient\Common\ComplexAttribute; use Bpost\BpostApiClient\Common\XmlHelper; +use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException; +use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException; use Bpost\BpostApiClient\Exception\BpostNotImplementedException; use Bpost\BpostApiClient\Exception\XmlException\BpostXmlInvalidItemException; use DomDocument; @@ -232,24 +235,10 @@ public static function createFromXML(SimpleXMLElement $nationalXml, National $se ); } - if (isset($nationalXml->options) && !empty($nationalXml->options)) { + if (!empty($nationalXml->options)) { /** @var SimpleXMLElement $optionData */ - foreach ($nationalXml->options as $optionData) { - $optionData = $optionData->children(Bpost::NS_V3_COMMON); - - if (in_array($optionData->getName(), array( - Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED, - Messaging::MESSAGING_TYPE_INFO_NEXT_DAY, - Messaging::MESSAGING_TYPE_INFO_REMINDER, - Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED, - )) - ) { - $option = Messaging::createFromXML($optionData); - } else { - $option = self::getOptionFromOptionData($optionData); - } - - $self->addOption($option); + foreach ($nationalXml->options->children(Bpost::NS_V3_COMMON) as $optionData) { + $self->addOption(self::getOptionFromOptionData($optionData)); } } @@ -280,15 +269,30 @@ public static function createFromXML(SimpleXMLElement $nationalXml, National $se * @return Option * * @throws BpostNotImplementedException + * @throws BpostInvalidLengthException + * @throws BpostInvalidValueException */ protected static function getOptionFromOptionData(SimpleXMLElement $optionData) { - $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName()); - XmlHelper::assertMethodCreateFromXmlExists($className); + switch ($optionData->getName()) { + case Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED: + case Messaging::MESSAGING_TYPE_INFO_NEXT_DAY: + case Messaging::MESSAGING_TYPE_INFO_REMINDER: + case Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED: + return Messaging::createFromXML($optionData); - return call_user_func( - array($className, 'createFromXML'), - $optionData - ); + case 'cod': + return CashOnDelivery::createFromXML($optionData); + + default: + $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName()); + + XmlHelper::assertMethodCreateFromXmlExists($className); + + return call_user_func( + array($className, 'createFromXML'), + $optionData + ); + } } } diff --git a/src/Bpost/Order/Box/Option/AutomaticSecondPresentation.php b/src/Bpost/Order/Box/Option/AutomaticSecondPresentation.php index 86e7ff6a..719be9ad 100644 --- a/src/Bpost/Order/Box/Option/AutomaticSecondPresentation.php +++ b/src/Bpost/Order/Box/Option/AutomaticSecondPresentation.php @@ -31,14 +31,4 @@ public function toXML(DOMDocument $document, $prefix = 'common') { return $document->createElement(XmlHelper::getPrefixedTagName('automaticSecondPresentation', $prefix)); } - - /** - * @param SimpleXMLElement $xml - * - * @return static - */ - public static function createFromXML(SimpleXMLElement $xml) - { - return new static(); - } } diff --git a/src/Bpost/Order/Box/Option/CashOnDelivery.php b/src/Bpost/Order/Box/Option/CashOnDelivery.php index f9c930b9..bc0de4cc 100644 --- a/src/Bpost/Order/Box/Option/CashOnDelivery.php +++ b/src/Bpost/Order/Box/Option/CashOnDelivery.php @@ -2,9 +2,13 @@ namespace Bpost\BpostApiClient\Bpost\Order\Box\Option; +use Bpost\BpostApiClient\Bpost; use Bpost\BpostApiClient\Common\XmlHelper; +use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException; +use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException; use DomDocument; use DomElement; +use SimpleXMLElement; /** * bPost CashOnDelivery class @@ -132,4 +136,19 @@ public function toXML(DOMDocument $document, $prefix = 'common') return $cod; } + + /** + * @param SimpleXMLElement $xml + * + * @return static + * + * @throws BpostInvalidLengthException + * @throws BpostInvalidValueException + */ + public static function createFromXML(SimpleXMLElement $xml) + { + $details = $xml->children(Bpost::NS_V3_COMMON); + + return new static(floatval($details->codAmount), (string) $details->iban, (string) $details->bic); + } } diff --git a/src/Bpost/Order/Box/Option/Insured.php b/src/Bpost/Order/Box/Option/Insured.php index 3456648e..060f0538 100644 --- a/src/Bpost/Order/Box/Option/Insured.php +++ b/src/Bpost/Order/Box/Option/Insured.php @@ -166,10 +166,9 @@ public function toXML(DOMDocument $document, $prefix = 'common') */ public static function createFromXML(SimpleXMLElement $xml) { - $insuranceDetail = $xml->children('common', true); - + $insuranceDetail = $xml->children(Bpost::NS_V3_COMMON); $type = $insuranceDetail->getName(); - $value = $insuranceDetail->attributes()->value !== null ? (int) $insuranceDetail->attributes()->value : null; + $value = intval($insuranceDetail->attributes()->value) ?: null; if ($type === static::INSURANCE_TYPE_ADDITIONAL_INSURANCE && $value === 1) { $type = static::INSURANCE_TYPE_BASIC_INSURANCE; diff --git a/src/Bpost/Order/Box/Option/Option.php b/src/Bpost/Order/Box/Option/Option.php index cdad5e73..1f7549b8 100644 --- a/src/Bpost/Order/Box/Option/Option.php +++ b/src/Bpost/Order/Box/Option/Option.php @@ -4,6 +4,7 @@ use DOMDocument; use DOMElement; +use SimpleXMLElement; /** * bPost Option class @@ -24,4 +25,9 @@ abstract class Option * @return DOMElement */ abstract public function toXML(DOMDocument $document, $prefix = null); + + public static function createFromXML(SimpleXMLElement $xml) + { + return new static(); + } } diff --git a/src/Bpost/Order/Box/Option/Signed.php b/src/Bpost/Order/Box/Option/Signed.php index 566307c0..99ca5364 100644 --- a/src/Bpost/Order/Box/Option/Signed.php +++ b/src/Bpost/Order/Box/Option/Signed.php @@ -31,14 +31,4 @@ public function toXML(DOMDocument $document, $prefix = 'common') { return $document->createElement(XmlHelper::getPrefixedTagName('signed', $prefix)); } - - /** - * @param SimpleXMLElement $xml - * - * @return static - */ - public static function createFromXML(SimpleXMLElement $xml) - { - return new static(); - } } diff --git a/tests/Bpost/Order/Box/NationalTest.php b/tests/Bpost/Order/Box/NationalTest.php index 51252b60..6f40cd72 100644 --- a/tests/Bpost/Order/Box/NationalTest.php +++ b/tests/Bpost/Order/Box/NationalTest.php @@ -97,8 +97,35 @@ public function testCreateFromXml() /** @var Option[] $options */ $options = $self->getOptions(); - $this->assertNotNull($options); - // @todo Fix options feeding and test it + $this->assertCount(4, $options); + + /** @var Messaging $option */ + $option = $options[0]; + $this->assertSame('Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging', get_class($option)); + $this->assertSame(Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED, $option->getType()); + $this->assertSame(Messaging::MESSAGING_LANGUAGE_EN, $option->getLanguage()); + $this->assertNull($option->getEmailAddress()); + $this->assertSame('0476123456', $option->getMobilePhone()); + + /** @var Messaging $option */ + $option = $options[1]; + $this->assertSame('Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging', get_class($option)); + $this->assertSame(Messaging::MESSAGING_TYPE_INFO_NEXT_DAY, $option->getType()); + $this->assertSame(Messaging::MESSAGING_LANGUAGE_EN, $option->getLanguage()); + $this->assertSame('receiver@mail.be', $option->getEmailAddress()); + $this->assertNull($option->getMobilePhone()); + + /** @var Messaging $option */ + $option = $options[2]; + $this->assertSame('Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging', get_class($option)); + $this->assertSame(Messaging::MESSAGING_TYPE_INFO_REMINDER, $option->getType()); + $this->assertSame(Messaging::MESSAGING_LANGUAGE_EN, $option->getLanguage()); + $this->assertNull($option->getEmailAddress()); + $this->assertSame('0032475123456', $option->getMobilePhone()); + + /** @var SaturdayDelivery $option */ + $option = $options[3]; + $this->assertSame('Bpost\BpostApiClient\Bpost\Order\Box\Option\SaturdayDelivery', get_class($option)); $this->assertSame(500, $self->getWeight()); diff --git a/tests/Bpost/OrderTest.php b/tests/Bpost/OrderTest.php index 8b218613..3a9fee07 100644 --- a/tests/Bpost/OrderTest.php +++ b/tests/Bpost/OrderTest.php @@ -135,8 +135,45 @@ public function testCreateFromXml() $this->assertSame('2016-03-19+01:00', $nationalBox->getRequestedDeliveryDate()); $this->assertSame('Rue de l\'Autonomie', $nationalBox->getPugoAddress()->getStreetName()); - $this->assertNotNull($nationalBox->getOptions()); - // $this->assertCount(6, $nationalBox->getOptions()); + $this->assertCount(6, $nationalBox->getOptions()); + $options = $nationalBox->getOptions(); + + /** @var Box\Option\Messaging $option */ + $option = $options[0]; + $this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging', $option); + $this->assertSame(Box\Option\Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED, $option->getType()); + $this->assertSame(Box\Option\Messaging::MESSAGING_LANGUAGE_FR, $option->getLanguage()); + $this->assertNull($option->getMobilePhone()); + $this->assertSame('pomme@antidot.com', $option->getEmailAddress()); + + /** @var Box\Option\Messaging $option */ + $option = $options[1]; + $this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging', $option); + $this->assertSame(Box\Option\Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED, $option->getType()); + $this->assertSame(Box\Option\Messaging::MESSAGING_LANGUAGE_EN, $option->getLanguage()); + $this->assertNull($option->getMobilePhone()); + $this->assertSame('pomme@antidot.com', $option->getEmailAddress()); + + /** @var Box\Option\Insured $option */ + $option = $options[2]; + $this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\Insured', $option); + $this->assertSame(Box\Option\Insured::INSURANCE_TYPE_ADDITIONAL_INSURANCE, $option->getType()); + $this->assertSame(Box\Option\Insured::INSURANCE_AMOUNT_UP_TO_2500_EUROS, $option->getValue()); + + /** @var Box\Option\Signed $option */ + $option = $options[3]; + $this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\Signed', $option); + + /** @var Box\Option\SaturdayDelivery $option */ + $option = $options[4]; + $this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\SaturdayDelivery', $option); + + /** @var Box\Option\CashOnDelivery $option */ + $option = $options[5]; + $this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\CashOnDelivery', $option); + $this->assertSame(1234.56, $option->getAmount()); + $this->assertSame('BE19 2100 2350 8812', $option->getIban()); + $this->assertSame('GEBABEBB', $option->getBic()); } private function getFetchOrderWithReferenceXml() @@ -198,7 +235,7 @@ private function getFetchOrderXml() - 1234 + 1234.56 BE19 2100 2350 8812 GEBABEBB