From 2c85c965448a15f5abc0348f5ed1c21cb6a2495d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Do=CC=88nicke?= Date: Sat, 8 Oct 2022 21:20:20 +0200 Subject: [PATCH] Add demo use cases --- .../xcschemes/DomainLayer.xcscheme | 78 +++++++++++++++++++ .../DomainLayer/Sources/DomainLayer.swift | 6 -- .../DomainLayer/Sources/Entities/Todo.swift | 20 +++++ .../Repositories/TodoRepository.swift | 13 ++++ .../UseCases/CompleteTodoUseCase.swift | 20 +++++ .../Sources/UseCases/GetAllTodosUseCase.swift | 20 +++++ .../DomainLayer/Tests/DomainTests.swift | 11 --- .../Tests/Mocks/Entities/Todo+Mock.swift | 18 +++++ .../Repositories/MockTodayRepository.swift | 22 ++++++ .../UseCases/GetAllTodosUseCaseTests.swift | 24 ++++++ .../Tests/UseCases/GetTodayUseCaseTests.swift | 24 ++++++ 11 files changed, 239 insertions(+), 17 deletions(-) create mode 100644 CleanApp/Modules/DomainLayer/.swiftpm/xcode/xcshareddata/xcschemes/DomainLayer.xcscheme delete mode 100644 CleanApp/Modules/DomainLayer/Sources/DomainLayer.swift create mode 100644 CleanApp/Modules/DomainLayer/Sources/Entities/Todo.swift create mode 100644 CleanApp/Modules/DomainLayer/Sources/Protocols/Repositories/TodoRepository.swift create mode 100644 CleanApp/Modules/DomainLayer/Sources/UseCases/CompleteTodoUseCase.swift create mode 100644 CleanApp/Modules/DomainLayer/Sources/UseCases/GetAllTodosUseCase.swift delete mode 100644 CleanApp/Modules/DomainLayer/Tests/DomainTests.swift create mode 100644 CleanApp/Modules/DomainLayer/Tests/Mocks/Entities/Todo+Mock.swift create mode 100644 CleanApp/Modules/DomainLayer/Tests/Mocks/Repositories/MockTodayRepository.swift create mode 100644 CleanApp/Modules/DomainLayer/Tests/UseCases/GetAllTodosUseCaseTests.swift create mode 100644 CleanApp/Modules/DomainLayer/Tests/UseCases/GetTodayUseCaseTests.swift diff --git a/CleanApp/Modules/DomainLayer/.swiftpm/xcode/xcshareddata/xcschemes/DomainLayer.xcscheme b/CleanApp/Modules/DomainLayer/.swiftpm/xcode/xcshareddata/xcschemes/DomainLayer.xcscheme new file mode 100644 index 0000000..b572137 --- /dev/null +++ b/CleanApp/Modules/DomainLayer/.swiftpm/xcode/xcshareddata/xcschemes/DomainLayer.xcscheme @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/CleanApp/Modules/DomainLayer/Sources/DomainLayer.swift b/CleanApp/Modules/DomainLayer/Sources/DomainLayer.swift deleted file mode 100644 index 31fedcd..0000000 --- a/CleanApp/Modules/DomainLayer/Sources/DomainLayer.swift +++ /dev/null @@ -1,6 +0,0 @@ -public struct Domain { - public private(set) var text = "Hello, World!" - - public init() { - } -} diff --git a/CleanApp/Modules/DomainLayer/Sources/Entities/Todo.swift b/CleanApp/Modules/DomainLayer/Sources/Entities/Todo.swift new file mode 100644 index 0000000..2b7a831 --- /dev/null +++ b/CleanApp/Modules/DomainLayer/Sources/Entities/Todo.swift @@ -0,0 +1,20 @@ +// +// Todo.swift +// DomainLayer +// +// Created by Miguel Dönicke on 08.10.22. +// + +import Foundation + +public struct Todo: Equatable { + public var id: Int? + public var title: String + public var completed: Bool + + public init(id: Int? = nil, title: String, completed: Bool = false) { + self.id = id + self.title = title + self.completed = completed + } +} diff --git a/CleanApp/Modules/DomainLayer/Sources/Protocols/Repositories/TodoRepository.swift b/CleanApp/Modules/DomainLayer/Sources/Protocols/Repositories/TodoRepository.swift new file mode 100644 index 0000000..3175a30 --- /dev/null +++ b/CleanApp/Modules/DomainLayer/Sources/Protocols/Repositories/TodoRepository.swift @@ -0,0 +1,13 @@ +// +// TodoRepository.swift +// DomainLayer +// +// Created by Miguel Dönicke on 08.10.22. +// + +import Foundation + +public protocol TodoRepository { + func todos() async -> Result<[Todo], Error> + func complete(id: Int) async -> Result +} diff --git a/CleanApp/Modules/DomainLayer/Sources/UseCases/CompleteTodoUseCase.swift b/CleanApp/Modules/DomainLayer/Sources/UseCases/CompleteTodoUseCase.swift new file mode 100644 index 0000000..341a4f2 --- /dev/null +++ b/CleanApp/Modules/DomainLayer/Sources/UseCases/CompleteTodoUseCase.swift @@ -0,0 +1,20 @@ +// +// CompleteTodoUseCase.swift +// DomainLayer +// +// Created by Miguel Dönicke on 08.10.22. +// + +import Foundation + +public class CompleteTodoUseCase { + private let repository: TodoRepository + + public init(repository: TodoRepository) { + self.repository = repository + } + + public func execute(id: Int) async -> Result { + await repository.complete(id: id) + } +} diff --git a/CleanApp/Modules/DomainLayer/Sources/UseCases/GetAllTodosUseCase.swift b/CleanApp/Modules/DomainLayer/Sources/UseCases/GetAllTodosUseCase.swift new file mode 100644 index 0000000..87164ae --- /dev/null +++ b/CleanApp/Modules/DomainLayer/Sources/UseCases/GetAllTodosUseCase.swift @@ -0,0 +1,20 @@ +// +// GetAllTodosUseCase.swift +// DomainLayer +// +// Created by Miguel Dönicke on 08.10.22. +// + +import Foundation + +public class GetAllTodosUseCase { + private let repository: TodoRepository + + public init(repository: TodoRepository) { + self.repository = repository + } + + public func execute() async -> Result<[Todo], Error> { + await repository.todos() + } +} diff --git a/CleanApp/Modules/DomainLayer/Tests/DomainTests.swift b/CleanApp/Modules/DomainLayer/Tests/DomainTests.swift deleted file mode 100644 index 75c14f6..0000000 --- a/CleanApp/Modules/DomainLayer/Tests/DomainTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import DomainLayer - -final class DomainTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(Domain().text, "Hello, World!") - } -} diff --git a/CleanApp/Modules/DomainLayer/Tests/Mocks/Entities/Todo+Mock.swift b/CleanApp/Modules/DomainLayer/Tests/Mocks/Entities/Todo+Mock.swift new file mode 100644 index 0000000..03cf728 --- /dev/null +++ b/CleanApp/Modules/DomainLayer/Tests/Mocks/Entities/Todo+Mock.swift @@ -0,0 +1,18 @@ +// +// Todo+Mock.swift +// DomainLayerTests +// +// Created by Miguel Dönicke on 08.10.22. +// + +import DomainLayer +import Foundation + +private var todos = [Todo]() + +extension Todo { + static func mock() -> Todo { + let id = todos.count + 1 + return .init(id: id, title: "Mock #\(id)") + } +} diff --git a/CleanApp/Modules/DomainLayer/Tests/Mocks/Repositories/MockTodayRepository.swift b/CleanApp/Modules/DomainLayer/Tests/Mocks/Repositories/MockTodayRepository.swift new file mode 100644 index 0000000..7e35697 --- /dev/null +++ b/CleanApp/Modules/DomainLayer/Tests/Mocks/Repositories/MockTodayRepository.swift @@ -0,0 +1,22 @@ +// +// MockTodoRepository.swift +// DomainLayerTests +// +// Created by Miguel Dönicke on 08.10.22. +// + +import Foundation +import DomainLayer + +struct MockTodoRepository: TodoRepository { + var todosResult: Result<[Todo], Error>! + var completeResult: Result! + + func todos() async -> Result<[Todo], Error> { + todosResult + } + + func complete(id: Int) async -> Result { + completeResult + } +} diff --git a/CleanApp/Modules/DomainLayer/Tests/UseCases/GetAllTodosUseCaseTests.swift b/CleanApp/Modules/DomainLayer/Tests/UseCases/GetAllTodosUseCaseTests.swift new file mode 100644 index 0000000..6934a26 --- /dev/null +++ b/CleanApp/Modules/DomainLayer/Tests/UseCases/GetAllTodosUseCaseTests.swift @@ -0,0 +1,24 @@ +// +// GetAllTodosUseCaseTests.swift +// DomainLayerTests +// +// Created by Miguel Dönicke on 08.10.22. +// + +import DomainLayer +import XCTest + +final class GetAllTodosUseCaseTests: XCTestCase { + func testExecute() async throws { + // Arrange + let expected = [Todo.mock()] + let repository = MockTodoRepository(todosResult: .success(expected)) + let sut = GetAllTodosUseCase(repository: repository) + + // Act + let result = try await sut.execute().get() + + // Assert + XCTAssertEqual(result, expected) + } +} diff --git a/CleanApp/Modules/DomainLayer/Tests/UseCases/GetTodayUseCaseTests.swift b/CleanApp/Modules/DomainLayer/Tests/UseCases/GetTodayUseCaseTests.swift new file mode 100644 index 0000000..49d227c --- /dev/null +++ b/CleanApp/Modules/DomainLayer/Tests/UseCases/GetTodayUseCaseTests.swift @@ -0,0 +1,24 @@ +// +// CompleteTodoUseCaseTests.swift +// DomainLayerTests +// +// Created by Miguel Dönicke on 08.10.22. +// + +import DomainLayer +import XCTest + +final class CompleteTodoUseCaseTests: XCTestCase { + func testExecute() async throws { + // Arrange + let expected = Todo.mock() + let repository = MockTodoRepository(completeResult: .success(expected)) + let sut = CompleteTodoUseCase(repository: repository) + + // Act + let result = try await sut.execute(id: expected.id!).get() + + // Assert + XCTAssertEqual(result, expected) + } +}