Files
purchases-ios/Tests/UnitTests/Mocks/MockFileReader.swift
T
NachoSoto b87dfe5b51 Added new ReceiptParser.fetchAndParseLocalReceipt (#2204)
Fixes [CSDK-622].

### Changes:
- Extracted `FileReader` to be able to use it in `ReceiptParser`
- Extracted `MockBundle` to be able to use it in `ReceiptParser`
- Added new `public` `ReceiptParser.fetchAndParseLocalReceipt()`, **only
to `ReceiptParser`** (this is not available through `RevenueCat`).
- Added tests for the new API
- Added new API to `ReceiptParserAPITester`

Initially I thought about moving `ReceiptFetcher` to the
`Receipt|Parser` target. Unfortunately that would require moving a whole
bunch of other dependencies (`OperationDispatcher`, etc, etc).
We have no easy way to create a third "Core" framework in which to place
these common dependencies, so that wouldn't be trivial.

This solution adds a much simpler version of `ReceiptFetcher` that
simply reads from `Bundle.appStoreReceiptURL`. As it's documented there,
under normal circumstances `SKReceiptRefreshRequest` is not required, so
this implementation should work for 99% of use cases, and so this way we
avoid increasing the.codebase complexity unnecessarily.

[CSDK-622]:
https://revenuecats.atlassian.net/browse/CSDK-622?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
2023-01-17 12:35:56 -08:00

48 lines
1.3 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// MockFileReader.swift
//
// Created by Nacho Soto on 1/10/23.
@testable import RevenueCat
final class MockFileReader: FileReader {
enum Error: Swift.Error {
case noMockedData
case emptyMockedData
}
var mockedURLContents: [URL: [Data?]] = [:]
func mock(url: URL, with data: Data) {
self.mockedURLContents[url] = [data]
}
var invokedContentsOfURL: [URL: Int] = [:]
func contents(of url: URL) throws -> Data {
let previouslyInvokedContentsOfURL = self.invokedContentsOfURL[url] ?? 0
self.invokedContentsOfURL[url, default: 0] += 1
guard let mockedData = self.mockedURLContents[url] else { throw Error.noMockedData }
if mockedData.isEmpty {
throw Error.emptyMockedData
} else if let data = mockedData.onlyElement {
return try data.orThrow(Error.noMockedData)
} else {
return try mockedData[previouslyInvokedContentsOfURL].orThrow(Error.noMockedData)
}
}
}