Skip to content

Commit 9bdacfd

Browse files
committed
Move creating model instance function out from CoreDataStack mocks
Two functions are moved out: one for parsing JSON file function and the other for loading JSON file content into a Core Data model. Both aren't related to managing a Core Data stack.
1 parent bbb401c commit 9bdacfd

17 files changed

Lines changed: 74 additions & 131 deletions

WordPress/WordPress.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@
920920
46F584B82624E6380010A723 /* BlockEditorSettings+GutenbergEditorSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46F584B72624E6380010A723 /* BlockEditorSettings+GutenbergEditorSettings.swift */; };
921921
46F584B92624E6380010A723 /* BlockEditorSettings+GutenbergEditorSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46F584B72624E6380010A723 /* BlockEditorSettings+GutenbergEditorSettings.swift */; };
922922
46F58501262605930010A723 /* BlockEditorSettingsServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46F58500262605930010A723 /* BlockEditorSettingsServiceTests.swift */; };
923+
4A17C1A4281A823E0001FFE5 /* NSManagedObject+Fixture.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A17C1A3281A823E0001FFE5 /* NSManagedObject+Fixture.swift */; };
923924
4B2DD0F29CD6AC353C056D41 /* Pods_WordPressUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DCE7542239FBC709B90EA85 /* Pods_WordPressUITests.framework */; };
924925
4C8A715EBCE7E73AEE216293 /* Pods_WordPressShareExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F47DB4A8EC2E6844E213A3FA /* Pods_WordPressShareExtension.framework */; };
925926
4D520D4F22972BC9002F5924 /* acknowledgements.html in Resources */ = {isa = PBXBuildFile; fileRef = 4D520D4E22972BC9002F5924 /* acknowledgements.html */; };
@@ -5679,6 +5680,7 @@
56795680
46F584B72624E6380010A723 /* BlockEditorSettings+GutenbergEditorSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BlockEditorSettings+GutenbergEditorSettings.swift"; sourceTree = "<group>"; };
56805681
46F58500262605930010A723 /* BlockEditorSettingsServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockEditorSettingsServiceTests.swift; sourceTree = "<group>"; };
56815682
46F84612185A8B7E009D0DA5 /* PostContentProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PostContentProvider.h; sourceTree = "<group>"; };
5683+
4A17C1A3281A823E0001FFE5 /* NSManagedObject+Fixture.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSManagedObject+Fixture.swift"; sourceTree = "<group>"; };
56825684
4D520D4E22972BC9002F5924 /* acknowledgements.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = acknowledgements.html; path = "../Pods/Target Support Files/Pods-Apps-WordPress/acknowledgements.html"; sourceTree = "<group>"; };
56835685
51A5F017948878F7E26979A0 /* Pods-Apps-WordPress.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Apps-WordPress.release.xcconfig"; path = "../Pods/Target Support Files/Pods-Apps-WordPress/Pods-Apps-WordPress.release.xcconfig"; sourceTree = "<group>"; };
56845686
528B9926294302CD0A4EB5C4 /* Pods-WordPressScreenshotGeneration.release-alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressScreenshotGeneration.release-alpha.xcconfig"; path = "../Pods/Target Support Files/Pods-WordPressScreenshotGeneration/Pods-WordPressScreenshotGeneration.release-alpha.xcconfig"; sourceTree = "<group>"; };
@@ -14142,6 +14144,7 @@
1414214144
children = (
1414314145
E180BD4D1FB4681E00D0D781 /* MockCookieJar.swift */,
1414414146
E1B642121EFA5113001DC6D7 /* ModelTestHelper.swift */,
14147+
4A17C1A3281A823E0001FFE5 /* NSManagedObject+Fixture.swift */,
1414514148
93E9050519E6F3D8005513C9 /* ContextManagerMock.h */,
1414614149
93E9050619E6F3D8005513C9 /* ContextManagerMock.m */,
1414714150
8BD6E34B24775164009AE97C /* TestContextManager.h */,
@@ -19995,6 +19998,7 @@
1999519998
8B749E9025AF8D2E00023F03 /* JetpackCapabilitiesServiceTests.swift in Sources */,
1999619999
F1450CF72437E8F800A28BFE /* MediaHostTests.swift in Sources */,
1999720000
436D55F02115CB6800CEAA33 /* RegisterDomainDetailsSectionTests.swift in Sources */,
20001+
4A17C1A4281A823E0001FFE5 /* NSManagedObject+Fixture.swift in Sources */,
1999820002
E1B921BC1C0ED5A3003EA3CB /* MediaSizeSliderCellTest.swift in Sources */,
1999920003
C314543B262770BE005B216B /* BlogServiceAuthorTests.swift in Sources */,
2000020004
40F50B80221310D400CBBB73 /* FollowersStatsRecordValueTests.swift in Sources */,

WordPress/WordPressTest/ActivityContentFactoryTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ final class ActivityContentFactoryTests: XCTestCase {
1515
}
1616

1717
private func getDictionaryFromFile(named fileName: String) -> [String: AnyObject] {
18-
return contextManager.object(withContentOfFile: fileName) as! [String: AnyObject]
18+
return JSONLoader().loadFile(named: fileName) ?? [:]
1919
}
2020
}

WordPress/WordPressTest/ActivityLogTestData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ActivityLogTestData {
2828
}
2929

3030
private func getDictionaryFromFile(named fileName: String) -> [String: AnyObject] {
31-
return contextManager.object(withContentOfFile: fileName) as! [String: AnyObject]
31+
return JSONLoader().loadFile(named: fileName) ?? [:]
3232
}
3333

3434
func getCommentEventDictionary() -> [String: AnyObject] {

WordPress/WordPressTest/ContextManagerMock.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ NS_ASSUME_NONNULL_BEGIN
2222
*/
2323
@interface ContextManagerMock : ContextManager <ManagerMock, CoreDataStack>
2424

25-
26-
/**
27-
* @brief Loads the contents of any given JSON file into a new NSManagedObject instance
28-
* @details This helper method is useful for Unit Testing scenarios.
29-
*
30-
* @param entityName The name of the entity to be inserted.
31-
* @param filename The name of the JSON file to be loaded.
32-
*/
33-
- (NSManagedObject *)loadEntityNamed:(NSString *)entityName withContentsOfFile:(NSString *)filename;
34-
- (NSDictionary *)objectWithContentOfFile:(NSString *)filename;
35-
3625
@end
3726

3827
NS_ASSUME_NONNULL_END

WordPress/WordPressTest/ContextManagerMock.m

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -124,39 +124,4 @@ - (NSURL *)storeURL
124124
return [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"WordPressTest.sqlite"]];
125125
}
126126

127-
- (NSManagedObject *)loadEntityNamed:(NSString *)entityName withContentsOfFile:(NSString *)filename
128-
{
129-
NSParameterAssert(entityName);
130-
131-
NSDictionary *dict = [self objectWithContentOfFile:filename];
132-
133-
// Insert + Set Values
134-
NSManagedObject *object= [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self.mainContext];
135-
136-
for (NSString *key in dict.allKeys) {
137-
[object setValue:dict[key] forKey:key];
138-
}
139-
140-
return object;
141-
}
142-
143-
- (NSDictionary *)objectWithContentOfFile:(NSString *)filename
144-
{
145-
NSParameterAssert(filename);
146-
147-
// Load the Raw JSON
148-
NSString *name = filename.stringByDeletingPathExtension;
149-
NSString *extension = filename.pathExtension;
150-
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:name ofType:extension];
151-
NSData *contents = [NSData dataWithContentsOfFile:path];
152-
NSAssert(contents, @"Mockup data could not be loaded");
153-
154-
// Parse
155-
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:contents
156-
options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves
157-
error:nil];
158-
NSAssert(dict, @"Mockup data could not be parsed");
159-
return dict;
160-
}
161-
162127
@end

WordPress/WordPressTest/FormattableCommentContentTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ final class FormattableCommentContentTests: XCTestCase {
138138
}
139139

140140
private func getDictionaryFromFile(named fileName: String) -> [String: AnyObject] {
141-
return contextManager.object(withContentOfFile: fileName) as! [String: AnyObject]
141+
return JSONLoader().loadFile(named: fileName) ?? [:]
142142
}
143143

144144
private func loadLikeNotification() -> WordPress.Notification {

WordPress/WordPressTest/FormattableContentGroupTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ final class FormattableContentGroupTests: XCTestCase {
6464
}
6565

6666
private func getDictionaryFromFile(named fileName: String) -> [String: AnyObject] {
67-
return contextManager.object(withContentOfFile: fileName) as! [String: AnyObject]
67+
return JSONLoader().loadFile(named: fileName) ?? [:]
6868
}
6969
}

WordPress/WordPressTest/FormattableUserContentTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ final class FormattableUserContentTests: XCTestCase {
134134
}
135135

136136
private func getDictionaryFromFile(named fileName: String) -> [String: AnyObject] {
137-
return contextManager.object(withContentOfFile: fileName) as! [String: AnyObject]
137+
return JSONLoader().loadFile(named: fileName) ?? [:]
138138
}
139139

140140
private func loadLikeNotification() -> WordPress.Notification {
141-
return contextManager.loadEntityNamed(entityName, withContentsOfFile: "notifications-like.json") as! WordPress.Notification
141+
return .fixture(fromFile: "notifications-like.json", context: contextManager.mainContext)
142142
}
143143

144144
private func loadMeta() -> [String: AnyObject] {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import CoreData
2+
3+
extension NSManagedObject {
4+
5+
/// Loads the contents of any given JSON file into a new NSManagedObject instance.
6+
///
7+
/// This helper method is useful for Unit Testing scenarios.
8+
///
9+
/// - Parameters:
10+
/// - filename: The name of the JSON file to be loaded
11+
/// - context: The managed object context to use
12+
/// - Returns: A new instance with property values of the given JSON file.
13+
static func fixture(fromFile filename: String, context: NSManagedObjectContext) -> Self {
14+
guard let jsonObject = JSONLoader().loadFile(named: filename) else {
15+
fatalError("Mockup data could not be parsed, the filename is \(filename)")
16+
}
17+
let model = Self.init(context: context)
18+
for (key, value) in jsonObject {
19+
model.setValue(value, forKey: key)
20+
}
21+
return model
22+
}
23+
24+
}

WordPress/WordPressTest/NotificationContentRangeFactoryTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ final class NotificationContentRangeFactoryTests: XCTestCase {
6565
}
6666

6767
private func getDictionaryFromFile(named fileName: String) -> [String: AnyObject] {
68-
return contextManager.object(withContentOfFile: fileName) as! [String: AnyObject]
68+
return JSONLoader().loadFile(named: fileName) ?? [:]
6969
}
7070
}

0 commit comments

Comments
 (0)