Skip to content

Commit 3433a9b

Browse files
committed
Inject NSNotificationCenter.
1 parent faf0787 commit 3433a9b

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

Parse/Internal/Commands/CommandRunner/URLSession/PFURLSessionCommandRunner.m

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
@interface PFURLSessionCommandRunner () <PFURLSessionDelegate>
3434

35+
@property (nonatomic, strong) NSNotificationCenter *notificationCenter;
36+
3537
@end
3638

3739
@implementation PFURLSessionCommandRunner
@@ -57,7 +59,8 @@ - (instancetype)initWithDataSource:(id<PFInstallationIdentifierStoreProvider>)da
5759
PFCommandURLRequestConstructor *constructor = [PFCommandURLRequestConstructor constructorWithDataSource:dataSource];
5860
self = [self initWithDataSource:dataSource
5961
session:session
60-
requestConstructor:constructor];
62+
requestConstructor:constructor
63+
notificationCenter:[NSNotificationCenter defaultCenter]];
6164
if (!self) return nil;
6265

6366
_applicationId = [applicationId copy];
@@ -68,14 +71,16 @@ - (instancetype)initWithDataSource:(id<PFInstallationIdentifierStoreProvider>)da
6871

6972
- (instancetype)initWithDataSource:(id<PFInstallationIdentifierStoreProvider>)dataSource
7073
session:(PFURLSession *)session
71-
requestConstructor:(PFCommandURLRequestConstructor *)requestConstructor {
74+
requestConstructor:(PFCommandURLRequestConstructor *)requestConstructor
75+
notificationCenter:(NSNotificationCenter *)notificationCenter {
7276
self = [super init];
7377
if (!self) return nil;
7478

7579
_initialRetryDelay = PFCommandRunningDefaultRetryDelay;
7680

7781
_requestConstructor = requestConstructor;
7882
_session = session;
83+
_notificationCenter = notificationCenter;
7984

8085
return self;
8186
}
@@ -247,9 +252,9 @@ + (NSURLSessionConfiguration *)_urlSessionConfigurationForApplicationId:(NSStrin
247252

248253
- (void)urlSession:(PFURLSession *)session willPerformURLRequest:(NSURLRequest *)request {
249254
NSDictionary *userInfo = @{ PFCommandRunnerNotificationURLRequestUserInfoKey : request };
250-
[[NSNotificationCenter defaultCenter] postNotificationName:PFCommandRunnerWillSendURLRequestNotification
251-
object:self
252-
userInfo:userInfo];
255+
[self.notificationCenter postNotificationName:PFCommandRunnerWillSendURLRequestNotification
256+
object:self
257+
userInfo:userInfo];
253258
}
254259

255260
- (void)urlSession:(PFURLSession *)session didPerformURLRequest:(NSURLRequest *)request withURLResponse:(nullable NSURLResponse *)response {
@@ -258,9 +263,9 @@ - (void)urlSession:(PFURLSession *)session didPerformURLRequest:(NSURLRequest *)
258263
if (response) {
259264
userInfo[PFCommandRunnerNotificationURLResponseUserInfoKey] = response;
260265
}
261-
[[NSNotificationCenter defaultCenter] postNotificationName:PFCommandRunnerDidReceiveURLResponseNotification
262-
object:self
263-
userInfo:userInfo];
266+
[self.notificationCenter postNotificationName:PFCommandRunnerDidReceiveURLResponseNotification
267+
object:self
268+
userInfo:userInfo];
264269
}
265270

266271
@end

Parse/Internal/Commands/CommandRunner/URLSession/PFURLSessionCommandRunner_Private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ NS_ASSUME_NONNULL_BEGIN
2121

2222
- (instancetype)initWithDataSource:(id<PFInstallationIdentifierStoreProvider>)dataSource
2323
session:(PFURLSession *)session
24-
requestConstructor:(PFCommandURLRequestConstructor *)requestConstructor NS_DESIGNATED_INITIALIZER;
24+
requestConstructor:(PFCommandURLRequestConstructor *)requestConstructor
25+
notificationCenter:(NSNotificationCenter *)notificationCenter NS_DESIGNATED_INITIALIZER;
2526

2627
@end
2728

Tests/Unit/URLSessionCommandRunnerTests.m

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ - (void)testRunCommand {
5454
id mockedDataSource = PFStrictProtocolMock(@protocol(PFInstallationIdentifierStoreProvider));
5555
id mockedSession = PFStrictClassMock([PFURLSession class]);
5656
id mockedRequestConstructor = PFStrictClassMock([PFCommandURLRequestConstructor class]);
57+
id mockedNotificationCenter = PFStrictClassMock([NSNotificationCenter class]);
5758

5859
id mockedCommand = PFStrictClassMock([PFRESTCommand class]);
5960
id mockedCommandResult = PFStrictClassMock([PFCommandResult class]);
@@ -71,7 +72,8 @@ - (void)testRunCommand {
7172

7273
PFURLSessionCommandRunner *commandRunner = [[PFURLSessionCommandRunner alloc] initWithDataSource:mockedDataSource
7374
session:mockedSession
74-
requestConstructor:mockedRequestConstructor];
75+
requestConstructor:mockedRequestConstructor
76+
notificationCenter:mockedNotificationCenter];
7577

7678
XCTestExpectation *expecatation = [self currentSelectorTestExpectation];
7779
[[commandRunner runCommandAsync:mockedCommand withOptions:0] continueWithBlock:^id(BFTask *task) {
@@ -89,14 +91,16 @@ - (void)testRunCommandCancel {
8991
id mockedDataSource = PFStrictProtocolMock(@protocol(PFInstallationIdentifierStoreProvider));
9092
id mockedSession = PFStrictClassMock([PFURLSession class]);
9193
id mockedRequestConstructor = PFStrictClassMock([PFCommandURLRequestConstructor class]);
94+
id mockedNotificationCenter = PFStrictClassMock([NSNotificationCenter class]);
9295

9396
id mockedCommand = PFStrictClassMock([PFRESTCommand class]);
9497

9598
OCMStub([mockedSession invalidateAndCancel]);
9699

97100
PFURLSessionCommandRunner *commandRunner = [[PFURLSessionCommandRunner alloc] initWithDataSource:mockedDataSource
98101
session:mockedSession
99-
requestConstructor:mockedRequestConstructor];
102+
requestConstructor:mockedRequestConstructor
103+
notificationCenter:mockedNotificationCenter];
100104

101105
BFCancellationTokenSource *cancellationToken = [BFCancellationTokenSource cancellationTokenSource];
102106
[cancellationToken cancel];
@@ -117,6 +121,7 @@ - (void)testRunCommandRetry {
117121
id mockedDataSource = PFStrictProtocolMock(@protocol(PFInstallationIdentifierStoreProvider));
118122
id mockedSession = PFStrictClassMock([PFURLSession class]);
119123
id mockedRequestConstructor = PFStrictClassMock([PFCommandURLRequestConstructor class]);
124+
id mockedNotificationCenter = PFStrictClassMock([NSNotificationCenter class]);
120125

121126
id mockedCommand = PFStrictClassMock([PFRESTCommand class]);
122127

@@ -140,7 +145,8 @@ - (void)testRunCommandRetry {
140145

141146
PFURLSessionCommandRunner *commandRunner = [[PFURLSessionCommandRunner alloc] initWithDataSource:mockedDataSource
142147
session:mockedSession
143-
requestConstructor:mockedRequestConstructor];
148+
requestConstructor:mockedRequestConstructor
149+
notificationCenter:mockedNotificationCenter];
144150
commandRunner.initialRetryDelay = DBL_MIN; // Lets not needlessly sleep here.
145151

146152
XCTestExpectation *expecatation = [self currentSelectorTestExpectation];
@@ -162,6 +168,7 @@ - (void)testRunFileUpload {
162168
id mockedDataSource = PFStrictProtocolMock(@protocol(PFInstallationIdentifierStoreProvider));
163169
id mockedSession = PFStrictClassMock([PFURLSession class]);
164170
id mockedRequestConstructor = PFStrictClassMock([PFCommandURLRequestConstructor class]);
171+
id mockedNotificationCenter = PFStrictClassMock([NSNotificationCenter class]);
165172

166173
id mockedCommand = PFStrictClassMock([PFRESTCommand class]);
167174
id mockedCommandResult = PFStrictClassMock([PFCommandResult class]);
@@ -191,7 +198,8 @@ - (void)testRunFileUpload {
191198

192199
PFURLSessionCommandRunner *commandRunner = [[PFURLSessionCommandRunner alloc] initWithDataSource:mockedDataSource
193200
session:mockedSession
194-
requestConstructor:mockedRequestConstructor];
201+
requestConstructor:mockedRequestConstructor
202+
notificationCenter:mockedNotificationCenter];
195203

196204
XCTestExpectation *expecatation = [self currentSelectorTestExpectation];
197205
[[commandRunner runFileUploadCommandAsync:mockedCommand
@@ -213,6 +221,7 @@ - (void)testLocalIdResolution {
213221
id mockedDataSource = PFStrictProtocolMock(@protocol(PFInstallationIdentifierStoreProvider));
214222
id mockedSession = PFStrictClassMock([PFURLSession class]);
215223
id mockedRequestConstructor = PFStrictClassMock([PFCommandURLRequestConstructor class]);
224+
id mockedNotificationCenter = PFStrictClassMock([NSNotificationCenter class]);
216225

217226
id mockedCommand = PFStrictClassMock([PFRESTCommand class]);
218227
id mockedCommandResult = PFStrictClassMock([PFCommandResult class]);
@@ -230,7 +239,8 @@ - (void)testLocalIdResolution {
230239

231240
PFURLSessionCommandRunner *commandRunner = [[PFURLSessionCommandRunner alloc] initWithDataSource:mockedDataSource
232241
session:mockedSession
233-
requestConstructor:mockedRequestConstructor];
242+
requestConstructor:mockedRequestConstructor
243+
notificationCenter:mockedNotificationCenter];
234244

235245
XCTestExpectation *expecatation = [self currentSelectorTestExpectation];
236246
[[commandRunner runCommandAsync:mockedCommand withOptions:0] continueWithBlock:^id(BFTask *task) {

0 commit comments

Comments
 (0)