-
Notifications
You must be signed in to change notification settings - Fork 983
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1004 from stripe/csabol/IOS-829_zip_crash
Fixes crash in STPAddCardViewController with some prefilled billing addresses
- Loading branch information
Showing
6 changed files
with
243 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NSLocale+STPSwizzling.h | ||
// StripeiOS Tests | ||
// | ||
// Created by Cameron Sabol on 7/17/18. | ||
// Copyright © 2018 Stripe, Inc. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface NSLocale (STPSwizzling) | ||
|
||
+ (void)stp_setCurrentLocale:(NSLocale *)locale; | ||
+ (void)stp_resetCurrentLocale; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// NSLocale+STPSwizzling.m | ||
// StripeiOS Tests | ||
// | ||
// Created by Cameron Sabol on 7/17/18. | ||
// Copyright © 2018 Stripe, Inc. All rights reserved. | ||
// | ||
|
||
#import "NSLocale+STPSwizzling.h" | ||
|
||
#import <objc/runtime.h> | ||
|
||
@interface NSObject (STPSwizzling) | ||
|
||
+ (void)stp_swizzleClassMethod:(SEL)original withReplacement:(SEL)replacement; | ||
|
||
@end | ||
|
||
@implementation NSObject (STPSwizzling) | ||
|
||
+ (void)stp_swizzleClassMethod:(SEL)original withReplacement:(SEL)replacement | ||
{ | ||
method_exchangeImplementations(class_getClassMethod(self, original), class_getClassMethod(self, replacement)); | ||
} | ||
|
||
@end | ||
|
||
@implementation NSLocale (STPSwizzling) | ||
|
||
static NSLocale *_stpLocaleOverride = nil; | ||
|
||
+ (void)stp_setCurrentLocale:(NSLocale *)locale | ||
{ | ||
if (_stpLocaleOverride == nil & locale != nil) { | ||
[self stp_swizzleClassMethod:@selector(currentLocale) withReplacement:@selector(stp_currentLocale)]; | ||
[self stp_swizzleClassMethod:@selector(autoupdatingCurrentLocale) withReplacement:@selector(stp_autoUpdatingCurrentLocale)]; | ||
[self stp_swizzleClassMethod:@selector(systemLocale) withReplacement:@selector(stp_systemLocale)]; | ||
} | ||
_stpLocaleOverride = locale; | ||
} | ||
|
||
+ (void)stp_resetCurrentLocale | ||
{ | ||
[self stp_setCurrentLocale:nil]; | ||
} | ||
|
||
+ (instancetype)stp_currentLocale { | ||
return _stpLocaleOverride ?: [self stp_currentLocale]; | ||
} | ||
|
||
+ (instancetype)stp_autoUpdatingCurrentLocale { | ||
return _stpLocaleOverride ?: [self stp_autoUpdatingCurrentLocale]; | ||
} | ||
|
||
+ (instancetype)stp_systemLocale { | ||
return _stpLocaleOverride ?: [self stp_systemLocale]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// STPShippingAddressViewControllerTest.m | ||
// StripeiOS Tests | ||
// | ||
// Created by Cameron Sabol on 8/7/18. | ||
// Copyright © 2018 Stripe, Inc. All rights reserved. | ||
// | ||
|
||
#import <XCTest/XCTest.h> | ||
|
||
#import <Stripe/Stripe.h> | ||
#import "NSLocale+STPSwizzling.h" | ||
#import "STPFixtures.h" | ||
#import "STPPostalCodeValidator.h" | ||
|
||
@interface STPShippingAddressViewControllerTest : XCTestCase | ||
|
||
@end | ||
|
||
@implementation STPShippingAddressViewControllerTest | ||
|
||
- (void)testPrefilledBillingAddress_removeAddress { | ||
STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; | ||
config.requiredShippingAddressFields = [NSSet setWithObject:STPContactFieldPostalAddress]; | ||
|
||
STPAddress *address = [STPAddress new]; | ||
address.name = @"John Smith Doe"; | ||
address.phone = @"8885551212"; | ||
address.email = @"foo@example.com"; | ||
address.line1 = @"55 John St"; | ||
address.city = @"Harare"; | ||
address.postalCode = @"10002"; | ||
address.country = @"ZW"; // Zimbabwe does not require zip codes, while the default locale for tests (US) does | ||
// Sanity checks | ||
XCTAssertFalse([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"ZW"]); | ||
XCTAssertTrue([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"US"]); | ||
|
||
STPShippingAddressViewController *sut = [[STPShippingAddressViewController alloc] initWithConfiguration:config | ||
theme:[STPTheme defaultTheme] | ||
currency:nil | ||
shippingAddress:address | ||
selectedShippingMethod:nil | ||
prefilledInformation:nil]; | ||
|
||
XCTAssertNoThrow([sut loadView]); | ||
XCTAssertNoThrow([sut viewDidLoad]); | ||
} | ||
|
||
- (void)testPrefilledBillingAddress_addAddress { | ||
[NSLocale stp_setCurrentLocale:[NSLocale localeWithLocaleIdentifier:@"en_ZW"]]; | ||
// Zimbabwe does not require zip codes, while the default locale for tests (US) does | ||
// Sanity checks | ||
XCTAssertFalse([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"ZW"]); | ||
XCTAssertTrue([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"US"]); | ||
STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; | ||
config.requiredShippingAddressFields = [NSSet setWithObject:STPContactFieldPostalAddress]; | ||
|
||
STPAddress *address = [STPAddress new]; | ||
address.name = @"John Smith Doe"; | ||
address.phone = @"8885551212"; | ||
address.email = @"foo@example.com"; | ||
address.line1 = @"55 John St"; | ||
address.city = @"New York"; | ||
address.state = @"NY"; | ||
address.postalCode = @"10002"; | ||
address.country = @"US"; | ||
|
||
STPShippingAddressViewController *sut = [[STPShippingAddressViewController alloc] initWithConfiguration:config | ||
theme:[STPTheme defaultTheme] | ||
currency:nil | ||
shippingAddress:address | ||
selectedShippingMethod:nil | ||
prefilledInformation:nil]; | ||
|
||
XCTAssertNoThrow([sut loadView]); | ||
XCTAssertNoThrow([sut viewDidLoad]); | ||
[NSLocale stp_resetCurrentLocale]; | ||
} | ||
|
||
|
||
@end |