Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't call completeTransactions callback if there aren't any transactions to process #150

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion SwiftyStoreKit/CompleteTransactionsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class CompleteTransactionsController: TransactionController {
unhandledTransactions.append(transaction)
}
}
completeTransactions.callback(products)
if products.count > 0 {
completeTransactions.callback(products)
}

return unhandledTransactions
}
Expand Down
49 changes: 45 additions & 4 deletions SwiftyStoreKitTests/CompleteTransactionsControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class CompleteTransactionsControllerTests: XCTestCase {
let transaction = TestPaymentTransaction(payment: SKPayment(product: testProduct), transactionState: .restored)

var callbackCalled = false
let restorePurchases = CompleteTransactions(atomically: true) { products in
let completeTransactions = CompleteTransactions(atomically: true) { products in
callbackCalled = true
XCTAssertEqual(products.count, 1)
let product = products.first!
XCTAssertEqual(product.productId, productIdentifier)
}

let completeTransactionsController = makeCompleteTransactionsController(completeTransactions: restorePurchases)
let completeTransactionsController = makeCompleteTransactionsController(completeTransactions: completeTransactions)

let spy = PaymentQueueSpy()

Expand All @@ -67,7 +67,7 @@ class CompleteTransactionsControllerTests: XCTestCase {
]

var callbackCalled = false
let restorePurchases = CompleteTransactions(atomically: true) { products in
let completeTransactions = CompleteTransactions(atomically: true) { products in
callbackCalled = true
XCTAssertEqual(products.count, 4)

Expand All @@ -76,7 +76,7 @@ class CompleteTransactionsControllerTests: XCTestCase {
}
}

let completeTransactionsController = makeCompleteTransactionsController(completeTransactions: restorePurchases)
let completeTransactionsController = makeCompleteTransactionsController(completeTransactions: completeTransactions)

let spy = PaymentQueueSpy()

Expand All @@ -89,6 +89,47 @@ class CompleteTransactionsControllerTests: XCTestCase {
XCTAssertEqual(spy.finishTransactionCalledCount, 4)
}

func testProcessTransactions_when_zeroTransactions_then_noFinishedTransactions_noCallback_noTransactionsRemaining() {

let transactions: [TestPaymentTransaction] = []

let completeTransactions = CompleteTransactions(atomically: true) { _ in
XCTFail("Callback should not be called")
}

let completeTransactionsController = makeCompleteTransactionsController(completeTransactions: completeTransactions)

let spy = PaymentQueueSpy()

let remainingTransactions = completeTransactionsController.processTransactions(transactions, on: spy)

XCTAssertEqual(remainingTransactions.count, 0)

XCTAssertEqual(spy.finishTransactionCalledCount, 0)
}

func testProcessTransactions_when_onePurchasingTransaction_then_noFinishedTransactions_noCallback_oneTransactionsRemaining() {

let productIdentifier = "com.SwiftyStoreKit.product1"
let testProduct = TestProduct(productIdentifier: productIdentifier)

let transaction = TestPaymentTransaction(payment: SKPayment(product: testProduct), transactionState: .purchasing)

let completeTransactions = CompleteTransactions(atomically: true) { _ in
XCTFail("Callback should not be called")
}

let completeTransactionsController = makeCompleteTransactionsController(completeTransactions: completeTransactions)

let spy = PaymentQueueSpy()

let remainingTransactions = completeTransactionsController.processTransactions([transaction], on: spy)

XCTAssertEqual(remainingTransactions.count, 1)

XCTAssertEqual(spy.finishTransactionCalledCount, 0)
}

func makeTestPaymentTransaction(productIdentifier: String, transactionState: SKPaymentTransactionState) -> TestPaymentTransaction {

let testProduct = TestProduct(productIdentifier: productIdentifier)
Expand Down