-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathContextManagerMock.m
162 lines (133 loc) · 5.59 KB
/
ContextManagerMock.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#import "ContextManagerMock.h"
// This deserves a little bit of explanation – this was previously part of the public interface for `ContextManager`, which shouldn't make this API
// public to the hosting app. Rather than rework the `CoreDataMigrationTests` right away (which will be done later as part of adopting Woo's
// updated and well-tested migrator), we can use this hack for now to preserve the behaviour for those tests and come back to them later.
@interface ContextManager(DeprecatedAccessors)
- (NSPersistentStoreCoordinator *) persistentStoreCoordinator;
- (NSManagedObjectModel *) managedObjectModel;
@end
@implementation ContextManagerMock
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize mainContext = _mainContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize requiresTestExpectation = _requiresTestExpectation;
@synthesize testExpectation = _testExpectation;
- (instancetype)init
{
self = [super init];
if (self) {
// Override the shared ContextManager
[ContextManager internalSharedInstance];
[ContextManager overrideSharedInstance:self];
_requiresTestExpectation = YES;
}
return self;
}
- (NSManagedObjectModel *)managedObjectModel
{
return _managedObjectModel ?: [super managedObjectModel];
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator) {
return _persistentStoreCoordinator;
}
// This is important for automatic version migration. Leave it here!
NSDictionary *options = @{
NSInferMappingModelAutomaticallyOption : @(YES),
NSMigratePersistentStoresAutomaticallyOption : @(YES)
};
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:self.managedObjectModel];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:options
error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
- (NSPersistentStoreCoordinator *)standardPSC
{
return [super persistentStoreCoordinator];
}
- (NSManagedObjectContext *)mainContext
{
if (_mainContext) {
return _mainContext;
}
_mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_mainContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
return _mainContext;
}
- (void)saveContext:(NSManagedObjectContext *)context
{
[self saveContext:context withCompletionBlock:^{
if (self.testExpectation) {
[self.testExpectation fulfill];
self.testExpectation = nil;
} else if (self.requiresTestExpectation) {
NSLog(@"No test expectation present for context save");
}
}];
}
- (void)saveContextAndWait:(NSManagedObjectContext *)context
{
[super saveContextAndWait:context];
if (self.testExpectation) {
[self.testExpectation fulfill];
self.testExpectation = nil;
} else if (self.requiresTestExpectation) {
NSLog(@"No test expectation present for context save");
}
}
- (void)saveContext:(NSManagedObjectContext *)context withCompletionBlock:(void (^)(void))completionBlock
{
[super saveContext:context withCompletionBlock:^{
if (self.testExpectation) {
[self.testExpectation fulfill];
self.testExpectation = nil;
} else if (self.requiresTestExpectation) {
NSLog(@"No test expectation present for context save");
}
completionBlock();
}];
}
- (NSURL *)storeURL
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
return [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"WordPressTest.sqlite"]];
}
- (NSManagedObject *)loadEntityNamed:(NSString *)entityName withContentsOfFile:(NSString *)filename
{
NSParameterAssert(entityName);
NSDictionary *dict = [self objectWithContentOfFile:filename];
// Insert + Set Values
NSManagedObject *object= [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self.mainContext];
for (NSString *key in dict.allKeys) {
[object setValue:dict[key] forKey:key];
}
return object;
}
- (NSDictionary *)objectWithContentOfFile:(NSString *)filename
{
NSParameterAssert(filename);
// Load the Raw JSON
NSString *name = filename.stringByDeletingPathExtension;
NSString *extension = filename.pathExtension;
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:name ofType:extension];
NSData *contents = [NSData dataWithContentsOfFile:path];
NSAssert(contents, @"Mockup data could not be loaded");
// Parse
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:contents
options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves
error:nil];
NSAssert(dict, @"Mockup data could not be parsed");
return dict;
}
@end