Commit 080cb8cc by Dima Bart

Add fake safari view controller and use it in payment provider tests.

- add initializer to payment provider to inject fake safari controller
parent e64df449
//
// BUYFakeSafariController.h
// Mobile Buy SDK
//
// Created by Shopify.
// Copyright (c) 2015 Shopify Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <SafariServices/SafariServices.h>
@interface BUYFakeSafariController : SFSafariViewController
@end
//
// BUYFakeSafariController.m
// Mobile Buy SDK
//
// Created by Shopify.
// Copyright (c) 2015 Shopify Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#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
- (instancetype)initWithURL:(NSURL *)URL {
return [self initWithURL:URL entersReaderIfAvailable:YES];
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wobjc-designated-initializers"
- (instancetype)initWithURL:(NSURL *)URL entersReaderIfAvailable:(BOOL)entersReaderIfAvailable {
return (id)[[BUYFakeDelegator alloc] init];
}
#pragma clang diagnostic pop
@end
......@@ -14,12 +14,17 @@
#import "BUYWebCheckoutPaymentProvider.h"
#import "BUYClientTestBase.h"
#import "BUYPaymentController.h"
#import "BUYFakeSafariController.h"
#import <OHHTTPStubs/OHHTTPStubs.h>
@interface BUYPaymentController ()
@interface BUYPaymentController (Private)
- (id <BUYPaymentProvider>)providerForType:(NSString *)type;
@end
@interface BUYWebCheckoutPaymentProvider (Private)
- (instancetype)initWithClient:(BUYClient *)client webClass:(Class)webClass;
@end
@interface BUYPaymentProviderTests : XCTestCase <BUYPaymentProviderDelegate>
@property (nonatomic) NSMutableDictionary <NSString *, XCTestExpectation *> *expectations;
......@@ -157,7 +162,7 @@
{
[self mockRequests];
BUYWebCheckoutPaymentProvider *webProvider = [[BUYWebCheckoutPaymentProvider alloc] initWithClient:self.client];
BUYWebCheckoutPaymentProvider *webProvider = [[BUYWebCheckoutPaymentProvider alloc] initWithClient:self.client webClass:[BUYFakeSafariController class]];
webProvider.delegate = self;
self.expectations[@"presentController"] = [self expectationWithDescription:NSStringFromSelector(_cmd)];
......
......@@ -379,6 +379,7 @@
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 */; };
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, ); }; };
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, ); }; };
......@@ -659,6 +660,10 @@
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>"; };
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>"; };
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>"; };
......@@ -1019,6 +1024,8 @@
children = (
9A47CF1E1CE50EBB00A6D5BA /* BUYApplePayTestToken.h */,
9A47CF1F1CE50EBB00A6D5BA /* BUYApplePayTestToken.m */,
9ABBCC9F1CF5C9D10075B0C5 /* BUYFakeSafariController.h */,
9ABBCCA01CF5C9D10075B0C5 /* BUYFakeSafariController.m */,
);
name = "Test Objects";
sourceTree = "<group>";
......@@ -1643,6 +1650,7 @@
8498DCCC1CDD208200BD12A8 /* BUYCustomerTests.m in Sources */,
B2653EC51CEF55CC0012D57D /* BUYModelManager+ApplePay.m in Sources */,
8498DCCE1CDD208200BD12A8 /* BUYOrderTests.m in Sources */,
9ABBCCA11CF5C9D10075B0C5 /* BUYFakeSafariController.m in Sources */,
9A0B0C831CEB981C0037D68F /* BUYClient+RoutingTests.m in Sources */,
849110351CCE70CE00E53B93 /* BUYDictionaryAdditionsTests.m in Sources */,
90F5930A1B0D5F4C0026B382 /* BUYLineItemTest.m in Sources */,
......
......@@ -42,7 +42,7 @@ extern NSString * const BUYWebPaymentProviderId;
*
* @return an instance of `BUYWebCheckoutPaymentProvider`
*/
- (instancetype)initWithClient:(BUYClient *)client NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithClient:(BUYClient *)client;
- (instancetype)init NS_UNAVAILABLE;
@end
......
......@@ -27,6 +27,7 @@
#import "BUYWebCheckoutPaymentProvider.h"
#import "BUYCheckout.h"
#import "BUYClient+CheckoutHelpers.h"
#import "BUYAssert.h"
@import SafariServices;
......@@ -37,32 +38,58 @@ NSString * const BUYWebPaymentProviderId = @"BUYWebPaymentProviderId";
static NSString *const WebCheckoutCustomerAccessToken = @"customer_access_token";
@interface BUYWebCheckoutPaymentProvider () <SFSafariViewControllerDelegate>
@property (nonatomic, strong) BUYCheckout *checkout;
@property (nonatomic, strong) BUYClient *client;
@property (nonatomic, strong) Class webClass;
- (instancetype)initWithClient:(BUYClient *)client webClass:(Class)webClass NS_DESIGNATED_INITIALIZER;
@end
@implementation BUYWebCheckoutPaymentProvider
@synthesize delegate;
#pragma mark - Init -
- (instancetype)initWithClient:(BUYClient *)client
{
NSParameterAssert(client);
return [self initWithClient:client webClass:[SFSafariViewController class]];
}
- (instancetype)initWithClient:(BUYClient *)client webClass:(Class)webClass
{
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];
if (self) {
_client = client;
_client = client;
_webClass = webClass;
}
return self;
}
#pragma mark - Accessors -
- (BOOL)isAvailable
{
return YES;
}
- (BOOL)isInProgress
{
return (self.checkout != nil);
}
- (NSString *)identifier
{
return BUYWebPaymentProviderId;
}
#pragma mark - Equality -
- (NSUInteger)hash
{
return self.identifier.hash;
......@@ -73,10 +100,7 @@ static NSString *const WebCheckoutCustomerAccessToken = @"customer_access_token"
return ([object isKindOfClass:[self class]] && [self.identifier isEqual:[object identifier]]);
}
- (NSString *)identifier
{
return BUYWebPaymentProviderId;
}
#pragma mark - Checkout -
- (void)startCheckout:(BUYCheckout *)checkout
{
......@@ -108,11 +132,6 @@ static NSString *const WebCheckoutCustomerAccessToken = @"customer_access_token"
self.checkout = nil;
}
- (BOOL)isAvailable
{
return YES;
}
- (void)postCheckoutCompletion:(BUYCheckout *)checkout error:(NSError *)error
{
if (self.checkout && error == nil) {
......@@ -140,9 +159,9 @@ static NSString *const WebCheckoutCustomerAccessToken = @"customer_access_token"
- (void)openWebCheckout:(BUYCheckout *)checkout
{
NSURL *checkoutURL = [self authenticatedWebCheckoutURL:checkout.webCheckoutURL];
if ([SFSafariViewController class]) {
if (self.webClass) {
SFSafariViewController *safariViewController = [[SFSafariViewController alloc] initWithURL:checkoutURL];
SFSafariViewController *safariViewController = [[self.webClass alloc] initWithURL:checkoutURL];
safariViewController.delegate = self;
[self.delegate paymentProvider:self wantsControllerPresented:safariViewController];
}
......@@ -163,7 +182,7 @@ static NSString *const WebCheckoutCustomerAccessToken = @"customer_access_token"
return authenticatedComponents.URL;
}
#pragma mark - Web Checkout delegate methods
#pragma mark - Web Checkout Delegate Methods -
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller;
{
......
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