Skip to content

Commit 988d17a

Browse files
author
Christian Elies
committed
test(): implemented photo kit service tests
1 parent 9dbc557 commit 988d17a

File tree

6 files changed

+149
-5
lines changed

6 files changed

+149
-5
lines changed

Sources/RemoteImage/private/Services/PhotoKitService.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ protocol PhotoKitServiceProtocol {
99
_ completion: @escaping (Result<Data, Error>) -> Void)
1010
}
1111

12-
final class PhotoKitService {}
12+
final class PhotoKitService {
13+
static var asset: PHAsset.Type = PHAsset.self
14+
static var imageManager: PHImageManager = PHImageManager.default()
15+
}
1316

1417
extension PhotoKitService: PhotoKitServiceProtocol {
1518
func getPhotoData(localIdentifier: String,
1619
_ completion: @escaping (Result<Data, Error>) -> Void) {
17-
let fetchAssetsResult = PHAsset.fetchAssets(withLocalIdentifiers: [localIdentifier], options: nil)
20+
let fetchAssetsResult = Self.asset.fetchAssets(withLocalIdentifiers: [localIdentifier], options: nil)
1821
guard let phAsset = fetchAssetsResult.firstObject else {
1922
completion(.failure(PhotoKitServiceError.phAssetNotFound(localIdentifier: localIdentifier)))
2023
return
2124
}
2225

2326
let options = PHImageRequestOptions()
2427
options.isNetworkAccessAllowed = true
25-
PHImageManager.default().requestImageDataAndOrientation(for: phAsset,
26-
options: options,
27-
resultHandler: { data, _, _, info in
28+
Self.imageManager.requestImageDataAndOrientation(for: phAsset,
29+
options: options,
30+
resultHandler: { data, _, _, info in
2831
if let error = info?[PHImageErrorKey] as? Error {
2932
completion(.failure(error))
3033
} else if let data = data {

Sources/RemoteImage/public/Models/PhotoKitServiceError.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ public enum PhotoKitServiceError: Error {
55
case phAssetNotFound(localIdentifier: String)
66
}
77

8+
extension PhotoKitServiceError: Equatable {}
9+
810
extension PhotoKitServiceError: LocalizedError {
911
public var errorDescription: String? {
1012
switch self {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// MockImageManager.swift
3+
// RemoteImageTests
4+
//
5+
// Created by Christian Elies on 14.12.19.
6+
//
7+
8+
import Photos
9+
10+
final class MockImageManager: PHImageManager {
11+
var imageRequestID = PHImageRequestID()
12+
var dataToReturn: Data?
13+
var infoToReturn: [AnyHashable:Any]?
14+
15+
override func requestImageDataAndOrientation(for asset: PHAsset, options: PHImageRequestOptions?, resultHandler: @escaping (Data?, String?, CGImagePropertyOrientation, [AnyHashable : Any]?) -> Void) -> PHImageRequestID {
16+
resultHandler(dataToReturn, nil, .up, infoToReturn)
17+
return imageRequestID
18+
}
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// MockPHAsset.swift
3+
// RemoteImageTests
4+
//
5+
// Created by Christian Elies on 14.12.19.
6+
//
7+
8+
import Photos
9+
10+
final class MockPHAsset: PHAsset {
11+
static var fetchResult = MockPHAssetFetchResult()
12+
13+
override class func fetchAssets(withLocalIdentifiers identifiers: [String], options: PHFetchOptions?) -> PHFetchResult<PHAsset> {
14+
fetchResult
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// MockPHAssetFetchResult.swift
3+
// RemoteImageTests
4+
//
5+
// Created by Christian Elies on 14.12.19.
6+
//
7+
8+
import Photos
9+
10+
final class MockPHAssetFetchResult: PHFetchResult<PHAsset> {
11+
var firstObjectToReturn: PHAsset?
12+
13+
override var firstObject: PHAsset? { firstObjectToReturn }
14+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// PhotoKitServiceTests.swift
3+
// RemoteImageTests
4+
//
5+
// Created by Christian Elies on 15.12.19.
6+
//
7+
8+
@testable import RemoteImage
9+
import Photos
10+
import XCTest
11+
12+
final class PhotoKitServiceTests: XCTestCase {
13+
let imageManager = MockImageManager()
14+
let asset = MockPHAsset()
15+
let service = PhotoKitService()
16+
let localIdentifier = "TestIdentifier"
17+
18+
override func setUp() {
19+
PhotoKitService.asset = MockPHAsset.self
20+
PhotoKitService.imageManager = imageManager
21+
22+
MockPHAsset.fetchResult.firstObjectToReturn = nil
23+
imageManager.dataToReturn = nil
24+
imageManager.infoToReturn = nil
25+
}
26+
27+
func testPhotoDataNotFound() {
28+
let expectation = self.expectation(description: "PhotoDataResult")
29+
var result: Result<Data, Error>?
30+
service.getPhotoData(localIdentifier: localIdentifier) { res in
31+
result = res
32+
expectation.fulfill()
33+
}
34+
35+
waitForExpectations(timeout: 2)
36+
37+
switch result {
38+
case .failure(let error):
39+
guard case PhotoKitServiceError.phAssetNotFound(localIdentifier) = error else {
40+
XCTFail("Invalid error")
41+
return
42+
}
43+
default:
44+
XCTFail("Invalid photo data result")
45+
}
46+
}
47+
48+
func testPhotoDataFailure() {
49+
MockPHAsset.fetchResult.firstObjectToReturn = asset
50+
imageManager.infoToReturn = [PHImageErrorKey: PhotoKitServiceError.missingData]
51+
52+
let expectation = self.expectation(description: "PhotoDataResult")
53+
var result: Result<Data, Error>?
54+
service.getPhotoData(localIdentifier: localIdentifier) { res in
55+
result = res
56+
expectation.fulfill()
57+
}
58+
59+
waitForExpectations(timeout: 2)
60+
61+
switch result {
62+
case .failure(let error):
63+
XCTAssertEqual(error as? PhotoKitServiceError, .missingData)
64+
default:
65+
XCTFail("Invalid photo data result")
66+
}
67+
}
68+
69+
func testPhotoDataSuccess() {
70+
let expectedData = Data()
71+
MockPHAsset.fetchResult.firstObjectToReturn = asset
72+
imageManager.dataToReturn = expectedData
73+
74+
let expectation = self.expectation(description: "PhotoDataResult")
75+
var result: Result<Data, Error>?
76+
service.getPhotoData(localIdentifier: localIdentifier) { res in
77+
result = res
78+
expectation.fulfill()
79+
}
80+
81+
waitForExpectations(timeout: 2)
82+
83+
switch result {
84+
case .success(let data):
85+
XCTAssertEqual(data, expectedData)
86+
default:
87+
XCTFail("Invalid photo data result")
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)