PaymentsControllerTests.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // PaymentsControllerTests.swift
  3. // SwiftyStoreKit
  4. //
  5. // Copyright (c) 2017 Andrea Bizzotto (bizz84@gmail.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import XCTest
  25. import StoreKit
  26. @testable import SwiftyStoreKit
  27. class PaymentsControllerTests: XCTestCase {
  28. func testInsertPayment_hasPayment() {
  29. let payment = makeTestPayment(productIdentifier: "com.SwiftyStoreKit.product1") { result in }
  30. let paymentsController = makePaymentsController(appendPayments: [payment])
  31. XCTAssertTrue(paymentsController.hasPayment(payment))
  32. }
  33. func testProcessTransaction_when_onePayment_transactionStatePurchased_then_removesPayment_finishesTransaction_callsCallback() {
  34. let productIdentifier = "com.SwiftyStoreKit.product1"
  35. let testProduct = TestProduct(productIdentifier: productIdentifier)
  36. var callbackCalled = false
  37. let payment = makeTestPayment(product: testProduct) { result in
  38. callbackCalled = true
  39. if case .purchased(let product) = result {
  40. XCTAssertEqual(product.productId, productIdentifier)
  41. }
  42. else {
  43. XCTFail("expected purchased callback with product id")
  44. }
  45. }
  46. let paymentsController = makePaymentsController(appendPayments: [payment])
  47. let transaction = TestPaymentTransaction(payment: SKPayment(product: testProduct), transactionState: .purchased)
  48. let spy = PaymentQueueSpy()
  49. let remainingTransactions = paymentsController.processTransactions([transaction], on: spy)
  50. XCTAssertEqual(remainingTransactions.count, 0)
  51. XCTAssertFalse(paymentsController.hasPayment(payment))
  52. XCTAssertTrue(callbackCalled)
  53. XCTAssertEqual(spy.finishTransactionCalledCount, 1)
  54. }
  55. func testProcessTransaction_when_onePayment_transactionStateFailed_then_removesPayment_finishesTransaction_callsCallback() {
  56. let productIdentifier = "com.SwiftyStoreKit.product1"
  57. let testProduct = TestProduct(productIdentifier: productIdentifier)
  58. var callbackCalled = false
  59. let payment = makeTestPayment(product: testProduct) { result in
  60. callbackCalled = true
  61. if case .failed(_) = result {
  62. }
  63. else {
  64. XCTFail("expected failed callback with error")
  65. }
  66. }
  67. let paymentsController = makePaymentsController(appendPayments: [payment])
  68. let transaction = TestPaymentTransaction(payment: SKPayment(product: testProduct), transactionState: .failed)
  69. let spy = PaymentQueueSpy()
  70. let remainingTransactions = paymentsController.processTransactions([transaction], on: spy)
  71. XCTAssertEqual(remainingTransactions.count, 0)
  72. XCTAssertFalse(paymentsController.hasPayment(payment))
  73. XCTAssertTrue(callbackCalled)
  74. XCTAssertEqual(spy.finishTransactionCalledCount, 1)
  75. }
  76. func testProcessTransaction_when_twoPaymentsSameId_firstTransactionStatePurchased_secondTransactionStateFailed_then_removesPayments_finishesTransactions_callsCallbacks() {
  77. let productIdentifier = "com.SwiftyStoreKit.product1"
  78. let testProduct1 = TestProduct(productIdentifier: productIdentifier)
  79. var callback1Called = false
  80. let payment1 = makeTestPayment(product: testProduct1) { result in
  81. callback1Called = true
  82. if case .purchased(let product) = result {
  83. XCTAssertEqual(product.productId, productIdentifier)
  84. }
  85. else {
  86. XCTFail("expected purchased callback with product id")
  87. }
  88. }
  89. let testProduct2 = TestProduct(productIdentifier: productIdentifier)
  90. var callback2Called = false
  91. let payment2 = makeTestPayment(product: testProduct2) { result in
  92. callback2Called = true
  93. if case .failed(_) = result {
  94. }
  95. else {
  96. XCTFail("expected failed callback with error")
  97. }
  98. }
  99. let paymentsController = makePaymentsController(appendPayments: [payment1, payment2])
  100. let transaction1 = TestPaymentTransaction(payment: SKPayment(product: testProduct1), transactionState: .purchased)
  101. let transaction2 = TestPaymentTransaction(payment: SKPayment(product: testProduct2), transactionState: .failed)
  102. let spy = PaymentQueueSpy()
  103. let remainingTransactions = paymentsController.processTransactions([transaction1, transaction2], on: spy)
  104. XCTAssertEqual(remainingTransactions.count, 0)
  105. XCTAssertFalse(paymentsController.hasPayment(payment1))
  106. XCTAssertFalse(paymentsController.hasPayment(payment2))
  107. XCTAssertTrue(callback1Called)
  108. XCTAssertTrue(callback2Called)
  109. XCTAssertEqual(spy.finishTransactionCalledCount, 2)
  110. }
  111. func testProcessTransaction_when_twoPaymentsSameId_firstPayment_transactionStatePurchased_then_removesFirstPayment_finishesTransaction_callsCallback() {
  112. let productIdentifier = "com.SwiftyStoreKit.product1"
  113. let testProduct1 = TestProduct(productIdentifier: productIdentifier)
  114. var callback1Called = false
  115. let payment1 = makeTestPayment(product: testProduct1) { result in
  116. callback1Called = true
  117. if case .purchased(let product) = result {
  118. XCTAssertEqual(product.productId, productIdentifier)
  119. }
  120. else {
  121. XCTFail("expected purchased callback with product id")
  122. }
  123. }
  124. let testProduct2 = TestProduct(productIdentifier: productIdentifier)
  125. let payment2 = makeTestPayment(product: testProduct2) { result in
  126. XCTFail("unexpected callback for second payment")
  127. }
  128. let paymentsController = makePaymentsController(appendPayments: [payment1, payment2])
  129. let transaction1 = TestPaymentTransaction(payment: SKPayment(product: testProduct1), transactionState: .purchased)
  130. let spy = PaymentQueueSpy()
  131. let remainingTransactions = paymentsController.processTransactions([transaction1], on: spy)
  132. XCTAssertEqual(remainingTransactions.count, 0)
  133. // First one removed, but second one with same identifier still there
  134. XCTAssertTrue(paymentsController.hasPayment(payment2))
  135. XCTAssertTrue(callback1Called)
  136. XCTAssertEqual(spy.finishTransactionCalledCount, 1)
  137. }
  138. func makePaymentsController(appendPayments payments: [Payment]) -> PaymentsController {
  139. let paymentsController = PaymentsController()
  140. payments.forEach { paymentsController.append($0) }
  141. return paymentsController
  142. }
  143. func makeTestPayment(product: SKProduct, atomically: Bool = true, callback: @escaping (TransactionResult) -> ()) -> Payment {
  144. return Payment(product: product, atomically: atomically, applicationUsername: "", callback: callback)
  145. }
  146. func makeTestPayment(productIdentifier: String, atomically: Bool = true, callback: @escaping (TransactionResult) -> ()) -> Payment {
  147. let product = TestProduct(productIdentifier: productIdentifier)
  148. return makeTestPayment(product: product, atomically: atomically, callback: callback)
  149. }
  150. }