From a7c8233f5d52066f350a9de839d16ec30c4be37a Mon Sep 17 00:00:00 2001 From: Jason Judge Date: Sun, 10 Jul 2016 23:31:22 +0100 Subject: [PATCH] Issue #15 Start putting some tests in. --- src/AbstractMessage.php | 2 +- src/Model/Address.php | 2 +- tests/Model/AddressTest.php | 194 ++++++++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 tests/Model/AddressTest.php diff --git a/src/AbstractMessage.php b/src/AbstractMessage.php index 65f8ab0..c639035 100644 --- a/src/AbstractMessage.php +++ b/src/AbstractMessage.php @@ -19,7 +19,7 @@ abstract class AbstractMessage */ public static function constantList($prefix = null) { - $reflection = new ReflectionClass(__CLASS__); + $reflection = new ReflectionClass(get_called_class()); $constants = $reflection->getConstants(); if (isset($prefix)) { diff --git a/src/Model/Address.php b/src/Model/Address.php index 124c689..eef0f2c 100644 --- a/src/Model/Address.php +++ b/src/Model/Address.php @@ -32,7 +32,7 @@ class Address implements AddressInterface * @param string $country The country ISO 3166-2 two-letter code * @param string $state The last two letters of the ISO 3166-2:US state code */ - public function __construct($address1, $address2, $city, $postalCode, $country, $state) + public function __construct($address1, $address2, $city, $postalCode, $country, $state = null) { // These fields are always mandatory. foreach(array('address1', 'city', 'country') as $field_name) { diff --git a/tests/Model/AddressTest.php b/tests/Model/AddressTest.php new file mode 100644 index 0000000..62998d8 --- /dev/null +++ b/tests/Model/AddressTest.php @@ -0,0 +1,194 @@ + 'Address1', + 'address2' => 'Address2', + 'city' => 'City', + 'postalCode' => 'NE26 2SB', + 'country' => 'GB', + ]; + + protected $simpleUS = [ + 'address1' => 'Address1', + 'address2' => 'Address2', + 'city' => 'City', + 'postalCode' => 'NE26 2SB', + 'country' => 'US', + 'state' => 'AL', + ]; + + public function testSimpleValidUS() + { + $address = new Address( + $this->simpleUS['address1'], + $this->simpleUS['address2'], + $this->simpleUS['city'], + $this->simpleUS['postalCode'], + $this->simpleUS['country'], + $this->simpleUS['state'] + ); + + // A simple address with no prefix. + $this->assertEquals( + json_encode($address), + '{"address1":"Address1","address2":"Address2","city":"City","postalCode":"NE26 2SB","country":"US","state":"AL"}' + ); + + $address = $address->withFieldPrefix('prefix'); + + // A simple address with a field name prefix. + $this->assertEquals( + json_encode($address), + '{"prefixAddress1":"Address1","prefixAddress2":"Address2","prefixCity":"City","prefixPostalCode":"NE26 2SB","prefixCountry":"US","prefixState":"AL"}' + ); + } + + public function testSimpleValidGB() + { + $address = new Address( + $this->simpleGB['address1'], + $this->simpleGB['address2'], + $this->simpleGB['city'], + $this->simpleGB['postalCode'], + $this->simpleGB['country'] + ); + + // A simple address with no prefix. + $this->assertEquals( + json_encode($address), + '{"address1":"Address1","address2":"Address2","city":"City","postalCode":"NE26 2SB","country":"GB"}' + ); + + $address = $address->withFieldPrefix('prefix'); + + // A simple address with a field name prefix. + $this->assertEquals( + json_encode($address), + '{"prefixAddress1":"Address1","prefixAddress2":"Address2","prefixCity":"City","prefixPostalCode":"NE26 2SB","prefixCountry":"GB"}' + ); + } + + public function testSimpleValidFromDataGB() + { + $address = Address::fromData($this->simpleGB); + + // A simple address with no prefix. + $this->assertEquals( + json_encode($address), + '{"address1":"Address1","address2":"Address2","city":"City","postalCode":"NE26 2SB","country":"GB"}' + ); + } + + public function testSimpleValidFromDataUS() + { + $address = Address::fromData($this->simpleUS); + + // A simple address with no prefix. + $this->assertEquals( + json_encode($address), + '{"address1":"Address1","address2":"Address2","city":"City","postalCode":"NE26 2SB","country":"US","state":"AL"}' + ); + } + + /** + * @expectedException UnexpectedValueException + */ + public function testMissingAddress1() + { + $data = $this->simpleUS; + unset($data['address1']); + $address = Address::fromData($data); + } + + /** + * @expectedException UnexpectedValueException + */ + public function testMissingCity() + { + $data = $this->simpleUS; + unset($data['city']); + $address = Address::fromData($data); + } + + /** + * @expectedException UnexpectedValueException + */ + public function testMissingCountry() + { + $data = $this->simpleUS; + unset($data['country']); + $address = Address::fromData($data); + } + + /** + * @expectedException UnexpectedValueException + */ + public function testMissingState() + { + $data = $this->simpleUS; + unset($data['state']); + $address = Address::fromData($data); + } + + /** + * @expectedException UnexpectedValueException + */ + public function testInvalidState() + { + $data = $this->simpleUS; + $data['state'] = 'XX'; + $address = Address::fromData($data); + } + + /** + * @expectedException UnexpectedValueException + */ + public function testInvalidCountry() + { + $data = $this->simpleUS; + $data['country'] = 'XX'; + $address = Address::fromData($data); + } + + /** + * @expectedException UnexpectedValueException + */ + public function testUnexpectedState() + { + $data = $this->simpleGB; + $data['state'] = 'AL'; + $address = Address::fromData($data); + } + + /** + * @expectedException UnexpectedValueException + */ + public function testMissingPostalCode() + { + $data = $this->simpleUS; + unset($data['postalCode']); + $address = Address::fromData($data); + } + + /** + * Postal code is optional for IE. + */ + public function testMissingPostalCodeIE() + { + $data = $this->simpleGB; + $data['country'] = 'IE'; + + // Valid with a postalCode (no exceptions). + $address1 = Address::fromData($data); + + // Valid without a postalCode (no exceptions). + unset($data['postalCode']); + $address2 = Address::fromData($data); + } +}