FIRFakeApp.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. @implementation FIRFakeApp
  44. - (instancetype)initWithName:(NSString *)name URL:(NSString *_Nullable)url {
  45. self = [super init];
  46. if (self) {
  47. _name = name;
  48. _options = [[FIRFakeOptions alloc] initWithURL:url];
  49. _Nullable id (^authBlock)(FIRComponentContainer *, BOOL *) =
  50. ^(FIRComponentContainer *container, BOOL *isCacheable) {
  51. return [[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil];
  52. };
  53. FIRComponentCreationBlock databaseBlock =
  54. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  55. *isCacheable = YES;
  56. return [[FIRDatabaseComponent alloc] initWithApp:container.app];
  57. };
  58. NSDictionary<NSString *, FIRComponentCreationBlock> *components = @{
  59. NSStringFromProtocol(@protocol(FIRAuthInterop)) : authBlock,
  60. NSStringFromProtocol(@protocol(FIRDatabaseProvider)) : databaseBlock
  61. };
  62. _container = [[FIRComponentContainer alloc] initWithApp:(FIRApp *)self components:components];
  63. }
  64. return self;
  65. }
  66. @end