Skip to content

Commit

Permalink
Refactor MockEventLoggingDelegate to promoto immutability
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzkocer committed Mar 27, 2020
1 parent 8723261 commit c3debf1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
60 changes: 34 additions & 26 deletions Automattic-Tracks-iOSTests/Test Helpers/Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ enum MockError: Error {
}

class MockEventLoggingDataSource: EventLoggingDataSource {

private let _loggingEncryptionKey: String
private let _logUploadURL: URL

required init (loggingEncryptionKey: String, logUploadURL: URL) {
_loggingEncryptionKey = loggingEncryptionKey
_logUploadURL = logUploadURL
}

var loggingEncryptionKey: String {
return _loggingEncryptionKey
}

var logUploadURL: URL {
return _logUploadURL
}
Expand All @@ -40,46 +40,54 @@ class MockEventLoggingDataSource: EventLoggingDataSource {

class MockEventLoggingDelegate: EventLoggingDelegate {

var didStartUploadingTriggered = false
var shouldUploadLogFiles: Bool = true

private var _didStartUploadingTriggered = false
private var _didFinishUploadingTriggered = false
private var _uploadFailedTriggered = false
private var _uploadCancelledByDelegateTriggered = false

var didStartUploadingTriggered: Bool {
return _didStartUploadingTriggered
}

var didFinishUploadingTriggered: Bool {
return _didFinishUploadingTriggered
}

var uploadFailedTriggered: Bool {
return _uploadFailedTriggered
}

var uploadCancelledByDelegateTriggered: Bool {
return _uploadCancelledByDelegateTriggered
}

var didStartUploadingCallback: LogFileCallback?
var didFinishUploadingCallback: LogFileCallback?
var uploadCancelledByDelegateCallback: LogFileCallback?
var uploadFailedCallback: ErrorWithLogFileCallback?

func didStartUploadingLog(_ log: LogFile) {
didStartUploadingTriggered = true
_didStartUploadingTriggered = true
didStartUploadingCallback?(log)
}

var didFinishUploadingTriggered = false
var didFinishUploadingCallback: LogFileCallback?

func didFinishUploadingLog(_ log: LogFile) {
didFinishUploadingTriggered = true
_didFinishUploadingTriggered = true
didFinishUploadingCallback?(log)
}

var uploadCancelledByDelegateTriggered = false
var uploadCancelledByDelegateCallback: LogFileCallback?

func uploadCancelledByDelegate(_ log: LogFile) {
uploadCancelledByDelegateTriggered = true
_uploadCancelledByDelegateTriggered = true
uploadCancelledByDelegateCallback?(log)
}

var uploadFailedTriggered = false
var uploadFailedCallback: ErrorWithLogFileCallback?

func uploadFailed(withError error: Error, forLog log: LogFile) {
uploadFailedTriggered = true
_uploadFailedTriggered = true
uploadFailedCallback?(error, log)
}

func setShouldUploadLogFiles(_ newValue: Bool) {
_shouldUploadLogFiles = newValue
}

private var _shouldUploadLogFiles: Bool = true
var shouldUploadLogFiles: Bool {
return _shouldUploadLogFiles
}
}

class MockEventLoggingNetworkService: EventLoggingNetworkService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class EventLoggingUploadManagerTests: XCTestCase {
func testThatNetworkStartDoesNotFireWhenDelegateCancelsUpload() {

waitForExpectation(timeout: 1.0) { exp in
delegate.setShouldUploadLogFiles(false)
delegate.shouldUploadLogFiles = false
delegate.uploadCancelledByDelegateCallback = { _ in exp.fulfill() }
uploadManager.upload(LogFile.containingRandomString(), then: { _ in XCTFail("Callback should not be called") })
}
Expand All @@ -68,7 +68,7 @@ class EventLoggingUploadManagerTests: XCTestCase {

func testThatDelegateIsNotifiedOfMissingFiles() {
waitForExpectation(timeout: 1.0) { exp in
delegate.setShouldUploadLogFiles(true)
delegate.shouldUploadLogFiles = true
delegate.uploadFailedCallback = { error, _ in exp.fulfill() }
uploadManager.upload(LogFile.withInvalidPath(), then: { _ in XCTFail("Callback should not be called") })
}
Expand Down

0 comments on commit c3debf1

Please sign in to comment.