FIRFakeApp.m 3.0 KB

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