/** * Test validation in the setters */ public function testFaultyProperties() { $address = new Address(); try { $address->setBox(str_repeat('a', 9)); } catch (\Exception $e) { $this->assertInstanceOf('TijsVerkoyen\\Bpost\\Exception', $e); $this->assertEquals('Invalid length, maximum is 8.', $e->getMessage()); } try { $address->setCountryCode(str_repeat('a', 3)); } catch (\Exception $e) { $this->assertInstanceOf('TijsVerkoyen\\Bpost\\Exception', $e); $this->assertEquals('Invalid length, maximum is 2.', $e->getMessage()); } try { $address->setLocality(str_repeat('a', 41)); } catch (\Exception $e) { $this->assertInstanceOf('TijsVerkoyen\\Bpost\\Exception', $e); $this->assertEquals('Invalid length, maximum is 40.', $e->getMessage()); } try { $address->setNumber(str_repeat('a', 9)); } catch (\Exception $e) { $this->assertInstanceOf('TijsVerkoyen\\Bpost\\Exception', $e); $this->assertEquals('Invalid length, maximum is 8.', $e->getMessage()); } try { $address->setPostalCode(str_repeat('a', 41)); } catch (\Exception $e) { $this->assertInstanceOf('TijsVerkoyen\\Bpost\\Exception', $e); $this->assertEquals('Invalid length, maximum is 40.', $e->getMessage()); } try { $address->setStreetName(str_repeat('a', 41)); } catch (\Exception $e) { $this->assertInstanceOf('TijsVerkoyen\\Bpost\\Exception', $e); $this->assertEquals('Invalid length, maximum is 40.', $e->getMessage()); } }
/** * @param \SimpleXMLElement $xml * @return Address */ public static function createFromXML(\SimpleXMLElement $xml) { $address = new Address(); if (isset($xml->streetName) && $xml->streetName != '') { $address->setStreetName((string) $xml->streetName); } if (isset($xml->number) && $xml->number != '') { $address->setNumber((string) $xml->number); } if (isset($xml->box) && $xml->box != '') { $address->setBox((string) $xml->box); } if (isset($xml->postalCode) && $xml->postalCode != '') { $address->setPostalCode((string) $xml->postalCode); } if (isset($xml->locality) && $xml->locality != '') { $address->setLocality((string) $xml->locality); } if (isset($xml->countryCode) && $xml->countryCode != '') { $address->setCountryCode((string) $xml->countryCode); } return $address; }