Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gravatar] Image upload using Gravatar SDK #22657

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions WordPress/Classes/Services/GravatarService.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import CocoaLumberjack
import WordPressKit
import Gravatar

@objc public enum GravatarServiceError: Int, Error {
case invalidAccountInfo
Expand Down Expand Up @@ -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 {
Expand All @@ -67,6 +68,10 @@ open class GravatarService {

/// Overridden by tests for mocking.
///
func gravatarImageService() -> ImageServing {
return ImageService()
}

func gravatarServiceRemote() -> GravatarServiceRemote {
return GravatarServiceRemote()
}
Expand Down
38 changes: 35 additions & 3 deletions WordPress/WordPressTest/GravatarServiceTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import XCTest
import WordPressKit
import Gravatar
@testable import WordPress

/// GravatarService Unit Tests
Expand All @@ -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!
}
}
Expand Down
Loading