Skip to content

Commit d51d3f7

Browse files
authored
Extract models creation logic from CoreDataStack doubles (#18478)
2 parents 390b778 + df5b59a commit d51d3f7

19 files changed

+99
-173
lines changed

WordPress/WordPress.xcodeproj/project.pbxproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,7 @@
924924
46F584B82624E6380010A723 /* BlockEditorSettings+GutenbergEditorSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46F584B72624E6380010A723 /* BlockEditorSettings+GutenbergEditorSettings.swift */; };
925925
46F584B92624E6380010A723 /* BlockEditorSettings+GutenbergEditorSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46F584B72624E6380010A723 /* BlockEditorSettings+GutenbergEditorSettings.swift */; };
926926
46F58501262605930010A723 /* BlockEditorSettingsServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46F58500262605930010A723 /* BlockEditorSettingsServiceTests.swift */; };
927+
4A17C1A4281A823E0001FFE5 /* NSManagedObject+Fixture.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A17C1A3281A823E0001FFE5 /* NSManagedObject+Fixture.swift */; };
927928
4B2DD0F29CD6AC353C056D41 /* Pods_WordPressUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DCE7542239FBC709B90EA85 /* Pods_WordPressUITests.framework */; };
928929
4C8A715EBCE7E73AEE216293 /* Pods_WordPressShareExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F47DB4A8EC2E6844E213A3FA /* Pods_WordPressShareExtension.framework */; };
929930
4D520D4F22972BC9002F5924 /* acknowledgements.html in Resources */ = {isa = PBXBuildFile; fileRef = 4D520D4E22972BC9002F5924 /* acknowledgements.html */; };
@@ -983,7 +984,7 @@
983984
598DD1711B97985700146967 /* ThemeBrowserCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 598DD1701B97985700146967 /* ThemeBrowserCell.swift */; };
984985
59A3CADD1CD2FF0C009BFA1B /* BasePageListCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A3CADC1CD2FF0C009BFA1B /* BasePageListCell.m */; };
985986
59A9AB351B4C33A500A433DC /* ThemeService.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A9AB341B4C33A500A433DC /* ThemeService.m */; };
986-
59B48B621B99E132008EBB84 /* JSONLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 59B48B611B99E132008EBB84 /* JSONLoader.swift */; };
987+
59B48B621B99E132008EBB84 /* JSONObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 59B48B611B99E132008EBB84 /* JSONObject.swift */; };
987988
59DCA5211CC68AF3000F245F /* PageListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 59DCA5201CC68AF3000F245F /* PageListViewController.swift */; };
988989
59DD94341AC479ED0032DD6B /* WPLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = 59DD94331AC479ED0032DD6B /* WPLogger.m */; };
989990
59E1D46E1CEF77B500126697 /* Page.swift in Sources */ = {isa = PBXBuildFile; fileRef = 59E1D46C1CEF77B500126697 /* Page.swift */; };
@@ -5692,6 +5693,7 @@
56925693
46F584B72624E6380010A723 /* BlockEditorSettings+GutenbergEditorSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BlockEditorSettings+GutenbergEditorSettings.swift"; sourceTree = "<group>"; };
56935694
46F58500262605930010A723 /* BlockEditorSettingsServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockEditorSettingsServiceTests.swift; sourceTree = "<group>"; };
56945695
46F84612185A8B7E009D0DA5 /* PostContentProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PostContentProvider.h; sourceTree = "<group>"; };
5696+
4A17C1A3281A823E0001FFE5 /* NSManagedObject+Fixture.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSManagedObject+Fixture.swift"; sourceTree = "<group>"; };
56955697
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>"; };
56965698
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>"; };
56975699
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>"; };
@@ -5763,7 +5765,7 @@
57635765
59A9AB331B4C33A500A433DC /* ThemeService.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThemeService.h; sourceTree = "<group>"; };
57645766
59A9AB341B4C33A500A433DC /* ThemeService.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ThemeService.m; sourceTree = "<group>"; };
57655767
59A9AB391B4C3ECD00A433DC /* LocalCoreDataServiceTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocalCoreDataServiceTests.m; sourceTree = "<group>"; };
5766-
59B48B611B99E132008EBB84 /* JSONLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = JSONLoader.swift; path = TestUtilities/JSONLoader.swift; sourceTree = "<group>"; };
5768+
59B48B611B99E132008EBB84 /* JSONObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = JSONObject.swift; path = TestUtilities/JSONObject.swift; sourceTree = "<group>"; };
57675769
59DCA5201CC68AF3000F245F /* PageListViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PageListViewController.swift; sourceTree = "<group>"; };
57685770
59DD94321AC479ED0032DD6B /* WPLogger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WPLogger.h; sourceTree = "<group>"; };
57695771
59DD94331AC479ED0032DD6B /* WPLogger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WPLogger.m; sourceTree = "<group>"; };
@@ -10270,7 +10272,7 @@
1027010272
59B48B601B99E0B0008EBB84 /* TestUtilities */ = {
1027110273
isa = PBXGroup;
1027210274
children = (
10273-
59B48B611B99E132008EBB84 /* JSONLoader.swift */,
10275+
59B48B611B99E132008EBB84 /* JSONObject.swift */,
1027410276
E157D5DF1C690A6C00F04FB9 /* ImmuTableTestUtils.swift */,
1027510277
570BFD8F2282418A007859A8 /* PostBuilder.swift */,
1027610278
57B71D4D230DB5F200789A68 /* BlogBuilder.swift */,
@@ -14171,6 +14173,7 @@
1417114173
children = (
1417214174
E180BD4D1FB4681E00D0D781 /* MockCookieJar.swift */,
1417314175
E1B642121EFA5113001DC6D7 /* ModelTestHelper.swift */,
14176+
4A17C1A3281A823E0001FFE5 /* NSManagedObject+Fixture.swift */,
1417414177
93E9050519E6F3D8005513C9 /* ContextManagerMock.h */,
1417514178
93E9050619E6F3D8005513C9 /* ContextManagerMock.m */,
1417614179
8BD6E34B24775164009AE97C /* TestContextManager.h */,
@@ -19769,7 +19772,7 @@
1976919772
175CC17527205BFB00622FB4 /* DomainExpiryDateFormatterTests.swift in Sources */,
1977019773
B5416CFE1C1756B900006DD8 /* PushNotificationsManagerTests.m in Sources */,
1977119774
321955C124BE4EBF00E3F316 /* ReaderSelectInterestsCoordinatorTests.swift in Sources */,
19772-
59B48B621B99E132008EBB84 /* JSONLoader.swift in Sources */,
19775+
59B48B621B99E132008EBB84 /* JSONObject.swift in Sources */,
1977319776
3F82310F24564A870086E9B8 /* ReaderTabViewTests.swift in Sources */,
1977419777
C3E42AB027F4D30E00546706 /* MenuItemsViewControllerTests.swift in Sources */,
1977519778
D842EA4021FABB1800210E96 /* SiteSegmentTests.swift in Sources */,
@@ -19884,6 +19887,7 @@
1988419887
8B749E9025AF8D2E00023F03 /* JetpackCapabilitiesServiceTests.swift in Sources */,
1988519888
F1450CF72437E8F800A28BFE /* MediaHostTests.swift in Sources */,
1988619889
436D55F02115CB6800CEAA33 /* RegisterDomainDetailsSectionTests.swift in Sources */,
19890+
4A17C1A4281A823E0001FFE5 /* NSManagedObject+Fixture.swift in Sources */,
1988719891
E1B921BC1C0ED5A3003EA3CB /* MediaSizeSliderCellTest.swift in Sources */,
1988819892
C314543B262770BE005B216B /* BlogServiceAuthorTests.swift in Sources */,
1988919893
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 JSONObject.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 JSONObject.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
@@ -20,17 +20,6 @@ NS_ASSUME_NONNULL_BEGIN
2020
*/
2121
@interface ContextManagerMock : ContextManager <ManagerMock, CoreDataStack>
2222

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

3625
NS_ASSUME_NONNULL_END

WordPress/WordPressTest/ContextManagerMock.m

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

97-
- (NSManagedObject *)loadEntityNamed:(NSString *)entityName withContentsOfFile:(NSString *)filename
98-
{
99-
NSParameterAssert(entityName);
100-
101-
NSDictionary *dict = [self objectWithContentOfFile:filename];
102-
103-
// Insert + Set Values
104-
NSManagedObject *object= [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self.mainContext];
105-
106-
for (NSString *key in dict.allKeys) {
107-
[object setValue:dict[key] forKey:key];
108-
}
109-
110-
return object;
111-
}
112-
113-
- (NSDictionary *)objectWithContentOfFile:(NSString *)filename
114-
{
115-
NSParameterAssert(filename);
116-
117-
// Load the Raw JSON
118-
NSString *name = filename.stringByDeletingPathExtension;
119-
NSString *extension = filename.pathExtension;
120-
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:name ofType:extension];
121-
NSData *contents = [NSData dataWithContentsOfFile:path];
122-
NSAssert(contents, @"Mockup data could not be loaded");
123-
124-
// Parse
125-
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:contents
126-
options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves
127-
error:nil];
128-
NSAssert(dict, @"Mockup data could not be parsed");
129-
return dict;
130-
}
131-
13297
@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 JSONObject.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 JSONObject.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 JSONObject.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", insertInto: contextManager.mainContext)
142142
}
143143

144144
private func loadMeta() -> [String: AnyObject] {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import CoreData
2+
3+
extension NSManagedObject {
4+
5+
/// Loads the contents of any given JSON file into a new `NSManagedObject` instance.
6+
///
7+
/// - Parameters:
8+
/// - filename: The name of the JSON file to be loaded
9+
/// - context: The managed object context to use
10+
/// - Returns: A new instance with property values of the given JSON file.
11+
static func fixture(fromFile fileName: String, insertInto context: NSManagedObjectContext) -> Self {
12+
let jsonObject = JSONObject.loadFile(named: fileName)
13+
let model = Self.init(context: context)
14+
for (key, value) in jsonObject {
15+
model.setValue(value, forKey: key)
16+
}
17+
return model
18+
}
19+
20+
}

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 JSONObject.loadFile(named: fileName)
6969
}
7070
}

0 commit comments

Comments
 (0)