Skip to content

Commit a6696d8

Browse files
authored
Use ContextManager directly in ContextManagerTests (#18630)
2 parents f85deea + 1203d31 commit a6696d8

File tree

5 files changed

+120
-197
lines changed

5 files changed

+120
-197
lines changed

WordPress/Classes/Utility/ContextManager.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
NS_ASSUME_NONNULL_BEGIN
55

6+
/**
7+
A constant representing the current version of the data model.
8+
9+
@see -[ContextManager initWithModelName:storeURL:]
10+
*/
11+
FOUNDATION_EXTERN NSString * const ContextManagerModelNameCurrent;
12+
613
@protocol CoreDataStack
714
@property (nonatomic, readonly, strong) NSManagedObjectContext *mainContext;
815
- (NSManagedObjectContext *const)newDerivedContext;
@@ -34,6 +41,18 @@ NS_ASSUME_NONNULL_BEGIN
3441
*/
3542
+ (instancetype)internalSharedInstance;
3643

44+
/**
45+
Create a ContextManager instance with given model name and database location.
46+
47+
Note: This initialiser is only used for testing purpose at the moment.
48+
49+
@param modelName Model name in Core Data data model file.
50+
Use ContextManagerModelNameCurrent for current version, or
51+
"WordPress <version>" for specific version.
52+
@param storeURL Database location.
53+
*/
54+
- (instancetype)initWithModelName:(NSString *)modelName storeURL:(NSURL *)storeURL NS_DESIGNATED_INITIALIZER;
55+
3756

3857
///--------------------------
3958
///@name Contexts

WordPress/Classes/Utility/ContextManager.m

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#define SentryStartupEventAddError(event, error) [event addError:error file:__FILE__ function:__FUNCTION__ line:__LINE__]
77

8+
NSString * const ContextManagerModelNameCurrent = @"$CURRENT";
9+
810
// MARK: - Static Variables
911
//
1012
static ContextManager *_instance;
@@ -22,6 +24,8 @@ @interface ContextManager ()
2224
@property (nonatomic, strong) NSManagedObjectContext *mainContext;
2325
@property (nonatomic, strong) NSManagedObjectContext *writerContext;
2426
@property (nonatomic, assign) BOOL migrationFailed;
27+
@property (nonatomic, strong) NSString *modelName;
28+
@property (nonatomic, strong) NSURL *storeURL;
2529

2630
@end
2731

@@ -31,9 +35,25 @@ @interface ContextManager ()
3135
@implementation ContextManager
3236

3337
- (instancetype)init
38+
{
39+
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
40+
NSUserDomainMask,
41+
YES) lastObject];
42+
NSURL *storeURL = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"WordPress.sqlite"]];
43+
44+
return [self initWithModelName:ContextManagerModelNameCurrent storeURL:storeURL];
45+
}
46+
47+
- (instancetype)initWithModelName:(NSString *)modelName storeURL:(NSURL *)storeURL
3448
{
3549
self = [super init];
3650
if (self) {
51+
NSParameterAssert([modelName isEqualToString:ContextManagerModelNameCurrent] || [modelName hasPrefix:@"WordPress "]);
52+
NSParameterAssert([storeURL isFileURL]);
53+
54+
self.modelName = modelName;
55+
self.storeURL = storeURL;
56+
3757
[NSValueTransformer registerCustomTransformers];
3858
// Create `mainContext` and `writerContext` during initialisation to
3959
// ensure they are only created once.
@@ -236,7 +256,16 @@ - (NSManagedObjectModel *)managedObjectModel
236256
}
237257
NSString *modelPath = [self modelPath];
238258
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
239-
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
259+
if ([self.modelName isEqualToString:ContextManagerModelNameCurrent]) {
260+
// Use the current version defined in data model.
261+
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
262+
} else {
263+
// Find the specific version defined in data model.
264+
NSURL *versionedModelURL = [[modelURL URLByAppendingPathComponent:self.modelName] URLByAppendingPathExtension:@"mom"];
265+
NSAssert([NSFileManager.defaultManager fileExistsAtPath:versionedModelURL.path],
266+
@"Can't find model '%@' at %@", self.modelName, modelPath);
267+
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:versionedModelURL];
268+
}
240269
return _managedObjectModel;
241270
}
242271

@@ -324,15 +353,6 @@ - (NSArray *)sortedModelNames
324353
return modelNames;
325354
}
326355

327-
- (NSURL *)storeURL
328-
{
329-
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
330-
NSUserDomainMask,
331-
YES) lastObject];
332-
333-
return [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"WordPress.sqlite"]];
334-
}
335-
336356
- (NSString *)modelPath
337357
{
338358
return [[NSBundle mainBundle] pathForResource:@"WordPress" ofType:@"momd"];

WordPress/WordPressTest/ContextManagerMock.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,13 @@
44

55
NS_ASSUME_NONNULL_BEGIN
66

7-
@protocol ManagerMock
8-
@property (nonatomic, readwrite, strong) NSManagedObjectContext *mainContext;
9-
@property (nonatomic, readwrite, strong) NSManagedObjectModel *managedObjectModel;
10-
@property (nonatomic, readwrite, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;
11-
@property (nonatomic, readonly, strong) NSPersistentStoreCoordinator *standardPSC;
12-
@property (nonatomic, readonly, strong) NSURL *storeURL;
13-
@end
14-
157
/**
168
* @class ContextManagerMock
179
* @brief This class overrides standard ContextManager functionality, for testing purposes.
1810
* @details The SQLite-Backed PSC is replaced by an InMemory store. This allows us to easily reset
1911
* the contents of the database, without even hitting the filesystem.
2012
*/
21-
@interface ContextManagerMock : ContextManager <ManagerMock, CoreDataStack>
13+
@interface ContextManagerMock : ContextManager
2214

2315
- (void)setUpAsSharedInstance;
2416
- (void)tearDown;

WordPress/WordPressTest/ContextManagerMock.m

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,10 @@
11
#import "ContextManagerMock.h"
22

3-
// This deserves a little bit of explanation – this was previously part of the public interface for `ContextManager`, which shouldn't make this API
4-
// public to the hosting app. Rather than rework the `CoreDataMigrationTests` right away (which will be done later as part of adopting Woo's
5-
// 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.
6-
@interface ContextManager(DeprecatedAccessors)
7-
- (NSPersistentStoreCoordinator *) persistentStoreCoordinator;
8-
- (NSManagedObjectModel *) managedObjectModel;
9-
@end
10-
113
@implementation ContextManagerMock
124

13-
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
14-
@synthesize mainContext = _mainContext;
15-
@synthesize managedObjectModel = _managedObjectModel;
16-
17-
- (NSManagedObjectModel *)managedObjectModel
18-
{
19-
return _managedObjectModel ?: [super managedObjectModel];
20-
}
21-
22-
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
23-
{
24-
if (_persistentStoreCoordinator) {
25-
return _persistentStoreCoordinator;
26-
}
27-
28-
// This is important for automatic version migration. Leave it here!
29-
NSDictionary *options = @{
30-
NSInferMappingModelAutomaticallyOption : @(YES),
31-
NSMigratePersistentStoresAutomaticallyOption : @(YES)
32-
};
33-
34-
NSError *error = nil;
35-
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
36-
initWithManagedObjectModel:self.managedObjectModel];
37-
38-
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType
39-
configuration:nil
40-
URL:nil
41-
options:options
42-
error:&error]) {
43-
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
44-
abort();
45-
}
46-
47-
return _persistentStoreCoordinator;
48-
}
49-
50-
- (NSPersistentStoreCoordinator *)standardPSC
5+
- (instancetype)init
516
{
52-
return [super persistentStoreCoordinator];
53-
}
54-
55-
- (void)createMainContext
56-
{
57-
self.mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
58-
self.mainContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
7+
return [super initWithModelName:ContextManagerModelNameCurrent storeURL:[NSURL fileURLWithPath:@"/dev/null"]];
598
}
609

6110
- (void)saveContextAndWait:(NSManagedObjectContext *)context
@@ -67,15 +16,6 @@ - (void)saveContextAndWait:(NSManagedObjectContext *)context
6716
NSLog(@"Context save completed");
6817
}
6918

70-
- (NSURL *)storeURL
71-
{
72-
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
73-
NSUserDomainMask,
74-
YES) lastObject];
75-
76-
return [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"WordPressTest.sqlite"]];
77-
}
78-
7919
- (void)setUpAsSharedInstance
8020
{
8121
[ContextManager internalSharedInstance];

0 commit comments

Comments
 (0)