/** * Tests Customer->toXML */ public function testToXML() { $data = 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'); $expectedDocument = self::createDomDocument(); $sender = $expectedDocument->createElement('customer'); foreach ($data 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)); } } $expectedDocument->appendChild($sender); $actualDocument = self::createDomDocument(); $address = new Address($data['address']['streetName'], $data['address']['number'], $data['address']['box'], $data['address']['postalCode'], $data['address']['locality'], $data['address']['countryCode']); $customer = new Customer(); $customer->setName($data['name']); $customer->setCompany($data['company']); $customer->setAddress($address); $customer->setEmailAddress($data['emailAddress']); $customer->setPhoneNumber($data['phoneNumber']); $actualDocument->appendChild($customer->toXML($actualDocument, null)); $this->assertEquals($expectedDocument, $actualDocument); }
/** * @param \SimpleXMLElement $xml * @param Customer $instance * @return Customer */ public static function createFromXMLHelper(\SimpleXMLElement $xml, Customer $instance) { if (isset($xml->name) && $xml->name != '') { $instance->setName((string) $xml->name); } if (isset($xml->company) && $xml->company != '') { $instance->setCompany((string) $xml->company); } if (isset($xml->address)) { $instance->setAddress(Address::createFromXML($xml->address)); } if (isset($xml->emailAddress) && $xml->emailAddress != '') { $instance->setEmailAddress((string) $xml->emailAddress); } if (isset($xml->phoneNumber) && $xml->phoneNumber != '') { $instance->setPhoneNumber((string) $xml->phoneNumber); } return $instance; }