Browse Source

[auth-swift] Reimplement AuthTestingSupport in Swift (#12377)

Paul Beusterien 2 năm trước cách đây
mục cha
commit
d049a9d47f

+ 1 - 1
FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift

@@ -478,7 +478,7 @@ import Foundation
     private let callbackScheme: String
     private let usingClientIDScheme: Bool
 
-    private init(auth: Auth) {
+    init(auth: Auth) {
       self.auth = auth
       if let clientID = auth.app?.options.clientID {
         let reverseClientIDScheme = clientID.components(separatedBy: ".").reversed()

+ 6 - 15
FirebaseAuthTestingSupport.podspec

@@ -1,6 +1,6 @@
 Pod::Spec.new do |s|
   s.name                    = 'FirebaseAuthTestingSupport'
-  s.version                 = '1.0.0'
+  s.version                 = '2.0.0'
   s.summary                 = 'Firebase SDKs testing support types and utilities.'
 
   s.description      = <<-DESC
@@ -17,10 +17,10 @@ Pod::Spec.new do |s|
     :tag => 'CocoaPods-' + s.version.to_s
   }
 
-  ios_deployment_target = '11.0'
+  ios_deployment_target = '13.0'
   osx_deployment_target = '10.13'
-  tvos_deployment_target = '12.0'
-  watchos_deployment_target = '6.0'
+  tvos_deployment_target = '13.0'
+  watchos_deployment_target = '7.0'
 
   s.swift_version = '5.3'
 
@@ -36,19 +36,10 @@ Pod::Spec.new do |s|
   base_dir = 'FirebaseTestingSupport/Auth/'
 
   s.source_files = [
-    base_dir + 'Sources/**/*.{m,mm,h}',
+    base_dir + 'Sources/**/*.swift',
   ]
 
-  s.public_header_files = base_dir + '**/*.h'
-
-  s.dependency 'FirebaseAuth', '~> 10.0'
-
-  s.pod_target_xcconfig = {
-    'GCC_C_LANGUAGE_STANDARD' => 'c99',
-    'OTHER_CFLAGS' => '-fno-autolink',
-    'HEADER_SEARCH_PATHS' =>
-      '"${PODS_TARGET_SRCROOT}" '
-  }
+  s.dependency 'FirebaseAuth', '~> 10.22'
 
   s.test_spec 'unit' do |unit_tests|
     unit_tests.scheme = { :code_coverage => true }

+ 0 - 32
FirebaseTestingSupport/Auth/Sources/FIRPhoneAuthProviderFake.m

@@ -1,32 +0,0 @@
-// Copyright 2021 Google LLC
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#import "FirebaseTestingSupport/Auth/Sources/Public/FirebaseAuthTestingSupport/FIRPhoneAuthProviderFake.h"
-
-@implementation FIRPhoneAuthProviderFake
-
-- (instancetype)init {
-  // The object is partially initialized. Make sure the methods used during testing are overridden.
-  return self;
-}
-
-- (void)verifyPhoneNumber:(NSString *)phoneNumber
-               UIDelegate:(id<FIRAuthUIDelegate>)UIDelegate
-               completion:(FIRVerificationResultCallback)completion {
-  if (self.verifyPhoneNumberHandler) {
-    self.verifyPhoneNumberHandler(completion);
-  }
-}
-
-@end

+ 19 - 20
FirebaseTestingSupport/Auth/Sources/Public/FirebaseAuthTestingSupport/FIRPhoneAuthProviderFake.h → FirebaseTestingSupport/Auth/Sources/PhoneAuthProviderFake.swift

@@ -1,4 +1,4 @@
-// Copyright 2021 Google LLC
+// Copyright 2024 Google LLC
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -12,24 +12,23 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#import <FirebaseAuth/FirebaseAuth.h>
-
-NS_ASSUME_NONNULL_BEGIN
-
-typedef void (^FIRVerifyPhoneNumberHandler)(FIRVerificationResultCallback completion);
+@testable import FirebaseAuth
+import Foundation
 
 /// A fake object to replace a real `AuthAPNSTokenManager` in tests.
-NS_SWIFT_NAME(PhoneAuthProviderFake)
-@interface FIRPhoneAuthProviderFake : FIRPhoneAuthProvider
-
-- (instancetype)init;
-
-/// The block to be called each time when `verifyPhoneNumber(_:uiDelegate:completion:)` method is
-/// called.
-@property(nonatomic, nullable, copy) FIRVerifyPhoneNumberHandler verifyPhoneNumberHandler;
-
-// TODO: Implement other handlers as needed.
-
-@end
-
-NS_ASSUME_NONNULL_END
+public class PhoneAuthProviderFake: PhoneAuthProvider {
+  override init(auth: Auth) {
+    super.init(auth: auth)
+  }
+
+  var verifyPhoneNumberHandler: (((String?, Error?) -> Void) -> Void)?
+
+  override public func verifyPhoneNumber(_ phoneNumber: String,
+                                         uiDelegate: AuthUIDelegate? = nil,
+                                         completion: ((_: String?, _: Error?) -> Void)?) {
+    if let verifyPhoneNumberHandler,
+       let completion {
+      verifyPhoneNumberHandler(completion)
+    }
+  }
+}

+ 23 - 7
FirebaseTestingSupport/Auth/Tests/PhoneAuthProviderFakeTests.swift

@@ -12,31 +12,47 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+@testable import FirebaseAuth
 @testable import FirebaseAuthTestingSupport
+import FirebaseCore
 import Foundation
 import XCTest
 
 class PhoneAuthProviderFakeTests: XCTestCase {
+  var auth: Auth!
+  static var testNum = 0
+  override func setUp() {
+    super.setUp()
+    let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
+                                  gcmSenderID: "00000000000000000-00000000000-000000000")
+    options.apiKey = "TEST_API_KEY"
+    options.projectID = "myProjectID"
+    PhoneAuthProviderFakeTests.testNum = PhoneAuthProviderFakeTests.testNum + 1
+    let name = "test-name\(PhoneAuthProviderFakeTests.testNum)"
+    FirebaseApp.configure(name: name, options: options)
+    auth = Auth(
+      app: FirebaseApp.app(name: name)!
+    )
+  }
+
   func testPhoneAuthProviderFakeConstructor() throws {
-    let fakePhoneAuthProvider = PhoneAuthProviderFake()
+    let fakePhoneAuthProvider = PhoneAuthProviderFake(auth: auth)
     XCTAssertNotNil(fakePhoneAuthProvider)
-    XCTAssertTrue(fakePhoneAuthProvider.isKind(of: PhoneAuthProvider.self))
   }
 
   func testVerifyPhoneNumberHandler() {
-    let fakePhoneAuthProvider = PhoneAuthProviderFake()
+    let fakePhoneAuthProvider = PhoneAuthProviderFake(auth: auth)
 
     let handlerExpectation = expectation(description: "Handler called")
     fakePhoneAuthProvider.verifyPhoneNumberHandler = { completion in
       handlerExpectation.fulfill()
-
-      completion(nil, nil)
+      completion("test-id", nil)
     }
 
     let completionExpectation = expectation(description: "Completion called")
-    fakePhoneAuthProvider.verifyPhoneNumber("", uiDelegate: nil) { verficationID, error in
+    fakePhoneAuthProvider.verifyPhoneNumber("", uiDelegate: nil) { verificationID, error in
       completionExpectation.fulfill()
-      XCTAssertNil(verficationID)
+      XCTAssertEqual(verificationID, "test-id")
       XCTAssertNil(error)
     }