Commit ddfbb567 by Dima Bart

Modify implementation to avoid providing fake class in initializer.

parent 080cb8cc
...@@ -24,8 +24,10 @@ ...@@ -24,8 +24,10 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import <SafariServices/SafariServices.h> #import <Foundation/Foundation.h>
@interface BUYFakeSafariController : SFSafariViewController @interface BUYFakeSafariController : NSObject
@property (weak, nonatomic) id delegate;
@end @end
...@@ -26,35 +26,14 @@ ...@@ -26,35 +26,14 @@
#import "BUYFakeSafariController.h" #import "BUYFakeSafariController.h"
#pragma mark - Delegator -
@interface BUYFakeDelegator : NSObject
@property (weak, nonatomic) id delegate;
@end
@implementation BUYFakeDelegator
@end
#pragma mark - Fake Controller -
@interface BUYFakeSafariController ()
@end
@implementation BUYFakeSafariController @implementation BUYFakeSafariController
- (instancetype)initWithURL:(NSURL *)URL { - (instancetype)initWithURL:(NSURL *)URL {
return [self initWithURL:URL entersReaderIfAvailable:YES]; return [self initWithURL:URL entersReaderIfAvailable:YES];
} }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wobjc-designated-initializers"
- (instancetype)initWithURL:(NSURL *)URL entersReaderIfAvailable:(BOOL)entersReaderIfAvailable { - (instancetype)initWithURL:(NSURL *)URL entersReaderIfAvailable:(BOOL)entersReaderIfAvailable {
return (id)[[BUYFakeDelegator alloc] init]; return (id)[[[self class] alloc] init];
} }
#pragma clang diagnostic pop
@end @end
...@@ -17,14 +17,12 @@ ...@@ -17,14 +17,12 @@
#import "BUYFakeSafariController.h" #import "BUYFakeSafariController.h"
#import <OHHTTPStubs/OHHTTPStubs.h> #import <OHHTTPStubs/OHHTTPStubs.h>
extern Class SafariViewControllerClass;
@interface BUYPaymentController (Private) @interface BUYPaymentController (Private)
- (id <BUYPaymentProvider>)providerForType:(NSString *)type; - (id <BUYPaymentProvider>)providerForType:(NSString *)type;
@end @end
@interface BUYWebCheckoutPaymentProvider (Private)
- (instancetype)initWithClient:(BUYClient *)client webClass:(Class)webClass;
@end
@interface BUYPaymentProviderTests : XCTestCase <BUYPaymentProviderDelegate> @interface BUYPaymentProviderTests : XCTestCase <BUYPaymentProviderDelegate>
@property (nonatomic) NSMutableDictionary <NSString *, XCTestExpectation *> *expectations; @property (nonatomic) NSMutableDictionary <NSString *, XCTestExpectation *> *expectations;
...@@ -39,6 +37,15 @@ ...@@ -39,6 +37,15 @@
[super setUp]; [super setUp];
self.modelManager = [BUYModelManager modelManager]; self.modelManager = [BUYModelManager modelManager];
self.expectations = [@{} mutableCopy]; self.expectations = [@{} mutableCopy];
/* ---------------------------------
* We need to kick off the provider
* class initialization before setting
* the fake safari controller to
* prevent it getting overriden.
*/
[BUYWebCheckoutPaymentProvider class];
SafariViewControllerClass = [BUYFakeSafariController class];
} }
- (void)tearDown - (void)tearDown
...@@ -162,7 +169,7 @@ ...@@ -162,7 +169,7 @@
{ {
[self mockRequests]; [self mockRequests];
BUYWebCheckoutPaymentProvider *webProvider = [[BUYWebCheckoutPaymentProvider alloc] initWithClient:self.client webClass:[BUYFakeSafariController class]]; BUYWebCheckoutPaymentProvider *webProvider = [[BUYWebCheckoutPaymentProvider alloc] initWithClient:self.client];
webProvider.delegate = self; webProvider.delegate = self;
self.expectations[@"presentController"] = [self expectationWithDescription:NSStringFromSelector(_cmd)]; self.expectations[@"presentController"] = [self expectationWithDescription:NSStringFromSelector(_cmd)];
......
...@@ -374,12 +374,12 @@ ...@@ -374,12 +374,12 @@
9A47CF221CE5112A00A6D5BA /* BUYAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A47CF211CE5112A00A6D5BA /* BUYAssert.h */; }; 9A47CF221CE5112A00A6D5BA /* BUYAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A47CF211CE5112A00A6D5BA /* BUYAssert.h */; };
9A47CF231CE5112A00A6D5BA /* BUYAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A47CF211CE5112A00A6D5BA /* BUYAssert.h */; }; 9A47CF231CE5112A00A6D5BA /* BUYAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A47CF211CE5112A00A6D5BA /* BUYAssert.h */; };
9A6B03791CDA5D4F0054C26E /* BUYAccountCredentialsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A6B03781CDA5D4F0054C26E /* BUYAccountCredentialsTests.m */; }; 9A6B03791CDA5D4F0054C26E /* BUYAccountCredentialsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A6B03781CDA5D4F0054C26E /* BUYAccountCredentialsTests.m */; };
9ABBCCA11CF5C9D10075B0C5 /* BUYFakeSafariController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9ABBCCA01CF5C9D10075B0C5 /* BUYFakeSafariController.m */; };
B2653EC31CEF55CC0012D57D /* BUYModelManager+ApplePay.h in Headers */ = {isa = PBXBuildFile; fileRef = B2653EC11CEF55CC0012D57D /* BUYModelManager+ApplePay.h */; }; B2653EC31CEF55CC0012D57D /* BUYModelManager+ApplePay.h in Headers */ = {isa = PBXBuildFile; fileRef = B2653EC11CEF55CC0012D57D /* BUYModelManager+ApplePay.h */; };
B2653EC41CEF55CC0012D57D /* BUYModelManager+ApplePay.h in Headers */ = {isa = PBXBuildFile; fileRef = B2653EC11CEF55CC0012D57D /* BUYModelManager+ApplePay.h */; }; B2653EC41CEF55CC0012D57D /* BUYModelManager+ApplePay.h in Headers */ = {isa = PBXBuildFile; fileRef = B2653EC11CEF55CC0012D57D /* BUYModelManager+ApplePay.h */; };
B2653EC51CEF55CC0012D57D /* BUYModelManager+ApplePay.m in Sources */ = {isa = PBXBuildFile; fileRef = B2653EC21CEF55CC0012D57D /* BUYModelManager+ApplePay.m */; }; B2653EC51CEF55CC0012D57D /* BUYModelManager+ApplePay.m in Sources */ = {isa = PBXBuildFile; fileRef = B2653EC21CEF55CC0012D57D /* BUYModelManager+ApplePay.m */; };
B2653EC61CEF55CC0012D57D /* BUYModelManager+ApplePay.m in Sources */ = {isa = PBXBuildFile; fileRef = B2653EC21CEF55CC0012D57D /* BUYModelManager+ApplePay.m */; }; B2653EC61CEF55CC0012D57D /* BUYModelManager+ApplePay.m in Sources */ = {isa = PBXBuildFile; fileRef = B2653EC21CEF55CC0012D57D /* BUYModelManager+ApplePay.m */; };
B2653EC71CEF55CC0012D57D /* BUYModelManager+ApplePay.m in Sources */ = {isa = PBXBuildFile; fileRef = B2653EC21CEF55CC0012D57D /* BUYModelManager+ApplePay.m */; }; B2653EC71CEF55CC0012D57D /* BUYModelManager+ApplePay.m in Sources */ = {isa = PBXBuildFile; fileRef = B2653EC21CEF55CC0012D57D /* BUYModelManager+ApplePay.m */; };
9ABBCCA11CF5C9D10075B0C5 /* BUYFakeSafariController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9ABBCCA01CF5C9D10075B0C5 /* BUYFakeSafariController.m */; };
BE47340F1B66C4EF00AA721A /* BUYError.h in Headers */ = {isa = PBXBuildFile; fileRef = BE47340D1B66C4EF00AA721A /* BUYError.h */; settings = {ATTRIBUTES = (Public, ); }; }; BE47340F1B66C4EF00AA721A /* BUYError.h in Headers */ = {isa = PBXBuildFile; fileRef = BE47340D1B66C4EF00AA721A /* BUYError.h */; settings = {ATTRIBUTES = (Public, ); }; };
BE4734101B66C4EF00AA721A /* BUYError.m in Sources */ = {isa = PBXBuildFile; fileRef = BE47340E1B66C4EF00AA721A /* BUYError.m */; }; BE4734101B66C4EF00AA721A /* BUYError.m in Sources */ = {isa = PBXBuildFile; fileRef = BE47340E1B66C4EF00AA721A /* BUYError.m */; };
BE5DC3631B71022D00B2BC1E /* BUYMaskedCreditCard.h in Headers */ = {isa = PBXBuildFile; fileRef = BE5DC3611B71022D00B2BC1E /* BUYMaskedCreditCard.h */; settings = {ATTRIBUTES = (Public, ); }; }; BE5DC3631B71022D00B2BC1E /* BUYMaskedCreditCard.h in Headers */ = {isa = PBXBuildFile; fileRef = BE5DC3611B71022D00B2BC1E /* BUYMaskedCreditCard.h */; settings = {ATTRIBUTES = (Public, ); }; };
...@@ -656,14 +656,12 @@ ...@@ -656,14 +656,12 @@
9A47CF1F1CE50EBB00A6D5BA /* BUYApplePayTestToken.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYApplePayTestToken.m; sourceTree = "<group>"; }; 9A47CF1F1CE50EBB00A6D5BA /* BUYApplePayTestToken.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYApplePayTestToken.m; sourceTree = "<group>"; };
9A47CF211CE5112A00A6D5BA /* BUYAssert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BUYAssert.h; path = ../Additions/BUYAssert.h; sourceTree = "<group>"; }; 9A47CF211CE5112A00A6D5BA /* BUYAssert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BUYAssert.h; path = ../Additions/BUYAssert.h; sourceTree = "<group>"; };
9A6B03781CDA5D4F0054C26E /* BUYAccountCredentialsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYAccountCredentialsTests.m; sourceTree = "<group>"; }; 9A6B03781CDA5D4F0054C26E /* BUYAccountCredentialsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYAccountCredentialsTests.m; sourceTree = "<group>"; };
9ABBCC9F1CF5C9D10075B0C5 /* BUYFakeSafariController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYFakeSafariController.h; sourceTree = "<group>"; };
9ABBCCA01CF5C9D10075B0C5 /* BUYFakeSafariController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYFakeSafariController.m; sourceTree = "<group>"; };
B2653EC11CEF55CC0012D57D /* BUYModelManager+ApplePay.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "BUYModelManager+ApplePay.h"; sourceTree = "<group>"; }; B2653EC11CEF55CC0012D57D /* BUYModelManager+ApplePay.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "BUYModelManager+ApplePay.h"; sourceTree = "<group>"; };
B2653EC21CEF55CC0012D57D /* BUYModelManager+ApplePay.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "BUYModelManager+ApplePay.m"; sourceTree = "<group>"; }; B2653EC21CEF55CC0012D57D /* BUYModelManager+ApplePay.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "BUYModelManager+ApplePay.m"; sourceTree = "<group>"; };
BE33B4ED1B15FF4D0067982B /* BUYApplePayAuthorizationDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYApplePayAuthorizationDelegate.h; sourceTree = "<group>"; }; BE33B4ED1B15FF4D0067982B /* BUYApplePayAuthorizationDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYApplePayAuthorizationDelegate.h; sourceTree = "<group>"; };
BE33B4EE1B15FF4D0067982B /* BUYApplePayAuthorizationDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYApplePayAuthorizationDelegate.m; sourceTree = "<group>"; }; BE33B4EE1B15FF4D0067982B /* BUYApplePayAuthorizationDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYApplePayAuthorizationDelegate.m; sourceTree = "<group>"; };
9ABBCC9F1CF5C9D10075B0C5 /* BUYFakeSafariController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYFakeSafariController.h; sourceTree = "<group>"; };
9ABBCCA01CF5C9D10075B0C5 /* BUYFakeSafariController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYFakeSafariController.m; sourceTree = "<group>"; };
BE33B4ED1B15FF4D0067982B /* BUYApplePayHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYApplePayHelpers.h; sourceTree = "<group>"; };
BE33B4EE1B15FF4D0067982B /* BUYApplePayHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYApplePayHelpers.m; sourceTree = "<group>"; };
BE47340D1B66C4EF00AA721A /* BUYError.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYError.h; sourceTree = "<group>"; }; BE47340D1B66C4EF00AA721A /* BUYError.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYError.h; sourceTree = "<group>"; };
BE47340E1B66C4EF00AA721A /* BUYError.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYError.m; sourceTree = "<group>"; }; BE47340E1B66C4EF00AA721A /* BUYError.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYError.m; sourceTree = "<group>"; };
BE5DC3611B71022D00B2BC1E /* BUYMaskedCreditCard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYMaskedCreditCard.h; sourceTree = "<group>"; }; BE5DC3611B71022D00B2BC1E /* BUYMaskedCreditCard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYMaskedCreditCard.h; sourceTree = "<group>"; };
......
...@@ -42,7 +42,7 @@ extern NSString * const BUYWebPaymentProviderId; ...@@ -42,7 +42,7 @@ extern NSString * const BUYWebPaymentProviderId;
* *
* @return an instance of `BUYWebCheckoutPaymentProvider` * @return an instance of `BUYWebCheckoutPaymentProvider`
*/ */
- (instancetype)initWithClient:(BUYClient *)client; - (instancetype)initWithClient:(BUYClient *)client NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
@end @end
......
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
@import SafariServices; @import SafariServices;
Class SafariViewControllerClass;
NSString * BUYSafariCallbackURLNotification = @"kBUYSafariCallbackURLNotification"; NSString * BUYSafariCallbackURLNotification = @"kBUYSafariCallbackURLNotification";
NSString * BUYURLKey = @"url"; NSString * BUYURLKey = @"url";
NSString * const BUYWebPaymentProviderId = @"BUYWebPaymentProviderId"; NSString * const BUYWebPaymentProviderId = @"BUYWebPaymentProviderId";
...@@ -41,9 +43,6 @@ static NSString *const WebCheckoutCustomerAccessToken = @"customer_access_token" ...@@ -41,9 +43,6 @@ static NSString *const WebCheckoutCustomerAccessToken = @"customer_access_token"
@property (nonatomic, strong) BUYCheckout *checkout; @property (nonatomic, strong) BUYCheckout *checkout;
@property (nonatomic, strong) BUYClient *client; @property (nonatomic, strong) BUYClient *client;
@property (nonatomic, strong) Class webClass;
- (instancetype)initWithClient:(BUYClient *)client webClass:(Class)webClass NS_DESIGNATED_INITIALIZER;
@end @end
...@@ -51,22 +50,27 @@ static NSString *const WebCheckoutCustomerAccessToken = @"customer_access_token" ...@@ -51,22 +50,27 @@ static NSString *const WebCheckoutCustomerAccessToken = @"customer_access_token"
@synthesize delegate; @synthesize delegate;
#pragma mark - Init - #pragma mark - Initialize -
- (instancetype)initWithClient:(BUYClient *)client + (void)initialize
{ {
return [self initWithClient:client webClass:[SFSafariViewController class]]; /* ----------------------------------
* Used in tests to set a fake / mock
* SFSafariViewController to avoid
* instantiating a real one.
*/
SafariViewControllerClass = [SFSafariViewController class];
} }
- (instancetype)initWithClient:(BUYClient *)client webClass:(Class)webClass #pragma mark - Init -
- (instancetype)initWithClient:(BUYClient *)client
{ {
BUYAssert(client, @"Failed to initialize BUYWebCheckoutPaymentProvider, client must not be nil."); BUYAssert(client, @"Failed to initialize BUYWebCheckoutPaymentProvider, client must not be nil.");
BUYAssert([webClass isSubclassOfClass:[SFSafariViewController class]], @"Failed to initialize BUYWebCheckoutPaymentProvider, webClass must not be nil and be a subclass of SFSafariViewController.");
self = [super init]; self = [super init];
if (self) { if (self) {
_client = client; _client = client;
_webClass = webClass;
} }
return self; return self;
} }
...@@ -159,9 +163,9 @@ static NSString *const WebCheckoutCustomerAccessToken = @"customer_access_token" ...@@ -159,9 +163,9 @@ static NSString *const WebCheckoutCustomerAccessToken = @"customer_access_token"
- (void)openWebCheckout:(BUYCheckout *)checkout - (void)openWebCheckout:(BUYCheckout *)checkout
{ {
NSURL *checkoutURL = [self authenticatedWebCheckoutURL:checkout.webCheckoutURL]; NSURL *checkoutURL = [self authenticatedWebCheckoutURL:checkout.webCheckoutURL];
if (self.webClass) { if (SafariViewControllerClass) {
SFSafariViewController *safariViewController = [[self.webClass alloc] initWithURL:checkoutURL]; SFSafariViewController *safariViewController = [[SafariViewControllerClass alloc] initWithURL:checkoutURL];
safariViewController.delegate = self; safariViewController.delegate = self;
[self.delegate paymentProvider:self wantsControllerPresented:safariViewController]; [self.delegate paymentProvider:self wantsControllerPresented:safariViewController];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment