/** * @return Order */ protected function createAtHomeOrderObject() { // create order $orderId = time(); $order = new Order($orderId); $order->setCostCenter('Cost Center'); // add lines $line1 = new OrderLine('Beer', 1); $order->addLine($line1); $line2 = new OrderLine('Whisky', 100); $order->addLine($line2); // add box $address = new Address(); $address->setStreetName('Afrikalaan'); $address->setNumber('289'); $address->setPostalCode('9000'); $address->setLocality('Gent'); $address->setCountryCode('BE'); $sender = new Sender(); $sender->setAddress($address); $sender->setName('Tijs Verkoyen'); $sender->setCompany('Sumo Coders'); $sender->setPhoneNumber('+32 9 395 02 51'); $sender->setEmailAddress('*****@*****.**'); $box = new Box(); $box->setSender($sender); $box->setRemark('Remark'); // add label $address = new Address(); $address->setStreetName('Kerkstraat'); $address->setNumber('108'); $address->setPostalCode('9050'); $address->setLocality('Gentbrugge'); $address->setCountryCode('BE'); $receiver = new Receiver(); $receiver->setAddress($address); $receiver->setName('Tijs Verkoyen'); $receiver->setCompany('Sumo Coders'); $receiver->setPhoneNumber('+32 9 395 02 51'); $receiver->setEmailAddress('*****@*****.**'); // options $option = new Messaging('infoDistributed', 'NL', '*****@*****.**'); // @Home $atHome = new AtHome(); $atHome->setProduct('bpack 24h Pro'); $atHome->setWeight(2000); $atHome->setReceiver($receiver); $atHome->addOption($option); $box->setNationalBox($atHome); $order->addBox($box); return $order; }
/** * Tests Box->toXML */ public function testInternationalToXML() { $data = array('sender' => array('name' => 'Tijs Verkoyen', 'company' => 'Sumo Coders', 'address' => array('streetName' => 'Afrikalaan', 'number' => '289', 'box' => '3', 'postalCode' => '9000', 'locality' => 'Gent', 'countryCode' => 'BE'), 'emailAddress' => '*****@*****.**', 'phoneNumber' => '+32 9 395 02 51'), 'internationalBox' => array('international' => array('product' => 'bpack World Express Pro', 'receiver' => array('name' => 'Tijs Verkoyen', 'company' => 'Sumo Coders', 'address' => array('streetName' => 'Kerkstraat', 'number' => '108', 'postalCode' => '9050', 'locality' => 'Gentbrugge', 'countryCode' => 'BE'), 'emailAddress' => '*****@*****.**', 'phoneNumber' => '+32 9 395 02 51'))), 'remark' => 'remark'); $expectedDocument = self::createDomDocument(); $box = $expectedDocument->createElement('box'); $expectedDocument->appendChild($box); $sender = $expectedDocument->createElement('sender'); foreach ($data['sender'] as $key => $value) { $key = 'common:' . $key; if ($key == 'common:address') { $address = $expectedDocument->createElement($key); foreach ($value as $key2 => $value2) { $key2 = 'common:' . $key2; $address->appendChild($expectedDocument->createElement($key2, $value2)); } $sender->appendChild($address); } else { $sender->appendChild($expectedDocument->createElement($key, $value)); } } $nationalBox = $expectedDocument->createElement('internationalBox'); $atHome = $expectedDocument->createElement('international:international'); $nationalBox->appendChild($atHome); $atHome->appendChild($expectedDocument->createElement('international:product', $data['internationalBox']['international']['product'])); $receiver = $expectedDocument->createElement('international:receiver'); $atHome->appendChild($receiver); foreach ($data['internationalBox']['international']['receiver'] as $key => $value) { $key = 'common:' . $key; if ($key == 'common:address') { $address = $expectedDocument->createElement($key); foreach ($value as $key2 => $value2) { $key2 = 'common:' . $key2; $address->appendChild($expectedDocument->createElement($key2, $value2)); } $receiver->appendChild($address); } else { $receiver->appendChild($expectedDocument->createElement($key, $value)); } } $box->appendChild($sender); $box->appendChild($nationalBox); $box->appendChild($expectedDocument->createElement('remark', $data['remark'])); $actualDocument = self::createDomDocument(); $address = new Address($data['sender']['address']['streetName'], $data['sender']['address']['number'], $data['sender']['address']['box'], $data['sender']['address']['postalCode'], $data['sender']['address']['locality'], $data['sender']['address']['countryCode']); $sender = new Sender(); $sender->setName($data['sender']['name']); $sender->setCompany($data['sender']['company']); $sender->setAddress($address); $sender->setEmailAddress($data['sender']['emailAddress']); $sender->setPhoneNumber($data['sender']['phoneNumber']); $address = new Address($data['internationalBox']['international']['receiver']['address']['streetName'], $data['internationalBox']['international']['receiver']['address']['number'], null, $data['internationalBox']['international']['receiver']['address']['postalCode'], $data['internationalBox']['international']['receiver']['address']['locality'], $data['internationalBox']['international']['receiver']['address']['countryCode']); $receiver = new Receiver(); $receiver->setAddress($address); $receiver->setName($data['internationalBox']['international']['receiver']['name']); $receiver->setCompany($data['internationalBox']['international']['receiver']['company']); $receiver->setPhoneNumber($data['internationalBox']['international']['receiver']['phoneNumber']); $receiver->setEmailAddress($data['internationalBox']['international']['receiver']['emailAddress']); $international = new International(); $international->setProduct($data['internationalBox']['international']['product']); $international->setReceiver($receiver); $box = new Box(); $box->setSender($sender); $box->setInternationalBox($international); $box->setRemark($data['remark']); $actualDocument->appendChild($box->toXML($actualDocument, null)); $this->assertEquals($expectedDocument, $actualDocument); }
/** * @param \SimpleXMLElement $xml * @return Box */ public static function createFromXML(\SimpleXMLElement $xml) { $box = new Box(); if (isset($xml->sender)) { $box->setSender(Sender::createFromXML($xml->sender->children('http://schema.post.be/shm/deepintegration/v3/common'))); } if (isset($xml->nationalBox)) { $nationalBoxData = $xml->nationalBox->children('http://schema.post.be/shm/deepintegration/v3/national'); // build classname based on the tag name $className = '\\TijsVerkoyen\\Bpost\\Bpost\\Order\\Box\\' . ucfirst($nationalBoxData->getName()); if ($nationalBoxData->getName() == 'at24-7') { $className = '\\TijsVerkoyen\\Bpost\\Bpost\\Order\\Box\\At247'; } if (!method_exists($className, 'createFromXML')) { throw new Exception('Not Implemented'); } $nationalBox = call_user_func(array($className, 'createFromXML'), $nationalBoxData); $box->setNationalBox($nationalBox); } if (isset($xml->internationalBox)) { $internationalBoxData = $xml->internationalBox->children('http://schema.post.be/shm/deepintegration/v3/international'); // build classname based on the tag name $className = '\\TijsVerkoyen\\Bpost\\Bpost\\Order\\Box\\' . ucfirst($internationalBoxData->getName()); if (!method_exists($className, 'createFromXML')) { var_dump($className); throw new Exception('Not Implemented'); } $internationalBox = call_user_func(array($className, 'createFromXML'), $internationalBoxData); $box->setInternationalBox($internationalBox); } if (isset($xml->remark) && $xml->remark != '') { $box->setRemark((string) $xml->remark); } if (isset($xml->status) && $xml->status != '') { $box->setStatus((string) $xml->status); } return $box; }
/** * Tests Order->toXML */ public function testToXML() { $data = array('accountId' => ACCOUNT_ID, 'reference' => 'reference ' . time(), 'costCenter' => 'costcenter ' . time(), 'orderLine' => array(array('text' => 'Beer ' . time(), 'nbOfItems' => rand(1, 10)), array('text' => 'Whisky ' . time(), 'nbOfItems' => rand(1, 10))), 'box' => array(array('sender' => array('name' => 'Tijs Verkoyen', 'company' => 'Sumo Coders', 'address' => array('streetName' => 'Afrikalaan', 'number' => '289', 'postalCode' => '9000', 'locality' => 'Gent', 'countryCode' => 'BE'), 'emailAddress' => '*****@*****.**', 'phoneNumber' => '+32 9 395 02 51'), 'nationalBox' => array('at24-7' => array('product' => 'bpack 24h Pro', 'weight' => 2000, 'parcelsDepotId' => '014472', 'parcelsDepotName' => 'WIJNEGEM', 'parcelsDepotAddress' => array('streetName' => 'Turnhoutsebaan', 'number' => '468', 'box' => 'A', 'postalCode' => '2110', 'locality' => 'Wijnegem', 'countryCode' => 'BE'), 'memberId' => '188565346', 'receiverName' => 'Tijs Verkoyen', 'receiverCompany' => 'Sumo Coders')), 'remark' => 'remark ' . time()))); $expectedDocument = self::createDomDocument(); $order = $expectedDocument->createElement('tns:order'); $order->setAttribute('xmlns:common', 'http://schema.post.be/shm/deepintegration/v3/common'); $order->setAttribute('xmlns:tns', 'http://schema.post.be/shm/deepintegration/v3/'); $order->setAttribute('xmlns', 'http://schema.post.be/shm/deepintegration/v3/national'); $order->setAttribute('xmlns:international', 'http://schema.post.be/shm/deepintegration/v3/international'); $order->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $order->setAttribute('xsi:schemaLocation', 'http://schema.post.be/shm/deepintegration/v3/'); $expectedDocument->appendChild($order); $order->appendChild($expectedDocument->createElement('tns:accountId', $data['accountId'])); $order->appendChild($expectedDocument->createElement('tns:reference', $data['reference'])); $order->appendChild($expectedDocument->createElement('tns:costCenter', $data['costCenter'])); foreach ($data['orderLine'] as $row) { $line = $expectedDocument->createElement('tns:orderLine'); $line->appendChild($expectedDocument->createElement('tns:text', $row['text'])); $line->appendChild($expectedDocument->createElement('tns:nbOfItems', $row['nbOfItems'])); $order->appendChild($line); } $box = $expectedDocument->createElement('tns:box'); $order->appendChild($box); $sender = $expectedDocument->createElement('tns:sender'); foreach ($data['box'][0]['sender'] as $key => $value) { $key = 'common:' . $key; if ($key == 'common:address') { $address = $expectedDocument->createElement($key); foreach ($value as $key2 => $value2) { $key2 = 'common:' . $key2; $address->appendChild($expectedDocument->createElement($key2, $value2)); } $sender->appendChild($address); } else { $sender->appendChild($expectedDocument->createElement($key, $value)); } } $box->appendChild($sender); $nationalBox = $expectedDocument->createElement('tns:nationalBox'); $at247 = $expectedDocument->createElement('at24-7'); $nationalBox->appendChild($at247); $expectedDocument->appendChild($nationalBox); foreach ($data['box'][0]['nationalBox']['at24-7'] as $key => $value) { if ($key == 'parcelsDepotAddress') { $address = $expectedDocument->createElement($key); foreach ($value as $key2 => $value2) { $key2 = 'common:' . $key2; $address->appendChild($expectedDocument->createElement($key2, $value2)); } $at247->appendChild($address); } else { $at247->appendChild($expectedDocument->createElement($key, $value)); } } $box->appendChild($nationalBox); $box->appendChild($expectedDocument->createElement('tns:remark', $data['box'][0]['remark'])); $actualDocument = self::createDomDocument(); $address = new Address($data['box'][0]['sender']['address']['streetName'], $data['box'][0]['sender']['address']['number'], null, $data['box'][0]['sender']['address']['postalCode'], $data['box'][0]['sender']['address']['locality'], $data['box'][0]['sender']['address']['countryCode']); $sender = new Sender(); $sender->setName($data['box'][0]['sender']['name']); $sender->setCompany($data['box'][0]['sender']['company']); $sender->setAddress($address); $sender->setEmailAddress($data['box'][0]['sender']['emailAddress']); $sender->setPhoneNumber($data['box'][0]['sender']['phoneNumber']); $parcelsDepotAddress = new ParcelsDepotAddress($data['box'][0]['nationalBox']['at24-7']['parcelsDepotAddress']['streetName'], $data['box'][0]['nationalBox']['at24-7']['parcelsDepotAddress']['number'], $data['box'][0]['nationalBox']['at24-7']['parcelsDepotAddress']['box'], $data['box'][0]['nationalBox']['at24-7']['parcelsDepotAddress']['postalCode'], $data['box'][0]['nationalBox']['at24-7']['parcelsDepotAddress']['locality'], $data['box'][0]['nationalBox']['at24-7']['parcelsDepotAddress']['countryCode']); $at247 = new At247(); $at247->setProduct($data['box'][0]['nationalBox']['at24-7']['product']); $at247->setWeight($data['box'][0]['nationalBox']['at24-7']['weight']); $at247->setParcelsDepotId($data['box'][0]['nationalBox']['at24-7']['parcelsDepotId']); $at247->setParcelsDepotName($data['box'][0]['nationalBox']['at24-7']['parcelsDepotName']); $at247->setParcelsDepotAddress($parcelsDepotAddress); $at247->setMemberId($data['box'][0]['nationalBox']['at24-7']['memberId']); $at247->setReceiverName($data['box'][0]['nationalBox']['at24-7']['receiverName']); $at247->setReceiverCompany($data['box'][0]['nationalBox']['at24-7']['receiverCompany']); $box = new Box(); $box->setSender($sender); $box->setNationalBox($at247); $box->setRemark($data['box'][0]['remark']); $order = new Order($data['reference']); $order->setCostCenter($data['costCenter']); $line1 = new Order\Line($data['orderLine'][0]['text'], $data['orderLine'][0]['nbOfItems']); $line2 = new Line($data['orderLine'][1]['text'], $data['orderLine'][1]['nbOfItems']); $order->addLine($line1); $order->addLine($line2); // I know, the line below is kinda bogus, but it will make sure all code is tested $order->setLines(array($line1, $line2)); $order->addBox($box); // I know, the line below is kinda bogus, but it will make sure all code is tested $order->setBoxes(array($box)); $actualDocument->appendChild($order->toXML($actualDocument, ACCOUNT_ID)); $this->assertEquals($expectedDocument, $actualDocument); try { $xml = simplexml_load_string('<order> </order>'); $order = Order::createFromXML($xml); } catch (\Exception $e) { $this->assertInstanceOf('TijsVerkoyen\\Bpost\\Exception', $e); $this->assertEquals('No reference found.', $e->getMessage()); } }
$order->addLine($line2); // add box $address = new Address(); $address->setStreetName('Afrikalaan'); $address->setNumber('289'); $address->setPostalCode('9000'); $address->setLocality('Gent'); $address->setCountryCode('BE'); $sender = new Sender(); $sender->setAddress($address); $sender->setName('Tijs Verkoyen'); $sender->setCompany('Sumo Coders'); $sender->setPhoneNumber('+32 9 395 02 51'); $sender->setEmailAddress('*****@*****.**'); $box = new Box(); $box->setSender($sender); $box->setRemark('Remark'); // add label $address = new Address(); $address->setStreetName('Kerkstraat'); $address->setNumber('108'); $address->setPostalCode('9050'); $address->setLocality('Gentbrugge'); $address->setCountryCode('BE'); $receiver = new Receiver(); $receiver->setAddress($address); $receiver->setName('Tijs Verkoyen'); $receiver->setCompany('Sumo Coders'); $receiver->setPhoneNumber('+32 9 395 02 51'); $receiver->setEmailAddress('*****@*****.**'); // options