From 75d1224dcd907ce6b3f13822ed2556e053376d44 Mon Sep 17 00:00:00 2001 From: Haripriyan Date: Thu, 30 Nov 2023 18:21:58 +0530 Subject: [PATCH] Fixes unit tests --- .../flutter/sdk/ChargebeeFlutterSdkPlugin.kt | 13 +++- example/integration_test/chargebee_test.dart | 2 +- example/ios/Flutter/AppFrameworkInfo.plist | 2 +- example/ios/Podfile | 2 +- example/ios/Runner.xcodeproj/project.pbxproj | 22 ++++-- .../xcshareddata/xcschemes/Runner.xcscheme | 2 +- lib/src/chargebee.dart | 7 +- test/chargebee_test.dart | 74 +++++++++++-------- 8 files changed, 80 insertions(+), 44 deletions(-) diff --git a/android/src/main/kotlin/com/chargebee/flutter/sdk/ChargebeeFlutterSdkPlugin.kt b/android/src/main/kotlin/com/chargebee/flutter/sdk/ChargebeeFlutterSdkPlugin.kt index 0cf23be..648fa9b 100644 --- a/android/src/main/kotlin/com/chargebee/flutter/sdk/ChargebeeFlutterSdkPlugin.kt +++ b/android/src/main/kotlin/com/chargebee/flutter/sdk/ChargebeeFlutterSdkPlugin.kt @@ -542,7 +542,7 @@ private fun PricingPhase.toMap(product: CBProduct): Map { 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() @@ -557,15 +557,16 @@ fun defaultSubscriptionPeriod(): Map { } fun SubscriptionOffer.toMap(product: CBProduct): Map { + 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() ) } @@ -606,3 +607,7 @@ internal fun NonSubscription.toMap(): String { ) return Gson().toJson(resultMap) } + +fun PricingPhase.convertPriceAmountInMicros(): Double { + return amountInMicros / 1_000_000.0 +} \ No newline at end of file diff --git a/example/integration_test/chargebee_test.dart b/example/integration_test/chargebee_test.dart index 8e0efa6..d0b5073 100644 --- a/example/integration_test/chargebee_test.dart +++ b/example/integration_test/chargebee_test.dart @@ -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!'); diff --git a/example/ios/Flutter/AppFrameworkInfo.plist b/example/ios/Flutter/AppFrameworkInfo.plist index 8d4492f..9625e10 100644 --- a/example/ios/Flutter/AppFrameworkInfo.plist +++ b/example/ios/Flutter/AppFrameworkInfo.plist @@ -21,6 +21,6 @@ CFBundleVersion 1.0 MinimumOSVersion - 9.0 + 11.0 diff --git a/example/ios/Podfile b/example/ios/Podfile index 1e8c3c9..88359b2 100644 --- a/example/ios/Podfile +++ b/example/ios/Podfile @@ -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' diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 4a1ca7b..4a4553f 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 51; + objectVersion = 54; objects = { /* Begin PBXBuildFile section */ @@ -155,7 +155,7 @@ 97C146E61CF9000F007C117D /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 1300; + LastUpgradeCheck = 1430; ORGANIZATIONNAME = ""; TargetAttributes = { 97C146ED1CF9000F007C117D = { @@ -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 = ( @@ -213,6 +215,7 @@ }; 9740EEB61CF901F6004384FC /* Run Script */ = { isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; buildActionMask = 2147483647; files = ( ); @@ -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; @@ -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; @@ -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"; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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"; diff --git a/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index c87d15a..a6b826d 100644 --- a/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -1,6 +1,6 @@ {'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', @@ -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 ?? '', @@ -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, [ isMethodCall( Constants.mPurchaseProduct, - arguments: params, + arguments: paramsForAndroid, ) ]); expect(result.status, 'active'); @@ -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, [ 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, [ - isMethodCall( - Constants.mPurchaseProduct, - arguments: params, + arguments: paramsForiOS, ) ]); expect(result.status, 'active'); @@ -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, [ isMethodCall( Constants.mPurchaseProduct, - arguments: params, + arguments: paramsForiOS, ) ]); expect(result.status, 'active'); @@ -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', 'abc@gmail.com')); + final result = await Chargebee.purchaseProduct(androidProduct, customer: CBCustomer('abc_flutter_test', 'flutter', 'test', 'abc@gmail.com')); expect(callStack, [ 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', @@ -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', 'abc@gmail.com')); + final result = await Chargebee.purchaseProduct(iOSProduct, customer: CBCustomer('abc_flutter_test', 'flutter', 'test', 'abc@gmail.com')); expect(callStack, [ isMethodCall( Constants.mPurchaseProduct, arguments: { - Constants.product: product.id, + Constants.product: iOSProduct.id, Constants.customerId: 'abc_flutter_test', Constants.firstName: 'flutter', Constants.lastName: 'test', @@ -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()),); channel.setMockMethodCallHandler(null); }); @@ -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()),); channel.setMockMethodCallHandler(null); }); @@ -837,6 +847,9 @@ void main() { final map = {'unit': 'year', 'numberOfUnits': 1}; final product = Product( 'merchant.pro.android', + 'base.product', + 'offerId', + 'offerToken', 1500.00, '1500.00', 'title', @@ -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;