Skip to content

Commit

Permalink
Fixes unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-haripriyan committed Nov 30, 2023
1 parent 4d2b67d commit 75d1224
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ private fun PricingPhase.toMap(product: CBProduct): Map<String, Any> {
return mapOf(
"productId" to product.id,
"productTitle" to product.title,
"productPrice" to amountInMicros,
"productPrice" to convertPriceAmountInMicros(),
"productPriceString" to formattedPrice,
"currencyCode" to currencyCode,
"subscriptionPeriod" to defaultSubscriptionPeriod()
Expand All @@ -557,15 +557,16 @@ fun defaultSubscriptionPeriod(): Map<String, Any> {
}

fun SubscriptionOffer.toMap(product: CBProduct): Map<String, Any> {
val pricingPhase = pricingPhases.first()
return mapOf(
"productId" to product.id,
"baseProductId" to basePlanId,
"offerId" to offerId.orEmpty(),
"offerToken" to offerToken,
"productTitle" to product.title,
"productPrice" to pricingPhases.first().amountInMicros,
"productPriceString" to pricingPhases.first().formattedPrice,
"currencyCode" to pricingPhases.first().currencyCode,
"productPrice" to pricingPhase.convertPriceAmountInMicros(),
"productPriceString" to pricingPhase.formattedPrice,
"currencyCode" to pricingPhase.currencyCode,
"subscriptionPeriod" to subscriptionPeriod()
)
}
Expand Down Expand Up @@ -606,3 +607,7 @@ internal fun NonSubscription.toMap(): String {
)
return Gson().toJson(resultMap)
}

fun PricingPhase.convertPriceAmountInMicros(): Double {
return amountInMicros / 1_000_000.0
}
2 changes: 1 addition & 1 deletion example/integration_test/chargebee_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ChargebeeTest {
}

try {
final result = await Chargebee.purchaseProduct(product, 'abc');
final result = await Chargebee.purchaseProduct(product, customer: customer);
debugPrint('purchase result: $result');
expect(result.status, 'true');
tester.printToConsole('Product subscribed successfully!');
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>9.0</string>
<string>11.0</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'
# platform :ios, '11.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
22 changes: 17 additions & 5 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 51;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -155,7 +155,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -199,10 +199,12 @@
/* Begin PBXShellScriptBuildPhase section */
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand All @@ -213,6 +215,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -339,7 +342,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand All @@ -354,6 +357,8 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = 5QPY897A4W;
ENABLE_BITCODE = NO;
Expand All @@ -365,6 +370,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.chargebee.sdk;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
Expand Down Expand Up @@ -418,7 +424,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -467,7 +473,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand All @@ -484,6 +490,8 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = 5QPY897A4W;
ENABLE_BITCODE = NO;
Expand All @@ -495,6 +503,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.chargebee.sdk;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
Expand All @@ -508,6 +517,8 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = 5QPY897A4W;
ENABLE_BITCODE = NO;
Expand All @@ -519,6 +530,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.chargebee.sdk;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
7 changes: 5 additions & 2 deletions lib/src/chargebee.dart
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,16 @@ class Chargebee {
Product product, {
CBCustomer? customer,
}) {
return {
Map params = {
Constants.product: product.id,
Constants.offerToken: product.offerToken,
Constants.customerId: customer?.id ?? '',
Constants.firstName: customer?.firstName ?? '',
Constants.lastName: customer?.lastName ?? '',
Constants.email: customer?.email ?? '',
};
if(product.offerToken != null) {
params[Constants.offerToken] = product.offerToken;
}
return params;
}
}
74 changes: 45 additions & 29 deletions test/chargebee_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,22 @@ void main() {

group('purchaseProduct', () {
final map = <String, dynamic>{'unit': 'year', 'numberOfUnits': 1};
final product = Product(
final androidProduct = Product(
'merchant.pro.android',
'base.product',
'offerId',
'offerToken',
1500.00,
'1500.00',
'title',
'INR',
SubscriptionPeriod.fromMap(map),
);
final iOSProduct = Product(
'merchant.pro.android',
null,
null,
null,
1500.00,
'1500.00',
'title',
Expand All @@ -185,8 +199,16 @@ void main() {
'',
'',
);
final params = {
Constants.product: product.id,
final paramsForAndroid = {
Constants.product: androidProduct.id,
Constants.offerToken: androidProduct.offerToken,
Constants.customerId: customer.id ?? '',
Constants.firstName: customer.firstName ?? '',
Constants.lastName: customer.lastName ?? '',
Constants.email: customer.email ?? '',
};
final paramsForiOS = {
Constants.product: iOSProduct.id,
Constants.customerId: customer.id ?? '',
Constants.firstName: customer.firstName ?? '',
Constants.lastName: customer.lastName ?? '',
Expand All @@ -195,11 +217,11 @@ void main() {
test('returns subscription result for Android', () async {
channelResponse = purchaseResult;
debugDefaultTargetPlatformOverride = TargetPlatform.android;
final result = await Chargebee.purchaseProduct(product, 'abc');
final result = await Chargebee.purchaseProduct(androidProduct, customer: customer);
expect(callStack, <Matcher>[
isMethodCall(
Constants.mPurchaseProduct,
arguments: params,
arguments: paramsForAndroid,
)
]);
expect(result.status, 'active');
Expand All @@ -208,24 +230,11 @@ void main() {
test('returns subscription result for iOS', () async {
channelResponse = purchaseResult;
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
final result = await Chargebee.purchaseProduct(product, 'abc');
final result = await Chargebee.purchaseProduct(iOSProduct, customer:customer);
expect(callStack, <Matcher>[
isMethodCall(
Constants.mPurchaseProduct,
arguments: params,
)
]);
expect(result.status, 'active');
});

test('subscribed with customer id for Android', () async {
channelResponse = purchaseResult;
debugDefaultTargetPlatformOverride = TargetPlatform.android;
final result = await Chargebee.purchaseProduct(product, 'abc');
expect(callStack, <Matcher>[
isMethodCall(
Constants.mPurchaseProduct,
arguments: params,
arguments: paramsForiOS,
)
]);
expect(result.status, 'active');
Expand All @@ -234,11 +243,11 @@ void main() {
test('subscribed with customer id for iOS', () async {
channelResponse = purchaseResult;
debugDefaultTargetPlatformOverride = TargetPlatform.android;
final result = await Chargebee.purchaseProduct(product, 'abc');
final result = await Chargebee.purchaseProduct(iOSProduct, customer: customer);
expect(callStack, <Matcher>[
isMethodCall(
Constants.mPurchaseProduct,
arguments: params,
arguments: paramsForiOS,
)
]);
expect(result.status, 'active');
Expand All @@ -247,12 +256,13 @@ void main() {
test('subscribed with customer info for Android', () async {
channelResponse = purchaseResult;
debugDefaultTargetPlatformOverride = TargetPlatform.android;
final result = await Chargebee.purchaseProduct(product, customer: CBCustomer('abc_flutter_test', 'flutter', 'test', '[email protected]'));
final result = await Chargebee.purchaseProduct(androidProduct, customer: CBCustomer('abc_flutter_test', 'flutter', 'test', '[email protected]'));
expect(callStack, <Matcher>[
isMethodCall(
Constants.mPurchaseProduct,
arguments: {
Constants.product: product.id,
Constants.product: androidProduct.id,
Constants.offerToken: androidProduct.offerToken,
Constants.customerId: 'abc_flutter_test',
Constants.firstName: 'flutter',
Constants.lastName: 'test',
Expand All @@ -266,12 +276,12 @@ void main() {
test('subscribed with customer info for iOS', () async {
channelResponse = purchaseResult;
debugDefaultTargetPlatformOverride = TargetPlatform.android;
final result = await Chargebee.purchaseProduct(product, customer: CBCustomer('abc_flutter_test', 'flutter', 'test', '[email protected]'));
final result = await Chargebee.purchaseProduct(iOSProduct, customer: CBCustomer('abc_flutter_test', 'flutter', 'test', '[email protected]'));
expect(callStack, <Matcher>[
isMethodCall(
Constants.mPurchaseProduct,
arguments: {
Constants.product: product.id,
Constants.product: iOSProduct.id,
Constants.customerId: 'abc_flutter_test',
Constants.firstName: 'flutter',
Constants.lastName: 'test',
Expand All @@ -288,7 +298,7 @@ void main() {
code: 'PlatformError', message: 'An error occured',);
});
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
await expectLater(() => Chargebee.purchaseProduct(product),
await expectLater(() => Chargebee.purchaseProduct(iOSProduct),
throwsA(isA<PlatformException>()),);
channel.setMockMethodCallHandler(null);
});
Expand All @@ -299,7 +309,7 @@ void main() {
code: 'PlatformError', message: 'An error occured',);
});
debugDefaultTargetPlatformOverride = TargetPlatform.android;
await expectLater(() => Chargebee.purchaseProduct(product),
await expectLater(() => Chargebee.purchaseProduct(androidProduct),
throwsA(isA<PlatformException>()),);
channel.setMockMethodCallHandler(null);
});
Expand Down Expand Up @@ -837,6 +847,9 @@ void main() {
final map = <String, dynamic>{'unit': 'year', 'numberOfUnits': 1};
final product = Product(
'merchant.pro.android',
'base.product',
'offerId',
'offerToken',
1500.00,
'1500.00',
'title',
Expand Down Expand Up @@ -1011,11 +1024,14 @@ void main() {
group('validateReceiptForNonSubscriptions', () {
final product = Product(
'merchant.pro.android',
'base.product',
'offerId',
'offerToken',
1500.00,
'1500.00',
'title',
'INR',
SubscriptionPeriod.fromMap({'periodUnit': 'month', 'numberOfUnits': 1}),
SubscriptionPeriod.fromMap({'periodUnit': 'month', 'numberOfUnits': 1}),
);
const consumableProductType = ProductType.consumable;
const nonConsumableProductType = ProductType.non_consumable;
Expand Down

0 comments on commit 75d1224

Please sign in to comment.