diff --git a/WordPress/Classes/Services/GravatarService.swift b/WordPress/Classes/Services/GravatarService.swift index 516100ac42c0..9aa050640bc1 100644 --- a/WordPress/Classes/Services/GravatarService.swift +++ b/WordPress/Classes/Services/GravatarService.swift @@ -1,6 +1,7 @@ import Foundation import CocoaLumberjack import WordPressKit +import Gravatar @objc public enum GravatarServiceError: Int, Error { case invalidAccountInfo @@ -53,8 +54,8 @@ open class GravatarService { let email = accountEmail.trimmingCharacters(in: CharacterSet.whitespaces).lowercased() - let remote = gravatarServiceRemote() - remote.uploadImage(image, accountEmail: email, accountToken: accountToken) { (error) in + let imageService = gravatarImageService() + imageService.uploadImage(image, accountEmail: email, accountToken: accountToken) { (error) in if let theError = error { DDLogError("GravatarService.uploadImage Error: \(theError)") } else { @@ -67,6 +68,10 @@ open class GravatarService { /// Overridden by tests for mocking. /// + func gravatarImageService() -> ImageServing { + return ImageService() + } + func gravatarServiceRemote() -> GravatarServiceRemote { return GravatarServiceRemote() } diff --git a/WordPress/WordPressTest/GravatarServiceTests.swift b/WordPress/WordPressTest/GravatarServiceTests.swift index 6a18d9e55ac7..1252525b1416 100644 --- a/WordPress/WordPressTest/GravatarServiceTests.swift +++ b/WordPress/WordPressTest/GravatarServiceTests.swift @@ -1,6 +1,7 @@ import Foundation import XCTest import WordPressKit +import Gravatar @testable import WordPress /// GravatarService Unit Tests @@ -18,14 +19,45 @@ class GravatarServiceTests: CoreDataTestCase { completion(nil) } } + } + + class ImageServiceMock: ImageServing { + var capturedAccountToken: String = "" + var capturedAccountEmail: String = "" + + func uploadImage(_ image: UIImage, accountEmail: String, accountToken: String) async throws -> URLResponse { + capturedAccountEmail = accountEmail + capturedAccountToken = accountToken + return URLResponse() + } + func uploadImage(_ image: UIImage, accountEmail: String, accountToken: String, completion: ((NSError?) -> Void)?) { + capturedAccountEmail = accountEmail + capturedAccountToken = accountToken + } + + func fetchImage(with email: String, options: Gravatar.GravatarImageDownloadOptions, completionHandler: Gravatar.ImageDownloadCompletion?) -> Gravatar.CancellableDataTask { + fatalError("Not implemented") + } + + func fetchImage(with url: URL, forceRefresh: Bool, processor: Gravatar.ImageProcessor, completionHandler: Gravatar.ImageDownloadCompletion?) -> Gravatar.CancellableDataTask? { + fatalError("Not implemented") + } + + func fetchImage(with email: String, options: Gravatar.GravatarImageDownloadOptions) async throws -> Gravatar.GravatarImageDownloadResult { + fatalError("Not implemented") + } + + func fetchImage(with url: URL, forceRefresh: Bool, processor: Gravatar.ImageProcessor) async throws -> Gravatar.GravatarImageDownloadResult { + fatalError("Not implemented") + } } class GravatarServiceTester: GravatarService { - var gravatarServiceRemoteMock: GravatarServiceRemoteMock? + var gravatarServiceRemoteMock: ImageServiceMock? - override func gravatarServiceRemote() -> GravatarServiceRemote { - gravatarServiceRemoteMock = GravatarServiceRemoteMock() + override func gravatarImageService() -> ImageServing { + gravatarServiceRemoteMock = ImageServiceMock() return gravatarServiceRemoteMock! } }