MOBILESDK-292: Migrate STPAnalyticsClientTest Objc -> Swift (#236)

Migrates the tests from `STPAnalyticsClientTest.m` to `STPAnalyticsClientTest.swift` or `STPAnalyticsClientPaymentsTest.swift`, depending on whether the test logic is generic or payment-specific.
This commit is contained in:
Mel 2021-06-04 17:09:06 -07:00 committed by GitHub
parent dd73f8d6f3
commit 9a3f35f4f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 97 additions and 128 deletions

View File

@ -300,7 +300,6 @@
31C5B873252E773100A481A7 /* STPPaymentOptionsViewControllerLocalizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3111C391252BC78C00207E32 /* STPPaymentOptionsViewControllerLocalizationTests.swift */; };
31C5B87C252E859300A481A7 /* STPPaymentConfigurationTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B013C881F1E784A00DD831B /* STPPaymentConfigurationTest.m */; };
31C5B87E252E869D00A481A7 /* StripeErrorTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3111C3F8252BC79D00207E32 /* StripeErrorTest.swift */; };
31C5B886252E9AD200A481A7 /* STPAnalyticsClientTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C124A1841CCAB750007D42EE /* STPAnalyticsClientTest.m */; };
31C5B888252E9C7400A481A7 /* STPCustomerContextTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C1E4F8051EBBEB0F00E611F5 /* STPCustomerContextTest.m */; };
31C5B88A252E9DBB00A481A7 /* STPEphemeralKeyManagerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C184107D1EC2704700178149 /* STPEphemeralKeyManagerTest.m */; };
31C5B88C252E9E0600A481A7 /* STPLabeledFormTextFieldViewSnapshotTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 36AEBFBD241C3B7500CFCAE8 /* STPLabeledFormTextFieldViewSnapshotTests.m */; };
@ -1347,7 +1346,6 @@
C1054F901FE197AE0033C87E /* STPPaymentContextSnapshotTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = STPPaymentContextSnapshotTests.m; sourceTree = "<group>"; };
C1080F4B1CBED48A007B2D89 /* STPAddressTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPAddressTests.m; sourceTree = "<group>"; };
C11B14961E8AE316000F760C /* OCMock.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = OCMock.xcframework; path = Carthage/Build/OCMock.xcframework; sourceTree = "<group>"; };
C124A1841CCAB750007D42EE /* STPAnalyticsClientTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPAnalyticsClientTest.m; sourceTree = "<group>"; };
C17D24ED1E37DBAC005CB188 /* STPSourceTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPSourceTest.m; sourceTree = "<group>"; };
C184107D1EC2704700178149 /* STPEphemeralKeyManagerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPEphemeralKeyManagerTest.m; sourceTree = "<group>"; };
C18867D91E8B0C4100A77634 /* STPFixtures.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = STPFixtures.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
@ -2451,7 +2449,6 @@
3111C3BA252BC79300207E32 /* STPAddressViewModelTest.swift */,
E61BEEAF265F6CAC0002FA4F /* STPAnalyticsClientPaymentSheetTest.swift */,
E61BEEAD265F6BDC0002FA4F /* STPAnalyticsClientPaymentsTest.swift */,
C124A1841CCAB750007D42EE /* STPAnalyticsClientTest.m */,
B65E74AB258930500080D9B3 /* STPAnalyticsClientTest.swift */,
3111C3A9252BC79000207E32 /* STPAPIClientTest.swift */,
3111C401252BC79F00207E32 /* STPApplePayContextTest.swift */,
@ -3045,7 +3042,6 @@
3111C58D252BEC9900207E32 /* STPNumericStringValidatorTests.swift in Sources */,
3667949025B790250094831B /* STPCardFormViewTests.swift in Sources */,
3111C55F252BDB0000207E32 /* NSString+StripeTest.swift in Sources */,
31C5B886252E9AD200A481A7 /* STPAnalyticsClientTest.m in Sources */,
3111C56B252BDD5000207E32 /* FBSnapshotTestCase+STPViewControllerLoading.swift in Sources */,
31C5B898252EA19200A481A7 /* STPImageLibraryTest.m in Sources */,
368E1F6D254CC87300150A2D /* STPPostalCodeInputTextFieldSnapshotTests.swift in Sources */,

View File

@ -99,6 +99,88 @@ class STPAnalyticsClientPaymentsTest: XCTestCase {
XCTAssertNotNil(payload["product_usage"])
}
func testTokenTypeFromParameters() {
let card = STPFixtures.cardParams()
let cardDict = buildTokenParams(card)
XCTAssertEqual(STPAnalyticsClient.tokenType(fromParameters: cardDict), "card")
let account = STPFixtures.accountParams()
let accountDict = buildTokenParams(account)
XCTAssertEqual(STPAnalyticsClient.tokenType(fromParameters: accountDict), "account")
let bank = STPFixtures.bankAccountParams()
let bankDict = buildTokenParams(bank)
XCTAssertEqual(STPAnalyticsClient.tokenType(fromParameters: bankDict), "bank_account");
let applePay = STPFixtures.applePayPayment()
let applePayDict = addTelemetry(STPAPIClient.shared.parameters(for: applePay))
XCTAssertEqual(STPAnalyticsClient.tokenType(fromParameters: applePayDict), "apple_pay")
}
// MARK: - Tests various classes report usage
func testCardTextFieldAddsUsage() {
let _ = STPPaymentCardTextField()
XCTAssertTrue(STPAnalyticsClient.sharedClient.productUsage.contains("STPPaymentCardTextField"))
}
func testPaymentContextAddsUsage() {
let keyManager = STPEphemeralKeyManager(keyProvider: MockKeyProvider(), apiVersion: "1", performsEagerFetching: false)
let apiClient = STPAPIClient()
let customerContext = STPCustomerContext.init(keyManager: keyManager, apiClient: apiClient)
let _ = STPPaymentContext(customerContext: customerContext)
XCTAssertTrue(STPAnalyticsClient.sharedClient.productUsage.contains("STPCustomerContext"))
}
func testApplePayContextAddsUsage() {
let _ = STPApplePayContext(paymentRequest: STPFixtures.applePayRequest(), delegate: nil)
XCTAssertTrue(STPAnalyticsClient.sharedClient.productUsage.contains("STPApplePayContext"))
}
func testCustomerContextAddsUsage() {
let keyManager = STPEphemeralKeyManager(keyProvider: MockKeyProvider(), apiVersion: "1", performsEagerFetching: false)
let apiClient = STPAPIClient()
let _ = STPCustomerContext(keyManager: keyManager, apiClient: apiClient)
XCTAssertTrue(STPAnalyticsClient.sharedClient.productUsage.contains("STPCustomerContext"))
}
func testAddCardVCAddsUsage() {
let _ = STPAddCardViewController()
XCTAssertTrue(STPAnalyticsClient.sharedClient.productUsage.contains("STPAddCardViewController"))
}
func testBankSelectionVCAddsUsage() {
let _ = STPBankSelectionViewController()
XCTAssertTrue(STPAnalyticsClient.sharedClient.productUsage.contains("STPBankSelectionViewController"))
}
func testShippingVCAddsUsage() {
let config = STPFixtures.paymentConfiguration()
config.requiredShippingAddressFields = [STPContactField.postalAddress]
let _ = STPShippingAddressViewController(
configuration: config,
theme: .defaultTheme,
currency: nil,
shippingAddress: nil,
selectedShippingMethod: nil,
prefilledInformation: nil
)
XCTAssertTrue(STPAnalyticsClient.sharedClient.productUsage.contains("STPShippingAddressViewController"))
}
}
// MARK - Helpers
private extension STPAnalyticsClientPaymentsTest {
func buildTokenParams<T: STPFormEncodable & NSObject>(_ object: T) -> [String: Any] {
return addTelemetry(STPFormEncoder.dictionary(forObject: object))
}
func addTelemetry(_ params: [String: Any]) -> [String: Any] {
// STPAPIClient adds these before determining the token type,
// so do the same in the test
return STPTelemetryClient.shared.paramsByAddingTelemetryFields(toParams: params)
}
}
// MARK: - Mock types
@ -119,3 +201,11 @@ private struct MockAnalyticsClass1: STPAnalyticsProtocol {
private struct MockAnalyticsClass2: STPAnalyticsProtocol {
static let stp_analyticsIdentifier = "MockAnalyticsClass2"
}
private class MockKeyProvider: NSObject, STPEphemeralKeyProvider {
func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
guard apiVersion == "1" else { return }
completion(nil, NSError.stp_genericConnectionError())
}
}

View File

@ -1,121 +0,0 @@
//
// STPAnalyticsClientTest.m
// Stripe
//
// Created by Ben Guo on 4/22/16.
// Copyright © 2016 Stripe, Inc. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "STPFixtures.h"
@interface STPAPIClient (Testing)
+ (NSDictionary *)parametersForPayment:(PKPayment *)payment;
@end
@interface STPAnalyticsClient (Testing)
+ (BOOL)shouldCollectAnalytics;
@property (nonatomic) NSSet *productUsage;
@end
@interface STPAnalyticsClientTest : XCTestCase
@end
@implementation STPAnalyticsClientTest
- (void)testShouldCollectAnalytics_alwaysFalseInTest {
XCTAssertFalse([STPAnalyticsClient shouldCollectAnalytics]);
}
- (void)testTokenTypeFromParameters {
STPCardParams *card = [STPFixtures cardParams];
NSDictionary *cardDict = [self buildTokenParams:card];
XCTAssertEqualObjects([STPAnalyticsClient tokenTypeFromParameters:cardDict], @"card");
STPConnectAccountParams *account = [STPFixtures accountParams];
NSDictionary *accountDict = [self buildTokenParams:account];
XCTAssertEqualObjects([STPAnalyticsClient tokenTypeFromParameters:accountDict], @"account");
STPBankAccountParams *bank = [STPFixtures bankAccountParams];
NSDictionary *bankDict = [self buildTokenParams:bank];
XCTAssertEqualObjects([STPAnalyticsClient tokenTypeFromParameters:bankDict], @"bank_account");
PKPayment *applePay = [STPFixtures applePayPayment];
NSDictionary *applePayDict = [self addTelemetry:[[STPAPIClient sharedClient] parametersForPayment:applePay]];
XCTAssertEqualObjects([STPAnalyticsClient tokenTypeFromParameters:applePayDict], @"apple_pay");
}
#pragma mark - Tests various classes report usage
- (void)testCardTextFieldAddsUsage {
__unused STPPaymentCardTextField *_ = [[STPPaymentCardTextField alloc] init];
XCTAssertTrue([[STPAnalyticsClient sharedClient].productUsage containsObject:@"STPPaymentCardTextField"]);
}
- (id)mockKeyProvider {
id mockKeyProvider = OCMProtocolMock(@protocol(STPEphemeralKeyProvider));
OCMStub([mockKeyProvider createCustomerKeyWithAPIVersion:[OCMArg isEqual:@"1"]
completion:[OCMArg any]])
.andDo(^(NSInvocation *invocation) {
__unsafe_unretained STPJSONResponseCompletionBlock completion;
[invocation getArgument:&completion atIndex:3];
completion(nil, [NSError stp_genericConnectionError]);
});
return mockKeyProvider;
}
- (void)testPaymentContextAddsUsage{
STPEphemeralKeyManager *keyManager = [[STPEphemeralKeyManager alloc] initWithKeyProvider:[self mockKeyProvider] apiVersion:@"1" performsEagerFetching:NO];
STPAPIClient *apiClient = [STPAPIClient new];
STPCustomerContext *customerContext = [[STPCustomerContext alloc] initWithKeyManager:keyManager apiClient:apiClient];
__unused STPPaymentContext *_ = [[STPPaymentContext alloc] initWithCustomerContext:customerContext];
XCTAssertTrue([[STPAnalyticsClient sharedClient].productUsage containsObject:@"STPCustomerContext"]);
}
- (void)testApplePayContextAddsUsage{
id delegate;
__unused STPApplePayContext *_ = [[STPApplePayContext alloc] initWithPaymentRequest:[STPFixtures applePayRequest] delegate:delegate];
XCTAssertTrue([[STPAnalyticsClient sharedClient].productUsage containsObject:@"STPApplePayContext"]);
}
- (void)testCustomerContextAddsUsage {
STPEphemeralKeyManager *keyManager = [[STPEphemeralKeyManager alloc] initWithKeyProvider:[self mockKeyProvider] apiVersion:@"1" performsEagerFetching:NO];
STPAPIClient *apiClient = [STPAPIClient new];
__unused STPCustomerContext *_ = [[STPCustomerContext alloc] initWithKeyManager:keyManager apiClient:apiClient];
XCTAssertTrue([[STPAnalyticsClient sharedClient].productUsage containsObject:@"STPCustomerContext"]);
}
- (void)testAddCardVCAddsUsage {
__unused STPAddCardViewController *_ = [[STPAddCardViewController alloc] init];
XCTAssertTrue([[STPAnalyticsClient sharedClient].productUsage containsObject:@"STPAddCardViewController"]);
}
- (void)testBankSelectionVCAddsUsage {
__unused STPBankSelectionViewController *_ = [[STPBankSelectionViewController alloc] init];
XCTAssertTrue([[STPAnalyticsClient sharedClient].productUsage containsObject:@"STPBankSelectionViewController"]);
}
- (void)testShippingVCAddsUsage {
STPPaymentConfiguration *config = [STPFixtures paymentConfiguration];
config.requiredShippingAddressFields = [NSSet setWithObject:STPContactField.postalAddress];
__unused STPShippingAddressViewController *_ = [[STPShippingAddressViewController alloc] initWithConfiguration:config theme:[STPTheme defaultTheme] currency:nil shippingAddress:nil selectedShippingMethod:nil prefilledInformation:nil];
XCTAssertTrue([[STPAnalyticsClient sharedClient].productUsage containsObject:@"STPShippingAddressViewController"]);
}
#pragma mark - Helpers
- (NSDictionary *)buildTokenParams:(nonnull NSObject<STPFormEncodable> *)object {
return [self addTelemetry:[STPFormEncoder dictionaryForObject:object]];
}
- (NSDictionary *)addTelemetry:(NSDictionary *)params {
// STPAPIClient adds these before determining the token type,
// so do the same in the test
return [[STPTelemetryClient sharedInstance] paramsByAddingTelemetryFieldsToParams:params];
}
@end

View File

@ -11,10 +11,14 @@ import XCTest
@testable import Stripe
class STPAnalyticsClientTestSwift: XCTestCase {
class STPAnalyticsClientTest: XCTestCase {
func testShouldCollectAnalytics_alwaysFalseInTest() {
XCTAssertFalse(STPAnalyticsClient.shouldCollectAnalytics())
}
// MARK: - Test serialization
// TODO(mludowise|MOBILESDK-292): Migrate objc tests to swift
func testSerializeError() {
let userInfo = [
"key1": "value1",