|
| 1 | +// |
| 2 | +// MockDataService.swift |
| 3 | +// SwapiSwift |
| 4 | +// |
| 5 | +// Created by Joe Bramhall on 1/7/20. |
| 6 | +// Copyright © 2020 thinx. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import Combine |
| 11 | + |
| 12 | +/// A simple mock data service conforming to `SwapiService` initialized with a resource conforming to `SwapiResource`. |
| 13 | +/// Designed for use in testing and SwiftUI development. |
| 14 | +public struct MockDataService<T: SwapiResource>: SwapiService { |
| 15 | + |
| 16 | + let mockData: T |
| 17 | + |
| 18 | + /// Intializes a new instance of `MockDataService` with a specified resource. |
| 19 | + /// - Parameter mockData: The specified resource conforming to `SwapiResource` that will be returned by the mock data service. |
| 20 | + public init(mockData: T) { |
| 21 | + self.mockData = mockData |
| 22 | + } |
| 23 | + |
| 24 | + public func film(withId resourceId: String) -> AnyPublisher<Film, ServiceError> { |
| 25 | + guard let filmData = mockData as? Film else { |
| 26 | + fatalError("Expected mock data to be initialized with type Film") |
| 27 | + } |
| 28 | + return Result.Publisher(filmData).eraseToAnyPublisher() |
| 29 | + } |
| 30 | + |
| 31 | + public func person(withId resourceId: String) -> AnyPublisher<Person, ServiceError> { |
| 32 | + guard let personData = mockData as? Person else { |
| 33 | + fatalError("Expected mock data to be initialized with type Person") |
| 34 | + } |
| 35 | + return Result.Publisher(personData).eraseToAnyPublisher() |
| 36 | + } |
| 37 | + |
| 38 | + public func planet(withId resourceId: String) -> AnyPublisher<Planet, ServiceError> { |
| 39 | + guard let planetData = mockData as? Planet else { |
| 40 | + fatalError("Expected mock data to be initialized with type Planet") |
| 41 | + } |
| 42 | + return Result.Publisher(planetData).eraseToAnyPublisher() |
| 43 | + } |
| 44 | + |
| 45 | + public func planet(withResourceUrl url: String?) -> AnyPublisher<Planet, ServiceError> { |
| 46 | + guard let planetData = mockData as? Planet else { |
| 47 | + fatalError("Expected mock data to be initialized with type Planet") |
| 48 | + } |
| 49 | + return Result.Publisher(planetData).eraseToAnyPublisher() |
| 50 | + } |
| 51 | + |
| 52 | + public func species(withId resourceId: String) -> AnyPublisher<Species, ServiceError> { |
| 53 | + guard let speciesData = mockData as? Species else { |
| 54 | + fatalError("Expected mock data to be initialized with type Species") |
| 55 | + } |
| 56 | + return Result.Publisher(speciesData).eraseToAnyPublisher() |
| 57 | + } |
| 58 | + |
| 59 | + public func starship(withId resourceId: String) -> AnyPublisher<Starship, ServiceError> { |
| 60 | + guard let starshipData = mockData as? Starship else { |
| 61 | + fatalError("Expected mock data to be initialized with type Starship") |
| 62 | + } |
| 63 | + return Result.Publisher(starshipData).eraseToAnyPublisher() |
| 64 | + } |
| 65 | + |
| 66 | + public func vehicle(withId resourceId: String) -> AnyPublisher<Vehicle, ServiceError> { |
| 67 | + guard let vehicleData = mockData as? Vehicle else { |
| 68 | + fatalError("Expected mock data to be initialized with type Vehicle") |
| 69 | + } |
| 70 | + return Result.Publisher(vehicleData).eraseToAnyPublisher() |
| 71 | + } |
| 72 | + |
| 73 | + public func allFilms(page: String?) -> AnyPublisher<ResourceRoot<Film>, ServiceError> { |
| 74 | + guard let filmsData = mockData as? ResourceRoot<Film> else { |
| 75 | + fatalError("Expected mock data to be initialized with type FilmResourceRoot") |
| 76 | + } |
| 77 | + return Result.Publisher(filmsData).eraseToAnyPublisher() |
| 78 | + } |
| 79 | + |
| 80 | + public func allPeople(page: String?) -> AnyPublisher<ResourceRoot<Person>, ServiceError> { |
| 81 | + guard let peopleData = mockData as? ResourceRoot<Person> else { |
| 82 | + fatalError("Expected mock data to be initialzed with type PersonResourceRoot") |
| 83 | + } |
| 84 | + return Result.Publisher(peopleData).eraseToAnyPublisher() |
| 85 | + } |
| 86 | + |
| 87 | + public func allPlanets(page: String?) -> AnyPublisher<ResourceRoot<Planet>, ServiceError> { |
| 88 | + guard let planetsData = mockData as? ResourceRoot<Planet> else { |
| 89 | + fatalError("Expected mock data to be initialzed with type PlanetResourceRoot") |
| 90 | + } |
| 91 | + return Result.Publisher(planetsData).eraseToAnyPublisher() |
| 92 | + } |
| 93 | + |
| 94 | + public func allSpecies(page: String?) -> AnyPublisher<ResourceRoot<Species>, ServiceError> { |
| 95 | + guard let speciesData = mockData as? ResourceRoot<Species> else { |
| 96 | + fatalError("Expected mock data to be initialzed with type SpeciesResourceRoot") |
| 97 | + } |
| 98 | + return Result.Publisher(speciesData).eraseToAnyPublisher() |
| 99 | + } |
| 100 | + |
| 101 | + public func allStarships(page: String?) -> AnyPublisher<ResourceRoot<Starship>, ServiceError> { |
| 102 | + guard let starshipsData = mockData as? ResourceRoot<Starship> else { |
| 103 | + fatalError("Expected mock data to be initialzed with type StarshipResourceRoot") |
| 104 | + } |
| 105 | + return Result.Publisher(starshipsData).eraseToAnyPublisher() |
| 106 | + } |
| 107 | + |
| 108 | + public func allVehicles(page: String?) -> AnyPublisher<ResourceRoot<Vehicle>, ServiceError> { |
| 109 | + guard let vehiclesData = mockData as? ResourceRoot<Vehicle> else { |
| 110 | + fatalError("Expected mock data to be initialzed with type VehicleResourceRoot") |
| 111 | + } |
| 112 | + return Result.Publisher(vehiclesData).eraseToAnyPublisher() |
| 113 | + } |
| 114 | + |
| 115 | + public func people(fromResourceUrls urls: [String]) -> AnyPublisher<[Person], ServiceError> { |
| 116 | + guard let people = mockData as? [Person] else { |
| 117 | + fatalError("Expected mock data to be initialized with type Person array") |
| 118 | + } |
| 119 | + return Result.Publisher(people).eraseToAnyPublisher() |
| 120 | + } |
| 121 | + |
| 122 | + public func starships(fromResourceUrls urls: [String]) -> AnyPublisher<[Starship], ServiceError> { |
| 123 | + guard let starships = mockData as? [Starship] else { |
| 124 | + fatalError("Expected mock data to be initialized with type Starship array") |
| 125 | + } |
| 126 | + return Result.Publisher(starships).eraseToAnyPublisher() |
| 127 | + } |
| 128 | + |
| 129 | + public func planets(fromResourceUrls urls: [String]) -> AnyPublisher<[Planet], ServiceError> { |
| 130 | + guard let planets = mockData as? [Planet] else { |
| 131 | + fatalError("Expected mock data to be initialized with type Planet array") |
| 132 | + } |
| 133 | + return Result.Publisher(planets).eraseToAnyPublisher() |
| 134 | + } |
| 135 | + |
| 136 | + public func species(fromResourceUrls urls: [String]) -> AnyPublisher<[Species], ServiceError> { |
| 137 | + guard let species = mockData as? [Species] else { |
| 138 | + fatalError("Expected mock data to be initialized with type Species array") |
| 139 | + } |
| 140 | + return Result.Publisher(species).eraseToAnyPublisher() |
| 141 | + } |
| 142 | + |
| 143 | + public func vehicles(fromResourceUrls urls: [String]) -> AnyPublisher<[Vehicle], ServiceError> { |
| 144 | + guard let vehicles = mockData as? [Vehicle] else { |
| 145 | + fatalError("Expected mock data to be initialized with type Vehicle array") |
| 146 | + } |
| 147 | + return Result.Publisher(vehicles).eraseToAnyPublisher() |
| 148 | + } |
| 149 | + |
| 150 | + public func films(fromResourceUrls urls: [String]) -> AnyPublisher<[Film], ServiceError> { |
| 151 | + guard let films = mockData as? [Film] else { |
| 152 | + fatalError("Expected mock data to be initialized with type Film array") |
| 153 | + } |
| 154 | + return Result.Publisher(films).eraseToAnyPublisher() |
| 155 | + } |
| 156 | +} |
0 commit comments