forked from parse-community/Parse-SDK-iOS-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrentConfigControllerTests.m
190 lines (135 loc) · 6.58 KB
/
CurrentConfigControllerTests.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <OCMock/OCMock.h>
@import Bolts.BFTask;
#import "PFCommandResult.h"
#import "PFConfig.h"
#import "PFConfig_Private.h"
#import "PFCurrentConfigController.h"
#import "PFFileManager.h"
#import "PFTestCase.h"
@interface CurrentConfigControllerTests : PFTestCase
@end
@implementation CurrentConfigControllerTests
///--------------------------------------
#pragma mark - Helpers
///--------------------------------------
- (NSDictionary *)testConfigDictionary {
return @{ @"params" : @{@"testKey" : @"testValue"} };
}
- (PFFileManager *)mockedFileManagerWithConfigPath:(NSString *)path {
id fileManager = PFPartialMock([[PFFileManager alloc] initWithApplicationIdentifier:OCMOCK_ANY
applicationGroupIdentifier:@"com.parse.test"]);
OCMStub([fileManager parseDataItemPathForPathComponent:OCMOCK_ANY]).andReturn(path);
return fileManager;
}
- (NSString *)configPathForSelector:(SEL)cmd {
NSString *configPath = [[NSTemporaryDirectory() stringByAppendingPathComponent:NSStringFromSelector(cmd)]
stringByAppendingPathExtension:@"config"];
[[NSFileManager defaultManager] removeItemAtPath:configPath error:NULL];
return configPath;
}
///--------------------------------------
#pragma mark - Tests
///--------------------------------------
- (void)testConstructor {
id mockedFileManager = PFClassMock([PFFileManager class]);
PFCurrentConfigController *controller = [[PFCurrentConfigController alloc] initWithFileManager:mockedFileManager];
XCTAssertNotNil(controller);
XCTAssertEqual(controller.fileManager, mockedFileManager);
}
- (void)testGetCurrentConfig {
NSString *configPath = [self configPathForSelector:_cmd];
NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:configPath append:NO];
[outputStream open];
NSError *error = nil;
[NSJSONSerialization writeJSONObject:[self testConfigDictionary]
toStream:outputStream
options:0
error:&error];
[outputStream close];
XCTAssertNil(error);
PFFileManager *fileManager = [self mockedFileManagerWithConfigPath:configPath];
PFCurrentConfigController *currentController = [PFCurrentConfigController controllerWithFileManager:fileManager];
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[currentController getCurrentConfigAsync] continueWithBlock:^id(BFTask *task) {
XCTAssertNotNil(task.result);
XCTAssertTrue([task.result isKindOfClass:[PFConfig class]]);
XCTAssertEqualObjects(task.result[@"testKey"], @"testValue");
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
}
- (void)testSetCurrentConfig {
NSString *configPath = [self configPathForSelector:_cmd];
PFConfig *testConfig = [[PFConfig alloc] initWithFetchedConfig:[self testConfigDictionary]];
PFFileManager *fileManager = [self mockedFileManagerWithConfigPath:configPath];
PFCurrentConfigController *currentController = [PFCurrentConfigController controllerWithFileManager:fileManager];
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[currentController setCurrentConfigAsync:testConfig] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
NSData *data = [NSData dataWithContentsOfFile:configPath];
XCTAssertNotNil(data);
NSDictionary *contentsOfFile = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
XCTAssertEqualObjects(contentsOfFile, [self testConfigDictionary]);
}
- (void)testClearCurrentConfig {
NSString *configPath = [self configPathForSelector:_cmd];
PFConfig *testConfig = [[PFConfig alloc] initWithFetchedConfig:[self testConfigDictionary]];
PFFileManager *fileManager = [self mockedFileManagerWithConfigPath:configPath];
PFCurrentConfigController *currentController = [PFCurrentConfigController controllerWithFileManager:fileManager];
XCTestExpectation *saveExpectation = [self expectationWithDescription:@"Save"];
[[currentController setCurrentConfigAsync:testConfig] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
[saveExpectation fulfill];
return nil;
}];
[self waitForTestExpectations];
XCTestExpectation *clearExpectation = [self expectationWithDescription:@"Clear"];
[[currentController clearCurrentConfigAsync] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
[clearExpectation fulfill];
return nil;
}];
[self waitForTestExpectations];
NSData *data = [NSData dataWithContentsOfFile:configPath];
XCTAssertNil(data);
}
- (void)testClearMemoryCachedCurrentConfig {
NSString *configPath = [self configPathForSelector:_cmd];
PFConfig *testConfig = [[PFConfig alloc] initWithFetchedConfig:[self testConfigDictionary]];
PFFileManager *fileManager = [self mockedFileManagerWithConfigPath:configPath];
PFCurrentConfigController *currentController = [PFCurrentConfigController controllerWithFileManager:fileManager];
XCTestExpectation *saveExpectation = [self expectationWithDescription:@"Save"];
[[currentController setCurrentConfigAsync:testConfig] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
[saveExpectation fulfill];
return nil;
}];
[self waitForTestExpectations];
XCTestExpectation *clearExpectation = [self expectationWithDescription:@"Clear"];
[[currentController clearMemoryCachedCurrentConfigAsync] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
[clearExpectation fulfill];
return nil;
}];
[self waitForTestExpectations];
NSData *data = [NSData dataWithContentsOfFile:configPath];
XCTAssertNotNil(data);
// Ideally here we would check to ensure that we re-read from the path. However, you cannot re-stub
// the same method using OCMock (Ugh), so for now just assume that it properly removed the current config.
}
@end