@@ -28,81 +28,95 @@ class MockFormbricksService: FormbricksService {
2828 }
2929 }
3030
31- func execute< T: Decodable > ( _ response: MockResponse ,
32- completion: @escaping ( ResultType < T > ) -> Void ) {
33- let name = response. rawValue
34- var data : Data ?
35-
36- // 1️⃣ Try Bundle.module
37- if let url = Bundle . module. url ( forResource: name, withExtension: " json " ) {
38- print ( " 🔍 Found \( name) .json in Bundle.module at \( url. path) " )
39- data = try ? Data ( contentsOf: url)
40- } else {
41- print ( " ⚠️ \( name) .json not found in Bundle.module " )
42- }
43-
44- // 2️⃣ Try test bundle root
45- if data == nil {
46- let testBundle = Bundle ( for: type ( of: self ) )
47- if let url = testBundle. url ( forResource: name, withExtension: " json " ) {
48- print ( " 🔍 Found \( name) .json in test bundle at \( url. path) " )
49- data = try ? Data ( contentsOf: url)
50- } else {
51- print ( " ⚠️ \( name) .json not found in test bundle root " )
52- }
53- }
54-
55- print ( " is data set now? " )
56- print ( data)
57-
58- // 3️⃣ Try “Mock/” subdirectory
59- if data == nil {
60- let testBundle = Bundle ( for: type ( of: self ) )
61- if let url = testBundle. url ( forResource: name, withExtension: " json " , subdirectory: " Mock " ) {
62- print ( " 🔍 Found \( name) .json in test bundle Mock/ at \( url. path) " )
63- data = try ? Data ( contentsOf: url)
64- } else {
65- print ( " ⚠️ \( name) .json not found in test bundle Mock/ subdirectory " )
66- }
67- }
68-
69- // 4️⃣ If still missing, log folder listings
70- guard let jsonData = data else {
71- print ( " ❌ \( name) .json not found in any bundle location. " )
72-
73- // List Bundle.module resources
74- if let resDir = Bundle . module. resourceURL? . path {
75- print ( " 📂 Contents of Bundle.module.resourceURL ( \( resDir) ): " )
76- if let files = try ? FileManager . default. contentsOfDirectory ( atPath: resDir) {
77- files. forEach { print ( " • \( $0) " ) }
78- }
79- }
80- // List test bundle resources
81- let testBundle = Bundle ( for: type ( of: self ) )
82- if let testResDir = testBundle. resourceURL? . path {
83- print ( " 📂 Contents of test bundle.resourceURL ( \( testResDir) ): " )
84- if let files = try ? FileManager . default. contentsOfDirectory ( atPath: testResDir) {
85- files. forEach { print ( " • \( $0) " ) }
86- }
87- }
88-
89- completion ( . failure( RuntimeError ( message: " Unable to find or parse mock response " ) ) )
90- return
91- }
92-
93- // 6️⃣ Decode as before
94- do {
95- let body = try JSONDecoder . iso8601Full. decode ( T . self, from: jsonData)
96- if ( name == " Environment " ) {
97- print ( " Setting environmentResponseObjectKey " )
98- UserDefaults . standard. set ( data, forKey: " environmentResponseObjectKey " )
99- UserDefaults . standard. synchronize ( )
100- }
101- completion ( . success( body) )
102- } catch {
103- print ( " ❌ JSON Decode Error for \( name) .json: \( error) " )
104- completion ( . failure( error) )
105- }
31+ // func execute<T: Decodable>(_ response: MockResponse,
32+ // completion: @escaping (ResultType<T>) -> Void) {
33+ // let name = response.rawValue
34+ // var data: Data?
35+ //
36+ // // 1️⃣ Try Bundle.module
37+ // if let url = Bundle.module.url(forResource: name, withExtension: "json") {
38+ // print("🔍 Found \(name).json in Bundle.module at \(url.path)")
39+ // data = try? Data(contentsOf: url)
40+ // } else {
41+ // print("⚠️ \(name).json not found in Bundle.module")
42+ // }
43+ //
44+ // // 2️⃣ Try test bundle root
45+ // if data == nil {
46+ // let testBundle = Bundle(for: type(of: self))
47+ // if let url = testBundle.url(forResource: name, withExtension: "json") {
48+ // print("🔍 Found \(name).json in test bundle at \(url.path)")
49+ // data = try? Data(contentsOf: url)
50+ // } else {
51+ // print("⚠️ \(name).json not found in test bundle root")
52+ // }
53+ // }
54+ //
55+ // print("is data set now? ")
56+ // print(data)
57+ //
58+ // // 3️⃣ Try “Mock/” subdirectory
59+ // if data == nil {
60+ // let testBundle = Bundle(for: type(of: self))
61+ // if let url = testBundle.url(forResource: name, withExtension: "json", subdirectory: "Mock") {
62+ // print("🔍 Found \(name).json in test bundle Mock/ at \(url.path)")
63+ // data = try? Data(contentsOf: url)
64+ // } else {
65+ // print("⚠️ \(name).json not found in test bundle Mock/ subdirectory")
66+ // }
67+ // }
68+ //
69+ // // 4️⃣ If still missing, log folder listings
70+ // guard let jsonData = data else {
71+ // print("❌ \(name).json not found in any bundle location.")
72+ //
73+ // // List Bundle.module resources
74+ // if let resDir = Bundle.module.resourceURL?.path {
75+ // print("📂 Contents of Bundle.module.resourceURL (\(resDir)):")
76+ // if let files = try? FileManager.default.contentsOfDirectory(atPath: resDir) {
77+ // files.forEach { print(" • \($0)") }
78+ // }
79+ // }
80+ // // List test bundle resources
81+ // let testBundle = Bundle(for: type(of: self))
82+ // if let testResDir = testBundle.resourceURL?.path {
83+ // print("📂 Contents of test bundle.resourceURL (\(testResDir)):")
84+ // if let files = try? FileManager.default.contentsOfDirectory(atPath: testResDir) {
85+ // files.forEach { print(" • \($0)") }
86+ // }
87+ // }
88+ //
89+ // completion(.failure(RuntimeError(message: "Unable to find or parse mock response")))
90+ // return
91+ // }
92+ //
93+ // // 6️⃣ Decode as before
94+ // do {
95+ // let body = try JSONDecoder.iso8601Full.decode(T.self, from: jsonData)
96+ // if(name == "Environment") {
97+ // print("Setting environmentResponseObjectKey")
98+ // UserDefaults.standard.set(data, forKey: "environmentResponseObjectKey")
99+ // UserDefaults.standard.synchronize()
100+ // }
101+ // completion(.success(body))
102+ // } catch {
103+ // print("❌ JSON Decode Error for \(name).json: \(error)")
104+ // completion(.failure(error))
105+ // }
106+ // }
107+
108+ func execute< T: Decodable > ( _ response: MockResponse , completion: @escaping ( ResultType < T > ) -> Void ) {
109+ guard let url = Bundle . module. url ( forResource: response. rawValue, withExtension: " json " ) , let data = try ? Data ( contentsOf: url) else {
110+ completion ( . failure( RuntimeError ( message: " Unable to parse response " ) ) )
111+ return
112+ }
113+
114+ do {
115+ let body = try JSONDecoder . iso8601Full. decode ( T . self, from: data)
116+ completion ( . success( body) )
117+ } catch {
118+ completion ( . failure( error) )
119+ }
106120 }
107121
108122}
0 commit comments