PaymentsControllerTests.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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") { _ 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 payment) = result {
  40. XCTAssertEqual(payment.productId, productIdentifier)
  41. XCTAssertEqual(payment.quantity, 1)
  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. } else {
  63. XCTFail("expected failed callback with error")
  64. }
  65. }
  66. let paymentsController = makePaymentsController(appendPayments: [payment])
  67. let transaction = TestPaymentTransaction(payment: SKPayment(product: testProduct), transactionState: .failed)
  68. let spy = PaymentQueueSpy()
  69. let remainingTransactions = paymentsController.processTransactions([transaction], on: spy)
  70. XCTAssertEqual(remainingTransactions.count, 0)
  71. XCTAssertFalse(paymentsController.hasPayment(payment))
  72. XCTAssertTrue(callbackCalled)
  73. XCTAssertEqual(spy.finishTransactionCalledCount, 1)
  74. }
  75. func testProcessTransaction_when_twoPaymentsSameId_firstTransactionStatePurchased_secondTransactionStateFailed_then_removesPayments_finishesTransactions_callsCallbacks() {
  76. let productIdentifier = "com.SwiftyStoreKit.product1"
  77. let testProduct1 = TestProduct(productIdentifier: productIdentifier)
  78. var callback1Called = false
  79. let payment1 = makeTestPayment(product: testProduct1) { result in
  80. callback1Called = true
  81. if case .purchased(let payment) = result {
  82. XCTAssertEqual(payment.productId, productIdentifier)
  83. } else {
  84. XCTFail("expected purchased callback with product id")
  85. }
  86. }
  87. let testProduct2 = TestProduct(productIdentifier: productIdentifier)
  88. var callback2Called = false
  89. let payment2 = makeTestPayment(product: testProduct2) { result in
  90. callback2Called = true
  91. if case .failed = result {
  92. } else {
  93. XCTFail("expected failed callback with error")
  94. }
  95. }
  96. let paymentsController = makePaymentsController(appendPayments: [payment1, payment2])
  97. let transaction1 = TestPaymentTransaction(payment: SKPayment(product: testProduct1), transactionState: .purchased)
  98. let transaction2 = TestPaymentTransaction(payment: SKPayment(product: testProduct2), transactionState: .failed)
  99. let spy = PaymentQueueSpy()
  100. let remainingTransactions = paymentsController.processTransactions([transaction1, transaction2], on: spy)
  101. XCTAssertEqual(remainingTransactions.count, 0)
  102. XCTAssertFalse(paymentsController.hasPayment(payment1))
  103. XCTAssertFalse(paymentsController.hasPayment(payment2))
  104. XCTAssertTrue(callback1Called)
  105. XCTAssertTrue(callback2Called)
  106. XCTAssertEqual(spy.finishTransactionCalledCount, 2)
  107. }
  108. func testProcessTransaction_when_twoPaymentsSameId_firstPayment_transactionStatePurchased_then_removesFirstPayment_finishesTransaction_callsCallback() {
  109. let productIdentifier = "com.SwiftyStoreKit.product1"
  110. let testProduct1 = TestProduct(productIdentifier: productIdentifier)
  111. var callback1Called = false
  112. let payment1 = makeTestPayment(product: testProduct1) { result in
  113. callback1Called = true
  114. if case .purchased(let payment) = result {
  115. XCTAssertEqual(payment.productId, productIdentifier)
  116. } else {
  117. XCTFail("expected purchased callback with product id")
  118. }
  119. }
  120. let testProduct2 = TestProduct(productIdentifier: productIdentifier)
  121. let payment2 = makeTestPayment(product: testProduct2) { _ in
  122. XCTFail("unexpected callback for second payment")
  123. }
  124. let paymentsController = makePaymentsController(appendPayments: [payment1, payment2])
  125. let transaction1 = TestPaymentTransaction(payment: SKPayment(product: testProduct1), transactionState: .purchased)
  126. let spy = PaymentQueueSpy()
  127. let remainingTransactions = paymentsController.processTransactions([transaction1], on: spy)
  128. XCTAssertEqual(remainingTransactions.count, 0)
  129. // First one removed, but second one with same identifier still there
  130. XCTAssertTrue(paymentsController.hasPayment(payment2))
  131. XCTAssertTrue(callback1Called)
  132. XCTAssertEqual(spy.finishTransactionCalledCount, 1)
  133. }
  134. func testProcessTransaction_when_onePayment_transactionStatePurchased_quantityIs2_then_removesPayment_finishesTransaction_callsCallback_correctQuantity() {
  135. let productIdentifier = "com.SwiftyStoreKit.product1"
  136. let quantity = 2
  137. let testProduct = TestProduct(productIdentifier: productIdentifier)
  138. var callbackCalled = false
  139. let payment = makeTestPayment(product: testProduct) { result in
  140. callbackCalled = true
  141. if case .purchased(let payment) = result {
  142. XCTAssertEqual(payment.productId, productIdentifier)
  143. XCTAssertEqual(payment.quantity, quantity)
  144. } else {
  145. XCTFail("expected purchased callback with product id")
  146. }
  147. }
  148. let paymentsController = makePaymentsController(appendPayments: [payment])
  149. let skPayment = SKMutablePayment(product: testProduct)
  150. skPayment.quantity = quantity
  151. let transaction = TestPaymentTransaction(payment: skPayment, transactionState: .purchased)
  152. let spy = PaymentQueueSpy()
  153. let remainingTransactions = paymentsController.processTransactions([transaction], on: spy)
  154. XCTAssertEqual(remainingTransactions.count, 0)
  155. XCTAssertFalse(paymentsController.hasPayment(payment))
  156. XCTAssertTrue(callbackCalled)
  157. XCTAssertEqual(spy.finishTransactionCalledCount, 1)
  158. }
  159. func makePaymentsController(appendPayments payments: [Payment]) -> PaymentsController {
  160. let paymentsController = PaymentsController()
  161. payments.forEach { paymentsController.append($0) }
  162. return paymentsController
  163. }
  164. func makeTestPayment(product: SKProduct, atomically: Bool = true, callback: @escaping (TransactionResult) -> Void) -> Payment {
  165. return Payment(product: product, quantity: 1, atomically: atomically, applicationUsername: "", simulatesAskToBuyInSandbox: false, callback: callback)
  166. }
  167. func makeTestPayment(productIdentifier: String, atomically: Bool = true, callback: @escaping (TransactionResult) -> Void) -> Payment {
  168. let product = TestProduct(productIdentifier: productIdentifier)
  169. return makeTestPayment(product: product, atomically: atomically, callback: callback)
  170. }
  171. }