Skip to content

Commit d049a9d

Browse files
authored
[auth-swift] Reimplement AuthTestingSupport in Swift (#12377)
1 parent 8725ac5 commit d049a9d

File tree

5 files changed

+49
-75
lines changed

5 files changed

+49
-75
lines changed

FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ import Foundation
478478
private let callbackScheme: String
479479
private let usingClientIDScheme: Bool
480480

481-
private init(auth: Auth) {
481+
init(auth: Auth) {
482482
self.auth = auth
483483
if let clientID = auth.app?.options.clientID {
484484
let reverseClientIDScheme = clientID.components(separatedBy: ".").reversed()

FirebaseAuthTestingSupport.podspec

+6-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'FirebaseAuthTestingSupport'
3-
s.version = '1.0.0'
3+
s.version = '2.0.0'
44
s.summary = 'Firebase SDKs testing support types and utilities.'
55

66
s.description = <<-DESC
@@ -17,10 +17,10 @@ Pod::Spec.new do |s|
1717
:tag => 'CocoaPods-' + s.version.to_s
1818
}
1919

20-
ios_deployment_target = '11.0'
20+
ios_deployment_target = '13.0'
2121
osx_deployment_target = '10.13'
22-
tvos_deployment_target = '12.0'
23-
watchos_deployment_target = '6.0'
22+
tvos_deployment_target = '13.0'
23+
watchos_deployment_target = '7.0'
2424

2525
s.swift_version = '5.3'
2626

@@ -36,19 +36,10 @@ Pod::Spec.new do |s|
3636
base_dir = 'FirebaseTestingSupport/Auth/'
3737

3838
s.source_files = [
39-
base_dir + 'Sources/**/*.{m,mm,h}',
39+
base_dir + 'Sources/**/*.swift',
4040
]
4141

42-
s.public_header_files = base_dir + '**/*.h'
43-
44-
s.dependency 'FirebaseAuth', '~> 10.0'
45-
46-
s.pod_target_xcconfig = {
47-
'GCC_C_LANGUAGE_STANDARD' => 'c99',
48-
'OTHER_CFLAGS' => '-fno-autolink',
49-
'HEADER_SEARCH_PATHS' =>
50-
'"${PODS_TARGET_SRCROOT}" '
51-
}
42+
s.dependency 'FirebaseAuth', '~> 10.22'
5243

5344
s.test_spec 'unit' do |unit_tests|
5445
unit_tests.scheme = { :code_coverage => true }

FirebaseTestingSupport/Auth/Sources/FIRPhoneAuthProviderFake.m

-32
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 Google LLC
1+
// Copyright 2024 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,24 +12,23 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#import <FirebaseAuth/FirebaseAuth.h>
16-
17-
NS_ASSUME_NONNULL_BEGIN
18-
19-
typedef void (^FIRVerifyPhoneNumberHandler)(FIRVerificationResultCallback completion);
15+
@testable import FirebaseAuth
16+
import Foundation
2017

2118
/// A fake object to replace a real `AuthAPNSTokenManager` in tests.
22-
NS_SWIFT_NAME(PhoneAuthProviderFake)
23-
@interface FIRPhoneAuthProviderFake : FIRPhoneAuthProvider
24-
25-
- (instancetype)init;
26-
27-
/// The block to be called each time when `verifyPhoneNumber(_:uiDelegate:completion:)` method is
28-
/// called.
29-
@property(nonatomic, nullable, copy) FIRVerifyPhoneNumberHandler verifyPhoneNumberHandler;
30-
31-
// TODO: Implement other handlers as needed.
32-
33-
@end
34-
35-
NS_ASSUME_NONNULL_END
19+
public class PhoneAuthProviderFake: PhoneAuthProvider {
20+
override init(auth: Auth) {
21+
super.init(auth: auth)
22+
}
23+
24+
var verifyPhoneNumberHandler: (((String?, Error?) -> Void) -> Void)?
25+
26+
override public func verifyPhoneNumber(_ phoneNumber: String,
27+
uiDelegate: AuthUIDelegate? = nil,
28+
completion: ((_: String?, _: Error?) -> Void)?) {
29+
if let verifyPhoneNumberHandler,
30+
let completion {
31+
verifyPhoneNumberHandler(completion)
32+
}
33+
}
34+
}

FirebaseTestingSupport/Auth/Tests/PhoneAuthProviderFakeTests.swift

+23-7
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,47 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
@testable import FirebaseAuth
1516
@testable import FirebaseAuthTestingSupport
17+
import FirebaseCore
1618
import Foundation
1719
import XCTest
1820

1921
class PhoneAuthProviderFakeTests: XCTestCase {
22+
var auth: Auth!
23+
static var testNum = 0
24+
override func setUp() {
25+
super.setUp()
26+
let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
27+
gcmSenderID: "00000000000000000-00000000000-000000000")
28+
options.apiKey = "TEST_API_KEY"
29+
options.projectID = "myProjectID"
30+
PhoneAuthProviderFakeTests.testNum = PhoneAuthProviderFakeTests.testNum + 1
31+
let name = "test-name\(PhoneAuthProviderFakeTests.testNum)"
32+
FirebaseApp.configure(name: name, options: options)
33+
auth = Auth(
34+
app: FirebaseApp.app(name: name)!
35+
)
36+
}
37+
2038
func testPhoneAuthProviderFakeConstructor() throws {
21-
let fakePhoneAuthProvider = PhoneAuthProviderFake()
39+
let fakePhoneAuthProvider = PhoneAuthProviderFake(auth: auth)
2240
XCTAssertNotNil(fakePhoneAuthProvider)
23-
XCTAssertTrue(fakePhoneAuthProvider.isKind(of: PhoneAuthProvider.self))
2441
}
2542

2643
func testVerifyPhoneNumberHandler() {
27-
let fakePhoneAuthProvider = PhoneAuthProviderFake()
44+
let fakePhoneAuthProvider = PhoneAuthProviderFake(auth: auth)
2845

2946
let handlerExpectation = expectation(description: "Handler called")
3047
fakePhoneAuthProvider.verifyPhoneNumberHandler = { completion in
3148
handlerExpectation.fulfill()
32-
33-
completion(nil, nil)
49+
completion("test-id", nil)
3450
}
3551

3652
let completionExpectation = expectation(description: "Completion called")
37-
fakePhoneAuthProvider.verifyPhoneNumber("", uiDelegate: nil) { verficationID, error in
53+
fakePhoneAuthProvider.verifyPhoneNumber("", uiDelegate: nil) { verificationID, error in
3854
completionExpectation.fulfill()
39-
XCTAssertNil(verficationID)
55+
XCTAssertEqual(verificationID, "test-id")
4056
XCTAssertNil(error)
4157
}
4258

0 commit comments

Comments
 (0)