Skip to content

Commit

Permalink
fix: only the first option was filled
Browse files Browse the repository at this point in the history
  • Loading branch information
kouinkouin committed Oct 15, 2024
1 parent e02ca4b commit 413a417
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 51 deletions.
50 changes: 27 additions & 23 deletions src/Bpost/Order/Box/National.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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
);
}
}
}
10 changes: 0 additions & 10 deletions src/Bpost/Order/Box/Option/AutomaticSecondPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
19 changes: 19 additions & 0 deletions src/Bpost/Order/Box/Option/CashOnDelivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
5 changes: 2 additions & 3 deletions src/Bpost/Order/Box/Option/Insured.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/Bpost/Order/Box/Option/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DOMDocument;
use DOMElement;
use SimpleXMLElement;

/**
* bPost Option class
Expand All @@ -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();
}
}
10 changes: 0 additions & 10 deletions src/Bpost/Order/Box/Option/Signed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
31 changes: 29 additions & 2 deletions tests/Bpost/Order/Box/NationalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]', $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());

Expand Down
43 changes: 40 additions & 3 deletions tests/Bpost/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]', $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('[email protected]', $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()
Expand Down Expand Up @@ -198,7 +235,7 @@ private function getFetchOrderXml()
<ns2:signed/>
<ns2:saturdayDelivery/>
<ns2:cod>
<ns2:codAmount>1234</ns2:codAmount>
<ns2:codAmount>1234.56</ns2:codAmount>
<ns2:iban>BE19 2100 2350 8812</ns2:iban>
<ns2:bic>GEBABEBB</ns2:bic>
</ns2:cod>
Expand Down

0 comments on commit 413a417

Please sign in to comment.