Skip to content

Commit dc1202c

Browse files
committed
fix: only the first option was filled
1 parent e02ca4b commit dc1202c

File tree

11 files changed

+397
-52
lines changed

11 files changed

+397
-52
lines changed

src/Bpost/Order/Box/National.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
use Bpost\BpostApiClient\Bpost;
66
use Bpost\BpostApiClient\Bpost\Order\Box\OpeningHour\Day;
7+
use Bpost\BpostApiClient\Bpost\Order\Box\Option\CashOnDelivery;
78
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
89
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Option;
910
use Bpost\BpostApiClient\BpostException;
1011
use Bpost\BpostApiClient\Common\ComplexAttribute;
1112
use Bpost\BpostApiClient\Common\XmlHelper;
13+
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
14+
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
1215
use Bpost\BpostApiClient\Exception\BpostNotImplementedException;
1316
use Bpost\BpostApiClient\Exception\XmlException\BpostXmlInvalidItemException;
1417
use DomDocument;
@@ -232,24 +235,10 @@ public static function createFromXML(SimpleXMLElement $nationalXml, National $se
232235
);
233236
}
234237

235-
if (isset($nationalXml->options) && !empty($nationalXml->options)) {
238+
if (!empty($nationalXml->options)) {
236239
/** @var SimpleXMLElement $optionData */
237-
foreach ($nationalXml->options as $optionData) {
238-
$optionData = $optionData->children(Bpost::NS_V3_COMMON);
239-
240-
if (in_array($optionData->getName(), array(
241-
Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED,
242-
Messaging::MESSAGING_TYPE_INFO_NEXT_DAY,
243-
Messaging::MESSAGING_TYPE_INFO_REMINDER,
244-
Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED,
245-
))
246-
) {
247-
$option = Messaging::createFromXML($optionData);
248-
} else {
249-
$option = self::getOptionFromOptionData($optionData);
250-
}
251-
252-
$self->addOption($option);
240+
foreach ($nationalXml->options->children(Bpost::NS_V3_COMMON) as $optionData) {
241+
$self->addOption(self::getOptionFromOptionData($optionData));
253242
}
254243
}
255244

@@ -280,15 +269,30 @@ public static function createFromXML(SimpleXMLElement $nationalXml, National $se
280269
* @return Option
281270
*
282271
* @throws BpostNotImplementedException
272+
* @throws BpostInvalidLengthException
273+
* @throws BpostInvalidValueException
283274
*/
284275
protected static function getOptionFromOptionData(SimpleXMLElement $optionData)
285276
{
286-
$className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName());
287-
XmlHelper::assertMethodCreateFromXmlExists($className);
277+
switch ($optionData->getName()) {
278+
case Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED:
279+
case Messaging::MESSAGING_TYPE_INFO_NEXT_DAY:
280+
case Messaging::MESSAGING_TYPE_INFO_REMINDER:
281+
case Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED:
282+
return Messaging::createFromXML($optionData);
288283

289-
return call_user_func(
290-
array($className, 'createFromXML'),
291-
$optionData
292-
);
284+
case 'cod':
285+
return CashOnDelivery::createFromXML($optionData);
286+
287+
default:
288+
$className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName());
289+
290+
XmlHelper::assertMethodCreateFromXmlExists($className);
291+
292+
return call_user_func(
293+
array($className, 'createFromXML'),
294+
$optionData
295+
);
296+
}
293297
}
294298
}

src/Bpost/Order/Box/Option/AutomaticSecondPresentation.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,4 @@ public function toXML(DOMDocument $document, $prefix = 'common')
3131
{
3232
return $document->createElement(XmlHelper::getPrefixedTagName('automaticSecondPresentation', $prefix));
3333
}
34-
35-
/**
36-
* @param SimpleXMLElement $xml
37-
*
38-
* @return static
39-
*/
40-
public static function createFromXML(SimpleXMLElement $xml)
41-
{
42-
return new static();
43-
}
4434
}

src/Bpost/Order/Box/Option/CashOnDelivery.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace Bpost\BpostApiClient\Bpost\Order\Box\Option;
44

5+
use Bpost\BpostApiClient\Bpost;
56
use Bpost\BpostApiClient\Common\XmlHelper;
7+
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
8+
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
69
use DomDocument;
710
use DomElement;
11+
use SimpleXMLElement;
812

913
/**
1014
* bPost CashOnDelivery class
@@ -132,4 +136,19 @@ public function toXML(DOMDocument $document, $prefix = 'common')
132136

133137
return $cod;
134138
}
139+
140+
/**
141+
* @param SimpleXMLElement $xml
142+
*
143+
* @return static
144+
*
145+
* @throws BpostInvalidLengthException
146+
* @throws BpostInvalidValueException
147+
*/
148+
public static function createFromXML(SimpleXMLElement $xml)
149+
{
150+
$details = $xml->children(Bpost::NS_V3_COMMON);
151+
152+
return new static(floatval($details->codAmount), (string) $details->iban, (string) $details->bic);
153+
}
135154
}

src/Bpost/Order/Box/Option/Insured.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,9 @@ public function toXML(DOMDocument $document, $prefix = 'common')
166166
*/
167167
public static function createFromXML(SimpleXMLElement $xml)
168168
{
169-
$insuranceDetail = $xml->children('common', true);
170-
169+
$insuranceDetail = $xml->children(Bpost::NS_V3_COMMON);
171170
$type = $insuranceDetail->getName();
172-
$value = $insuranceDetail->attributes()->value !== null ? (int) $insuranceDetail->attributes()->value : null;
171+
$value = intval($insuranceDetail->attributes()->value) ?: null;
173172

174173
if ($type === static::INSURANCE_TYPE_ADDITIONAL_INSURANCE && $value === 1) {
175174
$type = static::INSURANCE_TYPE_BASIC_INSURANCE;

src/Bpost/Order/Box/Option/Option.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DOMDocument;
66
use DOMElement;
7+
use SimpleXMLElement;
78

89
/**
910
* bPost Option class
@@ -24,4 +25,9 @@ abstract class Option
2425
* @return DOMElement
2526
*/
2627
abstract public function toXML(DOMDocument $document, $prefix = null);
28+
29+
public static function createFromXML(SimpleXMLElement $xml)
30+
{
31+
return new static();
32+
}
2733
}

src/Bpost/Order/Box/Option/Signed.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,4 @@ public function toXML(DOMDocument $document, $prefix = 'common')
3131
{
3232
return $document->createElement(XmlHelper::getPrefixedTagName('signed', $prefix));
3333
}
34-
35-
/**
36-
* @param SimpleXMLElement $xml
37-
*
38-
* @return static
39-
*/
40-
public static function createFromXML(SimpleXMLElement $xml)
41-
{
42-
return new static();
43-
}
4434
}

tests/Bpost/Order/Box/NationalTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,35 @@ public function testCreateFromXml()
9797

9898
/** @var Option[] $options */
9999
$options = $self->getOptions();
100-
$this->assertNotNull($options);
101-
// @todo Fix options feeding and test it
100+
$this->assertCount(4, $options);
101+
102+
/** @var Messaging $option */
103+
$option = $options[0];
104+
$this->assertSame('Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging', get_class($option));
105+
$this->assertSame(Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED, $option->getType());
106+
$this->assertSame(Messaging::MESSAGING_LANGUAGE_EN, $option->getLanguage());
107+
$this->assertNull($option->getEmailAddress());
108+
$this->assertSame('0476123456', $option->getMobilePhone());
109+
110+
/** @var Messaging $option */
111+
$option = $options[1];
112+
$this->assertSame('Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging', get_class($option));
113+
$this->assertSame(Messaging::MESSAGING_TYPE_INFO_NEXT_DAY, $option->getType());
114+
$this->assertSame(Messaging::MESSAGING_LANGUAGE_EN, $option->getLanguage());
115+
$this->assertSame('receiver@mail.be', $option->getEmailAddress());
116+
$this->assertNull($option->getMobilePhone());
117+
118+
/** @var Messaging $option */
119+
$option = $options[2];
120+
$this->assertSame('Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging', get_class($option));
121+
$this->assertSame(Messaging::MESSAGING_TYPE_INFO_REMINDER, $option->getType());
122+
$this->assertSame(Messaging::MESSAGING_LANGUAGE_EN, $option->getLanguage());
123+
$this->assertNull($option->getEmailAddress());
124+
$this->assertSame('0032475123456', $option->getMobilePhone());
125+
126+
/** @var SaturdayDelivery $option */
127+
$option = $options[3];
128+
$this->assertSame('Bpost\BpostApiClient\Bpost\Order\Box\Option\SaturdayDelivery', get_class($option));
102129

103130
$this->assertSame(500, $self->getWeight());
104131

tests/Bpost/Order/Box/Option/InsuranceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private function getAdditionalValue1Xml()
120120
{
121121
return <<<XML
122122
<?xml version="1.0" encoding="UTF-8"?>
123-
<common:insured xmlns="http://schema.post.be/shm/deepintegration/v5/national" xmlns:common="http://schema.post.be/shm/deepintegration/v5/common" xmlns:tns="http://schema.post.be/shm/deepintegration/v5/" xmlns:international="http://schema.post.be/shm/deepintegration/v5/international" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schema.post.be/shm/deepintegration/v5/">
123+
<common:insured xmlns="http://schema.post.be/shm/deepintegration/v3/national" xmlns:common="http://schema.post.be/shm/deepintegration/v3/common" xmlns:tns="http://schema.post.be/shm/deepintegration/v3/" xmlns:international="http://schema.post.be/shm/deepintegration/v3/international" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schema.post.be/shm/deepintegration/v3/">
124124
<common:additionalInsurance value="1"/>
125125
</common:insured>
126126

tests/Bpost/OrderTest.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,45 @@ public function testCreateFromXml()
135135
$this->assertSame('2016-03-19+01:00', $nationalBox->getRequestedDeliveryDate());
136136
$this->assertSame('Rue de l\'Autonomie', $nationalBox->getPugoAddress()->getStreetName());
137137

138-
$this->assertNotNull($nationalBox->getOptions());
139-
// $this->assertCount(6, $nationalBox->getOptions());
138+
$this->assertCount(6, $nationalBox->getOptions());
139+
$options = $nationalBox->getOptions();
140+
141+
/** @var Box\Option\Messaging $option */
142+
$option = $options[0];
143+
$this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging', $option);
144+
$this->assertSame(Box\Option\Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED, $option->getType());
145+
$this->assertSame(Box\Option\Messaging::MESSAGING_LANGUAGE_FR, $option->getLanguage());
146+
$this->assertNull($option->getMobilePhone());
147+
$this->assertSame('pomme@antidot.com', $option->getEmailAddress());
148+
149+
/** @var Box\Option\Messaging $option */
150+
$option = $options[1];
151+
$this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging', $option);
152+
$this->assertSame(Box\Option\Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED, $option->getType());
153+
$this->assertSame(Box\Option\Messaging::MESSAGING_LANGUAGE_EN, $option->getLanguage());
154+
$this->assertNull($option->getMobilePhone());
155+
$this->assertSame('pomme@antidot.com', $option->getEmailAddress());
156+
157+
/** @var Box\Option\Insured $option */
158+
$option = $options[2];
159+
$this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\Insured', $option);
160+
$this->assertSame(Box\Option\Insured::INSURANCE_TYPE_ADDITIONAL_INSURANCE, $option->getType());
161+
$this->assertSame(Box\Option\Insured::INSURANCE_AMOUNT_UP_TO_2500_EUROS, $option->getValue());
162+
163+
/** @var Box\Option\Signed $option */
164+
$option = $options[3];
165+
$this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\Signed', $option);
166+
167+
/** @var Box\Option\SaturdayDelivery $option */
168+
$option = $options[4];
169+
$this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\SaturdayDelivery', $option);
170+
171+
/** @var Box\Option\CashOnDelivery $option */
172+
$option = $options[5];
173+
$this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\Option\CashOnDelivery', $option);
174+
$this->assertSame(1234.56, $option->getAmount());
175+
$this->assertSame('BE19 2100 2350 8812', $option->getIban());
176+
$this->assertSame('GEBABEBB', $option->getBic());
140177
}
141178

142179
private function getFetchOrderWithReferenceXml()
@@ -198,7 +235,7 @@ private function getFetchOrderXml()
198235
<ns2:signed/>
199236
<ns2:saturdayDelivery/>
200237
<ns2:cod>
201-
<ns2:codAmount>1234</ns2:codAmount>
238+
<ns2:codAmount>1234.56</ns2:codAmount>
202239
<ns2:iban>BE19 2100 2350 8812</ns2:iban>
203240
<ns2:bic>GEBABEBB</ns2:bic>
204241
</ns2:cod>

0 commit comments

Comments
 (0)