Skip to content

Commit

Permalink
Merge pull request #290 from stripe/bg-setcardparams
Browse files Browse the repository at this point in the history
Add setCard to STPPaymentCardTextField
  • Loading branch information
jack-stripe committed Jan 21, 2016
2 parents 23394c3 + 07431e5 commit 4686d80
Show file tree
Hide file tree
Showing 11 changed files with 615 additions and 9 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ script:
- "./ci_scripts/check_fauxpas.sh"
- "./ci_scripts/check_version.rb"
- set -o pipefail && xcodebuild test -workspace Stripe.xcworkspace -scheme "StripeiOS Tests" -configuration Debug -sdk iphonesimulator | xcpretty -c
- set -o pipefail && xcodebuild test -workspace Stripe.xcworkspace -scheme "Stripe iOS Application Tests" -configuration Debug -sdk iphonesimulator -destination "OS=9.0,name=iPhone 6" | xcpretty -c
- set -o pipefail && xcodebuild test -workspace Stripe.xcworkspace -scheme "StripeOSX Tests" | xcpretty -c
- set -o pipefail && xcodebuild build -workspace Stripe.xcworkspace -scheme "Stripe iOS Example (Simple)" -sdk iphonesimulator | xcpretty -c
- set -o pipefail && xcodebuild build -workspace Stripe.xcworkspace -scheme "Stripe iOS Example (Custom)" -sdk iphonesimulator | xcpretty -c
Expand Down
24 changes: 24 additions & 0 deletions Example/Stripe iOS Application Tests/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
117 changes: 117 additions & 0 deletions Example/Stripe iOS Application Tests/STPPaymentCardTextFieldUITests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//
// STPPaymentCardTextFieldUITests.m
// Stripe iOS Example (Simple)
//
// Created by Ben Guo on 1/12/16.
// Copyright © 2016 Stripe. All rights reserved.
//

#import <XCTest/XCTest.h>
#import <Stripe/Stripe.h>

@interface STPPaymentCardTextField (Testing)
@property(nonatomic, readwrite, weak)UIImageView *brandImageView;
@property(nonatomic, readwrite, weak)UITextField *numberField;
@property(nonatomic, readwrite, weak)UITextField *expirationField;
@property(nonatomic, readwrite, weak)UITextField *cvcField;
@property(nonatomic, assign)BOOL numberFieldShrunk;
+ (UIImage *)cvcImageForCardBrand:(STPCardBrand)cardBrand;
+ (UIImage *)brandImageForCardBrand:(STPCardBrand)cardBrand;
@end

@interface STPPaymentCardTextFieldUITests : XCTestCase
@property (nonatomic, strong) STPPaymentCardTextField *sut;
@property (nonatomic, strong) UIViewController *viewController;
@end

@implementation STPPaymentCardTextFieldUITests

- (void)setUp {
[super setUp];
self.viewController = [UIViewController new];
self.sut = [STPPaymentCardTextField new];
[self.viewController.view addSubview:self.sut];
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
window.rootViewController = self.viewController;
}

- (void)testSetCard_allFields_whileEditingNumber {
XCTAssertTrue([self.sut.numberField becomeFirstResponder]);
STPCardParams *card = [STPCardParams new];
NSString *number = @"4242424242424242";
NSString *cvc = @"123";
card.number = number;
card.expMonth = 10;
card.expYear = 99;
card.cvc = cvc;
[self.sut setCardParams:card];
NSData *imgData = UIImagePNGRepresentation(self.sut.brandImageView.image);
NSData *expectedImgData = UIImagePNGRepresentation([STPPaymentCardTextField cvcImageForCardBrand:STPCardBrandVisa]);

XCTAssertTrue(self.sut.numberFieldShrunk);
XCTAssertTrue([expectedImgData isEqualToData:imgData]);
XCTAssertEqualObjects(self.sut.numberField.text, number);
XCTAssertEqualObjects(self.sut.expirationField.text, @"10/99");
XCTAssertEqualObjects(self.sut.cvcField.text, cvc);
XCTAssertTrue([self.sut.cvcField isFirstResponder]);
XCTAssertTrue(self.sut.isValid);
}

- (void)testSetCard_partialNumberAndExpiration_whileEditingExpiration {
XCTAssertTrue([self.sut.expirationField becomeFirstResponder]);
STPCardParams *card = [STPCardParams new];
NSString *number = @"42";
card.number = number;
card.expMonth = 10;
card.expYear = 99;
[self.sut setCardParams:card];
NSData *imgData = UIImagePNGRepresentation(self.sut.brandImageView.image);
NSData *expectedImgData = UIImagePNGRepresentation([STPPaymentCardTextField brandImageForCardBrand:STPCardBrandVisa]);

XCTAssertFalse(self.sut.numberFieldShrunk);
XCTAssertTrue([expectedImgData isEqualToData:imgData]);
XCTAssertEqualObjects(self.sut.numberField.text, number);
XCTAssertEqualObjects(self.sut.expirationField.text, @"10/99");
XCTAssertEqual(self.sut.cvcField.text.length, (NSUInteger)0);
XCTAssertTrue([self.sut.expirationField isFirstResponder]);
XCTAssertFalse(self.sut.isValid);
}

- (void)testSetCard_number_whileEditingCVC {
XCTAssertTrue([self.sut.cvcField becomeFirstResponder]);
STPCardParams *card = [STPCardParams new];
NSString *number = @"4242424242424242";
card.number = number;
[self.sut setCardParams:card];
NSData *imgData = UIImagePNGRepresentation(self.sut.brandImageView.image);
NSData *expectedImgData = UIImagePNGRepresentation([STPPaymentCardTextField brandImageForCardBrand:STPCardBrandVisa]);

XCTAssertTrue(self.sut.numberFieldShrunk);
XCTAssertTrue([expectedImgData isEqualToData:imgData]);
XCTAssertEqualObjects(self.sut.numberField.text, number);
XCTAssertEqual(self.sut.expirationField.text.length, (NSUInteger)0);
XCTAssertEqual(self.sut.cvcField.text.length, (NSUInteger)0);
XCTAssertTrue([self.sut.expirationField isFirstResponder]);
XCTAssertFalse(self.sut.isValid);
}

- (void)testSetCard_empty_whileEditingNumber {
XCTAssertTrue([self.sut.numberField becomeFirstResponder]);
self.sut.numberField.text = @"4242424242424242";
self.sut.cvcField.text = @"123";
self.sut.expirationField.text = @"10/99";
STPCardParams *card = [STPCardParams new];
[self.sut setCardParams:card];
NSData *imgData = UIImagePNGRepresentation(self.sut.brandImageView.image);
NSData *expectedImgData = UIImagePNGRepresentation([STPPaymentCardTextField brandImageForCardBrand:STPCardBrandUnknown]);

XCTAssertFalse(self.sut.numberFieldShrunk);
XCTAssertTrue([expectedImgData isEqualToData:imgData]);
XCTAssertEqual(self.sut.numberField.text.length, (NSUInteger)0);
XCTAssertEqual(self.sut.expirationField.text.length, (NSUInteger)0);
XCTAssertEqual(self.sut.cvcField.text.length, (NSUInteger)0);
XCTAssertTrue([self.sut.numberField isFirstResponder]);
XCTAssertFalse(self.sut.isValid);
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ - (void)save:(id)sender {
return;
}
[self.activityIndicator startAnimating];
[[STPAPIClient sharedClient] createTokenWithCard:self.paymentTextField.card
[[STPAPIClient sharedClient] createTokenWithCard:self.paymentTextField.cardParams
completion:^(STPToken *token, NSError *error) {
[self.activityIndicator stopAnimating];
if (error) {
Expand Down
126 changes: 125 additions & 1 deletion Example/Stripe iOS Example (Simple).xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@
042CA4221A685E8D00D778E7 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 042CA41C1A685E8D00D778E7 /* ViewController.swift */; };
04D0761D1A69E66F00094431 /* Stripe.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 04D076191A69C14700094431 /* Stripe.framework */; };
04D0761E1A69E66F00094431 /* Stripe.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 04D076191A69C14700094431 /* Stripe.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
C11745DF1C456D6D0029936F /* STPPaymentCardTextFieldUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = C11745DE1C456D6D0029936F /* STPPaymentCardTextFieldUITests.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
C11745D81C456C730029936F /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 04823F701A6849200098400B /* Project object */;
proxyType = 1;
remoteGlobalIDString = 04823F771A6849200098400B;
remoteInfo = "Stripe iOS Example (Simple)";
};
/* End PBXContainerItemProxy section */

/* Begin PBXCopyFilesBuildPhase section */
04D0761F1A69E66F00094431 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
Expand All @@ -38,6 +49,10 @@
04823F781A6849200098400B /* Stripe iOS Example (Simple).app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Stripe iOS Example (Simple).app"; sourceTree = BUILT_PRODUCTS_DIR; };
04D075D91A69B82B00094431 /* Stripe iOS Example (Simple).entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = "Stripe iOS Example (Simple).entitlements"; sourceTree = "<group>"; };
04D076191A69C14700094431 /* Stripe.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Stripe.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C11745D31C456C730029936F /* Stripe iOS Application Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Stripe iOS Application Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
C11745D71C456C730029936F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C11745DD1C456D6D0029936F /* Stripe iOS Application Tests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Stripe iOS Application Tests-Bridging-Header.h"; sourceTree = "<group>"; };
C11745DE1C456D6D0029936F /* STPPaymentCardTextFieldUITests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPPaymentCardTextFieldUITests.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -49,6 +64,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
C11745D01C456C730029936F /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
Expand All @@ -69,6 +91,7 @@
isa = PBXGroup;
children = (
042CA4131A685E8D00D778E7 /* Stripe iOS Example (Simple) */,
C11745D41C456C730029936F /* Stripe iOS Application Tests */,
04823F9F1A6849850098400B /* Frameworks */,
04823F791A6849200098400B /* Products */,
);
Expand All @@ -78,6 +101,7 @@
isa = PBXGroup;
children = (
04823F781A6849200098400B /* Stripe iOS Example (Simple).app */,
C11745D31C456C730029936F /* Stripe iOS Application Tests.xctest */,
);
name = Products;
sourceTree = "<group>";
Expand All @@ -90,6 +114,16 @@
name = Frameworks;
sourceTree = "<group>";
};
C11745D41C456C730029936F /* Stripe iOS Application Tests */ = {
isa = PBXGroup;
children = (
C11745DE1C456D6D0029936F /* STPPaymentCardTextFieldUITests.m */,
C11745D71C456C730029936F /* Info.plist */,
C11745DD1C456D6D0029936F /* Stripe iOS Application Tests-Bridging-Header.h */,
);
path = "Stripe iOS Application Tests";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand All @@ -111,13 +145,31 @@
productReference = 04823F781A6849200098400B /* Stripe iOS Example (Simple).app */;
productType = "com.apple.product-type.application";
};
C11745D21C456C730029936F /* Stripe iOS Application Tests */ = {
isa = PBXNativeTarget;
buildConfigurationList = C11745DC1C456C730029936F /* Build configuration list for PBXNativeTarget "Stripe iOS Application Tests" */;
buildPhases = (
C11745CF1C456C730029936F /* Sources */,
C11745D01C456C730029936F /* Frameworks */,
C11745D11C456C730029936F /* Resources */,
);
buildRules = (
);
dependencies = (
C11745D91C456C730029936F /* PBXTargetDependency */,
);
name = "Stripe iOS Application Tests";
productName = "Stripe iOS Application Tests";
productReference = C11745D31C456C730029936F /* Stripe iOS Application Tests.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
/* End PBXNativeTarget section */

/* Begin PBXProject section */
04823F701A6849200098400B /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0710;
ORGANIZATIONNAME = Stripe;
TargetAttributes = {
Expand All @@ -132,6 +184,10 @@
};
};
};
C11745D21C456C730029936F = {
CreatedOnToolsVersion = 7.2;
TestTargetID = 04823F771A6849200098400B;
};
};
};
buildConfigurationList = 04823F731A6849200098400B /* Build configuration list for PBXProject "Stripe iOS Example (Simple)" */;
Expand All @@ -148,6 +204,7 @@
projectRoot = "";
targets = (
04823F771A6849200098400B /* Stripe iOS Example (Simple) */,
C11745D21C456C730029936F /* Stripe iOS Application Tests */,
);
};
/* End PBXProject section */
Expand All @@ -162,6 +219,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
C11745D11C456C730029936F /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand All @@ -174,8 +238,24 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
C11745CF1C456C730029936F /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C11745DF1C456D6D0029936F /* STPPaymentCardTextFieldUITests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
C11745D91C456C730029936F /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 04823F771A6849200098400B /* Stripe iOS Example (Simple) */;
targetProxy = C11745D81C456C730029936F /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
042CA4171A685E8D00D778E7 /* Main.storyboard */ = {
isa = PBXVariantGroup;
Expand Down Expand Up @@ -317,6 +397,42 @@
};
name = Release;
};
C11745DA1C456C730029936F /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ENABLE_MODULES = YES;
DEBUG_INFORMATION_FORMAT = dwarf;
GCC_NO_COMMON_BLOCKS = YES;
INFOPLIST_FILE = "Stripe iOS Application Tests/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.stripe.Stripe-iOS-Application-Tests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Stripe iOS Application Tests/Stripe iOS Application Tests-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Stripe iOS Example (Simple).app/Stripe iOS Example (Simple)";
};
name = Debug;
};
C11745DB1C456C730029936F /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ENABLE_MODULES = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_NO_COMMON_BLOCKS = YES;
INFOPLIST_FILE = "Stripe iOS Application Tests/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.stripe.Stripe-iOS-Application-Tests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Stripe iOS Application Tests/Stripe iOS Application Tests-Bridging-Header.h";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Stripe iOS Example (Simple).app/Stripe iOS Example (Simple)";
};
name = Release;
};
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
Expand All @@ -338,6 +454,14 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
C11745DC1C456C730029936F /* Build configuration list for PBXNativeTarget "Stripe iOS Application Tests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
C11745DA1C456C730029936F /* Debug */,
C11745DB1C456C730029936F /* Release */,
);
defaultConfigurationIsVisible = 0;
};
/* End XCConfigurationList section */
};
rootObject = 04823F701A6849200098400B /* Project object */;
Expand Down
Loading

0 comments on commit 4686d80

Please sign in to comment.