FIRFakeApp.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRFakeApp.h"
  17. #import "FIRAuthInteropFake.h"
  18. #import "FIRComponentTestUtilities.h"
  19. #import "FIRDatabaseComponent.h"
  20. @interface FIRFakeOptions: NSObject
  21. @property(nonatomic, readonly, copy) NSString *databaseURL;
  22. - (instancetype)initWithURL:(NSString *)url;
  23. @end
  24. @implementation FIRFakeOptions
  25. - (instancetype)initWithURL:(NSString *)url {
  26. self = [super init];
  27. if (self) {
  28. _databaseURL = url;
  29. }
  30. return self;
  31. }
  32. @end
  33. @interface FIRDatabaseComponent (Internal)
  34. - (instancetype)initWithApp:(FIRApp *)app;
  35. @end
  36. @interface FIRComponentContainer (TestInternal)
  37. @property(nonatomic, strong) NSMutableDictionary<NSString *, FIRComponentCreationBlock> *components;
  38. @end
  39. @interface FIRComponentContainer (TestInternalImplementations)
  40. - (instancetype)initWithApp:(FIRApp *)app
  41. components:(NSDictionary<NSString *, FIRComponentCreationBlock> *)components;
  42. @end
  43. @implementation FIRComponentContainer (TestInternalImplementations)
  44. - (instancetype)initWithApp:(FIRApp *)app
  45. components:(NSDictionary<NSString *, FIRComponentCreationBlock> *)components {
  46. self = [self initWithApp:app registrants:[[NSMutableSet alloc] init]];
  47. if (self) {
  48. self.components = [components mutableCopy];
  49. }
  50. return self;
  51. }
  52. @end
  53. @implementation FIRFakeApp
  54. - (instancetype)initWithName:(NSString *)name URL:(NSString *)url {
  55. self = [super init];
  56. if (self) {
  57. _name = name;
  58. _options = [[FIRFakeOptions alloc] initWithURL:url];
  59. _Nullable id (^authBlock)(FIRComponentContainer *, BOOL *) =
  60. ^(FIRComponentContainer *container, BOOL *isCacheable) {
  61. return [[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil];
  62. };
  63. FIRComponentCreationBlock databaseBlock =
  64. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  65. *isCacheable = YES;
  66. return [[FIRDatabaseComponent alloc] initWithApp:container.app];
  67. };
  68. NSDictionary<NSString *, FIRComponentCreationBlock> *components =
  69. @{NSStringFromProtocol(@protocol(FIRAuthInterop)) : authBlock,
  70. NSStringFromProtocol(@protocol(FIRDatabaseProvider)) : databaseBlock};
  71. _container = [[FIRComponentContainer alloc] initWithApp:(FIRApp *)self components:components];
  72. }
  73. return self;
  74. }
  75. @end