Skip to content

Commit 64af9c6

Browse files
author
Christian Elies
committed
fix(): added remove functions to remote image cache; test(): implemented remote image service tests
1 parent 988d17a commit 64af9c6

11 files changed

+325
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// URLSession+RemoteImageURLDataPublisher.swift
3+
// RemoteImage
4+
//
5+
// Created by Christian Elies on 15.12.19.
6+
//
7+
8+
import Combine
9+
import Foundation
10+
11+
extension URLSession: RemoteImageURLDataPublisher {
12+
func dataPublisher(for request: URLRequest) -> AnyPublisher<(data: Data, response: URLResponse), URLError> {
13+
dataTaskPublisher(for: request).eraseToAnyPublisher()
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// RemoteImageURLDataPublisher.swift
3+
// RemoteImage
4+
//
5+
// Created by Christian Elies on 15.12.19.
6+
//
7+
8+
import Combine
9+
import Foundation
10+
11+
protocol RemoteImageURLDataPublisher {
12+
func dataPublisher(for request: URLRequest) -> AnyPublisher<(data: Data, response: URLResponse), URLError>
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// RemoteImageURLDataPublisherProvider.swift
3+
// RemoteImage
4+
//
5+
// Created by Christian Elies on 15.12.19.
6+
//
7+
8+
protocol RemoteImageURLDataPublisherProvider {
9+
var remoteImageURLDataPublisher: RemoteImageURLDataPublisher { get }
10+
}

Sources/RemoteImage/private/Services/DefaultRemoteImageCache.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ extension DefaultRemoteImageCache: RemoteImageCache {
1515
func object(forKey key: AnyObject) -> PlatformSpecificImageType? { cache.object(forKey: key) }
1616

1717
func setObject(_ object: PlatformSpecificImageType, forKey key: AnyObject) { cache.setObject(object, forKey: key) }
18+
19+
func removeObject(forKey key: AnyObject) { cache.removeObject(forKey: key) }
20+
21+
func removeAllObjects() { cache.removeAllObjects() }
1822
}

Sources/RemoteImage/private/Services/RemoteImageServiceDependencies.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77

88
import Foundation
99

10-
protocol RemoteImageServiceDependenciesProtocol: PhotoKitServiceProvider {
10+
protocol RemoteImageServiceDependenciesProtocol: PhotoKitServiceProvider, RemoteImageURLDataPublisherProvider {
1111

1212
}
1313

1414
struct RemoteImageServiceDependencies: RemoteImageServiceDependenciesProtocol {
1515
let photoKitService: PhotoKitServiceProtocol
16+
let remoteImageURLDataPublisher: RemoteImageURLDataPublisher
1617

1718
init() {
1819
photoKitService = PhotoKitService()
20+
remoteImageURLDataPublisher = URLSession.shared
1921
}
2022
}

Sources/RemoteImage/public/Protocols/RemoteImageCache.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ import Foundation
1010
public protocol RemoteImageCache {
1111
func object(forKey key: AnyObject) -> PlatformSpecificImageType?
1212
func setObject(_ object: PlatformSpecificImageType, forKey key: AnyObject)
13+
func removeObject(forKey key: AnyObject)
14+
func removeAllObjects()
1315
}

Sources/RemoteImage/public/Services/RemoteImageService.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ extension RemoteImageService {
4949
return
5050
}
5151

52-
let urlSession = URLSession.shared
5352
let urlRequest = URLRequest(url: url)
5453

55-
cancellable = urlSession.dataTaskPublisher(for: urlRequest)
54+
cancellable = dependencies.remoteImageURLDataPublisher.dataPublisher(for: urlRequest)
5655
.map { PlatformSpecificImageType(data: $0.data) }
5756
.receive(on: RunLoop.main)
5857
.sink(receiveCompletion: { completion in
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// MockPhotoKitService.swift
3+
// RemoteImageTests
4+
//
5+
// Created by Christian Elies on 15.12.19.
6+
//
7+
8+
import Foundation
9+
@testable import RemoteImage
10+
11+
final class MockPhotoKitService: PhotoKitServiceProtocol {
12+
var resultToReturn: Result<Data, Error> = .success(Data())
13+
14+
func getPhotoData(localIdentifier: String, _ completion: @escaping (Result<Data, Error>) -> Void) {
15+
completion(resultToReturn)
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// MockRemoteImageServiceDependencies.swift
3+
// RemoteImageTests
4+
//
5+
// Created by Christian Elies on 15.12.19.
6+
//
7+
8+
@testable import RemoteImage
9+
10+
struct MockRemoteImageServiceDependencies: RemoteImageServiceDependenciesProtocol {
11+
let photoKitService: PhotoKitServiceProtocol
12+
let remoteImageURLDataPublisher: RemoteImageURLDataPublisher
13+
14+
init() {
15+
photoKitService = MockPhotoKitService()
16+
remoteImageURLDataPublisher = MockRemoteImageURLDataPublisher()
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// MockRemoteImageURLDataPublisher.swift
3+
// RemoteImageTests
4+
//
5+
// Created by Christian Elies on 15.12.19.
6+
//
7+
8+
import Combine
9+
import Foundation
10+
@testable import RemoteImage
11+
12+
final class MockRemoteImageURLDataPublisher: RemoteImageURLDataPublisher {
13+
var publisher = PassthroughSubject<(data: Data, response: URLResponse), URLError>()
14+
15+
func dataPublisher(for request: URLRequest) -> AnyPublisher<(data: Data, response: URLResponse), URLError> {
16+
publisher.eraseToAnyPublisher()
17+
}
18+
}

0 commit comments

Comments
 (0)