FIRFakeApp.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "FirebaseDatabase/Tests/Helpers/FIRFakeApp.h"
  17. #import "FirebaseDatabase/Sources/Api/FIRDatabaseComponent.h"
  18. #import "SharedTestUtilities/FIRAuthInteropFake.h"
  19. #import "SharedTestUtilities/FIRComponentTestUtilities.h"
  20. @interface FIRFakeOptions : NSObject
  21. @property(nonatomic, readonly, copy) NSString *_Nullable databaseURL;
  22. @property(nonatomic, readonly, copy) NSString *projectID;
  23. @property(nonatomic, readonly, copy) NSString *googleAppID;
  24. - (instancetype)initWithURL:(NSString *_Nullable)url;
  25. @end
  26. @implementation FIRFakeOptions
  27. - (instancetype)initWithURL:(NSString *_Nullable)url {
  28. self = [super init];
  29. if (self) {
  30. _databaseURL = url;
  31. _googleAppID = @"fake-app-id";
  32. _projectID = @"fake-project-id";
  33. }
  34. return self;
  35. }
  36. @end
  37. @interface FIRDatabaseComponent (Internal)
  38. - (instancetype)initWithApp:(FIRApp *)app;
  39. @end
  40. @interface FIRComponentContainer (TestInternal)
  41. @property(nonatomic, strong) NSMutableDictionary<NSString *, FIRComponentCreationBlock> *components;
  42. @end
  43. @interface FIRComponentContainer (TestInternalImplementations)
  44. - (instancetype)initWithApp:(FIRApp *)app
  45. components:(NSDictionary<NSString *, FIRComponentCreationBlock> *)components;
  46. @end
  47. @implementation FIRComponentContainer (TestInternalImplementations)
  48. - (instancetype)initWithApp:(FIRApp *)app
  49. components:(NSDictionary<NSString *, FIRComponentCreationBlock> *)components {
  50. self = [self initWithApp:app registrants:[[NSMutableSet alloc] init]];
  51. if (self) {
  52. self.components = [components mutableCopy];
  53. }
  54. return self;
  55. }
  56. @end
  57. @implementation FIRFakeApp
  58. - (instancetype)initWithName:(NSString *)name URL:(NSString *_Nullable)url {
  59. self = [super init];
  60. if (self) {
  61. _name = name;
  62. _options = [[FIRFakeOptions alloc] initWithURL:url];
  63. _Nullable id (^authBlock)(FIRComponentContainer *, BOOL *) =
  64. ^(FIRComponentContainer *container, BOOL *isCacheable) {
  65. return [[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil];
  66. };
  67. FIRComponentCreationBlock databaseBlock =
  68. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  69. *isCacheable = YES;
  70. return [[FIRDatabaseComponent alloc] initWithApp:container.app];
  71. };
  72. NSDictionary<NSString *, FIRComponentCreationBlock> *components = @{
  73. NSStringFromProtocol(@protocol(FIRAuthInterop)) : authBlock,
  74. NSStringFromProtocol(@protocol(FIRDatabaseProvider)) : databaseBlock
  75. };
  76. _container = [[FIRComponentContainer alloc] initWithApp:(FIRApp *)self components:components];
  77. }
  78. return self;
  79. }
  80. @end