Skip to content
Permalink
Browse files

Refactor RegisterUserUsecase removing validation logics to entities

  • Loading branch information...
ronanrodrigo committed Mar 3, 2017
1 parent 4cc5d76 commit 537e61410c4f82fa49ac67f73373987df0981781
@@ -20,12 +20,19 @@
3449A33A1E304A8D005A6755 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3449A3391E304A8D005A6755 /* Assets.xcassets */; };
3449A33D1E304A8D005A6755 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3449A33B1E304A8D005A6755 /* LaunchScreen.storyboard */; };
3449A3581E304D36005A6755 /* RegisterUserUsecaseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3449A3571E304D36005A6755 /* RegisterUserUsecaseTests.swift */; };
346C63FC1E699E2B00F909FD /* ValidateEmailEntityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346C63FB1E699E2B00F909FD /* ValidateEmailEntityTests.swift */; };
346C63FE1E69A16700F909FD /* ValidateNameEntityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346C63FD1E69A16700F909FD /* ValidateNameEntityTests.swift */; };
346C64001E69A1DF00F909FD /* ValidatePasswordEntityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346C63FF1E69A1DF00F909FD /* ValidatePasswordEntityTests.swift */; };
3480D3F21E328D1C00EAF9F4 /* AuthenticationGatewayFirebase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3480D3F11E328D1C00EAF9F4 /* AuthenticationGatewayFirebase.swift */; };
349CB9891E361B3F00BC5751 /* AuthenticationErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 349CB9881E361B3F00BC5751 /* AuthenticationErrorTests.swift */; };
349CB98C1E361F0900BC5751 /* AuthenticationGatewayFirebaseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 349CB98B1E361F0900BC5751 /* AuthenticationGatewayFirebaseTests.swift */; };
34B1962E1E305AFE009587C8 /* RegisterUserUsecase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34B1962D1E305AFE009587C8 /* RegisterUserUsecase.swift */; };
34B196311E305B1B009587C8 /* RegisterUserPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34B196301E305B1B009587C8 /* RegisterUserPresenter.swift */; };
34B196351E305B43009587C8 /* RegisterUserPresenterStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34B196341E305B43009587C8 /* RegisterUserPresenterStub.swift */; };
34FB40E31E690AD0006B72C0 /* ValidateEmailEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34FB40E21E690AD0006B72C0 /* ValidateEmailEntity.swift */; };
34FB40E51E690AE3006B72C0 /* ValidateNameEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34FB40E41E690AE3006B72C0 /* ValidateNameEntity.swift */; };
34FB40E71E690AF8006B72C0 /* ValidatePasswordEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34FB40E61E690AF8006B72C0 /* ValidatePasswordEntity.swift */; };
34FB40E91E691CA2006B72C0 /* AuthenticationErrorValidator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34FB40E81E691CA2006B72C0 /* AuthenticationErrorValidator.swift */; };
96E2C5D8FFC02FA20ADA4B6C /* Pods_Birthtalk.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0FE151884EDA031A2DB74921 /* Pods_Birthtalk.framework */; };
/* End PBXBuildFile section */

@@ -58,12 +65,19 @@
3449A3431E304A8D005A6755 /* BirthtalkTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = BirthtalkTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3449A3491E304A8E005A6755 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3449A3571E304D36005A6755 /* RegisterUserUsecaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RegisterUserUsecaseTests.swift; sourceTree = "<group>"; };
346C63FB1E699E2B00F909FD /* ValidateEmailEntityTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidateEmailEntityTests.swift; sourceTree = "<group>"; };
346C63FD1E69A16700F909FD /* ValidateNameEntityTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidateNameEntityTests.swift; sourceTree = "<group>"; };
346C63FF1E69A1DF00F909FD /* ValidatePasswordEntityTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidatePasswordEntityTests.swift; sourceTree = "<group>"; };
3480D3F11E328D1C00EAF9F4 /* AuthenticationGatewayFirebase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthenticationGatewayFirebase.swift; sourceTree = "<group>"; };
349CB9881E361B3F00BC5751 /* AuthenticationErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthenticationErrorTests.swift; sourceTree = "<group>"; };
349CB98B1E361F0900BC5751 /* AuthenticationGatewayFirebaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthenticationGatewayFirebaseTests.swift; sourceTree = "<group>"; };
34B1962D1E305AFE009587C8 /* RegisterUserUsecase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = RegisterUserUsecase.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
34B196301E305B1B009587C8 /* RegisterUserPresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = RegisterUserPresenter.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
34B196341E305B43009587C8 /* RegisterUserPresenterStub.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = RegisterUserPresenterStub.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
34FB40E21E690AD0006B72C0 /* ValidateEmailEntity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidateEmailEntity.swift; sourceTree = "<group>"; };
34FB40E41E690AE3006B72C0 /* ValidateNameEntity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidateNameEntity.swift; sourceTree = "<group>"; };
34FB40E61E690AF8006B72C0 /* ValidatePasswordEntity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidatePasswordEntity.swift; sourceTree = "<group>"; };
34FB40E81E691CA2006B72C0 /* AuthenticationErrorValidator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthenticationErrorValidator.swift; sourceTree = "<group>"; };
406DD59A7D68DB86E4BB166A /* Pods-Birthtalk.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Birthtalk.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Birthtalk/Pods-Birthtalk.debug.xcconfig"; sourceTree = "<group>"; };
9666A1836DBBFFA28048E5FF /* Pods-Birthtalk-BirthtalkTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Birthtalk-BirthtalkTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Birthtalk-BirthtalkTests/Pods-Birthtalk-BirthtalkTests.debug.xcconfig"; sourceTree = "<group>"; };
C4CA944E658853B5F6DA602A /* Pods_Birthtalk_BirthtalkTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Birthtalk_BirthtalkTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -93,9 +107,9 @@
340687561E31782000B5B0E2 /* Commons */ = {
isa = PBXGroup;
children = (
343908351E63B6D00098A1FF /* DatabasePath.swift */,
343908331E63B52B0098A1FF /* Enviroment.swift */,
340687571E31782D00B5B0E2 /* Result.swift */,
343908351E63B6D00098A1FF /* DatabasePath.swift */,
);
path = Commons;
sourceTree = "<group>";
@@ -104,7 +118,11 @@
isa = PBXGroup;
children = (
340687541E31744500B5B0E2 /* AuthenticationError.swift */,
34FB40E81E691CA2006B72C0 /* AuthenticationErrorValidator.swift */,
341B09261E310EA6009DFC9A /* User.swift */,
34FB40E21E690AD0006B72C0 /* ValidateEmailEntity.swift */,
34FB40E41E690AE3006B72C0 /* ValidateNameEntity.swift */,
34FB40E61E690AF8006B72C0 /* ValidatePasswordEntity.swift */,
);
path = Entities;
sourceTree = "<group>";
@@ -208,6 +226,9 @@
isa = PBXGroup;
children = (
349CB9881E361B3F00BC5751 /* AuthenticationErrorTests.swift */,
346C63FB1E699E2B00F909FD /* ValidateEmailEntityTests.swift */,
346C63FD1E69A16700F909FD /* ValidateNameEntityTests.swift */,
346C63FF1E69A1DF00F909FD /* ValidatePasswordEntityTests.swift */,
);
path = Entities;
sourceTree = "<group>";
@@ -504,16 +525,20 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
34FB40E91E691CA2006B72C0 /* AuthenticationErrorValidator.swift in Sources */,
3449A3331E304A8D005A6755 /* AppDelegate.swift in Sources */,
340687551E31744500B5B0E2 /* AuthenticationError.swift in Sources */,
3480D3F21E328D1C00EAF9F4 /* AuthenticationGatewayFirebase.swift in Sources */,
341B09271E310EA6009DFC9A /* User.swift in Sources */,
34B196311E305B1B009587C8 /* RegisterUserPresenter.swift in Sources */,
34FB40E71E690AF8006B72C0 /* ValidatePasswordEntity.swift in Sources */,
343908361E63B6D00098A1FF /* DatabasePath.swift in Sources */,
34FB40E31E690AD0006B72C0 /* ValidateEmailEntity.swift in Sources */,
343908341E63B52B0098A1FF /* Enviroment.swift in Sources */,
340687581E31782D00B5B0E2 /* Result.swift in Sources */,
341B092E1E311214009DFC9A /* AuthenticationGateway.swift in Sources */,
34B1962E1E305AFE009587C8 /* RegisterUserUsecase.swift in Sources */,
34FB40E51E690AE3006B72C0 /* ValidateNameEntity.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -524,7 +549,10 @@
34B196351E305B43009587C8 /* RegisterUserPresenterStub.swift in Sources */,
3449A3581E304D36005A6755 /* RegisterUserUsecaseTests.swift in Sources */,
349CB9891E361B3F00BC5751 /* AuthenticationErrorTests.swift in Sources */,
346C64001E69A1DF00F909FD /* ValidatePasswordEntityTests.swift in Sources */,
346C63FC1E699E2B00F909FD /* ValidateEmailEntityTests.swift in Sources */,
341B09311E3113E3009DFC9A /* AuthenticationGatewayStub.swift in Sources */,
346C63FE1E69A16700F909FD /* ValidateNameEntityTests.swift in Sources */,
349CB98C1E361F0900BC5751 /* AuthenticationGatewayFirebaseTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -0,0 +1,6 @@
import Foundation

protocol AuthenticationErrorValidator {
var error: AuthenticationError { get }
func isValid() -> Bool
}
@@ -0,0 +1,21 @@
import Foundation

struct ValidateEmailEntity: AuthenticationErrorValidator {

var error = AuthenticationError.invalidEmail
private let email: String

init(email: String) {
self.email = email
}

func isValid() -> Bool {
if email.isEmpty { return false }

let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegEx)

return emailTest.evaluate(with: email)
}

}
@@ -0,0 +1,17 @@
import Foundation

struct ValidateNameEntity: AuthenticationErrorValidator {

var error = AuthenticationError.invalidName
private let name: String

init(name: String) {
self.name = name
}

func isValid() -> Bool {
if name.isEmpty { return false }
return true
}

}
@@ -0,0 +1,17 @@
import Foundation

struct ValidatePasswordEntity: AuthenticationErrorValidator {

var error = AuthenticationError.invalidPassword
private let password: String

init(password: String) {
self.password = password
}

func isValid() -> Bool {
if password.characters.count < 5 { return false }
return true
}

}
@@ -11,36 +11,25 @@ struct RegisterUserUsecase {
}

func register(name: String, email: String, password: String, birthdate: Date) {
guard isValid(name: name, email: email, password: password) else { return }
gateway.register(name: name, email: email, password: password, birthdate: birthdate, completion: presentResult)
let invalidInputs: [AuthenticationErrorValidator] = [
ValidateEmailEntity(email: email),
ValidateNameEntity(name: name),
ValidatePasswordEntity(password: password)]
.filter { !$0.isValid() }

invalidInputs.forEach { presenter.failure(error: $0.error) }

let isWithoutErrors = invalidInputs.count == 0
if isWithoutErrors {
gateway.register(name: name, email: email, password: password, birthdate: birthdate, completion: presentResult)
}
}

private func presentResult(result: Result<UserEntity, AuthenticationError>) {
private func presentResult(result: RegisterResult) {
switch result {
case .success: self.presenter.success()
case let .failure(error): self.presenter.failure(error: error)
}
}

private func isValid(name: String, email: String, password: String) -> Bool {
var errors = [AuthenticationError]()
if name.isEmpty { errors.append(.invalidName) }
if password.characters.count < 5 { errors.append(.invalidPassword) }
if !isValid(email: email) { errors.append(.invalidEmail) }

errors.forEach(presenter.failure)

let isWhithoutErrors = errors.count == 0
return isWhithoutErrors
}

private func isValid(email: String) -> Bool {
if email.isEmpty { return false }

let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegEx)

return emailTest.evaluate(with: email)
}

}
@@ -0,0 +1,54 @@
import XCTest
@testable import Birthtalk

class ValidateEmailEntityTests: XCTestCase {

func testIsValidIsFalseWhenEmailIsEmpty() {
let isValid = ValidateEmailEntity(email: "").isValid()

XCTAssertFalse(isValid)
}

func testIsValidIsFalseWhenEmailIsWithoutDomain() {
let isValid = ValidateEmailEntity(email: "invalid@gmail").isValid()

XCTAssertFalse(isValid)
}

func testIsValidIsFalseWhenEmailIsWithInvalidCharacter() {
let isValid = ValidateEmailEntity(email: "invalid@gmail,com").isValid()

XCTAssertFalse(isValid)
}

func testIsValidIsFalseWhenEmailIsWithouthProvider() {
let isValid = ValidateEmailEntity(email: "invalid@.com").isValid()

XCTAssertFalse(isValid)
}

func testIsValidIsFalseWhenEmailIsIncomplete() {
let isValid = ValidateEmailEntity(email: "invalid@").isValid()

XCTAssertFalse(isValid)
}

func testIsValidIsFalseWhenEmailHasOnlyTheUsername() {
let isValid = ValidateEmailEntity(email: "invalid").isValid()

XCTAssertFalse(isValid)
}

func testIsValidIsFalseWhenEmailHas💩() {
let isValid = ValidateEmailEntity(email: "invalid💩@gmail.com").isValid()

XCTAssertFalse(isValid)
}

func testIsValidIsTrueWhenEmailIsComplete() {
let isValid = ValidateEmailEntity(email: "valid@gmail.com").isValid()

XCTAssertTrue(isValid)
}

}
@@ -0,0 +1,18 @@
import XCTest
@testable import Birthtalk

class ValidateNameEntityTests: XCTestCase {

func testIsValidIsFalseWhenNameIsBlank() {
let isValid = ValidateNameEntity(name: "").isValid()

XCTAssertFalse(isValid)
}

func testIsValidIsTrueWhenNameIsNotBlank() {
let isValid = ValidateNameEntity(name: "name").isValid()

XCTAssertTrue(isValid)
}

}
@@ -0,0 +1,30 @@
import XCTest
@testable import Birthtalk

class ValidatePasswordEntityTests: XCTestCase {

func testIsValidIsFalseWhenPasswordIsBlank() {
let isValid = ValidatePasswordEntity(password: "").isValid()

XCTAssertFalse(isValid)
}

func testIsValidIsFalseWhenPasswordIsLess5Char() {
let isValid = ValidatePasswordEntity(password: "1234").isValid()

XCTAssertFalse(isValid)
}

func testIsValidIsTrueWhenPasswordIsGreatherThan5Char() {
let isValid = ValidatePasswordEntity(password: "123456").isValid()

XCTAssertTrue(isValid)
}

func testIsValidIsTrueWhenPasswordIsEqual5Char() {
let isValid = ValidatePasswordEntity(password: "12345").isValid()

XCTAssertTrue(isValid)
}

}

0 comments on commit 537e614

Please sign in to comment.
You can’t perform that action at this time.