Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EbayEnterprise_Address_Test_Model_ValidatorTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EbayEnterprise_Address_Test_Model_ValidatorTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class EbayEnterprise_Address_Test_Model_ValidatorTest extends EbayEnterprise_Eb2cCore_Test_Base |
||
| 22 | { |
||
| 23 | /** @var eBayEnterprise\RetailOrderManagement\Api\IBidirectionalApi (mock) */ |
||
| 24 | protected $_sdkApi; |
||
| 25 | /** @var EbayEnterprise_Eb2cCore_Helper_Data (mock) */ |
||
| 26 | protected $_coreHelper; |
||
| 27 | /** @var EbayEnterprise_Address_Helper_Data (mock) */ |
||
| 28 | protected $_addressHelper; |
||
| 29 | /** @var EbayEnterprise_Eb2cCore_Model_Config_Registry */ |
||
| 30 | protected $_addressConfig; |
||
| 31 | |||
| 32 | public function setUp() |
||
| 33 | { |
||
| 34 | parent::setUp(); |
||
| 35 | $this->_mockCustomerSession(); |
||
| 36 | $this->_mockCheckoutSession(); |
||
| 37 | |||
| 38 | $this->_sdkApi = $this->_mockSdkApi(); |
||
| 39 | |||
| 40 | $this->_coreHelper = $this->getHelperMock('eb2ccore/data', ['getSdkApi']); |
||
| 41 | $this->_coreHelper->expects($this->any()) |
||
| 42 | ->method('getSdkApi') |
||
| 43 | ->will($this->returnValue($this->_sdkApi)); |
||
| 44 | |||
| 45 | // Stub the __get method with config key => value value map in tests that need to mock config |
||
| 46 | $this->_addressConfig = $this->buildCoreConfigRegistry(); |
||
|
|
|||
| 47 | |||
| 48 | $this->_addressHelper = $this->getHelperMock('ebayenterprise_address/data', ['__', 'getConfigModel']); |
||
| 49 | $this->_addressHelper->expects($this->any()) |
||
| 50 | ->method('__') |
||
| 51 | ->will($this->returnArgument(0)); |
||
| 52 | $this->_addressHelper->expects($this->any()) |
||
| 53 | ->method('getConfigModel') |
||
| 54 | ->will($this->returnValue($this->_addressConfig)); |
||
| 55 | |||
| 56 | $this->_validatorRequest = $this->getModelMockBuilder('ebayenterprise_address/validation_request') |
||
| 57 | ->disableOriginalConstructor() |
||
| 58 | ->setMethods(['prepareRequest', 'getRequest']) |
||
| 59 | ->getMock(); |
||
| 60 | $this->_validatorResponse = $this->getModelMockBuilder('ebayenterprise_address/validation_response') |
||
| 61 | ->disableOriginalConstructor() |
||
| 62 | ->setMethods([ |
||
| 63 | 'getValidAddress', 'getOriginalAddress', 'getHasSuggestions', |
||
| 64 | 'getAddressSuggestions', 'isAddressValid' |
||
| 65 | ]) |
||
| 66 | ->getMock(); |
||
| 67 | |||
| 68 | $this->_validator = $this->getModelMockBuilder('ebayenterprise_address/validator') |
||
| 69 | // mock out interactions with validation request and response models, |
||
| 70 | // prevents deep mocking of SDK objects |
||
| 71 | ->setMethods(['_prepareApiForAddressRequest', '_getValidationResponse']) |
||
| 72 | ->setConstructorArgs([['core_helper' => $this->_coreHelper, 'helper' => $this->_addressHelper]]) |
||
| 73 | ->getMock(); |
||
| 74 | $this->_validator->expects($this->any()) |
||
| 75 | ->method('_getValidationResponse') |
||
| 76 | ->will($this->returnValue($this->_validatorResponse)); |
||
| 77 | |||
| 78 | // suppressing the real session from starting |
||
| 79 | $session = $this->getModelMockBuilder('core/session') |
||
| 80 | ->disableOriginalConstructor() |
||
| 81 | ->setMethods(null) |
||
| 82 | ->getMock(); |
||
| 83 | $this->replaceByMock('singleton', 'core/session', $session); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Create an address object to pass off to the validator. |
||
| 88 | * @return Mage_Customer_Model_Address |
||
| 89 | */ |
||
| 90 | protected function _createAddress($fields = []) |
||
| 91 | { |
||
| 92 | $addr = Mage::getModel('customer/address'); |
||
| 93 | $addr->setData($fields); |
||
| 94 | return $addr; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Mock an SDK IBidirectionalApi object. |
||
| 99 | * |
||
| 100 | * @return \eBayEnterprise\RetailOrderManagement\Api\IBidirectionalApi (mock) |
||
| 101 | */ |
||
| 102 | View Code Duplication | protected function _mockSdkApi() |
|
| 103 | { |
||
| 104 | $sdk = $this->getMockBuilder('\eBayEnterprise\RetailOrderManagement\Api\IBidirectionalApi') |
||
| 105 | // Constructor disabled to prevent needing to create and inject |
||
| 106 | // API configuration. |
||
| 107 | ->disableOriginalConstructor() |
||
| 108 | // Payload getters mocked but not set to return anything. The validator |
||
| 109 | // shouln't need to worry about manipulating the payloads so shouldn't |
||
| 110 | // be necessary to create valid or mocked payloads. |
||
| 111 | ->setMethods(['getRequestBody', 'setRequestBody', 'send', 'getResponseBody']) |
||
| 112 | ->getMock(); |
||
| 113 | $sdk->expects($this->any()) |
||
| 114 | ->method('setRequestBody') |
||
| 115 | ->will($this->returnSelf()); |
||
| 116 | $sdk->expects($this->any()) |
||
| 117 | ->method('send') |
||
| 118 | ->will($this->returnSelf()); |
||
| 119 | return $sdk; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Replace the customer session object with a mock. |
||
| 124 | * @return PHPUnit_Framework_MockObject_MockObject - the mock session model |
||
| 125 | */ |
||
| 126 | View Code Duplication | protected function _mockCustomerSession() |
|
| 127 | { |
||
| 128 | $sessionMock = $this->getModelMockBuilder('customer/session') |
||
| 129 | ->disableOriginalConstructor() // This one removes session_start and other methods usage |
||
| 130 | ->setMethods(null) // Enables original methods usage, because by default it overrides all methods |
||
| 131 | ->getMock(); |
||
| 132 | $this->replaceByMock('singleton', 'customer/session', $sessionMock); |
||
| 133 | return $sessionMock; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Replace the customer session object with a mock. |
||
| 138 | * @return PHPUnit_Framework_MockObject_MockObject - the mock session model |
||
| 139 | */ |
||
| 140 | View Code Duplication | protected function _mockCheckoutSession() |
|
| 141 | { |
||
| 142 | $sessionMock = $this->getModelMockBuilder('checkout/session') |
||
| 143 | ->disableOriginalConstructor() // This one removes session_start and other methods usage |
||
| 144 | ->setMethods(null) // Enables original methods usage, because by default it overrides all methods |
||
| 145 | ->getMock(); |
||
| 146 | $this->replaceByMock('singleton', 'checkout/session', $sessionMock); |
||
| 147 | return $sessionMock; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Stub validator response methods with expected bahaviors. |
||
| 152 | * |
||
| 153 | * @param bool |
||
| 154 | * @param bool |
||
| 155 | * @param Mage_Customer_Model_Address_Abstract|null |
||
| 156 | * @param Mage_Customer_Model_Address_Abstract|null |
||
| 157 | * @param Mage_Customer_Model_Address_Abstract[] |
||
| 158 | * @return self |
||
| 159 | */ |
||
| 160 | protected function _mockValidationResponse( |
||
| 161 | $isAddressValid = false, |
||
| 162 | $hasSuggestions = false, |
||
| 163 | $originalAddress = null, |
||
| 164 | $validAddress = null, |
||
| 165 | $addressSuggestions = [] |
||
| 166 | ) { |
||
| 167 | $this->_validatorResponse->expects($this->any()) |
||
| 168 | ->method('isAddressValid') |
||
| 169 | ->will($this->returnValue($isAddressValid)); |
||
| 170 | $this->_validatorResponse->expects($this->any()) |
||
| 171 | ->method('getHasSuggestions') |
||
| 172 | ->will($this->returnValue($hasSuggestions)); |
||
| 173 | $this->_validatorResponse->expects($this->any()) |
||
| 174 | ->method('getOriginalAddress') |
||
| 175 | ->will($this->returnValue($originalAddress)); |
||
| 176 | $this->_validatorResponse->expects($this->any()) |
||
| 177 | ->method('getValidAddress') |
||
| 178 | ->will($this->returnValue($validAddress)); |
||
| 179 | $this->_validatorResponse->expects($this->any()) |
||
| 180 | ->method('getAddressSuggestions') |
||
| 181 | ->will($this->returnValue($addressSuggestions)); |
||
| 182 | return $this; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get the session object used by address validation. |
||
| 187 | * @return Mage_Customer_Model_Session |
||
| 188 | */ |
||
| 189 | protected function _getSession() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Setup the session data with the supplied addresses. |
||
| 196 | * @param Mage_Customer_Model_Address_Abstract $original |
||
| 197 | * @param Mage_Customer_Model_Address_Abstract[] $suggestions |
||
| 198 | * @param bool $hasFreshSuggestions |
||
| 199 | */ |
||
| 200 | protected function _setupSessionWithSuggestions($original, $suggestions, $hasFreshSuggestions = true) |
||
| 201 | { |
||
| 202 | // populate the session with usable data - replaced with mock in setUp |
||
| 203 | $addresses = new EbayEnterprise_Address_Model_Suggestion_Group(); |
||
| 204 | $addresses->setOriginalAddress($original); |
||
| 205 | $addresses->setSuggestedAddresses($suggestions); |
||
| 206 | $addresses->setHasFreshSuggestions($hasFreshSuggestions); |
||
| 207 | $this->_getSession() |
||
| 208 | ->setData(EbayEnterprise_Address_Model_Validator::SESSION_KEY, $addresses); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Test the various session interactions when there is not session data |
||
| 213 | * for address validation initialized. |
||
| 214 | */ |
||
| 215 | public function testValidatorSessionNotInitialized() |
||
| 216 | { |
||
| 217 | $this->assertNull($this->_validator->getOriginalAddress()); |
||
| 218 | $this->assertNull($this->_validator->getSuggestedAddresses()); |
||
| 219 | $this->assertNull($this->_validator->getStashedAddressByKey('original_address')); |
||
| 220 | $this->assertFalse($this->_validator->hasSuggestions()); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Make sure getAddressCollection *always* returns a |
||
| 225 | * EbayEnterprise_Address_Model_Suggestion_Group even if the |
||
| 226 | * session data where it expects to find the suggestions is polluted |
||
| 227 | * with something else. |
||
| 228 | */ |
||
| 229 | public function testGettingAddressCollectionAlwaysReturnsProperObjectType() |
||
| 230 | { |
||
| 231 | $expectedType = 'EbayEnterprise_Address_Model_Suggestion_Group'; |
||
| 232 | $session = $this->_getSession(); |
||
| 233 | $sessionKey = EbayEnterprise_Address_Model_Validator::SESSION_KEY; |
||
| 234 | |||
| 235 | $session->setData($sessionKey, 'a string'); |
||
| 236 | $this->assertInstanceOf($expectedType, $this->_validator->getAddressCollection()); |
||
| 237 | |||
| 238 | $session->setData($sessionKey, new Varien_Object()); |
||
| 239 | $this->assertInstanceOf($expectedType, $this->_validator->getAddressCollection()); |
||
| 240 | |||
| 241 | $session->setData($sessionKey, 23); |
||
| 242 | $this->assertInstanceOf($expectedType, $this->_validator->getAddressCollection()); |
||
| 243 | |||
| 244 | $session->setData($sessionKey, new EbayEnterprise_Address_Model_Suggestion_Group()); |
||
| 245 | $this->assertInstanceOf($expectedType, $this->_validator->getAddressCollection()); |
||
| 246 | |||
| 247 | $session->unsetData($sessionKey); |
||
| 248 | $this->assertInstanceOf($expectedType, $this->_validator->getAddressCollection()); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Test updating the submitted address with data from the chosen suggestion. |
||
| 253 | * @dataProvider dataProvider |
||
| 254 | */ |
||
| 255 | public function testUpdateAddressWithSelections($postValue) |
||
| 256 | { |
||
| 257 | $originalAddress = $this->_createAddress([ |
||
| 258 | 'street' => '123 Main St', |
||
| 259 | 'city' => 'Fooville', |
||
| 260 | 'region_code' => 'NY', |
||
| 261 | 'country_id' => 'US', |
||
| 262 | 'postcode' => '12345', |
||
| 263 | 'has_been_validated' => true, |
||
| 264 | 'stash_key' => 'original_address', |
||
| 265 | ]); |
||
| 266 | $suggestions = [ |
||
| 267 | $this->_createAddress([ |
||
| 268 | 'street' => '321 Main Rd', |
||
| 269 | 'city' => 'Barton', |
||
| 270 | 'region_code' => 'NY', |
||
| 271 | 'country_id' => 'US', |
||
| 272 | 'postcode' => '54321-1234', |
||
| 273 | 'has_been_validated' => true, |
||
| 274 | 'stash_key' => 'suggested_addresses/0', |
||
| 275 | ]), |
||
| 276 | $this->_createAddress([ |
||
| 277 | 'street' => '321 Main St', |
||
| 278 | 'city' => 'Fooville', |
||
| 279 | 'country_id' => 'US', |
||
| 280 | 'postcode' => '12345-6789', |
||
| 281 | 'has_been_validated' => true, |
||
| 282 | 'stash_key' => 'suggested_addresses/1', |
||
| 283 | ]) |
||
| 284 | ]; |
||
| 285 | $this->_setupSessionWithSuggestions($originalAddress, $suggestions); |
||
| 286 | |||
| 287 | // set the submitted value in the request post data |
||
| 288 | Mage::app()->getRequest()->setPost(EbayEnterprise_Address_Block_Suggestions::SUGGESTION_INPUT_NAME, $postValue); |
||
| 289 | |||
| 290 | // create an address object to act as the address submitted by the user |
||
| 291 | $submittedAddress = $this->_createAddress([ |
||
| 292 | 'street' => '1 Street Rd', |
||
| 293 | 'city' => 'Foo', |
||
| 294 | 'region_code' => 'PA', |
||
| 295 | 'region_id' => 51, |
||
| 296 | 'country_id' => 'US', |
||
| 297 | 'postcode' => '23456', |
||
| 298 | ]); |
||
| 299 | |||
| 300 | // this is necessary due to expectation not allowing a / in the key to get expectations |
||
| 301 | $expectationKey = str_replace('/', '', $postValue); |
||
| 302 | |||
| 303 | EcomDev_Utils_Reflection::invokeRestrictedMethod($this->_validator, '_updateAddressWithSelection', [$submittedAddress]); |
||
| 304 | |||
| 305 | $this->assertSame( |
||
| 306 | $this->expected($expectationKey)->getStreet1(), |
||
| 307 | $submittedAddress->getStreet1() |
||
| 308 | ); |
||
| 309 | $this->assertSame( |
||
| 310 | $this->expected($expectationKey)->getCity(), |
||
| 311 | $submittedAddress->getCity() |
||
| 312 | ); |
||
| 313 | $this->assertSame( |
||
| 314 | $this->expected($expectationKey)->getPostcode(), |
||
| 315 | $submittedAddress->getPostcode() |
||
| 316 | ); |
||
| 317 | $this->assertSame( |
||
| 318 | $this->expected($expectationKey)->getHasBeenValidated(), |
||
| 319 | $submittedAddress->getHasBeenValidated() |
||
| 320 | ); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Test validation when the response indicates the address is correct |
||
| 325 | * and there are no suggestions or changes. |
||
| 326 | */ |
||
| 327 | public function testValidateAddressVerified() |
||
| 328 | { |
||
| 329 | // address to feed to the validator |
||
| 330 | $address = $this->_createAddress([ |
||
| 331 | 'street' => '1671 Clark Street Rd', |
||
| 332 | 'city' => 'Auburn', |
||
| 333 | 'region_code' => 'NY', |
||
| 334 | 'country_id' => 'US', |
||
| 335 | 'postcode' => '13021-9523', |
||
| 336 | 'has_been_validated' => true, |
||
| 337 | ]); |
||
| 338 | // original address from the response model |
||
| 339 | $origAddress = $this->_createAddress([ |
||
| 340 | 'street' => '1671 Clark Street Rd', |
||
| 341 | 'city' => 'Auburn', |
||
| 342 | 'region_code' => 'NY', |
||
| 343 | 'country_id' => 'US', |
||
| 344 | 'postcode' => '13021', |
||
| 345 | ]); |
||
| 346 | |||
| 347 | $this->_mockValidationResponse(true, false, $address, $address); |
||
| 348 | $errorMessage = $this->_validator->validateAddress($origAddress); |
||
| 349 | $this->assertNull($errorMessage, 'Validation of a valid address produces no errors.'); |
||
| 350 | $this->assertTrue($origAddress->getHasBeenValidated(), 'has_been_validated "magic" data set on the address that has been validated.'); |
||
| 351 | $this->assertSame($origAddress->getStreet1(), '1671 Clark Street Rd'); |
||
| 352 | $this->assertSame($origAddress->getCity(), 'Auburn'); |
||
| 353 | $this->assertSame($origAddress->getRegionCode(), 'NY'); |
||
| 354 | $this->assertSame($origAddress->getCountryId(), 'US'); |
||
| 355 | $this->assertSame($origAddress->getPostcode(), '13021-9523', 'Corrections from address validation applied to the "original" address.'); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Test validation when the response indicates the address is not correct |
||
| 360 | * and multiple suggestions are available. |
||
| 361 | */ |
||
| 362 | public function testValidateAddressMultiSuggestions() |
||
| 363 | { |
||
| 364 | // address to feed to validator |
||
| 365 | $address = $this->_createAddress([ |
||
| 366 | 'street' => '1671 Clark Street Rd', |
||
| 367 | 'city' => 'Auburn', |
||
| 368 | 'region_code' => 'NY', |
||
| 369 | 'country_id' => 'US', |
||
| 370 | 'postcode' => '13025', |
||
| 371 | ]); |
||
| 372 | // original address from response model |
||
| 373 | $origAddress = $this->_createAddress([ |
||
| 374 | 'street' => '1671 Clark Street Rd', |
||
| 375 | 'city' => 'Auburn', |
||
| 376 | 'region_code' => 'NY', |
||
| 377 | 'country_id' => 'US', |
||
| 378 | 'postcode' => '13025-1234', |
||
| 379 | 'has_been_validated' => true, |
||
| 380 | ]); |
||
| 381 | // suggestions from the response model |
||
| 382 | $suggestions = [ |
||
| 383 | $this->_createAddress([ |
||
| 384 | 'street' => 'Suggestion 1 Line 1', |
||
| 385 | 'city' => 'Suggestion 1 City', |
||
| 386 | 'region_id' => 'NY', |
||
| 387 | 'country_id' => 'US', |
||
| 388 | 'postcode' => '13021-9876', |
||
| 389 | 'has_been_validated' => true, |
||
| 390 | ]), |
||
| 391 | $this->_createAddress([ |
||
| 392 | 'street' => '1671 W Clark Street Rd', |
||
| 393 | 'city' => 'Auburn', |
||
| 394 | 'region_id' => 'NY', |
||
| 395 | 'country_id' => 'US', |
||
| 396 | 'postcode' => '13021-1234', |
||
| 397 | 'has_been_validated' => true, |
||
| 398 | ]), |
||
| 399 | ]; |
||
| 400 | $this->_mockValidationResponse(false, true, $origAddress, null, $suggestions); |
||
| 401 | |||
| 402 | $errorMessage = $this->_validator->validateAddress($address); |
||
| 403 | |||
| 404 | $this->assertTrue( |
||
| 405 | $address->getHasBeenValidated(), |
||
| 406 | '"has_been_validated" data added to the address object.' |
||
| 407 | ); |
||
| 408 | $this->assertSame( |
||
| 409 | $origAddress->getPostcode(), |
||
| 410 | $address->getPostcode(), |
||
| 411 | 'Corrected data in the "original_address" fields from service should still be copied over.' |
||
| 412 | ); |
||
| 413 | $this->assertSame( |
||
| 414 | EbayEnterprise_Address_Model_Validator::SUGGESTIONS_ERROR_MESSAGE, |
||
| 415 | $errorMessage, |
||
| 416 | 'Invalid address with suggestions should have the appropriate message.' |
||
| 417 | ); |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * If a previously validated address is passed to the validate method, |
||
| 422 | * validation should assume the address is correct and |
||
| 423 | * should be successful (no errors returned) and |
||
| 424 | * should clear out any session data. |
||
| 425 | */ |
||
| 426 | public function testWithValidatedAddress() |
||
| 427 | { |
||
| 428 | $address = $this->_createAddress([ |
||
| 429 | 'has_been_validated' => true |
||
| 430 | ]); |
||
| 431 | |||
| 432 | // add some data into the customer session mock. |
||
| 433 | $session = $this->_getSession(); |
||
| 434 | $session->setData(EbayEnterprise_Address_Model_Validator::SESSION_KEY, 'this should be cleared out'); |
||
| 435 | $errors = $this->_validator->validateAddress($address); |
||
| 436 | |||
| 437 | $this->assertNull($errors, 'An address that is marked as already having been validated is assumed valid, hence no errors.'); |
||
| 438 | $this->assertInstanceOf('EbayEnterprise_Address_Model_Suggestion_Group', $session->getData(EbayEnterprise_Address_Model_Validator::SESSION_KEY)); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Test the session interactions of the validation. |
||
| 443 | * Test for when - address does not need to be validated/has already been validated |
||
| 444 | * and is a billing/use for shipping address. |
||
| 445 | * @dataProvider dataProvider |
||
| 446 | */ |
||
| 447 | public function testSessionInteractionsNoValidationNecessary($useForShipping) |
||
| 448 | { |
||
| 449 | $address = $this->_createAddress([ |
||
| 450 | 'street' => '123 Main St', |
||
| 451 | 'city' => 'Foo', |
||
| 452 | 'region_id' => 51, |
||
| 453 | 'country_id' => 'US', |
||
| 454 | 'postcode' => '12345', |
||
| 455 | 'address_type' => 'billing', |
||
| 456 | 'has_been_validated' => true |
||
| 457 | ]); |
||
| 458 | |||
| 459 | Mage::app()->getRequest()->setPost('billing', ['use_for_shipping' => $useForShipping]); |
||
| 460 | |||
| 461 | $this->_validator->validateAddress($address); |
||
| 462 | |||
| 463 | // inspect session to make sure everything that needed to get set was |
||
| 464 | // properly set |
||
| 465 | $session = $this->_getSession(); |
||
| 466 | $group = $session->getData(EbayEnterprise_Address_Model_Validator::SESSION_KEY); |
||
| 467 | |||
| 468 | $this->assertNotNull($group); |
||
| 469 | |||
| 470 | // as the address was not validated/already valid, there shouldn't be |
||
| 471 | // any address data on the group object |
||
| 472 | // make retrieving address/suggestions don't change the has_fresh_suggestions flag |
||
| 473 | $this->assertNull($group->getOriginalAddress(true)); |
||
| 474 | $this->assertNull($group->getSuggestedAddresses(true)); |
||
| 475 | $this->assertNull($group->getResponseMessage()); |
||
| 476 | $this->assertFalse($group->getHasFreshSuggestions()); |
||
| 477 | |||
| 478 | $this->assertInstanceOf('Mage_Customer_Model_Address_Abstract', $group->getValidatedAddress('billing')); |
||
| 479 | if ($useForShipping) { |
||
| 480 | $this->assertInstanceOf('Mage_Customer_Model_Address_Abstract', $group->getValidatedAddress('shipping')); |
||
| 481 | } |
||
| 482 | |||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Test session interaction. |
||
| 487 | * Test for when - address is invalid |
||
| 488 | */ |
||
| 489 | public function testSessionInteractionsInvalidAddress() |
||
| 490 | { |
||
| 491 | // address to feed to validator |
||
| 492 | $address = $this->_createAddress([ |
||
| 493 | 'firstname' => 'Foo', |
||
| 494 | 'lastname' => 'Bar', |
||
| 495 | 'street' => '1671 Clark Street Rd', |
||
| 496 | 'city' => 'Auburn', |
||
| 497 | 'region_code' => 'NY', |
||
| 498 | 'country_id' => 'US', |
||
| 499 | 'postcode' => '13025', |
||
| 500 | 'address_type' => 'shipping' |
||
| 501 | ]); |
||
| 502 | // original address from response model |
||
| 503 | $origAddress = $this->_createAddress([ |
||
| 504 | 'street' => '1671 Clark Street Rd', |
||
| 505 | 'city' => 'Auburn', |
||
| 506 | 'region_code' => 'NY', |
||
| 507 | 'country_id' => 'US', |
||
| 508 | 'postcode' => '13025', |
||
| 509 | 'has_been_validated' => true, |
||
| 510 | ]); |
||
| 511 | // suggestions from the response model |
||
| 512 | $suggestions = [ |
||
| 513 | $this->_createAddress([ |
||
| 514 | 'street' => 'Suggestion 1 Line 1', |
||
| 515 | 'city' => 'Suggestion 1 City', |
||
| 516 | 'region_id' => 'NY', |
||
| 517 | 'country_id' => 'US', |
||
| 518 | 'postcode' => '13021-9876', |
||
| 519 | 'has_been_validated' => true, |
||
| 520 | ]), |
||
| 521 | $this->_createAddress([ |
||
| 522 | 'street' => '1671 W Clark Street Rd', |
||
| 523 | 'city' => 'Auburn', |
||
| 524 | 'region_id' => 'NY', |
||
| 525 | 'country_id' => 'US', |
||
| 526 | 'postcode' => '13021-1234', |
||
| 527 | 'has_been_validated' => true, |
||
| 528 | ]), |
||
| 529 | ]; |
||
| 530 | |||
| 531 | // invalid response, with suggestions, original address, no valid address and suggestions |
||
| 532 | $this->_mockValidationResponse(false, true, $origAddress, null, $suggestions); |
||
| 533 | |||
| 534 | $this->_validator->validateAddress($address); |
||
| 535 | |||
| 536 | $session = $this->_getSession(); |
||
| 537 | $group = $session->getData(EbayEnterprise_Address_Model_Validator::SESSION_KEY); |
||
| 538 | |||
| 539 | $this->assertTrue($group->getHasFreshSuggestions()); |
||
| 540 | |||
| 541 | $originalAddress = $group->getOriginalAddress(true); |
||
| 542 | $this->assertInstanceOf('Mage_Customer_Model_Address_Abstract', $originalAddress); |
||
| 543 | // ensure the stash key was added to the session object |
||
| 544 | $this->assertSame('original_address', $originalAddress->getStashKey()); |
||
| 545 | // ensure name data was copied over to the address from the response |
||
| 546 | $this->assertSame($address->getName(), $originalAddress->getName()); |
||
| 547 | |||
| 548 | $suggestions = $group->getSuggestedAddresses(true); |
||
| 549 | // should have 2 suggestions from the response |
||
| 550 | $this->assertEquals(2, count($suggestions)); |
||
| 551 | // make sure stash keys were added |
||
| 552 | $this->assertSame('suggested_addresses/0', $suggestions[0]->getStashKey()); |
||
| 553 | $this->assertSame('suggested_addresses/1', $suggestions[1]->getStashKey()); |
||
| 554 | // make sure name data was all copied over |
||
| 555 | $this->assertSame($address->getName(), $suggestions[0]->getName()); |
||
| 556 | $this->assertSame($address->getName(), $suggestions[1]->getName()); |
||
| 557 | |||
| 558 | // the response message should have been added to the session in case it needs to |
||
| 559 | // be queried for address data again later |
||
| 560 | $this->assertSame($this->_validatorResponse, $group->getResponseMessage()); |
||
| 561 | |||
| 562 | // make sure the validated address data was added to the session |
||
| 563 | // should have a shipping address but no billing address |
||
| 564 | $this->assertInstanceOf('Mage_Customer_Model_Address_Abstract', $group->getValidatedAddress('shipping')); |
||
| 565 | $this->assertNull($group->getValidatedAddress('billing')); |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Test the removal of session values. |
||
| 570 | */ |
||
| 571 | public function testCleaningOfSession() |
||
| 572 | { |
||
| 573 | $mockSession = $this->_getSession(); |
||
| 574 | $staleData = 'STALE_DATA'; |
||
| 575 | $mockSession->setAddressValidationAddresses($staleData); |
||
| 576 | $this->assertNotNull($mockSession->getAddressValidationAddresses(), 'Session has address validation data.'); |
||
| 577 | $this->_validator->clearSessionAddresses(); |
||
| 578 | $this->assertNull($mockSession->getAddressValidationAddresses(), 'Session does not have address valdidation data.'); |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Asking the validator model to validate a new address should clear out |
||
| 583 | * any values it has populated in the session. |
||
| 584 | */ |
||
| 585 | public function ensureSessionClearedOnNewValidation() |
||
| 586 | { |
||
| 587 | // address to feed to validator |
||
| 588 | $address = $this->_createAddress([ |
||
| 589 | 'street' => '1671 Clark Street Rd', |
||
| 590 | 'city' => 'Auburn', |
||
| 591 | 'region_code' => 'NY', |
||
| 592 | 'country_id' => 'US', |
||
| 593 | 'postcode' => '13025', |
||
| 594 | ]); |
||
| 595 | // original address from response model |
||
| 596 | $origAddress = $this->_createAddress([ |
||
| 597 | 'street' => '1671 Clark Street Rd', |
||
| 598 | 'city' => 'Auburn', |
||
| 599 | 'region_code' => 'NY', |
||
| 600 | 'country_id' => 'US', |
||
| 601 | 'postcode' => '13025', |
||
| 602 | 'has_been_validated' => true, |
||
| 603 | ]); |
||
| 604 | // suggestions from the response model |
||
| 605 | $suggestions = [ |
||
| 606 | $this->_createAddress([ |
||
| 607 | 'street' => 'Suggestion 1 Line 1', |
||
| 608 | 'city' => 'Suggestion 1 City', |
||
| 609 | 'region_id' => 'NY', |
||
| 610 | 'country_id' => 'US', |
||
| 611 | 'postcode' => '13021-9876', |
||
| 612 | 'has_been_validated' => true, |
||
| 613 | ]), |
||
| 614 | $this->_createAddress([ |
||
| 615 | 'street' => '1671 W Clark Street Rd', |
||
| 616 | 'city' => 'Auburn', |
||
| 617 | 'region_id' => 'NY', |
||
| 618 | 'country_id' => 'US', |
||
| 619 | 'postcode' => '13021-1234', |
||
| 620 | 'has_been_validated' => true, |
||
| 621 | ]), |
||
| 622 | ]; |
||
| 623 | $this->_mockValidationResponse(false, true, $origAddress, null, $suggestions); |
||
| 624 | |||
| 625 | // set up some stale data in the session that should be overwritten by the validator model |
||
| 626 | $staleSessionData = 'STALE_DATA'; |
||
| 627 | $sessionKey = EbayEnterprise_Address_Model_Validator::SESSION_KEY; |
||
| 628 | $mockSession = $this->_getSession(); |
||
| 629 | $mockSession->setData($sessionKey, $staleSessionData); |
||
| 630 | // make sure it has been set |
||
| 631 | $this->assertSame($staleSessionData, $mockSession->getData($sessionKey), 'Session has initial address validation data.'); |
||
| 632 | |||
| 633 | $this->_validator->validateAddress($address); |
||
| 634 | |||
| 635 | $this->assertNotEquals( |
||
| 636 | $staleSessionData, |
||
| 637 | $mockSession->getData($sessionKey), |
||
| 638 | 'Stale session data replaced by new address validation data.' |
||
| 639 | ); |
||
| 640 | } |
||
| 641 | |||
| 642 | /** |
||
| 643 | * This is a very odd scenario and really should never happen. |
||
| 644 | */ |
||
| 645 | public function errorMessageWithInvalidMessageAndNoSuggestions() |
||
| 646 | { |
||
| 647 | // address to feed to validator |
||
| 648 | $address = $this->_createAddress([ |
||
| 649 | 'street' => '1671 Clark Street Rd', |
||
| 650 | 'city' => 'Auburn', |
||
| 651 | 'region_code' => 'NY', |
||
| 652 | 'country_id' => 'US', |
||
| 653 | 'postcode' => '13025', |
||
| 654 | ]); |
||
| 655 | // original address from response model |
||
| 656 | $origAddress = $this->_createAddress([ |
||
| 657 | 'street' => '1671 Clark Street Rd', |
||
| 658 | 'city' => 'Auburn', |
||
| 659 | 'region_code' => 'NY', |
||
| 660 | 'country_id' => 'US', |
||
| 661 | 'postcode' => '13025', |
||
| 662 | 'has_been_validated' => true, |
||
| 663 | ]); |
||
| 664 | $this->_mockValidationResponse(false, false, $origAddress, null); |
||
| 665 | |||
| 666 | $errorMessage = $this->_validator->validateAddress($address); |
||
| 667 | |||
| 668 | $this->assertSame( |
||
| 669 | $errorMessage, |
||
| 670 | EbayEnterprise_Address_Model_Validator::NO_SUGGESTIONS_ERROR_MESSAGE |
||
| 671 | ); |
||
| 672 | } |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Test that when no message is received from the address validation service |
||
| 676 | * the address is considered valid. |
||
| 677 | */ |
||
| 678 | public function testEmptyResponseFromService() |
||
| 679 | { |
||
| 680 | // address to validate |
||
| 681 | $address = $this->_createAddress([ |
||
| 682 | 'street' => '123 Main St', |
||
| 683 | 'city' => 'Auburn', |
||
| 684 | 'region_code' => 'NY', |
||
| 685 | 'country_id' => 'US', |
||
| 686 | 'postcode' => '13025', |
||
| 687 | ]); |
||
| 688 | $this->_sdkApi->expects($this->any()) |
||
| 689 | ->method('send') |
||
| 690 | ->will($this->throwException(new eBayEnterprise\RetailOrderManagement\Api\Exception\NetworkError)); |
||
| 691 | $errors = $this->_validator->validateAddress($address); |
||
| 692 | |||
| 693 | $this->assertNull($errors); |
||
| 694 | } |
||
| 695 | /** |
||
| 696 | * Test retrieval of address objects from the validator by key. |
||
| 697 | * Each address should have a stash_key which will be used to get |
||
| 698 | * the address back out of the address collection stored in the session. |
||
| 699 | */ |
||
| 700 | public function getStashedAddressByKeyByKey() |
||
| 701 | { |
||
| 702 | $originalAddress = $this->_createAddress([ |
||
| 703 | 'street' => '123 Main St', |
||
| 704 | 'city' => 'Fooville', |
||
| 705 | 'region_code' => 'NY', |
||
| 706 | 'country_id' => 'US', |
||
| 707 | 'postcode' => '12345', |
||
| 708 | 'has_been_validated' => true, |
||
| 709 | 'stash_key' => 'original_address', |
||
| 710 | ]); |
||
| 711 | $suggestions = [ |
||
| 712 | $this->_createAddress([ |
||
| 713 | 'street' => '321 Main Rd', |
||
| 714 | 'city' => 'Barton', |
||
| 715 | 'region_code' => 'NY', |
||
| 716 | 'country_id' => 'US', |
||
| 717 | 'postcode' => '54321-1234', |
||
| 718 | 'has_been_validated' => true, |
||
| 719 | 'stash_key' => 'suggested_addresses/0', |
||
| 720 | ]), |
||
| 721 | $this->_createAddress([ |
||
| 722 | 'street' => '321 Main St', |
||
| 723 | 'city' => 'Fooville', |
||
| 724 | 'country_id' => 'US', |
||
| 725 | 'postcode' => '12345-6789', |
||
| 726 | 'has_been_validated' => true, |
||
| 727 | 'stash_key' => 'suggested_addresses/1', |
||
| 728 | ]) |
||
| 729 | ]; |
||
| 730 | $this->_setupSessionWithSuggestions($originalAddress, $suggestions); |
||
| 731 | |||
| 732 | $this->assertSame( |
||
| 733 | $originalAddress, |
||
| 734 | $this->_validator->getStashedAddressByKey($originalAddress->getStashKey()) |
||
| 735 | ); |
||
| 736 | $this->assertSame( |
||
| 737 | $suggestions[1], |
||
| 738 | $this->_validator->getStashedAddressByKey($suggestions[1]->getStashKey()) |
||
| 739 | ); |
||
| 740 | } |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Trying to get a validated address with an unknown key will return null |
||
| 744 | */ |
||
| 745 | public function gettingValidatedAddressByUnknownKey() |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Test getting the original address out of the session |
||
| 752 | */ |
||
| 753 | public function testGetOriginalAddress() |
||
| 754 | { |
||
| 755 | // set up an address object to be put into the session |
||
| 756 | $origAddress = $this->_createAddress(([ |
||
| 757 | 'street' => '123 Main St', |
||
| 758 | 'city' => 'Fooville', |
||
| 759 | 'region_code' => 'NY', |
||
| 760 | 'country_code' => 'US', |
||
| 761 | 'postcode' => '12345', |
||
| 762 | 'has_been_validated' => true, |
||
| 763 | 'stash_key' => 'original_address' |
||
| 764 | ])); |
||
| 765 | // create the EbayEnterprise_Address_Model_Suggestion_Group that stores address data in the session |
||
| 766 | $addresses = new EbayEnterprise_Address_Model_Suggestion_Group(); |
||
| 767 | $addresses->setOriginalAddress($origAddress); |
||
| 768 | // add the address data to the session |
||
| 769 | $this->_getSession()->setData( |
||
| 770 | EbayEnterprise_Address_Model_Validator::SESSION_KEY, |
||
| 771 | $addresses |
||
| 772 | ); |
||
| 773 | |||
| 774 | $this->assertSame( |
||
| 775 | $origAddress, |
||
| 776 | $this->_validator->getOriginalAddress() |
||
| 777 | ); |
||
| 778 | } |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Test getting the list of suggested addresses. |
||
| 782 | */ |
||
| 783 | public function testGetSuggestedAddresses() |
||
| 784 | { |
||
| 785 | // set up suggested addresses to place into the session |
||
| 786 | $suggestions = [ |
||
| 787 | $this->_createAddress( |
||
| 788 | [ |
||
| 789 | 'street' => '123 Main St', |
||
| 790 | 'city' => 'Fooville', |
||
| 791 | 'region_code' => 'NY', |
||
| 792 | 'country_code' => 'US', |
||
| 793 | 'postcode' => '12345', |
||
| 794 | ], |
||
| 795 | [ |
||
| 796 | 'street' => '321 Main Rd', |
||
| 797 | 'city' => 'Bartown', |
||
| 798 | 'region_code' => 'PA', |
||
| 799 | 'country_code' => 'US', |
||
| 800 | 'postcode' => '19231', |
||
| 801 | ] |
||
| 802 | ) |
||
| 803 | ]; |
||
| 804 | // create the EbayEnterprise_Address_Model_Suggestion_Group that stores the address data in the session |
||
| 805 | $addressCollection = new EbayEnterprise_Address_Model_Suggestion_Group(); |
||
| 806 | $addressCollection->setSuggestedAddresses($suggestions); |
||
| 807 | // add suggestions to the session |
||
| 808 | $this->_getSession()->setData( |
||
| 809 | EbayEnterprise_Address_Model_Validator::SESSION_KEY, |
||
| 810 | $addressCollection |
||
| 811 | ); |
||
| 812 | |||
| 813 | $this->assertSame( |
||
| 814 | $suggestions, |
||
| 815 | $this->_validator->getSuggestedAddresses() |
||
| 816 | ); |
||
| 817 | } |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Test copying name data form the source address to the destination address. |
||
| 821 | */ |
||
| 822 | public function testCopyNameData() |
||
| 823 | { |
||
| 824 | $destData = [ |
||
| 825 | 'street' => '123 Main St', |
||
| 826 | 'city' => 'Fooville', |
||
| 827 | 'region_code' => 'PA', |
||
| 828 | 'country_code' => 'US', |
||
| 829 | 'postcode' => '12345', |
||
| 830 | ]; |
||
| 831 | $destAddr = $this->_createAddress($destData); |
||
| 832 | $sourceData = [ |
||
| 833 | 'firstname' => 'The', |
||
| 834 | 'lastname' => 'Bar', |
||
| 835 | 'street' => '555 Foo St', |
||
| 836 | 'city' => 'Bartown', |
||
| 837 | 'region_code' => 'NY', |
||
| 838 | 'country_code' => 'US', |
||
| 839 | 'postcode' => '12345-6789', |
||
| 840 | ]; |
||
| 841 | $sourceAddr = $this->_createAddress($sourceData); |
||
| 842 | |||
| 843 | EcomDev_Utils_Reflection::invokeRestrictedMethod($this->_validator, '_copyAddressName', [$destAddr, $sourceAddr]); |
||
| 844 | |||
| 845 | // first and last name should be copied to the dest address |
||
| 846 | $this->assertSame($sourceAddr->getFirstname(), $destAddr->getFirstname()); |
||
| 847 | $this->assertSame($sourceAddr->getLastname(), $destAddr->getLastname()); |
||
| 848 | // rest of the dest address should not have changed |
||
| 849 | $this->assertSame($destData['city'], $destAddr->getCity()); |
||
| 850 | $this->assertSame($destData['postcode'], $destAddr->getPostcode()); |
||
| 851 | } |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Test checking for fresh suggestions. |
||
| 855 | * Really just a pass through to another object which is responsible for managing the flag. |
||
| 856 | */ |
||
| 857 | public function testCheckSessionFreshness() |
||
| 858 | { |
||
| 859 | $this->_setupSessionWithSuggestions(null, null, false); |
||
| 860 | $this->assertFalse($this->_validator->hasFreshSuggestions()); |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Test the test for there to be suggestions. |
||
| 865 | * @dataProvider dataProvider |
||
| 866 | */ |
||
| 867 | public function testHasSuggestions($shouldHaveSuggestions) |
||
| 868 | { |
||
| 869 | // something to populate the Suggestion_Group suggestions with, |
||
| 870 | // anything not empty will produce a true result for hasSuggestions |
||
| 871 | $suggestions = ($shouldHaveSuggestions) |
||
| 872 | ? [Mage::getModel('customer/address'), Mage::getModel('customer/address')] |
||
| 873 | : []; |
||
| 874 | $group = $this->getModelMock( |
||
| 875 | 'ebayenterprise_address/suggestion_group', |
||
| 876 | ['getSuggestedAddresses'] |
||
| 877 | ); |
||
| 878 | $group->expects($this->once()) |
||
| 879 | ->method('getSuggestedAddresses') |
||
| 880 | ->with($this->equalTo(true)) |
||
| 881 | ->will($this->returnValue($suggestions)); |
||
| 882 | $this->replaceByMock('model', 'ebayenterprise_address/suggestion_group', $group); |
||
| 883 | |||
| 884 | $this->assertEquals($shouldHaveSuggestions, $this->_validator->hasSuggestions()); |
||
| 885 | } |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Test the should validate method for addresses that have already been validated, |
||
| 889 | * in particular, addresses that have had a 'has_been_validated' flag set, |
||
| 890 | * e.g. addresses that have been extracted from the response message. |
||
| 891 | */ |
||
| 892 | public function testShouldValidateFlaggedAsValidated() |
||
| 893 | { |
||
| 894 | $address = $this->getModelMock('customer/address', ['getHasBeenValidated']); |
||
| 895 | $address->expects($this->once()) |
||
| 896 | ->method('getHasBeenValidated') |
||
| 897 | ->will($this->returnValue(true)); |
||
| 898 | $this->assertFalse($this->_validator->shouldValidateAddress($address)); |
||
| 899 | } |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Test the should validate method for addresses that have already been validated, |
||
| 903 | * in particular addresses that match an address of the same type that has already |
||
| 904 | * been validated. This check is primarilly used for detecting superflous calls |
||
| 905 | * to the validateAddress method. Tests any scenarios that will result in a false response |
||
| 906 | * indicating the address has already been validated and should not be validated. |
||
| 907 | * |
||
| 908 | * @dataProvider dataProvider |
||
| 909 | */ |
||
| 910 | public function testShouldValidatedAlreadyValidatedAddress($addressType, $postBillingUseForShipping, $validatedType) |
||
| 911 | { |
||
| 912 | $address = $this->getModelMock('customer/address', ['getHasBeenValidated', 'getAddressType', 'getData']); |
||
| 913 | $address->expects($this->any()) |
||
| 914 | ->method('getHasBeenValidated') |
||
| 915 | ->will($this->returnValue(false)); |
||
| 916 | $address->expects($this->any()) |
||
| 917 | ->method('getAddressType') |
||
| 918 | ->will($this->returnValue($addressType)); |
||
| 919 | $addressData = [ |
||
| 920 | ['street', null, '123 Main St'], |
||
| 921 | ['city', null, 'Fooville'], |
||
| 922 | ['region_id', null, 51], |
||
| 923 | ]; |
||
| 924 | $address->expects($this->any()) |
||
| 925 | ->method('getData') |
||
| 926 | ->will($this->returnValueMap($addressData)); |
||
| 927 | |||
| 928 | // setup the POST data accordingly |
||
| 929 | if ($postBillingUseForShipping !== null) { |
||
| 930 | $_POST['billing'] = ['use_for_shipping' => $postBillingUseForShipping]; |
||
| 931 | } |
||
| 932 | |||
| 933 | $group = $this->getModelMock('ebayenterprise_address/suggestion_group', ['getValidatedAddress']); |
||
| 934 | $validatedAddress = $this->getModelMock('customer/address', ['getData']); |
||
| 935 | $validatedAddressData = [ |
||
| 936 | 'street' => '123 Main St', |
||
| 937 | 'city' => 'Fooville', |
||
| 938 | 'region_id' => '51', |
||
| 939 | 'address_type' => 'shipping', |
||
| 940 | ]; |
||
| 941 | $validatedAddress->expects($this->any()) |
||
| 942 | ->method('getData') |
||
| 943 | ->will($this->returnValue($validatedAddressData)); |
||
| 944 | $group->expects($this->any()) |
||
| 945 | ->method('getValidatedAddress') |
||
| 946 | ->with($this->equalTo($validatedType)) |
||
| 947 | ->will($this->returnValue($validatedAddress)); |
||
| 948 | $this->replaceByMock('model', 'ebayenterprise_address/suggestion_group', $group); |
||
| 949 | |||
| 950 | $this->assertFalse($this->_validator->shouldValidateAddress($address)); |
||
| 951 | } |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Test that an address should be validated when: |
||
| 955 | * - not a checkout address |
||
| 956 | * - and has not yet been validated. |
||
| 957 | */ |
||
| 958 | public function testShouldValidateNonCheckouNotValidated() |
||
| 959 | { |
||
| 960 | $address = $this->getModelMock('customer/address', ['getHasBeenValidated', 'getAddressType', 'getData']); |
||
| 961 | $address->expects($this->any()) |
||
| 962 | ->method('getHasBeenValidated') |
||
| 963 | ->will($this->returnValue(false)); |
||
| 964 | $address->expects($this->any()) |
||
| 965 | ->method('getAddressType') |
||
| 966 | ->will($this->returnValue('shipping')); |
||
| 967 | // any one of these being different from the validatedAddressData below |
||
| 968 | // will cause the comparison check fail |
||
| 969 | $addressData = [ |
||
| 970 | ['street', null, 'Borg'], |
||
| 971 | ['city', null, 'Barton'], |
||
| 972 | ['region_id', null, 41], |
||
| 973 | ]; |
||
| 974 | $address->expects($this->any()) |
||
| 975 | ->method('getData') |
||
| 976 | ->will($this->returnValueMap($addressData)); |
||
| 977 | |||
| 978 | $group = $this->getModelMock('ebayenterprise_address/suggestion_group', ['getValidatedAddress']); |
||
| 979 | $validatedAddress = $this->getModelMock('customer/address', ['getData']); |
||
| 980 | $validatedAddressData = [ |
||
| 981 | 'street' => '123 Main St', |
||
| 982 | 'city' => 'Fooville', |
||
| 983 | 'region_id' => '51', |
||
| 984 | 'address_type' => 'shipping', |
||
| 985 | ]; |
||
| 986 | $validatedAddress->expects($this->any()) |
||
| 987 | ->method('getData') |
||
| 988 | ->will($this->returnValue($validatedAddressData)); |
||
| 989 | $group->expects($this->any()) |
||
| 990 | ->method('getValidatedAddress') |
||
| 991 | ->will($this->returnValue($validatedAddress)); |
||
| 992 | $this->replaceByMock('model', 'ebayenterprise_address/suggestion_group', $group); |
||
| 993 | |||
| 994 | $this->assertTrue($this->_validator->shouldValidateAddress($address)); |
||
| 995 | } |
||
| 996 | |||
| 997 | /** |
||
| 998 | * Test detecting that an address is being used in checkout. |
||
| 999 | */ |
||
| 1000 | public function testDetectingACheckoutAddress() |
||
| 1001 | { |
||
| 1002 | $address = Mage::getModel('customer/address'); |
||
| 1003 | $address->addData([ |
||
| 1004 | 'street' => '123 Main St', |
||
| 1005 | 'city' => 'Foo', |
||
| 1006 | 'region_id' => 41, |
||
| 1007 | 'country_id' => 'US', |
||
| 1008 | ]); |
||
| 1009 | |||
| 1010 | // checkout address will have a quote_id |
||
| 1011 | $address->setData('quote_id', 12); |
||
| 1012 | $this->assertTrue(EcomDev_Utils_Reflection::invokeRestrictedMethod($this->_validator, '_isCheckoutAddress', [$address])); |
||
| 1013 | |||
| 1014 | // non-checkout address will not |
||
| 1015 | $address->unsetData('quote_id'); |
||
| 1016 | $this->assertFalse(EcomDev_Utils_Reflection::invokeRestrictedMethod($this->_validator, '_isCheckoutAddress', [$address])); |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Test that address being loaded from the address book are not validated when |
||
| 1021 | * used in checkout. |
||
| 1022 | * @dataProvider dataProvider |
||
| 1023 | * @long |
||
| 1024 | */ |
||
| 1025 | public function testAddressBookAddressShouldNotBeValidated($id, $customerId, $customerAddressId) |
||
| 1026 | { |
||
| 1027 | $address = $this->getModelMock('customer/address', ['hasData', 'getId', 'getCustomerId', 'getCustomerAddressId']); |
||
| 1028 | // make sure this is a checkout address |
||
| 1029 | $address->expects($this->any()) |
||
| 1030 | ->method('hasData') |
||
| 1031 | ->with($this->matches('quote_id')) |
||
| 1032 | ->will($this->returnValue(1)); |
||
| 1033 | $address->expects($this->any()) |
||
| 1034 | ->method('getId') |
||
| 1035 | ->will($this->returnValue($id)); |
||
| 1036 | $address->expects($this->any()) |
||
| 1037 | ->method('getCustomerId') |
||
| 1038 | ->will($this->returnValue($customerId)); |
||
| 1039 | $address->expects($this->any()) |
||
| 1040 | ->method('getCustomerAddressId') |
||
| 1041 | ->will($this->returnValue($customerAddressId)); |
||
| 1042 | |||
| 1043 | $this->_validator = $this->getModelMock('ebayenterprise_address/validator', ['_isMissingRequiredFields']); |
||
| 1044 | $this->assertEquals( |
||
| 1045 | !($id && $customerId && $customerAddressId), |
||
| 1046 | $this->_validator->shouldValidateAddress($address) |
||
| 1047 | ); |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Check for an address that is marked to be saved in the users address book. |
||
| 1052 | * As address that are saved get validated, which is the normal case, |
||
| 1053 | * in order to ensure unique results for addresses being saved, we need to force |
||
| 1054 | * one other the other, non-validation triggers to catch. Most direct way is likely by |
||
| 1055 | * marking the order as a virtual order. As a result, |
||
| 1056 | * this also tests should validate for virtual orders. |
||
| 1057 | * @dataProvider dataProvider |
||
| 1058 | */ |
||
| 1059 | public function testAddressBeingSavedInAddressBookAndIsVirtual($postType, $postFlag, $checkoutMethod) |
||
| 1060 | { |
||
| 1061 | $address = Mage::getModel('customer/address'); |
||
| 1062 | // address must be checkout address - have a quote_id, |
||
| 1063 | // and not be from the address book - no id, customer_id or customer_address_id |
||
| 1064 | $address->addData([ |
||
| 1065 | 'quote_id' => 12, |
||
| 1066 | 'address_type' => 'shipping', |
||
| 1067 | ]); |
||
| 1068 | |||
| 1069 | // set up the checkout session with a quote mock which will report |
||
| 1070 | // it as being a virtual order |
||
| 1071 | $quote = $this->getModelMock('sales/quote', ['isVirtual']); |
||
| 1072 | $quote->expects($this->any()) |
||
| 1073 | ->method('isVirtual') |
||
| 1074 | ->will($this->returnValue(true)); |
||
| 1075 | |||
| 1076 | $checkout = $this->getModelMockBuilder('checkout/session') |
||
| 1077 | ->disableOriginalConstructor() // This one removes session_start and other methods usage |
||
| 1078 | ->setMethods(['getQuote']) // Enables original methods usage, because by default it overrides all methods |
||
| 1079 | ->getMock(); |
||
| 1080 | $checkout->expects($this->any()) |
||
| 1081 | ->method('getQuote') |
||
| 1082 | ->will($this->returnValue($quote)); |
||
| 1083 | $this->replaceByMock('singleton', 'checkout/session', $checkout); |
||
| 1084 | |||
| 1085 | /* |
||
| 1086 | * set up the post data necessary, there needs to be either a |
||
| 1087 | * shipping[save_in_address_book] or billing[save_in_address_book] submitted |
||
| 1088 | */ |
||
| 1089 | if ($postType === 'billing' || $postType === 'shipping') { |
||
| 1090 | $_POST[$postType] = ['save_in_address_book' => $postFlag]; |
||
| 1091 | } |
||
| 1092 | |||
| 1093 | // set up the checkout type |
||
| 1094 | $onepage = $this->getModelMock('checkout/type_onepage', ['getCheckoutMethod']); |
||
| 1095 | $onepage->expects($this->once()) |
||
| 1096 | ->method('getCheckoutMethod') |
||
| 1097 | ->will($this->returnValue($checkoutMethod)); |
||
| 1098 | $this->replaceByMock('singleton', 'checkout/type_onepage', $onepage); |
||
| 1099 | |||
| 1100 | $expectation = $this->expected('%s-%s-%s', $postType, $postFlag, $checkoutMethod); |
||
| 1101 | $this->assertSame( |
||
| 1102 | $expectation->getShouldValidateAddress(), |
||
| 1103 | $this->_validator->shouldValidateAddress($address) |
||
| 1104 | ); |
||
| 1105 | } |
||
| 1106 | |||
| 1107 | /** |
||
| 1108 | * When there is an order present, method should get the isVirtual result |
||
| 1109 | * from it, otherwise should return false. |
||
| 1110 | * @dataProvider dataProvider |
||
| 1111 | */ |
||
| 1112 | public function testVirtualQuoteAddress($hasQuote, $isVirtual) |
||
| 1113 | { |
||
| 1114 | $quote = null; |
||
| 1115 | if ($hasQuote) { |
||
| 1116 | $quote = $this->getModelMock('sales/quote', ['isVirtual']); |
||
| 1117 | $quote->expects($this->once()) |
||
| 1118 | ->method('isVirtual') |
||
| 1119 | ->will($this->returnValue($isVirtual)); |
||
| 1120 | } |
||
| 1121 | $sessionMock = $this->getModelMockBuilder('checkout/session') |
||
| 1122 | ->disableOriginalConstructor() |
||
| 1123 | ->setMethods(['getQuote']) |
||
| 1124 | ->getMock(); |
||
| 1125 | $sessionMock->expects($this->once()) |
||
| 1126 | ->method('getQuote') |
||
| 1127 | ->will($this->returnValue($quote)); |
||
| 1128 | $this->replaceByMock('singleton', 'checkout/session', $sessionMock); |
||
| 1129 | |||
| 1130 | $this->assertSame($isVirtual, EcomDev_Utils_Reflection::invokeRestrictedMethod($this->_validator, '_isVirtualOrder')); |
||
| 1131 | } |
||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * Detect that an address is only used for billing and does not |
||
| 1135 | * need to be validated. |
||
| 1136 | * @dataProvider dataProvider |
||
| 1137 | */ |
||
| 1138 | public function testShouldValidateBillingOnlyAddress($addressType, $hasBillingPost, $useForShipping) |
||
| 1139 | { |
||
| 1140 | $address = Mage::getModel('customer/address'); |
||
| 1141 | // must be checkout address - have quote_id |
||
| 1142 | $address->addData([ |
||
| 1143 | 'quote_id' => 1, |
||
| 1144 | 'address_type' => $addressType |
||
| 1145 | ]); |
||
| 1146 | if ($hasBillingPost) { |
||
| 1147 | $_POST['billing'] = ['use_for_shipping' => $useForShipping]; |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | $expectations = $this->expected('%s-%s-%s', $addressType, $hasBillingPost, $useForShipping); |
||
| 1151 | $this->_validator = $this->getModelMock('ebayenterprise_address/validator', ['_isMissingRequiredFields']); |
||
| 1152 | $this->assertSame( |
||
| 1153 | $expectations->getShouldValidate(), |
||
| 1154 | $this->_validator->shouldValidateAddress($address) |
||
| 1155 | ); |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | /** |
||
| 1159 | * When a shipping address is marked same_as_billing but is not valid, the |
||
| 1160 | * address should no longer be marked same_as_billing. This is part of the |
||
| 1161 | * means of preventing a non-validated billing only address from |
||
| 1162 | * being used as a shipping address and bypassing validation. |
||
| 1163 | * |
||
| 1164 | * @dataProvider dataProvider |
||
| 1165 | */ |
||
| 1166 | public function testSetSameAsBillingFlagWhenAddressIsInvalid($isValid, $sameAsBilling) |
||
| 1167 | { |
||
| 1168 | $this->_validatorResponse->expects($this->any()) |
||
| 1169 | ->method('getOriginalAddress') |
||
| 1170 | ->will($this->returnValue(Mage::getModel('customer/address'))); |
||
| 1171 | $this->_validatorResponse->expects($this->any()) |
||
| 1172 | ->method('getAddressSuggestions') |
||
| 1173 | ->will($this->returnValue([Mage::getModel('customer/address'), Mage::getModel('customer/address')])); |
||
| 1174 | $this->_validatorResponse->expects($this->any()) |
||
| 1175 | ->method('isAddressValid') |
||
| 1176 | ->will($this->returnValue($isValid)); |
||
| 1177 | $this->_validatorResponse->expects($this->any()) |
||
| 1178 | ->method('getValidAddress') |
||
| 1179 | ->will($this->returnValue($isValid ? $this->_validatorResponse->getOriginalAddress() : null)); |
||
| 1180 | |||
| 1181 | $address = $this->getModelMock('customer/address', ['getSameAsBilling', 'setSameAsBilling']); |
||
| 1182 | // when valid, don't care about the same_as_billing flag so neither should be called |
||
| 1183 | if ($isValid) { |
||
| 1184 | $address->expects($this->never()) |
||
| 1185 | ->method('getSameAsBilling'); |
||
| 1186 | $address->expects($this->never()) |
||
| 1187 | ->method('setSameAsBilling'); |
||
| 1188 | } else { |
||
| 1189 | // when invalid address, need to check if the address is marked same as billing |
||
| 1190 | $address->expects($this->once()) |
||
| 1191 | ->method('getSameAsBilling') |
||
| 1192 | ->will($this->returnValue($sameAsBilling)); |
||
| 1193 | // when same_as_billing is true, flag should be set to false |
||
| 1194 | if ($sameAsBilling) { |
||
| 1195 | $address->expects($this->once()) |
||
| 1196 | ->method('setSameAsBilling') |
||
| 1197 | ->with($this->matches(false)); |
||
| 1198 | } else { |
||
| 1199 | // when not the same, no attempt to change it should be made |
||
| 1200 | $address->expects($this->never()) |
||
| 1201 | ->method('setSameAsBilling'); |
||
| 1202 | } |
||
| 1203 | } |
||
| 1204 | |||
| 1205 | $this->_validator->validateAddress($address); |
||
| 1206 | } |
||
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Test for the last response from address validation to have |
||
| 1210 | * contained a valid address. |
||
| 1211 | * @dataProvider dataProvider |
||
| 1212 | */ |
||
| 1213 | public function testIsValid($hasResponse, $isValid) |
||
| 1214 | { |
||
| 1215 | $group = $this->getModelMock('ebayenterprise_address/suggestion_group', ['getResponseMessage']); |
||
| 1216 | if ($hasResponse) { |
||
| 1217 | $this->_validatorResponse->expects($this->any()) |
||
| 1218 | ->method('isAddressValid') |
||
| 1219 | ->will($this->returnValue($isValid)); |
||
| 1220 | $group->expects($this->any()) |
||
| 1221 | ->method('getResponseMessage') |
||
| 1222 | ->will($this->returnValue($this->_validatorResponse)); |
||
| 1223 | } else { |
||
| 1224 | $group->expects($this->any()) |
||
| 1225 | ->method('getResponseMessage') |
||
| 1226 | ->will($this->returnValue(null)); |
||
| 1227 | } |
||
| 1228 | $this->replaceByMock('model', 'ebayenterprise_address/suggestion_group', $group); |
||
| 1229 | $this->assertSame( |
||
| 1230 | $this->expected('%s-%s', $hasResponse, $isValid)->getIsValid(), |
||
| 1231 | $this->_validator->isValid() |
||
| 1232 | ); |
||
| 1233 | } |
||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * provide methods that should return the equivalent of a true result. |
||
| 1237 | */ |
||
| 1238 | public function provideNonEmptyGetters() |
||
| 1239 | { |
||
| 1240 | return [ |
||
| 1241 | [['getStreet1', 'getCity', 'getCountry'], true], |
||
| 1242 | [['getStreet1', 'getCity'], false], |
||
| 1243 | [['getStreet1'], false], |
||
| 1244 | [[], false], |
||
| 1245 | ]; |
||
| 1246 | } |
||
| 1247 | /** |
||
| 1248 | * if any one of the required fields are missing, return true; false otherwise. |
||
| 1249 | * @param array $nonEmptyGetters |
||
| 1250 | * @param bool $result |
||
| 1251 | * @dataProvider provideNonEmptyGetters |
||
| 1252 | */ |
||
| 1253 | public function testShouldValidateIfIsMissingRequiredFields($nonEmptyGetters, $result) |
||
| 1275 | |||
| 1276 | /** |
||
| 1277 | * Provide exceptions that can be thrown from the SDK and the exception |
||
| 1278 | * expected to be thrown after handling the SDK exception. |
||
| 1279 | * |
||
| 1280 | * @return array |
||
| 1281 | */ |
||
| 1282 | View Code Duplication | public function provideSdkExceptions() |
|
| 1283 | { |
||
| 1284 | $invalidPayload = '\eBayEnterprise\RetailOrderManagement\Payload\Exception\InvalidPayload'; |
||
| 1285 | $networkError = '\eBayEnterprise\RetailOrderManagement\Api\Exception\NetworkError'; |
||
| 1286 | $unsupportedOperation = '\eBayEnterprise\RetailOrderManagement\Api\Exception\UnsupportedOperation'; |
||
| 1287 | $unsupportedHttpAction = '\eBayEnterprise\RetailOrderManagement\Api\Exception\UnsupportedHttpAction'; |
||
| 1288 | $baseException = 'Exception'; |
||
| 1289 | return [ |
||
| 1290 | [$invalidPayload, $invalidPayload], |
||
| 1291 | [$networkError, null], |
||
| 1292 | [$unsupportedOperation, $unsupportedOperation], |
||
| 1293 | [$unsupportedHttpAction, $unsupportedHttpAction], |
||
| 1294 | [$baseException, $baseException], |
||
| 1295 | ]; |
||
| 1296 | } |
||
| 1297 | |||
| 1298 | /** |
||
| 1299 | * GIVEN An <sdkApi> that will thrown an <exception> of <exceptionType> when making a request. |
||
| 1300 | * WHEN A request is made. |
||
| 1301 | * THEN The <exception> will be caught. |
||
| 1302 | * AND An exception of <expectedExceptionType> will be thrown or null will be returned. |
||
| 1303 | * |
||
| 1304 | * @param string |
||
| 1305 | * @param string|null |
||
| 1306 | * @dataProvider provideSdkExceptions |
||
| 1307 | */ |
||
| 1308 | public function testSdkExceptionHandling($exceptionType, $expectedExceptionType = null) |
||
| 1309 | { |
||
| 1310 | $exception = new $exceptionType(__METHOD__ . ': Test Exception'); |
||
| 1311 | $this->_sdkApi->method('send') |
||
| 1312 | ->will($this->throwException($exception)); |
||
| 1328 | } |
||
| 1329 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..